@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
69 lines (65 loc) • 2.66 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/fields/boolean.ts
import { adapterBooleanFieldParser } 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/boolean.ts
var booleanFieldParser = adapterBooleanFieldParser({
translate: /* @__PURE__ */ __name(async (args) => {
const defaultOptions = await args.fieldParser.translate(args);
const field = args.field;
const mainType = args.engine.instance.mainType;
const builderArgsFormatted = getBuilderArgs({
type: mainType === "sqlite" ? "integer" : "boolean",
databaseName: field.databaseName,
args: mainType === "sqlite" ? "{ mode: 'number' }" : void 0
}, (defaultBuilderArgs) => {
if (defaultOptions.primaryKey) defaultBuilderArgs.push([
"primaryKey",
""
]);
if (typeof defaultOptions.default === "boolean") if (mainType === "sqlite") defaultBuilderArgs.push([
"default",
defaultOptions.default ? "1" : "0"
]);
else defaultBuilderArgs.push([
"default",
JSON.stringify(defaultOptions.default)
]);
if (defaultOptions.nullable !== true) defaultBuilderArgs.push([
"notNull",
""
]);
if (defaultOptions.unique) defaultBuilderArgs.push([
"unique",
""
]);
return defaultBuilderArgs;
})(args.customAttributes.args, args.customAttributes.options);
return builderArgsFormatted;
}, "translate")
});
export {
booleanFieldParser
};