2019-09-20 12:44:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-03-05 17:24:46 +01:00
|
|
|
find(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.find(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
findOne(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.findOne(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
create(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.create(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
update(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.update(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
delete(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.delete(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
count(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.count(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
search(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.search(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:24:46 +01:00
|
|
|
countSearch(params = {}, ...args) {
|
|
|
|
const newParams = replaceIdByPrimaryKey(params, this.model);
|
|
|
|
return this.connectorQuery.countSearch(newParams, ...args);
|
2019-09-20 12:44:24 +02:00
|
|
|
}
|
|
|
|
}
|