I'm looking to find a way to populate virtuals ( or something similar ) to a mongoose model.
I have a model which I would like to have non persisted properties.
I'm attempting to have the ability to have helping pointers such as 'isFavourited : true' on the model init instead of looping through results and decorating these values.
Cheers
Found the answer
Schema.set("toObject", { virtuals: true });
Schema.set("toJSON", { virtuals: true });
will render the virtuals on the object/s when returned from finds
Virtuals will certainly do this for you out of the box:
someSchema.virtual('isFavourited').get(function() {
return true
})
It would give you a transient isFavorited property that can't be set.
What you might want to get arbitrary transient properties on a model is a plugin that looks something like this:
function IHaveContext(schema, init={}) {
schema.virtual('context')
.get(function () {
return this._context;
})
.set(function (item) {
if (!this._context)
this._context = init;
this._context = Object.assign({}, this._context, item);
});
}
Then you apply this to any model, optionally setting up defaults:
let Game = new Schema({ ... })
Game.plugin(IHaveContext, {'isFavourited': false})
Now you have an api to decorate your model with arbitrary properties:
let item - new Game()
item.set('context', {'isFavourited': true});
item.get('context')('isFavourited');
Related
I am new to sequilizejs and basically am trying to refactor code that i've written in the controller and came across classMethods and instanceMethods. I see instance methods defined like so:
/lib/model/db/users.js
module.exports = function(sequelize, DataTypes) {
var instance_methods = get_instance_methods(sequelize);
var User = sequelize.define("User", {
email : {
type : DataTypes.STRING,
allowNull : false
},
}, {
classMethods: class_methods,
instanceMethods : instance_methods,
});
return User;
};
function get_instance_methods(sequelize) {
return {
is_my_password : function( password ) {
return sequelize.models.User.hashify_password( password ) === this.password;
},
};
function get_class_methods(sequelize) {
return {
hashify_password : function( password ) {
return crypto
.createHash('md5')
.update(
password + config.get('crypto_secret'),
(config.get('crypto_hash_encoding') || 'binary')
)
.digest('hex');
},
};
My understanding of the above is that classMethods are generic functions defined for the whole model and instanceMethods are basically a reference to a given row in a table/model, am i right in assuming this ? this would be my primary question.
Also i don't see any reference of classMethods and instanceMethods in the docs HERE. I only found this previous answer HERE. That provides a somewhat comprehensive understanding of the difference between instanceMethods and classMethods.
Basically i'am just trying to confirm weather my understanding matches the intended usage for class vs instance methods and also links to the official docs for the same would be highly appreciated.
The official way to add both static and instance methods is using classes like this:
class User extends Model {
static classLevelMethod() {
return 'foo';
}
instanceLevelMethod() {
return 'bar';
}
getFullname() {
return [this.firstname, this.lastname].join(' ');
}
}
User.init({
firstname: Sequelize.TEXT,
lastname: Sequelize.TEXT
}, { sequelize });
See Models as classes
Your understand is correct. In short: classes can have instances. Models are classes. So, Models can have instances. When working with an instance method, you will notice the this — which is the context, which refers to that particular class/model instance.
Hence, if you have a User model that has:
an instance method called is_my_password
a class model called hashify_password
User.hashify_password('123') will return the hashed version of 123. The User instance is not needed here. hashify_password is general function attached to the User model (class).
Now, if you'd like to call is_my_password() you do need a User instance:
User.findOne({...}).then(function (user) {
if (user.is_my_password('123')) {
// ^ Here we call `is_my_password` as a method of the user instance.
...
}
}).catch(console.error)
In general, when you have functions that do not need the particular model instance data, you will define them as class methods. They are static methods.
And when the function works with the instance data, you define it as instance method to make it easier and nicer to call.
I'm having a little trouble with an integration test for my mongoose application. The problem is, that my unique setting gets constantly ignored. The Schema looks more or less like this (so no fancy stuff in there)
const RealmSchema:Schema = new mongoose.Schema({
Title : {
type : String,
required : true,
unique : true
},
SchemaVersion : {
type : String,
default : SchemaVersion,
enum: [ SchemaVersion ]
}
}, {
timestamps : {
createdAt : "Created",
updatedAt : "Updated"
}
});
It looks like basically all the rules set in the schema are beeing ignored. I can pass in a Number/Boolean where string was required. The only thing that is working is fields that have not been declared in the schema won't be saved to the db
First probable cause:
I have the feeling, that it might have to do with the way I test. I have multiple integration tests. After each one my database gets dropped (so I have the same condition for every test and precondition the database in that test).
Is is possible that the reason is my indices beeing droped with the database and not beeing reinitiated when the next text creates database and collection again? And if this is the case, how could I make sure that after every test I get an empty database that still respects all my schema settings?
Second probable cause:
I'm using TypeScript in this project. Maybe there is something wrong in defining the Schema and the Model. This is what i do.
1. Create the Schema (code from above)
2. Create an Interface for the model (where IRealmM extends the Interface for the use in mongoose)
import { SpecificAttributeSelect } from "../classes/class.specificAttribute.Select";
import { SpecificAttributeText } from "../classes/class.specificAttribute.Text";
import { Document } from "mongoose";
interface IRealm{
Title : String;
Attributes : (SpecificAttributeSelect | SpecificAttributeText)[];
}
interface IRealmM extends IRealm, Document {
}
export { IRealm, IRealmM }
3. Create the model
import { RealmSchema } from '../schemas/schema.Realm';
import { Model } from 'mongoose';
import { IRealmM } from '../interfaces/interface.realm';
// Apply Authentication Plugin and create Model
const RealmModel:Model<IRealmM> = mongoose.model('realm', RealmSchema);
// Export the Model
export { RealmModel }
Unique options is not a validator. Check out this link from Mongoose docs.
OK i finally figured it out. The key issue is described here
Mongoose Unique index not working!
Solstice333 states in his answer that ensureIndex is deprecated (a warning I have been getting for some time now, I thought it was still working though)
After adding .createIndexes() to the model leaving me with the following code it works (at least as far as I'm not testing. More on that after the code)
// Apply Authentication Plugin and create Model
const RealmModel:Model<IRealmM> = mongoose.model('realm', RealmSchema);
RealmModel.createIndexes();
Now the problem with this will be that the indexes are beeing set when you're connection is first established, but not if you drop the database in your process (which at least for me occurs after every integration test)
So in my tests the resetDatabase function will look like this to make sure all the indexes are set
const resetDatabase = done => {
if(mongoose.connection.readyState === 1){
mongoose.connection.db.dropDatabase( async () => {
await resetIndexes(mongoose.models);
done();
});
} else {
mongoose.connection.once('open', () => {
mongoose.connection.db.dropDatabase( async () => {
await resetIndexes(mongoose.models);
done();
});
});
}
};
const resetIndexes = async (Models:Object) => {
let indexesReset: any[] = [];
for(let key in Models){
indexesReset.push(Models[key].createIndexes());
}
Promise.all(indexesReset).then( () => {
return true;
});
}
I would like baffle.where({id: 1}).fetch() to always get typeName attribute as a part of baffle model, without fetching it from baffleType explicitly each time.
The following works for me but it seems that withRelated will load relations if baffle model is fetched directly, not by relation:
let baffle = bookshelf.Model.extend({
constructor: function() {
bookshelf.Model.apply(this, arguments);
this.on('fetching', function(model, attrs, options) {
options.withRelated = options.withRelated || [];
options.withRelated.push('type');
});
},
virtuals: {
typeName: {
get: function () {
return this.related('type').attributes.typeName;
}
}
},
type: function () {
return this.belongsTo(baffleType, 'type_id');
}
});
let baffleType = bookshelf.Model.extend({});
What is the proper way to do that?
Issue on repo is related to Fetched event, However Fetching event is working fine (v0.9.2).
So just for example if you have a 3rd model like
var Test = Bookshelf.model.extend({
tableName : 'test',
baffleField : function(){
return this.belongsTo(baffle)
}
})
and then do Test.forge().fetch({ withRelated : ['baffleField']}), fetching event on baffle will fire. However ORM will not include type (sub Related model) unless you specifically tell it to do so by
Test.forge().fetch({ withRelated : ['baffleField.type']})
However I would try to avoid this if it is making N Query for N records.
UPDATE 1
I was talking about same thing that you were doing on fetching event like
fetch: function fetch(options) {
var options = options || {}
options.withRelated = options.withRelated || [];
options.withRelated.push('type');
// Fetch uses all set attributes.
return this._doFetch(this.attributes, options);
}
in model.extend. However as you can see, this might fail on version changes.
This question is super old, but I'm answering anyway.
I solved this by just adding a new function, fetchFull, which keeps things pretty DRY.
let MyBaseModel = bookshelf.Model.extend({
fetchFull: function() {
let args;
if (this.constructor.withRelated) {
args = {withRelated: this.constructor.withRelated};
}
return this.fetch(args);
},
};
let MyModel = MyBaseModel.extend({
tableName: 'whatever',
}, {
withRelated: [
'relation1',
'relation1.related2'
]
}
);
Then whenever you're querying, you can either call Model.fetchFull() to load everything, or in cases where you don't want to take a performance hit, you can still resort to Model.fetch().
When I make a Sequelize query it returns to me an object (or array) which I'm guessing is a Sequelize model (or array of Models (a collection type??)) but it's not documented anywhere, so I'm just guessing. I would always like the results to be JSON. Is there anything I can pass in the query to force this? I would prefer not to hand massage every result I get back to be JSON if possible.
The documentation show this to return a string:
console.log(JSON.stringify(users))
So there's some built in serialization. Right now I'm doing this, using the undocumented toJSON() method:
query().then(function(result) {
if(result.length) {
return result.toJSON();
} else {
return result.map(function(item) {
return item.toJSON();
});
}
});
which is cumbersome.
You can use raw: true in the Query however this does not always behave as you might expect, especially with associations.
query({
// ...
raw: true
}).then(function(result) {
// Result is JSON!
});
However in the case where you're using associations you may get something like this:
{
foo: true,
"associated.bar": true
}
Instead of what you might expect:
{
foo: true,
associated: {
bar: true
}
}
When you retrieve the model from the database, you can call the .get({ plain: true}) on the result and it will handle the conversion for you. You can assign the value of the function call to a variable. For example
..).then(function(user){
var _user = user.get({ plain: true});
console.log(_user); //Should be valid json object
});
Hope this helps.
If you're doing a query with which has multiple results you should expect an array to be returned. You should find that each element in the result array is a JSON object.
You should be able to access a given field like this: result[0].myfieldname
I want to automatically add a query option for all queries related for a specific mongoose model without affecting other models
I saw this answer where Mongoose.Query is patched and that will affect all mongoose models.
I was able to do this for my soft deleted items. Haven't tested it extensively yet though.
function findNotDeletedMiddleware(next) {
this.where('deleted').equals(false);
next();
}
MySchema.pre('find', findNotDeletedMiddleware);
MySchema.pre('findOne', findNotDeletedMiddleware);
MySchema.pre('findOneAndUpdate', findNotDeletedMiddleware);
MySchema.pre('count', findNotDeletedMiddleware);
I see two possible easy ways to do this:
Alternative #1
Add a static dict with the options you want to be applied to your specific Mongoose schema:
FooSchema.statics.options = {
...
};
Now, when you query you need to do:
Foo.find({}, null, Foo.options, function(err, foos) {
...
});
Alternative #2
Implement a wrapper to the find method that always uses your specific options:
FooSchema.statics.findWithOptions = function(query, next) {
var options = { ... };
this.find(query, null, options, next);
};
And use this method like so:
Foo.findWithOptions({}, function(err, foos) {
...
})
Reusability
To make these wrapper methods more reusable, you can make a dict with all your wrappers:
var withOptionsWrappers = {
findWithOptions: function(query, next) {
this.find(query, null, this.options, next);
},
findByIdWithOptions: ...
findOneWithOptions: ...
...
};
Since we're referring to this there will be no problem reusing this. And now have this be applied to all your schemas along with your schema specific options:
FooSchema.statics = withOptionsWrappers;
FooSchema.statics.options = {
...
};
BarSchema.statics = withOptionsWrappers;
BarSchema.statics.options = {
...
};