Not create multiple table in Realm. - react-native

I'm creating tables of Realm database using React native. My function of creating table is,
const Realm = require('realm');
exports.createTables = function(tableName, pkey, structure) {
let realm = new Realm({
schema: [{name: tableName, primaryKey: pkey, properties: structure}]
});
return realm;
};
and i calling this method,
import realmDatabase from './RealmDatabase';
realmDatabase.createTables("MstUnitType", "UnitTypeId", {
"UnitTypeName" : "string",
"UnitTypeId" : "string",
} );
realmDatabase.createTables("MstTime", "TimeId", {
"TimeId" : "string",
"From" : "string",
"To" : "string",
} );
realmDatabase.createTables("MstQuestions", "QuestionId", {
"QuestionId" : "string",
"Question" : "string",
} );
I got only MstUnitType table in defualt.realm file other 2 table not created while i run above 3 create table methods one by one.

Yes i found solution of above. Following way we can create multiple tables at a time,
var Realm = require('realm');
const CarSchema = {
name: 'Car',
properties: {
make: 'string',
model: 'string',
miles: {type: 'int', default: 0},
}
};
const PersonSchema = {
name: 'Person',
properties: {
name: 'string',
birthday: 'date',
cars: {type: 'list', objectType: 'Car'},
picture: {type: 'data', optional: true}, // optional property
}
};
// Initialize a Realm with Car and Person models
let realm = new Realm({schema: [CarSchema, PersonSchema]});

Related

Data type for an array of Object in realm - react-native realm

I'm using realm for Javascript using React-native
I want to generate a realm schema to hold an array of Object as this:
arrayOfObj:[{ key1:1111, key2:2222, key3:333 }]
What I have tried so far is using the mixed type in my schema
const mySchema = {
name: "mySchema",
properties: {
_id: "objectId",
arrOfObj: "mixed" //'have used mixed[] too but they all don't work
}
}
I have tried using mixed and also using mixed[] but when I try to insert the array of object i get the error: mySchema.arrOfObj must be of type 'mixed?[]', got 'object' ([object Object])].
Now, What is the correct data type for the array of object in realm?
const myScheme = {
name: "myScheme",
primaryKey: "_id",
properties: {
_id: "objectId",
_partition: "string",
name: "string",
tasks: "myData[]"
}
};
const myData = {
name: "myData",
primaryKey: "_id",
properties: {
_id: "objectId",
_partition: "string",
firstname: "string",
lastname: "string",
}
};

Fulltext mongodb $text search query in graphql-compose-mongoose

I'm unable to figure out how to construct a graphql query for performing the mongodb fulltext search using the text index. https://docs.mongodb.com/manual/text-search/
I've already created a text index on my string in the mongoose schema but I don't see anything in the schemas that show up in the grapqhl playground.
A bit late, though I was able to implement it like so
const FacilitySchema: Schema = new Schema(
{
name: { type: String, required: true, maxlength: 50, text: true },
short_description: { type: String, required: true, maxlength: 150, text: true },
description: { type: String, maxlength: 1000 },
location: { type: LocationSchema, required: true },
},
{
timestamps: true,
}
);
FacilitySchema.index(
{
name: 'text',
short_description: 'text',
'category.name': 'text',
'location.address': 'text',
'location.city': 'text',
'location.state': 'text',
'location.country': 'text',
},
{
name: 'FacilitiesTextIndex',
default_language: 'english',
weights: {
name: 10,
short_description: 5,
// rest fields get weight equals to 1
},
}
);
After creating your ObjectTypeComposer for the model, add this
const paginationResolver = FacilityTC.getResolver('pagination').addFilterArg({
name: 'search',
type: 'String',
query: (query, value, resolveParams) => {
resolveParams.args.sort = {
score: { $meta: 'textScore' },
};
query.$text = { $search: value, $language: 'en' };
resolveParams.projection.score = { $meta: 'textScore' };
},
});
FacilityTC.setResolver('pagination', paginationResolver);
Then you can assign like so
const schemaComposer = new SchemaComposer();
schemaComposer.Query.addFields({
// ...
facilities: Facility.getResolver('pagination')
// ...
});
On your client side, perform the query like so
{
facilities(filter: { search: "akure" }) {
count
items {
name
}
}
}

realm react-native - 2 different databases/schemas

can someone show me how to create 2 databases with realm?
I have 2 screens. Screen "Coins" contains working code below. I want to add another realm database to the screen "Cars".
Unfortunately an error occurs:
"Realm at path '/Users/......./Documents/default.realm' already opened on current thread with different schema
//Screen "Coins"
var Realm = require('realm');
realm_coins = new Realm({
schema: [{
name: "Coins",
properties: {
coins: "int"
}
}]
})
If i add the following to the Screen "Cars", the mentioned error occurrs
//Screen "Cars"
var Realm = require('realm');
realm_cars = new Realm({
schema: [{
name: "Cars",
properties: {
name: "string"
}
}]
})
When creating your Realm instance, you pass in an array of your schema definitions:
const realm = new Realm({
schema: [{
name: "Coins",
properties: {
coins: "int"
}
}, {
name: "Cars",
properties: {
name: "string"
}
}]
});

Create index on multikey and nested document using mongoose

I'm creating index using mongoose it will check uniqueness of Name, PName and OName (Name+PName+OName should be unique). Please check below implementation
var MySchema = new mongoose.Schema({
Name: { type: String, required: true},
Details: [{
PName: { type: String, required: true},
OName: { type: String, required: true}
}]
});
MySchema.index({Name: 1, Details.PName: 1, Details.OName:1 }, {unique: true});
Document
{"Name" : "Testing123","Details" : [{"PName" : "Page1", "OName" : "Ob1"},
{"PName" : "Page1", "OName" : "Ob1"}]}
I need to restrict above document for insertion as the Name, PName and OName is not unique.

Deleted records from store keeps showing up

I have created a local store and model for remembering username and password:
Store:
ToolbarDemo.stores.localsettingsstore = new Ext.data.Store({
model: 'UserSettings',
proxy: new Ext.data.LocalStorageProxy(
{
id: 'data',
proxy:
{
idProperty: 'id'
}
}),
autoLoad: true,
autoSave: true,
listeners:
{
beforesync: function()
{
console.log("SYNCING");
console.log("Number of data: ");
console.log(this.getCount());
},
datachanged: function()
{
console.log(this.getProxy());
console.log("DATA CHANGED");
console.log("Number of data: ");
console.log(this.getCount());
}
}
});
Model:
Ext.regModel('UserSettings', {
fields: [
{name: 'username', type: 'string'},
{name: 'password', type: 'string'},
{name: 'storeUsernamePassword', type: 'boolean'}
]
});
If the user want to store the username and password, this function is invoked:
function setLocalUsernameAndPassword(localUsername, localPassword, bStoreUsernameAndPassword)
{
removeLocalUsernameAndPassword(false); // Remove all previous inputs (Should just be one)
ToolbarDemo.stores.localsettingsstore.add({username: localUsername, password: localPassword, storeUsernamePassword: bStoreUsernameAndPassword});
}
The store is set to autoload and autosave, so it should not be nessecary to run a .sync() on the store.
If the user chooses to not store the username and password, i remove all records from the store by invoking:
function removeLocalUsernameAndPassword(bClearFields)
{
//ToolbarDemo.stores.localsettingsstore.removeAll();
ToolbarDemo.stores.localsettingsstore.each(function(record)
{
console.log("Removing " + record.data.username);
ToolbarDemo.stores.localsettingsstore.remove(record);
});
if(bClearFields)
{
Ext.getCmp("usernameField").value = "";
Ext.getCmp("passwordField").value = "";
Ext.getCmp("checkboxStoreUserInfo").checked = false;
}
}
Afterwards i can see that the store is empty, BUT if i refresh the page (Start the app once again), all the records are back plus the one i stored.
Can anyone see what i'm missing to do this properly?
Thanks in advance.
I finally found a guy that had exactly the same problem.
The solution:
You have to add a field with name "id", and type "int".
This makes sencha able to delete the record.
Ext.regModel('UserSettings', {
fields: [
{name: 'id', type: 'int'},
{name: 'username', type: 'string'},
{name: 'password', type: 'string'},
{name: 'storeUsernamePassword', type: 'boolean'}
]
});
After i did this, i also had to do a store.save() after each update.