I want to combine two schemas in realm db and perform operations on them, just like join in sql. But i don't know how to do it. I did not
List item
understand anything from the document. How can I do that?I have two schematics
const barkod = {
name: 'barkod',
properties: {
StokNo: { type: 'int', indexed: true },
Barkod: { type: 'string', indexed: true },
Birim: 'string',
BarkodTipi: 'string',
Aciklama: 'string?',
YStokNo: 'int'
}
// primaryKey: 'Barkod',
}
const stok = {
name: 'stok',
primaryKey: 'StokNo',
properties: {
StokNo: 'int',
StokAdi: { type: 'string', indexed: true },
StokKisaAdi: 'string',
StokKodu: 'string',
StokTanimi: 'string',
GrupKodu: 'string',
KdvOranP: { type: 'int', default: 0 },
KDVOranT: { type: 'int', default: 0 },
OzelKodu1: 'string',
OzelKodu2: 'string',
OzelKodu3: 'string'
}
}`enter code here`
and I want to join these two schemas
'SELECT Stok.StokAdi, Barkod, StokNo FROM ? AS Barkod JOIN ? AS Stok ON Barkod.StokNo = Stok.StokNo',
I found the solution to the problem. And this is how I did it. Hope it helps for those who have this problem.
const CarSchema = {
name: 'Car',
properties: {
make: 'string',
model: 'string',
miles: {type: 'int', default: 0},
},
};
const PersonSchema = {
name: 'Person',
properties: {
name: 'string',
birthday: 'date',
cars: 'Car[]',
// picture: 'data?' // optional property
},
};
const exp1 = () => {
var person;
Realm.open({schema: [CarSchema, PersonSchema]})
.then(realm => {
realm.write(() => {
person = realm.create('Person', {
name: 'ayşegül',
birthday: new Date(1995, 11, 25),
// cars: 'Car[]',
});
console.log(person.cars);
let carList = person.cars;
carList.push({make: 'Honda', model: 'model1', miles: 100});
carList.push({make: 'Toyota', model: 'model2', miles: 200});
console.log(person);
});
})
.catch(error => {
console.log(error);
});
};
Related
async function createDatabase() {
addRxPlugin(RxDBReplicationCouchDBPlugin);
addPouchPlugin(PouchdbAdapterIdb);
addRxPlugin(RxDBLeaderElectionPlugin);
const database = await createRxDatabase({
name: 'dictionaries',
storage: getRxStoragePouch('idb')
});
await database.addCollections({
dictionaries: {
schema: {
title: 'dictionary',
description: '',
version: 0,
primaryKey: 'id',
type: 'object',
keyCompression: false,
properties: {
id: {
type: 'string',
maxLength: 100,
},
name: {
type: 'string',
minLength: 1,
maxLength: 30,
},
},
required: ['id', 'name']
}
}
})
}
When I pass some value as an id, it is used as part of an auto-generated key:
const db = await createDatabase();
await db.dictionaries.newDocument({
id: 'bar',
name: 'English'
}).save(); // _doc_id_rev inside the db would be of format "bar::<auto-generated value>"
My question is is there any way to say RxDB that generating id's values for dictionaries is of its own concern or my single option is always to provide a dummy value, such as bar?
I have a unit test I am trying check the field but cannot find anything in the jest documentation to ignore the one value in this case, randomNumber.
const expected = [
{
model: 'string',
type: 'string',
randomNumber: 43,
name: 'string',
},
]
The random number changes every test so I can't hard code the test to check that number. I would like to test to make sure the expected array has all of the fields, but ignore the values.
const requiredKeys = ["model", "type", "randomNumber", "name"];
const actual = [
{
model: 'string',
type: 'string',
randomNumber: 43,
name: 'string',
},
{
type: 'string',
randomNumber: 43,
name: 'string',
},
]
actual.forEach(x => {
const keys = Object.keys(x);
const hasAllKeys = requiredKeys.every(key => keys.includes(key));
expect(hasAllkeys).toBeTruthy();
});
You can do something along the lines of this
const data = {
model: 'string',
type: 'string',
randomNumber: 43,
name: 'string',
}
const dataExpected = {
model: 'string',
type: 'string',
name: 'string',
}
const {randomNumber, ...randomNumOmittedData} = data;
expect(randomNumOmittedData).toEqual(dataExpected);
I would like to get the result of my event based on the filter.
const filter = [
{ type: 'type', value: ['In Person'] },
{ type: 'city', value: ['Boston', 'Miami', 'New York'] },
];
const events = [
{ node: { city: 'Boston', type: 'In Person', name: 'Boston Party' } },
{ node: { city: 'New Jersey', type: 'In Person', name: 'Hello Cookie' } },
{ node: { city: 'Boston', type: 'Virtual', name: 'Sales Kick Off' } },
];
const result = events.filter(o => Object
.entries(o.node)
.every(([k, v]) => filter
.some(({ type, value }) => type === k && value.includes(v)),),)
console.log(result)
I want the first object of my events because filter contain Boston and In Person. (This function will work if I remove the key name on my events). How this function can return a result if i have more key and value on my events.
You could filter with filter.
const
filter = [{ type: 'type', value: ['In Person', 'Virtual'] }, { type: 'city', value: ['Boston', 'Miami', 'New York'] }],
events = [{ node: { city: 'Boston', type: 'In Person', name: 'Boston Party' } }, { node: { city: 'New Jersey', type: 'In Person', name: 'Hello Cookie' } }, { node: { city: 'Boston', type: 'Virtual', name: 'Sales Kick Off' } }],
result = events.filter(({ node }) =>
filter.every(({ type, value }) => !value.length || value.includes(node[type]))
);
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have an dataset like this:
const array = [
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sedan',
carname: 'Audi a8',
type: 'car'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'cruiser',
carname: 'Harley',
type: 'bike'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sports',
carname: 'Ninja',
type: 'bike'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sedan',
carname: 'BMW',
type: 'car'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sports',
carname: 'Hayabusa',
type: 'bike'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'hatchback',
carname: 'Micra',
type: 'car'
},
]
NOTE: This is raw data with shorter length. Actual data will be like 10M+ enteries.
What I want is like I want to preserve the only 1 or 2 data from the array and rest bike data will be filter out from an Array.
Now for this, the method I choose is like this:
let newArray = []
let cou = 0
array.map((x) => {
if (x.type === 'bike') {
cou++;
// Want to preserve only 1 data of bike
if (cou < 2) {
newArray.push(x)
}
} else {
newArray.push(x)
}
})
Is there any best approach for handling this type of problem. Actually I want to reduce the array iteration as it consume the memory and time of the code.
Any solution or suggestion is highly appreciated. Thanks in advance for the interaction with the problem set.
You can use the filter method:
var bikeCount = 0;
var res = array.filter(x => {
if (x.type === "bike") {
bikeCount++;
return bikeCount < 2;
}
return true;
});
In this way you don't have to create a new array and push every single element into it.
I've used an online benchmark tool and it seems 35-40% faster than your solution.
You can solve that using Array.prototype.slice method:
const array = [
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sedan',
carname: 'Audi a8',
type: 'car'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'cruiser',
carname: 'Harley',
type: 'bike'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sports',
carname: 'Ninja',
type: 'bike'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sedan',
carname: 'BMW',
type: 'car'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'sports',
carname: 'Hayabusa',
type: 'bike'
},
{
id: '5c1b4ffc18e2d84b7d6feaf3',
model: 'hatchback',
carname: 'Micra',
type: 'car'
},
]
/*
Array.prototype.slice slices an arrray and returns sliced content
1st param : slice start index
2nd param : slice end index
*/
const filteredArray = array.slice(0, 2);
console.log(filteredArray);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Sorry if this is a bit confusing, but I have this array:
[
{
name: 'id',
type: 'string',
primary: true
},
{
name: 'notes',
type: 'text',
default: '[]'
},
{
name: 'reminders',
type: 'text',
default: '[]'
}
]
How would I turn it into
{
notes: '[]',
reminders: '[]'
}
? Here's the logic: Any index with a default property, set the key in the new object to the index's name, and set the value to the index's default.
You can make it with reduce
var array = [
{
name: 'id',
type: 'string',
primary: true
},
{
name: 'notes',
type: 'text',
default: '[]'
},
{
name: 'reminders',
type: 'text',
default: '[]'
}
]
var result = array.reduce((prev, el) => {
if (el.default) {
prev[el.name] = el.default
}
return prev;
},{})
console.log(result);
You can use filter with Object.keys().
// Your initial array
const initialArray = [{
name: 'id',
type: 'string',
primary: true
},
{
name: 'notes',
type: 'text',
default: '[]'
},
{
name: 'reminders',
type: 'text',
default: '[]'
}
];
// Array just with items that haves a default value
const itensWithDefaultValue = initialArray.filter(item => Object.keys(item).includes('default'));
// Object to save items
let objectWithValues = {};
// Save items in object with name and you default value
itensWithDefaultValue.map(item => objectWithValues[item.name] = item.default);
console.log(objectWithValues);
Hope this helps!
const data = [
{
name: 'id',
type: 'string',
primary: true
},
{
name: 'notes',
type: 'text',
default: '[]'
},
{
name: 'reminders',
type: 'text',
default: '[]'
}
];
const result = Object.fromEntries(
data
.filter(it => "default" in it)
.map(it => ([it.name, it.default]))
);
console.log(result);
Use reduce:
const arr = [{
name: 'id',
type: 'string',
primary: true
},
{
name: 'notes',
type: 'text',
default: '[]'
},
{
name: 'reminders',
type: 'text',
default: '[]'
}
];
const obj = arr.reduce((acc, curr) => curr.default ? { ...acc,
[curr.name]: curr.default
} : acc);
console.log(obj);
You could spread new objects into a single object.
var data = [{ name: 'id', type: 'string', primary: true }, { name: 'notes', type: 'text', default: '[]' }, { name: 'reminders', type: 'text', default: '[]' }],
result = Object.assign({}, ...data.map(({ name, default: d }) => d && { [name]: d }));
console.log(result);