Writing into a nested schema for Realm - nested

Based on my api response I made my schema for realm but I'm not able to push values into realm according to schema. plase let me know is there any way to do like this
export const WORKFLOW_SCHEMA = 'work_flow';
export const WORKFLOW_NAMES_SCHEMA = 'workflow_name';
export const WorkflowNamesSchema = {
name: WORKFLOW_SCHEMA,
primaryKey :'workflow_Name',
properties: {
workflow_Name: 'string',
default_State: 'int',
states: { type: 'list', objectType: WORKFLOW_NAMES_SCHEMA }
}
};
export const NEXTSTATE_SCHEMA = 'next_states';
export const WorkFlowSchema = {
name: WORKFLOW_NAMES_SCHEMA,
primaryKey :'state_ID',
properties: {
state_ID: 'int',
action_Name: 'string',
state: 'string',
next_States: { type: 'list', objectType: NEXTSTATE_SCHEMA }
}
};
export const NextStatesSchema = {
name: NEXTSTATE_SCHEMA,
properties: {
action: 'string',
state: 'string',
},
};

Related

Infer object with Pothos without to call implement

I have a z object created like this:
import z from 'zod';
import { ObjectId } from 'mongodb';
interface IAppMenuItem {
folder: string;
unit: string;
items: IAppMenuItem[];
}
const zAppMenuItem: z.ZodType<IAppMenuItem> = z.lazy(() =>
z.object({
folder: z.string().trim().min(1).max(20),
unit: z.string().trim().min(1).max(20),
items: z.array(zAppMenuItem)
})
);
export const zApp = z.object({
orgId: z.instanceof(ObjectId),
name: z.string().trim().min(1).max(50),
caption: z.string().trim().min(1).max(50),
menu: z.array(zAppMenuItem).optional(),
});
....
import { z } from 'zod';
import { zApp } from '../validators';
import { ChangeLog } from './ChangeLog';
export type App = z.infer<typeof zApp> & { changeLog: ChangeLog };
then I have a builder file
const builder = new SchemaBuilder<{
Objects: {
AppObject: WithId<App>;
};
Scalars: {
Id: { Input: Id; Output: Id };
Date: { Input: Date; Output: Date };
DateTime: { Input: Date; Output: Date };
};
Context: Context;
}>({});
export const AppObject = builder.objectRef<WithId<App>>('App');
export const ChangeLogObject = builder.objectRef<ChangeLog>('ChangeLog');
ChangeLogObject.implement({
fields: t => ({
createdAt: t.expose('createdAt', { type: 'DateTime', nullable: true }),
createdById: t.expose('createdById', { type: 'Id', nullable: true }),
createdByName: t.expose('createdByName', { type: 'String', nullable: true }),
updatedAt: t.expose('updatedAt', { type: 'DateTime', nullable: true }),
updatedById: t.expose('updatedById', { type: 'Id', nullable: true }),
updatedByName: t.expose('updatedByName', { type: 'String', nullable: true })
})
});
AppObject.implement({
fields: t => ({
_id: t.expose('_id', { type: 'Id' }),
orgId: t.expose('orgId', { type: 'Id' }),
name: t.expose('name', { type: 'String' }),
caption: t.expose('caption', { type: 'String' }),
// menu should be implemented here...
changeLog: t.expose('changeLog', { type: ChangeLogObject })
})
});
export { builder };
Because the zApp object will have even more nested objects like menu and units, it's not practical to implement for each the corresponding model under pothos.
Is there any way to infer the App type, so that Pothos will understand the model?

I have this error Unauthorised admin to execute command mongoose + Graphql

I used mongoose and Graphql to send my queries to the database but for some reason it doesn't let me create documents. I have tried creating a new user with full admin privileges it hasn't worked I tried changing the default user password but it didn't work.
I rechecked my mongoose model no errors so what might be the problem.
FYI the problem arose with the return (author.save()) and the database connects normally
Author Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const authorSchema = new Schema({
name: String,
age: Number
});
module.exports = mongoose.model('Author', authorSchema);
schema.js
const graphql = require('graphql');
const Book = require('../models/book');
const Author = require('../models/Author');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList
} = graphql;
const BookType = new GraphQLObjectType({
name: 'Book',
fields: ( ) => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString },
author: {
type: AuthorType,
resolve(parent, args){
//return _.find(authors, { id: parent.authorId });
}
}
})
});
const AuthorType = new GraphQLObjectType({
name: 'Author',
fields: ( ) => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
age: { type: GraphQLInt },
books: {
type: new GraphQLList(BookType),
resolve(parent, args){
//return _.filter(books, { authorId: parent.id });
}
}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args){
//return _.find(books, { id: args.id });
}
},
author: {
type: AuthorType,
args: { id: { type: GraphQLID } },
resolve(parent, args){
//return _.find(authors, { id: args.id });
}
},
books: {
type: new GraphQLList(BookType),
resolve(parent, args){
//return books;
}
},
authors: {
type: new GraphQLList(AuthorType),
resolve(parent, args){
//return authors;
}
}
}
});
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addAuthor: {
type: AuthorType,
args: {
name: { type: GraphQLString },
age: { type: GraphQLInt }
},
resolve(parent, args){
let author = new Author({
name: args.name,
age: args.age
});
return (author.save())
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
})
;
error message
(node:31482) MongoError: (Unauthorized) not authorized on admin to execute command {
insert: "authors", documents: [[{name gyfdgyiszukjfheusdzyih} {age 88} {_id
ObjectID("60af9c682215ea7afad86f4c")} {__v 0}]], ordered: false, writeConcern: { w:
"majority" }
Found this issue, after trying practice by GraphQL tutorial on Youtube.
To solve it, you need to update your mongoose model to the last version.

Mongoose Async Accessors

Consider the Schemas
const Person = Schema({
name: String,
pets: {
type: [Schema.Types.ObjectId],
ref: 'Pets'
}
})
const Pet = Schema({
name: String,
color: String
})
How is it possible on Person.findBy(personId) to return the following and not an array of ids?
{
name: 'John Doe',
pets: [
{ name: 'Lilah', color: 'white' },
{ name: 'Hanna', color: 'brown' }
]
}
I Tried to use mongoose async accessors
const Person = Schema ({
...
pets: {
type: [Schema.Types.ObjectId],
ref: 'Pets',
get: async pets => Pets.find({ '_id': { $id: pets} })
}
...
})
But feels like overengineering (and it runs the function everytime the property is accessed)

Schema with embedded discriminators in arrays not cloned properly

I have scheme with embedded discriminators, and I want to clone it. But when I've create model from this cloned schema, and try to create document, some properties, related to this discriminators are goes away.
Here the code
const mongoose = require('mongoose');
const propertiesSchema = new mongoose.Schema({
name: { type: String },
},
{ discriminatorKey: 'type', _id: false });
const collectionSchema = new mongoose.Schema({
name: { type: String },
properties: [propertiesSchema]
});
const propertiesArray = collectionSchema.path(`properties`);
propertiesArray.discriminator(`type1`,
new mongoose.Schema({ type1: String }, { _id: false })
);
propertiesArray.discriminator(`type2`,
new mongoose.Schema({ type2: String }, { _id: false })
);
const Collection = new mongoose.model('Collection', collectionSchema);
const Clone = new mongoose.model('Clone', Collection.schema.clone());
const data = {
name: "Collection",
properties: [
{ type: "type1", type1: "type1-1" },
{ type: "type2", type2: "type2-2" }
]
}
console.log(new Collection(data));
console.log(new Clone(data));
Result is:
{ _id: 5d1b583e6d2d8b519c8849b8,
name: 'Collection',
properties:
[ { type: 'type1', type1: 'type1-1' },
{ type: 'type2', type2: 'type2-2' } ] }
{ _id: 5d1b583e6d2d8b519c8849b9,
name: 'Collection',
properties: [ { type: 'type1' }, { type: 'type2' } ] }
So question is - why documents are different, and how to correctly clone, or "re-apply" discriminators on cloned scheme ?
node: v10.15.3
mongoose: 5.6.2
Issue will be fixed in version 5.6.4
Github fix commit

How to write a RealmList in a node realm schema

I have a schema for OrderEntry which has a RealmList of another realm object ItemEntry:
export const OrderEntrySchema: Realm.ObjectSchema = {
name: 'OrderEntry',
primaryKey: '_id',
properties: {
...
items: ???
}
}
export const ItemEntrySchema: Realm.ObjectSchema = {
name: 'ItemEntry',
primaryKey: 'id',
properties: {
...
}
}
How to define item as a RealmList<ItemEntry> as can be done in android?
Note: Both schemas are in separate files.
You can specify a list of ItemEntry like this
items: {type: 'list', objectType: 'ItemEntry'}
Here is an example with your schemas.
const OrderEntrySchema = {
name: 'OrderEntry',
primaryKey: 'id',
properties: {
id: 'string',
items: {type: 'list', objectType: 'ItemEntry'}
}
};
const ItemEntrySchema = {
name: 'ItemEntry',
primaryKey: 'id',
properties: {
id: 'string'
}
};
If you want to put them in separate files, you will need to import ItemEntry in the file with OrderEntry

Resources