@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
73 lines (69 loc) • 3 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/fields/enum.ts
import { adapterEnumFieldParser } 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/enum.ts
var enumFieldParse = adapterEnumFieldParser({
translate: /* @__PURE__ */ __name(async (args) => {
const defaultOptions = await args.fieldParser.translate(args);
const field = args.field;
const mainType = args.engine.instance.mainType;
const optionsAsString = args.field.choices.map((choice) => `"${choice}"`).join(", ");
const enumVariableName = `${field.fieldName}Enum`;
if (mainType === "postgres") {
args.lazyEvaluate({
type: "enum",
data: `export const ${enumVariableName} = d.pgEnum('${field.databaseName}', [${optionsAsString}]);`
});
}
return getBuilderArgs({
type: mainType === "sqlite" ? "text" : mainType === "postgres" ? enumVariableName : "mysqlEnum",
databaseName: field.databaseName,
args: mainType === "sqlite" ? `{ enum: [${optionsAsString}] }` : mainType === "mysql" ? `[${optionsAsString}]` : "",
withoutD: mainType === "postgres" ? true : false
}, (defaultBuilderArgs) => {
if (defaultOptions.primaryKey) defaultBuilderArgs.push([
"primaryKey",
""
]);
if (defaultOptions.default) defaultBuilderArgs.push([
"default",
`${typeof defaultOptions.default === "string" ? `'${defaultOptions.default}'` : defaultOptions.default}`
]);
if (defaultOptions.nullable !== true) defaultBuilderArgs.push([
"notNull",
""
]);
if (defaultOptions.unique) defaultBuilderArgs.push([
"unique",
""
]);
return defaultBuilderArgs;
})(args.customAttributes.args, args.customAttributes.options);
}, "translate")
});
export {
enumFieldParse
};