Realm-js schema with nested objects - react-native

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.

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 store Array of multipe types with realm in 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 !!!

Using BigQuery repeated / nested fields in Power BI

Problem: When connecting Power BI to a BigQuery table (using the native BQ connector) with repeated / nested fields, these fields do not appear in Power BI for use in report creation.
Example: Using a Public BigQuery data set called bigquery-public-data:samples, there is a table called github_nested which has repeated fields such as payload.pages.action (see below)
However, when connecting to this BigQuery table using Power BI I only get a handful of fields (see below)
As I understand, this is because the Power BI Big Query connector doesn't support repeated / nested or record fields.
Question: Is there any workaround to have all columns / fields in a BigQuery table (regardless of whether they are repeated / nested / records) available for use when building Power BI reports, specifically using a live connection over the native Power BI Big Query connector?
I do have write permissions in BigQuery so creating views on top of these tables with repeated / nested fields is possible if required as part of a solution.
One potential workaround might be to create a view in Big Query which un-nests repeated fields and then connect Power BI to said view. Open to any workaround suggestions.
Any help would be greatly appreciated.
Power BI does not have (as of 29/10/2018) a native Big Query connector, it just uses a generic ODBC connector. I would advise not to use it for production, current limitations are :
GCP billing accounts are not supported if your billing project is different than your dataset project
Google Service Accounts are not supported
Nested and repeated fields are not supported
Requests are not optimized (bad finops)
Dataset linked to Google Sheets through Big Query are not supported
Some data types are not correctly handled
Requests cannot be manually edited (using BQ Standard SQL for exemple)
Learn more in this article (i am the author): https://medium.com/#remy_david/which-bi-tool-for-big-query-d9eb838ff7ad
Example schema of products-and-stocks table:
[
{
name: 'timestamp',
type: 'TIMESTAMP'
}, {
name: 'data',
type: 'RECORD',
mode: "REPEATED",
fields: [
{
name: 'itemId',
type: 'STRING'
}, {
name: 'prodId',
type: 'STRING'
}, {
name: 'name',
type: 'STRING'
}, {
name: 'stockA',
type: 'INTEGER'
}, {
name: 'stockB',
type: 'INTEGER'
}, {
name: 'stockQ',
type: 'INTEGER'
}, {
name: 'stockD',
type: 'INTEGER'
}, {
name: 'stockE',
type: 'INTEGER'
}, {
name: 'weight',
type: 'INTEGER'
}, {
name: 'size',
type: 'RECORD',
fields: [
{
name: 'length',
type: 'INTEGER'
}, {
name: 'width',
type: 'INTEGER'
}, {
name: 'height',
type: 'INTEGER'
}
]
}, {
name: 'cnt',
type: 'INTEGER'
}, {
name: 'cntInBox',
type: 'INTEGER'
}, {
name: 'lifetime',
type: 'INTEGER'
}, {
name: 'barcode',
type: 'STRING',
mode: 'REPEATED',
}, {
name: 'dateStockUpdate',
type: 'TIMESTAMP'
}, {
name: 'label',
type: 'STRING'
}, {
name: 'comment',
type: 'STRING'
}, {
name: 'commentPack',
type: 'STRING'
}, {
name: 'skuBox',
type: 'INTEGER'
}, {
name: 'snRuleRegularExpression',
type: 'STRING'
}
]
}
]
Create flattening query in the BigQuery and save it as a View.
SELECT
`timestamp`,
repeated.*,
repeated.size.length as `size_length`,
repeated.size.width as `size_width`,
repeated.size.height as `size_height`,
ARRAY_TO_STRING(barcode, ", ") as barcodesFlat
FROM
`my-project-id.my-dataset.products-and-stocks`
CROSS JOIN UNNEST(`data`) as repeated
Import this view into Power BI.

Realm Subquery in React Native

I'm new to realm. I have a schema like the following:
class MealItem {}
MealItem.schema = {
name: 'MealItem',
properties: {
mealId: {type: 'int'},
foodId: {type: 'int'},
},
};
I want to do the equivalent of :
SELECT
mealId
FROM MealItem
WHERE foodId = #food1
AND mealId IN (SELECT mealId FROM MealItem WHERE foodId = #food2
AND mealId IN (SELECT mealId FROM MealItem WHERE foodId = #food3))
I'm not sure where to begin other than to write A LOT of javascript for loops. Is there a less verbose way to do it?
more info
Basically I have two other objects like this:
class Meal {}
Meal.schema = {
name: 'Meal',
properties: {
mealId: {type: 'int'},
mealName: {type: 'string'},
},
};
class Food{}
Food.schema = {
name: 'Food',
properties: {
foodId: {type: 'int'},
foodName: {type: 'string'},
},
};
So my original query is meant to determine if given 3 different foods, does my restaurant offer a meal / combo for it.
For example, if you set food1 to Big Mac, food2 to Fries, food3 to Soft Drink, you should get a meal back called Big Mac Meal.
If you set food1 to Durian, food2 to Hot Pockets, food3 to Krusty Brand Imitation Gruel, no meal id will return, because my restaurant doesn't serve combo like that.
I don't know much about SQL but what I understand from your question is that you are trying to find meals that contains specific foods.
Realm queries and models works like a NoSQL database so you might need to think a little different while creating your models.
For example, lets say you have 3 tables. One contains Meals, one contains foods and one contains data about which MealId contains which FoodId. In Realm you don't need the 3 table. You can think as its a javascript object.
Example
const meal = {
mealId: 0001,
name: 'Big Fat Menu',
price: '4.99'
foods: [{
foodId: 11,
name: 'Burger'
}, {
foodId: 12,
name: 'Fries'
}, {
foodId: 13,
name: 'Coke'
}]
}
This javascript object can be modeled with Realm like this
const FoodSchema = {
name: 'Food',
primaryKey: 'foodId',
properties: {
foodId: 'string'
name: 'string',
}
};
const MealsSchema = {
name: 'Meal',
primaryKey: 'mealId',
properties: {
mealId: 'string'
name: 'string',
price: 'double',
foods: { type: 'list', , objectType: 'Food'}
}
};
With this schema you can make following query to get a meal that contains a specific food,
const meals = realm..objects('Meal').filtered(`foods.id == ${foodId}`)
This is just a small example to help you out a bit. Hope it helps.

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