78 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-09-20 12:44:24 +02:00
'use strict';
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 {
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') {
throw new Error(
`Custom queries must be functions received ${typeof mapping[this.orm]}`
);
}
return mapping[this.model.orm].call(this, { model: this.model });
}
find(...args) {
2019-09-20 12:44:24 +02:00
return this.connectorQuery.find(...args);
}
findOne(...args) {
2019-09-20 12:44:24 +02:00
return this.connectorQuery.findOne(...args);
}
create(...args) {
return this.connectorQuery.create(...args);
2019-09-20 12:44:24 +02:00
}
update(...args) {
return this.connectorQuery.update(...args);
2019-09-20 12:44:24 +02:00
}
delete(...args) {
return this.connectorQuery.delete(...args);
2019-09-20 12:44:24 +02:00
}
count(...args) {
2019-09-20 12:44:24 +02:00
return this.connectorQuery.count(...args);
}
search(...args) {
2019-09-20 12:44:24 +02:00
return this.connectorQuery.search(...args);
}
countSearch(...args) {
2019-09-20 12:44:24 +02:00
return this.connectorQuery.countSearch(...args);
}
}