How to store Array of multipe types with realm in react native - react-native

When using realm, if i need to store filed with value like [783, "Name of"],
This is my code, when i use mixed i got null,
export const Schema = {
name: 'Schema',
primaryKey: 'id',
properties: {
id: 'int',
name: {type: 'string', indexed: true},
field: 'string[]', // mixed[] not work too
}
}
Any help please !!!

Related

Display all properties of an array of objects in Sanity.io

In a Sanity project, I've created a schema containing an array of objects.
In Sanity Studio, this appears as a list of those objects' first property, but I really need to see at least 2 properties for it to be meaningful.
I can't find a way to do this. Is it possible?
Below is my schema. It represents a blog article which holds an array of translations. Each translation is defined by a language and a reference to another article.
// sanity\schemas\documents\article.ts
import { defineArrayMember, defineField, defineType } from "sanity"
import translation from "../objects/translation";
export default defineType({
title: 'Article',
name: 'article',
type: 'document',
fields: [
defineField({ title: 'Title', name: 'title', type: 'string' }),
defineField({ title: 'Translations', name: 'translations', type: 'array', of: [defineArrayMember(translation)] }),
// sanity\schemas\objects\translation.ts
import { defineField, defineType } from "sanity"
export default defineType({
title: 'Translation',
name: 'translation',
type: 'object',
fields: [
defineField({ title: 'Language', name: 'language', type: 'string' }),
defineField({ title: 'Article', name: 'article', type: 'reference', to: [{ type: 'article' }] }),
]
});
This is how it shows up:
I would like to see "fr - title of the article in french".
Note that I'd like to do this for other fields too, so I'm not looking for a package to handle internationalisation. Is it doable?
Found the answer: sanity.io/docs/previews-list-views
I hadn't realised this applies to lists within fields too, not just lists of documents. Adding the "preview" property at the root of my schema did the trick:
preview: {
select: {
title: 'field1',
subtitle: 'field2'
}
}

How to filter by item contained in a list in realm react native?

I'm doing the following query:
realm.objects('Maker').filtered("categories CONTAINS $0", categoryObject)
But I'm getting this error:
Only 'equal' and 'not equal' operators are supported for object comparison
And here's my schema:
{
name: 'MakerOption',
primaryKey: 'serverId',
properties: {
serverId: 'int',
name: 'string',
categories: {type: 'list', objectType: 'Category'},
}
{
name: 'Category',
primaryKey: 'serverId',
properties: {
serverId: 'int',
name: 'string'
}
The documentation is quite sparse on this subject. Is there an alternative method for doing this?
Filtering by properties on linked or child objects can be done by specifying a keypath in the query e.g. car.color == 'blue'
So you are looking for the following query:
realm.objects('Maker').filtered("categories.serverId == $0", categoryObject.serverId)

Not create multiple table in Realm.

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]});

Realm-js schema with nested objects

I want to easily query such results:
[{
name: 'john_doe',
info: {
age: 24,
notes: 'custom text',
phoneNumbers: {
home: 112345678,
work: 1234567,
},
},
}, {...}, {...}...]
... by such query:
contacts.filtered("info.age = 24 AND info.notes CONTAINS 'custom'");
How should i create such schema? docs are very confusing about data types and nested properties:
https://realm.io/docs/react-native/0.14.0/api/Realm.html#~PropertyType
https://realm.io/docs/react-native/latest/#nested-objects
I do not need to retrieve any parts of this data separately - only complete object with all nested objects at once.
You could put all fields into a single object:
var ContactSchema = {
name: 'Contact',
properties: {
name: 'string',
age: 'int',
notes: 'string',
homePhone: 'string',
workPhone: 'string'
}
};
Alternatively you could create child objects for info and phoneNumbers but if you are not sharing this data across multiple contacts then this probably isn't needed.

keystoneJS relationship to self

I want to create a Category model that can hold another category, but having a problem with reference field that I can set my current category to it self
Any suggestions how to achieve hierarchical categories?
Does KeystoneJS have filter like 'not equal'?
In other hand, maybe I can set default reference field to it self and it will be like a root...
My current code below:
var keystone = require('keystone'),
Types = keystone.Field.Types;
var PageCategory = keystone.List('PageCategory', {
map: { name: 'name' },
autokey : { from: 'name', path: 'key'}
});
PageCategory.add({
name: { type: String, required: true, unique: true},
image: { type: Types.CloudinaryImage, label: "Category Image"},
description : { type: Types.Html, wysiwyg: true},
parent: { type: Types.Relationship, ref: "PageCategory", label: "Parent category"}
});
PageCategory.relationship({ ref: "PageCategory", path: "parent"});
PageCategory.register();
I think you have misunderstood how Model.relationship() works.
It has three options:
path, this is the "virtual" field name that will hold the values
ref, this is the model that we reference
refPath, this is the field in the referenced model that we populate path with
I think something in line with this will work for you
PageCategory.relationship({ ref: "PageCategory", path: "children", refPath: "parent"});