UNPKG

@node-in-layers/core

Version:

The core library for the Node In Layers rapid web development framework.

56 lines 1.91 kB
import merge from 'lodash/merge.js'; import { memoizeValueSync } from '../utils.js'; /** * Creates Model Cruds for a given model. * @param model - The model to wrap * @param options - Additional options and overrides */ const createModelCruds = (model, options) => { const _getModel = memoizeValueSync(() => { if (typeof model === 'function') { return model(); } return model; }); const createFunction = (data) => { // @ts-ignore const instance = _getModel().create(data); return instance.save(); }; const retrieveFunction = (primaryKey) => { return _getModel().retrieve(primaryKey); }; const updateFunction = (primaryKey, data) => { const pkName = _getModel().getModelDefinition().primaryKeyName; // @ts-ignore const instance = _getModel().create(merge({ [pkName]: primaryKey }, data)); return instance.save(); }; const deleteFunction = (primaryKey) => { return _getModel().delete(primaryKey); }; const searchFunction = (ormSearch) => { return _getModel().search(ormSearch); }; const bulkInsertFunction = async (data) => { const model = _getModel(); await model.bulkInsert(data.map(x => model.create(x))); }; const bulkDeleteFunction = async (primaryKeys) => { await _getModel().bulkDelete(primaryKeys); }; return { getModel: _getModel, create: createFunction, retrieve: retrieveFunction, update: updateFunction, delete: deleteFunction, search: searchFunction, bulkInsert: bulkInsertFunction, bulkDelete: bulkDeleteFunction, // If we have overrides this will overwrite our function ...(options && options.overrides ? options.overrides : {}), }; }; export { createModelCruds }; //# sourceMappingURL=libs.js.map