UNPKG

@palmares/databases

Version:

Add support for working with databases with palmares framework

97 lines (94 loc) 3.75 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/engine/exceptions.ts var NotImplementedAdapterException = class _NotImplementedAdapterException extends Error { static { __name(this, "NotImplementedAdapterException"); } constructor(methodName) { super(`Method ${methodName} was not implemented in your Adapter, it should be in order to fully work.`); this.name = _NotImplementedAdapterException.name; } }; // src/engine/query/get.ts function adapterGetQuery(args) { let CustomAdapterGetQuery = class CustomAdapterGetQuery extends AdapterGetQuery { static { __name(this, "CustomAdapterGetQuery"); } queryData = args.queryData; }; return CustomAdapterGetQuery; } __name(adapterGetQuery, "adapterGetQuery"); var AdapterGetQuery = class { static { __name(this, "AdapterGetQuery"); } /** * This is a simple query, by default you should always implement this function in your AdapterGetQuery. * * This will guarantee that you are able to retrieve the data, it's not much performatic because it will do * many small queries to the database, which might slow things down, but you will be guaranteed to work 100% * with the types. * * For a more performatic approach you should implement `queryDataNatively`. That will translate the query to the * native query, but the second can be harder to implement since it relies on knowing about palmares objects and * model structure. * * A simple Sequelize example: * @example * ```ts * async queryData( * _engine: DatabaseAdapter, * args: { * modelOfEngineInstance: ModelCtor<Model>; * search: any; * fields: readonly string[]; * ordering?: Order; * limit?: number; * offset?: number | string; * } * ) { * const findAllOptions: Parameters<ModelCtor<Model>['findAll']>[0] = { * attributes: args.fields as string[], * where: args.search, * nest: true, * raw: true, * }; * if (args.ordering) findAllOptions.order = args.ordering; * if (args.limit) findAllOptions.limit = args.limit; * if (args.offset) findAllOptions.offset = Number(args.offset); * return args.modelOfEngineInstance.findAll(findAllOptions); * } * ``` * * @param _engine - The engine instance that is running the query. * @param _args - The arguments of the query. * @param _args.modelOfEngineInstance - The model instance to query, this is what your ORM has translated on * `AdapterModel.translate` function. * @param _args.fields - The fields to retrieve from the database, sometimes the user doesn't want to retrieve * all of the fields from the database. * @param _args.ordering - The ordering to use on the query, this ordering is translated from the `parseOrdering` * inside {@link AdapterQueryOrdering} * @param _args.search - The search argument to search on the database. This was translated from the `parseSearch` * inside {@link AdapterQuerySearch} * @param _args.limit - The limit of the query, this is used for pagination. * @param _args.offset - The offset of the query, this is used for pagination. * * @returns - Returns an array, always should return an array, if the data doesn't exist, return an empty array * instead of undefined or null. */ // eslint-disable-next-line ts/require-await async queryData(_engine, _args) { return []; } // eslint-disable-next-line ts/require-await async queryDataNatively(_engine, _modelConstructor, _search, _fields, _includes, _defaultParseSearch) { throw new NotImplementedAdapterException("queryDataNatively"); } }; export { AdapterGetQuery, adapterGetQuery };