@shadow-dev/orm
Version:
Lightweight dynamic MySQL ORM designed for ShadowCore and modular apps.
52 lines (51 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.syncSchema = syncSchema;
const core_1 = require("../core");
function normalizeField(value) {
if (typeof value === "string")
return { type: value };
return value;
}
function formatDefault(value) {
if (typeof value === "string")
return `'${value}'`;
if (typeof value === "boolean")
return value ? "TRUE" : "FALSE";
if (value === null || value === undefined)
return "NULL";
return value.toString();
}
function mapType(type) {
switch (type.toLowerCase()) {
case "string": return "VARCHAR(255)";
case "json": return "JSON";
case "datetime": return "DATETIME";
case "number": return "INT";
case "float": return "FLOAT";
case "boolean": return "BOOLEAN";
default: return type;
}
}
async function syncSchema() {
const models = (0, core_1.getAllModels)();
const pool = (0, core_1.getPool)();
for (const [name, model] of models.entries()) {
const columns = [];
for (const [key, value] of Object.entries(model.schema)) {
const { type, pk, default: def, required } = normalizeField(value);
let col = `\`${key}\` ${mapType(type)}`;
if (required || pk)
col += " NOT NULL";
if (pk)
col += " PRIMARY KEY";
if (def !== undefined)
col += ` DEFAULT ${formatDefault(def)}`;
columns.push(col);
}
const fks = model.foreignKeys.map(fk => `FOREIGN KEY (\`${fk.column}\`) REFERENCES ${fk.reference}`);
const sql = `CREATE TABLE IF NOT EXISTS \`${name}\` (\n ${[...columns, ...fks].join(',\n ')}\n);`;
await pool.execute(sql);
}
console.log("✅ Schema synchronized.");
}