UNPKG

@palmares/databases

Version:

Add support for working with databases with palmares framework

108 lines (106 loc) 3.83 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/engine/query/set.ts var set_exports = {}; __export(set_exports, { AdapterSetQuery: () => AdapterSetQuery, adapterSetQuery: () => adapterSetQuery }); module.exports = __toCommonJS(set_exports); 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 } ]); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AdapterSetQuery, adapterSetQuery });