UNPKG

quick-erd

Version:

quick and easy text-based ERD + code generator for migration, query, typescript types and orm entity

70 lines (67 loc) 1.7 kB
"use strict"; 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 command = options?.command || 'npx erd-to-proxy < erd.txt > proxy.ts'; const { tableTypes, proxyFields, schemaFields } = (0, text_to_types_1.textToTypes)(text); let code = ` /** * This file is auto generated, do not edit it manually. * * update command: ${command} */ `.trimStart(); 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); }