UNPKG

@palmares/databases

Version:

Add support for working with databases with palmares framework

84 lines (82 loc) 2.82 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/engine/query/set.ts function adapterSetQuery(args) { let CustomAdapterSetQuery = class CustomAdapterSetQuery extends AdapterSetQuery { static { __name(this, "CustomAdapterSetQuery"); } queryData = args.queryData; }; return CustomAdapterSetQuery; } __name(adapterSetQuery, "adapterSetQuery"); var AdapterSetQuery = class { static { __name(this, "AdapterSetQuery"); } /** * This is a simple upsert query, by default you should always implement this function in your AdapterSetQuery. * * _Note_: If `args.search` is not null or undefined, you should update the data, otherwise you should create it. * _Note 2_: You should return an array, the first argument is true if the data was created, false otherwise. The * second argument is the data that was created or updated. * * @example * ```ts * async queryData( * _: DatabaseAdapter, * args: { * modelOfEngineInstance: ModelCtor<Model>; * search: any; * data?: any; * transaction?: Transaction; * } * ): Promise<[boolean, any][]> { * return Promise.all( * args.data.map(async (eachData: any) => { * if (args.search === undefined) * return [ * true, * ( * await args.modelOfEngineInstance.create(eachData, { * transaction: args.transaction, * }) * ).toJSON(), * ]; * const [instance, hasCreated] = await args.modelOfEngineInstance.upsert(eachData, { * transaction: args.transaction, * returning: true, * }); * return [hasCreated ? hasCreated : false, instance.toJSON()]; * }) * ); * } * ``` * * @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.search - The search argument to search on the database. * @param _args.data - The data to be inserted or updated. * @param _args.transaction - The transaction to use to run the query, That's what you pass to the callback * function on `DatabaseAdapter.transaction`. * * @returns - Returns an array of tuples, the first argument is true if the data was created, false otherwise. * The second argument is the data that was created or updated. */ // eslint-disable-next-line ts/require-await async queryData(_engine, _args) { return _args.data.map((eachData) => [ true, { ...eachData } ]); } }; export { AdapterSetQuery, adapterSetQuery };