Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.2
8.9.1
67 changes: 35 additions & 32 deletions lib/belongsTo.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
var ObjectId = require('mongoose').Schema.ObjectId,
i = require('i')();

module.exports = function (schema, associationName, options) {
options = options || {};
var paths = {},
pathName = associationName,
idCast = {
type: ObjectId,
ref: (options.modelName || i.classify(associationName)),
relationshipType: 'belongsTo',
index: true,
required: !!options.required
};

if (options.polymorphic) {
idCast.polymorphic = true;
'use strict';

var typeCast = {
polymorphic: true,
type: String,
required: idCast.required,
enum: options.enum
module.exports = function (mongoose, i) {
return function (associationName, options) {
options = options || {};
let paths = {};
let pathName = associationName;
let idCast = {
type: mongoose.Schema.ObjectId,
ref: (options.modelName || i.classify(associationName)),
relationshipType: 'belongsTo',
index: true,
required: !!options.required
};

paths[pathName + '_type'] = typeCast;
}
if (options.polymorphic) {
idCast.polymorphic = true;

let typeCast = {
polymorphic: true,
type: String,
required: idCast.required,
enum: options.enum
};

paths[pathName] = idCast;
paths[pathName + '_type'] = typeCast;
}

schema.add(paths);
paths[pathName] = idCast;

if (options.touch) {
schema.pre('save', function(next) {
this.populate(associationName, function(err, model) {
this[associationName].__touch(next);
}.bind(this));
});
this.add(paths);

if (options.touch) {
this.pre('save', function (next) {
if (this.isNew) { return next(); }
this.populate(associationName, (err, model) => {
this[associationName].__touch(next);
});
});
};
};

};
17 changes: 9 additions & 8 deletions lib/hasAndBelongsToMany.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

let mongoose = require('mongoose');
let i = require('i')();
module.exports = function (mongoose, i) {

return function hasAndBelongsToMany (model, options) {
let modelName = i.classify(model);
let path = { };
path[model] = [{ type: mongoose.Schema.ObjectId, ref: modelName }];
this.add(path);
this.paths[model].options.siblingPathName = options.inverseOf;
};

module.exports = function hasAndBelongsToMany (schema, model, options) {
let modelName = i.classify(model);
let path = { };
path[model] = [{ type: mongoose.Schema.ObjectId, ref: modelName }];
schema.add(path);
schema.paths[model].options.siblingPathName = options.inverseOf;
};
Loading