2025-08-23 00:03:20 +02:00
|
|
|
/*!
|
|
|
|
|
* Copyright (c) 2024 PLANKA Software GmbH
|
|
|
|
|
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const makeWhereQueryBuilder = (Model) => (criteria) => {
|
|
|
|
|
if (_.isPlainObject(criteria)) {
|
|
|
|
|
if (Object.keys(criteria).length === 0) {
|
|
|
|
|
throw new Error('Empty criteria');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parts = [];
|
|
|
|
|
const values = [];
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
|
for (const [key, value] of Object.entries(criteria)) {
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
|
const columnName = Model._transformer._transformations[key];
|
|
|
|
|
|
|
|
|
|
if (!columnName) {
|
|
|
|
|
throw new Error('Unknown column');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values.push(value);
|
2025-08-25 17:23:59 +02:00
|
|
|
parts.push(`${columnName} = $${values.length}`);
|
2025-08-23 00:03:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [parts.join(' AND '), values];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['id = $1', [criteria]];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
makeWhereQueryBuilder,
|
|
|
|
|
};
|