What is the difference between "describe" and "schema.define"? - node.js

As I've progressed thru the world of CompoundJS I've came across two ways of defining a schema:
First:
var Product = describe('Product', function () {
property('upc', String);
property('name', String);
set('restPath', pathTo.products);
});
Second:
var Schema = require('jugglingdb').Schema;
var schema = new Schema('memory');
var Product = schema.define('Product', {
upc: { type: Number, index: true },
name: { type: String, limit: 150, index: true },
createdAt: { type: Date, default: Date.now },
modifiedAt: { type: Date, default: Date.now }
}, {
restPath: pathTo.products
});
The first, works, but looks like an old design. The second, does not work, but this is the way to do it according to JugglingDB docs.
Which one should I use? Why wouldn't the second one work for me?
UPDATE:
This is the error I get when I use the second one:
Express
500 TypeError: Cannot call method 'all' of undefined in products controller during "index" action
at Object.index (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\app\controllers\products.js:47:13)
at Array.2 (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:150:28)
at ActionContext.run [as innerNext] (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:103:31)
at Controller.BaseController.next (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\base.js:107:22)
at Controller.protectFromForgery (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\helpers.js:76:21)
at Object.protectFromForgeryHook (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\app\controllers\application.js:3:13)
at Array.1 (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:150:28)
at run (C:\Users\Eran\Documents\Relay Foods\nutrition-facts-editor\node_modules\compound\node_modules\kontroller\lib\flow-control.js:103:31)
... snip ...

I think that describe and define are the same. The issue here is the usage scope which is global in my first implementation and local in the second one. So I'll need it to be global in order to work with the rest of the application.

Related

GraphQL mutation that accepts an array of dynamic size in one request as input with NodeJs

I want to pass an object array of [{questionId1,value1},{questionId2,value2},{questionId3,value3}] of dynamic size in GraphQL Mutation with NodeJS
.........
args: {
input: {
type: new GraphQLNonNull(new GraphQLInputObjectType({
name: 'AssessmentStep3Input',
fields: {
questionId:{
name:'Question ID',
type: new GraphQLNonNull(GraphQLID)
},
value:{
name:'Question Value',
type: new GraphQLNonNull(GraphQLBoolean)
}
}
}))
}
},
.........
How can I do that with the given sample of code?
Thanks
If you want to pass an object array with GraphQL Mutation you need to use "GraphQLList" which allows you to pass an array with dynamic size of given input.
Here is the example
........
........
args: {
input: {
type: new GraphQLNonNull(GraphQLList(new GraphQLInputObjectType({
name: 'AssessmentStep3Input',
fields: {
questionId:{
name:'Question ID',
type: new GraphQLNonNull(GraphQLID)
},
value:{
name:'Question Value',
type: new GraphQLNonNull(GraphQLBoolean)
}
}
}))
)
}
},
........
........
Hope it helps.
Thanks
i just published the article on that, so that you can take a look if you would like to know more detail. This is the repository with the examples, where the createUsers mutation is implemented https://github.com/atherosai/express-graphql-demo/blob/feature/5-modifiers/server/graphql/users/userMutations.js. You can take a look how it is implemented, but in general the above answer is correct. You can input as many objects as you would like to in the array (if you have not implemented some number of items limiting, but it is not there by default).

Mongoose create new data with incremented index-like field

What I want to do:
Whenever I add a new item to the collection (in my case a game), it will have a incremented "index"-like value (in my case I'm naming it index too).
My games collection should looks like:
[
{ "index":0, ... data }
{ "index":1, ... data }
{ "index":2, ... data }
]
The term is so hard to search. I always end up with:
$inc for update. Not this, I want to have incremented number on create.
Schema.index does look like what I want, but somehow it doesn't work at all:
const gameModel = new Schema({
index: {
type: Number,
default: 0
},
players: [{
name: String,
score: Number
}]
}, {
timestamps: {
createdAt: 'date'
}
});
gameModel.index({
index: 1
});
With this I always get index: 0. If I turn off default no index is created.
What do I do now? (I would prefer to keep the _id intact)
You can use a npm package named mongoose-auto-increment which provides this functionality. It is also very easy and well documented

String + autoincrement number on mongoose

I want to create a String + number every time a value is inserted on database (the number must be autoincrement).
Is it possible to do in Schema? Or do I need to do that before the value's inserted on database?
'question' -> String followed by an autoincrement number
var random = [{
question: 'question1'
},{
question: 'question2'
},{
question: 'question3'
},{
question: 'question4'
}];
const RandomSchema = new Schema({
question: {
type: String,
unique: true
}
})
Autoincrement fields in Mongodb, don't work exactly the same way that they do in an RDBMS. There is a lot more overhead involved. Never the less, creating an auto field is a solved problem. There is also a third party mongoose-autoincrement package that make it a lot easier.
Extending, from that. Your problem is a solved problem too. Because
the string part will always be the same
Simply use string concatenation to prepend 'question' to the auto increment field at display time.
Here is what I implemented with one of the approaches #e4c5 pointed out, without the use of a third-party package.
Define add.js as below:
db.counters.insert(
{
_id: "itemid",
seq: 0
}
)
function getNextSequence(id) {
var ret = db.counters.findAndModify(
{
query: { _id: id },
update: { $inc: { seq: 1 } },
new: true
}
);
return ret.seq;
}
db.randoms.insert(
{
question: "question"+getNextSequence("itemid"),
}
)
db.randoms.insert(
{
question: "question"+getNextSequence("itemid"),
}
)
After starting a mongod instance, do mongo < add.js.

Trying to understand why simple jsonix unmarshalling is failing

I am new to jsonix and interested mostly in using it to unmarshall xml data. I wrote a very basic test example but have been unsuccessful in getting it to work.
var MyModule = {
name: 'MyModule',
typeInfos: [{
type: 'classInfo',
localName: 'AnyElementType',
propertyInfos: [{
type: 'anyElement',
allowDom: true,
allowTypedObject:true,
name: 'any',
collection: false
}]
}],
elementInfos: [{
elementName: 'sos:Capabilities',
typeInfo: 'MyModule.AnyElementType'
}]
};
var context = new Jsonix.Context([MyModule], {namespacePrefixes: {'http://www.opengis.net/sos/2.0':'sos'}});
var unmarshaller = context.createUnmarshaller();
var data = unmarshaller.unmarshalString('<sos:Capabilities version=\"2.0.0\">hello</sos:Capabilities>');
return data;
I hardcoded a single simple element that has a namespace and contains 'hello' for the test xml. I was interested in the 'any element mapping' for generic unmarshalling. I feel like I have the namespace configured appropriately etc when creating the context yet I keep getting the following error:
Element [sos:Capabilities] could not be unmarshalled as is not known in this context and the property does not allow DOM content. Thoughts? and thanks in advance.
Disclaimer: I am the author of Jsonix.
There are two issues here.
First, you're missing xmlns:sos="http://www.opengis.net/sos/2.0" in your XML.
Second, currently you'll need to define the element name as an object with namespaceURI and localPart. If you just use string, Jsonix will use defaultElementNamespaceURI (which is not defined here). The namespacePrefixes option is currently not applied in elementInfos. This would be a fine feature, please file an issue if you want this.
Here's a working JSFiddle with you module.
var MyModule = {
name: 'MyModule',
typeInfos: [{
type: 'classInfo',
localName: 'AnyElementType',
propertyInfos: [{
type: 'anyElement',
allowDom: true,
allowTypedObject: true,
name: 'any',
collection: false
}]
}],
elementInfos: [{
elementName: {
namespaceURI: 'http://www.opengis.net/sos/2.0',
localPart: 'Capabilities'
},
// 'sos:Capabilities',
typeInfo: 'MyModule.AnyElementType'
}]
};
var context = new Jsonix.Context([MyModule], {
namespacePrefixes: {
'http://www.opengis.net/sos/2.0': 'sos'
}
});
var unmarshaller = context.createUnmarshaller();
var data = unmarshaller.unmarshalString('<sos:Capabilities version=\"2.0.0\" xmlns:sos=\"http://www.opengis.net/sos/2.0\">hello</sos:Capabilities>');
console.log(data);

Sequelize is returning integer as string

I using nodejs v4 with sequelize, and I have a model like this:
var Device = sequelize.define('Device', {
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true
},
tenantId: {
type: DataTypes.BIGINT,
allowNull: false
},
token: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'devices'
});
When I select a device by id the type of id is a string, exemple:
Device.findById(9).then( function(result) {
console.log(result.toJSON().id + 10);
});
The output will be 910, rather than 19, so I look at json and a saw this:
{
id: "9"
tenantId: "123"
token: "adsadsdsa"
}
The id in found device is a string, but I defined it as a number...
Doesn't it should be { "id": 9 } ?
How can I select a device with the types that I defined previously?
BIGINT maximum value is 2^63-1, javascript can safely represent up to 2^53. To be on the safe side libraries return those numbers as strings.
If you want to have numbers instead of strings, you can use this library https://github.com/mirek/node-pg-safe-numbers which deals with this issue.
I found a fix to this problem on sequelize repo.
https://github.com/sequelize/sequelize/issues/4523
The pg module used for sequelize returns bigint as string because bigints are not guaranteed to fit into js numbers. So I change my model to use integer (DataTypes.INTEGER)
IMO, a smoother solution is to force parsing of int to int, instead of strings. Checkout this issue and comments.
Trying to put this line before your logic code worked for me, this forces parsing of int8 to int, instead of strings:
require("pg").defaults.parseInt8 = true;
...
try {
const list = await Posts.findAll();
console.log(JSON.stringify(list));
} catch (e) {
console.log(e.message);
}
By default, sequelize try format your results. You can set raw :true for get raw data

Resources