mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 13:19:44 +02:00
parent
ad7fb51cfa
commit
2ee1166747
1557 changed files with 76832 additions and 47042 deletions
55
server/api/hooks/query-methods/index.js
Normal file
55
server/api/hooks/query-methods/index.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
/**
|
||||
* query-methods hook
|
||||
*
|
||||
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions,
|
||||
* and/or initialization logic.
|
||||
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function defineQueryMethodsHook(sails) {
|
||||
const addQueryMethods = () => {
|
||||
const queryMethodsByModelName = fs.readdirSync(path.join(__dirname, 'models')).reduce(
|
||||
(result, filename) => ({
|
||||
...result,
|
||||
// eslint-disable-next-line global-require, import/no-dynamic-require
|
||||
[path.parse(filename).name]: require(path.join(__dirname, 'models', filename)),
|
||||
}),
|
||||
{},
|
||||
);
|
||||
|
||||
_(sails.models).forEach((Model) => {
|
||||
const queryMethods = queryMethodsByModelName[Model.globalId];
|
||||
|
||||
if (queryMethods) {
|
||||
Object.assign(Model, {
|
||||
qm: queryMethods,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
/**
|
||||
* Runs when this Sails app loads/lifts.
|
||||
*/
|
||||
|
||||
async initialize() {
|
||||
sails.log.info('Initializing custom hook (`query-methods`)');
|
||||
|
||||
return new Promise((resolve) => {
|
||||
sails.after('hook:orm:loaded', () => {
|
||||
addQueryMethods();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue