UNPKG

@palmares/drizzle-engine

Version:

This is the engine that wraps the hole drizzle interface in a way palmares can understand and have full control of

103 lines (99 loc) 4.01 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/fields/date.ts var date_exports = {}; __export(date_exports, { dateFieldParser: () => dateFieldParser }); module.exports = __toCommonJS(date_exports); var import_databases = require("@palmares/databases"); // src/fields/utils.ts function getBuilderArgs(builder, callback) { const defaultOptions = callback([]); return (customAttributesArgs, customAttributesOptions) => { const customAttributesDefined = new Set(Object.keys(customAttributesOptions || {})); const builderArgs = Object.entries(customAttributesOptions || {}).map(([method, args]) => { return [ method, args.map((arg) => typeof arg === "object" ? JSON.stringify(arg) : arg.toString()).join(", ") ]; }); for (const [method, args] of defaultOptions) if (!customAttributesDefined.has(method)) builderArgs.push([ method, args ]); const builderArguments = builderArgs.map(([method, args]) => { return (args || []).length > 0 ? `.${method}(${args})` : `.${method}()`; }).join(""); return `${builder.withoutD ? "" : "d."}${builder.type}('${builder.databaseName}'${customAttributesArgs !== void 0 || builder.args ? `, ${customAttributesArgs !== void 0 ? JSON.stringify(customAttributesArgs) : builder.args}` : ""})${builderArguments}`; }; } __name(getBuilderArgs, "getBuilderArgs"); // src/fields/date.ts var dateFieldParser = (0, import_databases.adapterDateFieldParser)({ translate: /* @__PURE__ */ __name(async (args) => { const defaultOptions = await args.fieldParser.translate(args); const field = args.field; const mainType = args.engine.instance.mainType; return getBuilderArgs({ type: mainType === "sqlite" ? "text" : mainType === "postgres" ? "timestamp" : "datetime", databaseName: field.databaseName, args: mainType === "postgres" ? "{ precision: 6, withTimezone: true, mode: 'date' }" : mainType === "sqlite" ? "" : "{ fsp: 6, mode: 'date' }" }, (defaultBuilderArgs) => { if (defaultOptions.primaryKey) defaultBuilderArgs.push([ "primaryKey", "" ]); if (defaultOptions.default) defaultBuilderArgs.push([ "default", defaultOptions.default ]); if (defaultOptions.nullable !== true) defaultBuilderArgs.push([ "notNull", "" ]); if (defaultOptions.unique) defaultBuilderArgs.push([ "unique", "" ]); if (field.autoNowAdd) if (mainType === "sqlite") defaultBuilderArgs.push([ "default", "drzl.sql`(current_timestamp)`" ]); else defaultBuilderArgs.push([ "defaultNow", "" ]); if (field.autoNow) if (mainType === "sqlite") defaultBuilderArgs.push([ "$onUpdate", "() => drzl.sql`CURRENT_TIMESTAMP`" ]); else defaultBuilderArgs.push([ "$onUpdate", "() => drzl.sql`now()`" ]); return defaultBuilderArgs; })(args.customAttributes.args, args.customAttributes.options); }, "translate") }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { dateFieldParser });