2019-09-20 12:44:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-04-20 22:27:20 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
const { replaceIdByPrimaryKey } = require('../utils/primary-key');
|
|
|
|
|
2019-12-17 20:59:57 +01:00
|
|
|
module.exports = function createQuery(opts) {
|
|
|
|
return new Query(opts);
|
2019-09-20 12:44:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Query {
|
2020-01-08 11:12:41 +01:00
|
|
|
constructor({ model, connectorQuery }) {
|
2019-09-20 12:44:24 +02:00
|
|
|
this.connectorQuery = connectorQuery;
|
|
|
|
this.model = model;
|
|
|
|
}
|
|
|
|
|
|
|
|
get orm() {
|
|
|
|
return this.model.orm;
|
|
|
|
}
|
|
|
|
|
|
|
|
get primaryKey() {
|
|
|
|
return this.model.primaryKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
get associations() {
|
|
|
|
return this.model.associations;
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:41:36 +02:00
|
|
|
async executeHook(hook, ...args) {
|
|
|
|
if (_.has(this.model, hook)) {
|
|
|
|
await this.model[hook](...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 12:44:24 +02:00
|
|
|
/**
|
|
|
|
* Run custom database logic
|
|
|
|
*/
|
|
|
|
custom(mapping) {
|
|
|
|
if (typeof mapping === 'function') {
|
|
|
|
return mapping.bind(this, { model: this.model });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mapping[this.orm]) {
|
|
|
|
throw new Error(`Missing mapping for orm ${this.orm}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof mapping[this.orm] !== 'function') {
|
2020-03-05 17:24:46 +01:00
|
|
|
throw new Error(`Custom queries must be functions received ${typeof mapping[this.orm]}`);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mapping[this.model.orm].call(this, { model: this.model });
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:27:20 +02:00
|
|
|
async find(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-20 22:27:20 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeFetchAll', newParams, ...args);
|
|
|
|
const results = await this.connectorQuery.find(newParams, ...args);
|
|
|
|
await this.executeHook('afterFetchAll', results);
|
|
|
|
|
|
|
|
return results;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:27:20 +02:00
|
|
|
async findOne(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-20 22:27:20 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeFetch', newParams, ...args);
|
|
|
|
const result = await this.connectorQuery.findOne(newParams, ...args);
|
|
|
|
await this.executeHook('afterFetch', result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-20 22:27:20 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeCreate', newParams, ...args);
|
|
|
|
const entry = await this.connectorQuery.create(newParams, ...args);
|
|
|
|
await this.executeHook('afterCreate', entry);
|
|
|
|
|
|
|
|
return entry;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:27:20 +02:00
|
|
|
async update(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-20 22:27:20 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeUpdate', newParams, ...args);
|
|
|
|
const entry = await this.connectorQuery.update(newParams, ...args);
|
|
|
|
await this.executeHook('afterUpdate', entry);
|
|
|
|
|
|
|
|
return entry;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:27:20 +02:00
|
|
|
async delete(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-20 22:27:20 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeDestroy', newParams, ...args);
|
|
|
|
const entry = await this.connectorQuery.delete(newParams, ...args);
|
|
|
|
await this.executeHook('afterDestroy', entry);
|
|
|
|
|
|
|
|
return entry;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:41:36 +02:00
|
|
|
async count(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-21 18:41:36 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeCount', newParams, ...args);
|
|
|
|
const count = await this.connectorQuery.count(newParams, ...args);
|
|
|
|
await this.executeHook('afterCount', count);
|
|
|
|
|
|
|
|
return count;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:41:36 +02:00
|
|
|
async search(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-21 18:41:36 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeSearch', newParams, ...args);
|
|
|
|
const results = await this.connectorQuery.search(newParams, ...args);
|
|
|
|
await this.executeHook('afterSearch', results);
|
|
|
|
|
|
|
|
return results;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:41:36 +02:00
|
|
|
async countSearch(params = {}, ...args) {
|
2020-03-05 17:24:46 +01:00
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
2020-04-21 18:41:36 +02:00
|
|
|
|
|
|
|
await this.executeHook('beforeCountSearch', newParams, ...args);
|
|
|
|
const count = await this.connectorQuery.countSearch(newParams, ...args);
|
|
|
|
await this.executeHook('afterCountSearch', count);
|
|
|
|
|
|
|
|
return count;
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
}
|