@shadow-dev/orm 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/Model.d.ts +13 -4
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/syncSchema.js +37 -18
- package/dist/utils/types.d.ts +3 -0
- package/dist/utils/types.js +2 -0
- package/package.json +1 -1
package/dist/core/Model.d.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
export interface BaseSchema {
|
|
2
|
-
id
|
|
2
|
+
id?: string;
|
|
3
3
|
data?: any;
|
|
4
|
-
createdAt
|
|
4
|
+
createdAt?: Date;
|
|
5
5
|
}
|
|
6
6
|
export interface ForeignKeyDefinition {
|
|
7
7
|
column: string;
|
|
8
8
|
reference: string;
|
|
9
9
|
}
|
|
10
|
+
export type SimpleFieldType = "string" | "int" | "float" | "boolean" | "json" | "datetime";
|
|
11
|
+
export interface FieldOptions {
|
|
12
|
+
type: SimpleFieldType;
|
|
13
|
+
pk?: boolean;
|
|
14
|
+
required?: boolean;
|
|
15
|
+
default?: any;
|
|
16
|
+
}
|
|
17
|
+
export type SchemaValue = SimpleFieldType | FieldOptions;
|
|
18
|
+
export type FlexibleSchema<T> = Record<keyof T, SchemaValue>;
|
|
10
19
|
export declare class Model<T extends Partial<BaseSchema> = BaseSchema> {
|
|
11
20
|
readonly name: string;
|
|
12
|
-
readonly schema:
|
|
21
|
+
readonly schema: FlexibleSchema<T>;
|
|
13
22
|
readonly foreignKeys: ForeignKeyDefinition[];
|
|
14
|
-
constructor(name: string, schema:
|
|
23
|
+
constructor(name: string, schema: FlexibleSchema<T>, foreignKeys?: ForeignKeyDefinition[]);
|
|
15
24
|
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./getNextId"), exports);
|
|
18
18
|
__exportStar(require("./syncSchema"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
package/dist/utils/syncSchema.js
CHANGED
|
@@ -2,23 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.syncSchema = syncSchema;
|
|
4
4
|
const core_1 = require("../core");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const sql = `CREATE TABLE IF NOT EXISTS \`${name}\` (\n ${columnDefs}\n);`;
|
|
19
|
-
await pool.execute(sql);
|
|
20
|
-
}
|
|
21
|
-
console.log("✅ Schema synchronized.");
|
|
5
|
+
function normalizeField(value) {
|
|
6
|
+
if (typeof value === "string")
|
|
7
|
+
return { type: value };
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
function formatDefault(value) {
|
|
11
|
+
if (typeof value === "string")
|
|
12
|
+
return `'${value}'`;
|
|
13
|
+
if (typeof value === "boolean")
|
|
14
|
+
return value ? "TRUE" : "FALSE";
|
|
15
|
+
if (value === null || value === undefined)
|
|
16
|
+
return "NULL";
|
|
17
|
+
return value.toString();
|
|
22
18
|
}
|
|
23
19
|
function mapType(type) {
|
|
24
20
|
switch (type.toLowerCase()) {
|
|
@@ -26,7 +22,30 @@ function mapType(type) {
|
|
|
26
22
|
case "json": return "JSON";
|
|
27
23
|
case "datetime": return "DATETIME";
|
|
28
24
|
case "number": return "INT";
|
|
25
|
+
case "float": return "FLOAT";
|
|
29
26
|
case "boolean": return "BOOLEAN";
|
|
30
|
-
default: return type;
|
|
27
|
+
default: return type;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function syncSchema() {
|
|
31
|
+
const models = (0, core_1.getAllModels)();
|
|
32
|
+
const pool = (0, core_1.getPool)();
|
|
33
|
+
for (const [name, model] of models.entries()) {
|
|
34
|
+
const columns = [];
|
|
35
|
+
for (const [key, value] of Object.entries(model.schema)) {
|
|
36
|
+
const { type, pk, default: def, required } = normalizeField(value);
|
|
37
|
+
let col = `\`${key}\` ${mapType(type)}`;
|
|
38
|
+
if (required || pk)
|
|
39
|
+
col += " NOT NULL";
|
|
40
|
+
if (pk)
|
|
41
|
+
col += " PRIMARY KEY";
|
|
42
|
+
if (def !== undefined)
|
|
43
|
+
col += ` DEFAULT ${formatDefault(def)}`;
|
|
44
|
+
columns.push(col);
|
|
45
|
+
}
|
|
46
|
+
const fks = model.foreignKeys.map(fk => `FOREIGN KEY (\`${fk.column}\`) REFERENCES ${fk.reference}`);
|
|
47
|
+
const sql = `CREATE TABLE IF NOT EXISTS \`${name}\` (\n ${[...columns, ...fks].join(',\n ')}\n);`;
|
|
48
|
+
await pool.execute(sql);
|
|
31
49
|
}
|
|
50
|
+
console.log("✅ Schema synchronized.");
|
|
32
51
|
}
|