From 9c98dc8d24b946225d7b230129a19357458fa3a9 Mon Sep 17 00:00:00 2001 From: Mac Date: Fri, 1 Oct 2021 15:42:59 +0530 Subject: [PATCH] add support for associations --- src/model.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/model.js b/src/model.js index 313f447..5ce261d 100644 --- a/src/model.js +++ b/src/model.js @@ -463,7 +463,15 @@ fakeModel.prototype.build = function (values, options) { } values = _.extend({}, this._defaults, values); - + const keys = Object.keys(this.Instance.prototype); + + keys.forEach(key => { + if (key.substring(0, 10) === 'getValueOf') { + const modelName = Utils.lowercaseFirst(key.split('getValueOf')[1]); + values[modelName] = this.Instance.prototype[key]() + values[Utils.pluralize(modelName)] = this.Instance.prototype[key]() + } + }) return new this.Instance(values, options); }; /** @@ -630,9 +638,11 @@ fakeModel.prototype.belongsTo = fakeModel.prototype.hasOne = function (item, opt self = this, noop = function () { return Promise.resolve(self); }; - if(isString) { + if (isString) { this.Instance.prototype['get' + singular] = function (opts) { return Promise.resolve(new self.Instance(opts && opts.where ? opts.where : opts)); }; + this.Instance.prototype['getValueOf' + singular] = () => new self.Instance(opts && opts.where ? opts.where : opts); } else { + this.Instance.prototype['getValueOf' + singular] = () => item._defaults; this.Instance.prototype['get' + singular] = item.findOne.bind(item); } this.Instance.prototype['set' + singular] = noop;