quick-erd
Version:
quick and easy text-based ERD + code generator for migration, query, typescript types and orm entity
63 lines (60 loc) • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.textToSqliteProxy = textToSqliteProxy;
const text_to_types_1 = require("./text-to-types");
function textToSqliteProxy(text, options) {
const mode = options?.mode || 'singleton';
const type = options?.type || 'commonjs';
const { tableTypes, proxyFields, schemaFields } = (0, text_to_types_1.textToTypes)(text);
let code = '';
if (mode === 'singleton') {
const importPath = type === 'commonjs' ? './db' : './db.js';
code += `
import { proxySchema } from 'better-sqlite3-proxy'
import { db } from '${importPath}'
`;
}
else if (mode === 'factory') {
code += `
import { proxySchema, ProxySchemaOptions } from 'better-sqlite3-proxy'
`;
}
else {
throw new TypeError('unknown mode: ' + mode);
}
code += `
${tableTypes}
export type DBProxy = {
${proxyFields}
}
`;
if (mode === 'singleton') {
code += `
export let proxy = proxySchema<DBProxy>({
db,
tableFields: {
${schemaFields}
},
})
`;
}
else if (mode === 'factory') {
code += `
export let tableFields: ProxySchemaOptions<DBProxy>['tableFields'] = {
${schemaFields}
}
export function createProxy(
options: Omit<ProxySchemaOptions<DBProxy>, 'tableFields'>,
) {
return proxySchema<DBProxy>({
tableFields,
...options,
})
}
`;
}
else {
throw new TypeError('unknown mode: ' + mode);
}
return (0, text_to_types_1.trimCode)(code);
}