@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
67 lines (63 loc) • 2.53 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/fields/field.ts
import { adapterFieldParser } from "@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/field.ts
var fieldParser = adapterFieldParser({
// eslint-disable-next-line ts/require-await
translate: /* @__PURE__ */ __name(async (args) => {
if (args.customAttributes.type) return getBuilderArgs({
type: args.customAttributes.type,
databaseName: args.field.databaseName,
args: args.customAttributes.args
}, (defaultBuilderArgs) => defaultBuilderArgs)(args.customAttributes.args, args.customAttributes.options);
if (args.field.dbIndex) {
args.lazyEvaluate({
type: "index",
indexAttributes: {
modelName: args.modelName,
fieldName: args.field.fieldName,
databaseName: args.field.databaseName,
unique: args.field.unique
}
});
}
const fieldData = {
fieldName: args.field.fieldName,
primaryKey: args.field.primaryKey,
unique: args.field.unique,
nullable: args.field.allowNull,
dbIndex: args.field.dbIndex,
default: args.field.defaultValue,
autoincrement: args.field.isAuto,
databaseName: args.field.databaseName,
custom: args.customAttributes
};
return fieldData;
}, "translate")
});
export {
fieldParser
};