2017-06-17 17:01:50 +02:00
|
|
|
module.exports = {
|
2017-08-29 19:34:34 +02:00
|
|
|
find: async function (params) {
|
2017-09-14 16:33:56 +02:00
|
|
|
return await this
|
2017-06-17 17:01:50 +02:00
|
|
|
.forge()
|
2017-06-18 17:23:58 +02:00
|
|
|
.fetchAll({
|
2017-09-14 16:33:56 +02:00
|
|
|
withRelated: this.associations.map(x => x.alias).join(' ')
|
2017-06-18 17:23:58 +02:00
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
count: async function (params) {
|
2017-09-14 16:33:56 +02:00
|
|
|
return await this
|
2017-06-17 17:01:50 +02:00
|
|
|
.forge()
|
|
|
|
.count();
|
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
findOne: async function (params) {
|
2017-09-14 18:47:10 +02:00
|
|
|
const record = await this
|
2017-09-14 16:33:56 +02:00
|
|
|
.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey]
|
|
|
|
})
|
2017-09-14 18:47:10 +02:00
|
|
|
.fetch({
|
|
|
|
withRelated: this.associations.map(x => x.alias).join(' ')
|
|
|
|
});
|
|
|
|
|
|
|
|
return record ? record.toJSON() : record;
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
create: async function (params) {
|
|
|
|
return await this
|
2017-06-17 17:01:50 +02:00
|
|
|
.forge()
|
|
|
|
.save(params.values);
|
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
update: async function (params) {
|
|
|
|
return await this
|
|
|
|
.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey]
|
|
|
|
})
|
|
|
|
.save(params.values, {
|
|
|
|
patch: true
|
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
delete: async function (params) {
|
|
|
|
return await params.model
|
|
|
|
.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey]
|
|
|
|
})
|
2017-06-17 17:01:50 +02:00
|
|
|
.destroy();
|
2017-09-14 18:47:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
addRelation: async function (params) {
|
|
|
|
const association = this.associations.filter(x => x.via === params.foreignKey)[0];
|
|
|
|
|
|
|
|
if (!association) {
|
|
|
|
// Resolve silently.
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (association.nature) {
|
|
|
|
case 'oneToOne':
|
|
|
|
case 'oneToMany':
|
|
|
|
return module.exports.update.call(this, params);
|
|
|
|
case 'manyToMany':
|
|
|
|
return this.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey]
|
|
|
|
})[association.alias]().attach(params.values.id);
|
|
|
|
default:
|
|
|
|
// Resolve silently.
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeRelation: async function (params) {
|
|
|
|
const association = this.associations.filter(x => x.via === params.foreignKey)[0];
|
|
|
|
|
|
|
|
if (!association) {
|
|
|
|
// Resolve silently.
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (association.nature) {
|
|
|
|
case 'oneToOne':
|
|
|
|
case 'oneToMany':
|
|
|
|
return module.exports.update.call(this, params);
|
|
|
|
case 'manyToMany':
|
|
|
|
return this.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey]
|
|
|
|
})[association.alias]().detach(params.values.id);
|
|
|
|
default:
|
|
|
|
// Resolve silently.
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-06-17 17:01:50 +02:00
|
|
|
}
|
2017-08-29 19:34:34 +02:00
|
|
|
};
|