@stonyx/orm 0.2.7-alpha.0 → 0.3.2-alpha.0
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/README.md +482 -15
- package/config/environment.js +63 -6
- package/dist/aggregates.d.ts +21 -0
- package/dist/aggregates.js +93 -0
- package/dist/attr.d.ts +2 -0
- package/dist/attr.js +22 -0
- package/dist/belongs-to.d.ts +11 -0
- package/dist/belongs-to.js +59 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +148 -0
- package/dist/commands.d.ts +7 -0
- package/dist/commands.js +146 -0
- package/dist/db.d.ts +21 -0
- package/dist/db.js +180 -0
- package/dist/exports/db.d.ts +7 -0
- package/{src → dist}/exports/db.js +2 -4
- package/dist/has-many.d.ts +11 -0
- package/dist/has-many.js +58 -0
- package/dist/hooks.d.ts +75 -0
- package/dist/hooks.js +110 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +39 -0
- package/dist/main.d.ts +62 -0
- package/dist/main.js +231 -0
- package/dist/manage-record.d.ts +13 -0
- package/dist/manage-record.js +123 -0
- package/dist/meta-request.d.ts +6 -0
- package/dist/meta-request.js +52 -0
- package/dist/migrate.d.ts +2 -0
- package/dist/migrate.js +57 -0
- package/dist/model-property.d.ts +9 -0
- package/dist/model-property.js +29 -0
- package/dist/model.d.ts +15 -0
- package/dist/model.js +18 -0
- package/dist/mysql/connection.d.ts +14 -0
- package/dist/mysql/connection.js +24 -0
- package/dist/mysql/migration-generator.d.ts +45 -0
- package/dist/mysql/migration-generator.js +254 -0
- package/dist/mysql/migration-runner.d.ts +12 -0
- package/dist/mysql/migration-runner.js +88 -0
- package/dist/mysql/mysql-db.d.ts +100 -0
- package/dist/mysql/mysql-db.js +425 -0
- package/dist/mysql/query-builder.d.ts +10 -0
- package/dist/mysql/query-builder.js +44 -0
- package/dist/mysql/schema-introspector.d.ts +19 -0
- package/dist/mysql/schema-introspector.js +257 -0
- package/dist/mysql/type-map.d.ts +21 -0
- package/dist/mysql/type-map.js +36 -0
- package/dist/orm-request.d.ts +38 -0
- package/dist/orm-request.js +475 -0
- package/dist/plural-registry.d.ts +4 -0
- package/dist/plural-registry.js +9 -0
- package/dist/postgres/connection.d.ts +15 -0
- package/dist/postgres/connection.js +32 -0
- package/dist/postgres/migration-generator.d.ts +45 -0
- package/dist/postgres/migration-generator.js +280 -0
- package/dist/postgres/migration-runner.d.ts +10 -0
- package/dist/postgres/migration-runner.js +87 -0
- package/dist/postgres/postgres-db.d.ts +119 -0
- package/dist/postgres/postgres-db.js +477 -0
- package/dist/postgres/query-builder.d.ts +27 -0
- package/dist/postgres/query-builder.js +98 -0
- package/dist/postgres/schema-introspector.d.ts +29 -0
- package/dist/postgres/schema-introspector.js +296 -0
- package/dist/postgres/type-map.d.ts +23 -0
- package/dist/postgres/type-map.js +56 -0
- package/dist/record.d.ts +75 -0
- package/dist/record.js +129 -0
- package/dist/relationships.d.ts +10 -0
- package/dist/relationships.js +41 -0
- package/dist/schema-helpers.d.ts +20 -0
- package/dist/schema-helpers.js +48 -0
- package/dist/serializer.d.ts +17 -0
- package/dist/serializer.js +136 -0
- package/dist/setup-rest-server.d.ts +1 -0
- package/dist/setup-rest-server.js +52 -0
- package/dist/standalone-db.d.ts +58 -0
- package/dist/standalone-db.js +142 -0
- package/dist/store.d.ts +62 -0
- package/dist/store.js +286 -0
- package/dist/timescale/query-builder.d.ts +43 -0
- package/dist/timescale/query-builder.js +115 -0
- package/dist/timescale/timescale-db.d.ts +45 -0
- package/dist/timescale/timescale-db.js +84 -0
- package/dist/transforms.d.ts +2 -0
- package/dist/transforms.js +17 -0
- package/dist/types/orm-types.d.ts +153 -0
- package/dist/types/orm-types.js +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +17 -0
- package/dist/view-resolver.d.ts +8 -0
- package/dist/view-resolver.js +171 -0
- package/dist/view.d.ts +11 -0
- package/dist/view.js +18 -0
- package/package.json +64 -11
- package/src/aggregates.ts +109 -0
- package/src/{attr.js → attr.ts} +2 -2
- package/src/belongs-to.ts +90 -0
- package/src/cli.ts +183 -0
- package/src/commands.ts +179 -0
- package/src/db.ts +232 -0
- package/src/exports/db.ts +7 -0
- package/src/has-many.ts +92 -0
- package/src/hooks.ts +151 -0
- package/src/{index.js → index.ts} +17 -2
- package/src/main.ts +288 -0
- package/src/manage-record.ts +161 -0
- package/src/{meta-request.js → meta-request.ts} +17 -14
- package/src/migrate.ts +72 -0
- package/src/model-property.ts +35 -0
- package/src/model.ts +21 -0
- package/src/mysql/connection.ts +43 -0
- package/src/mysql/migration-generator.ts +337 -0
- package/src/mysql/migration-runner.ts +121 -0
- package/src/mysql/mysql-db.ts +543 -0
- package/src/mysql/query-builder.ts +69 -0
- package/src/mysql/schema-introspector.ts +310 -0
- package/src/mysql/type-map.ts +42 -0
- package/src/orm-request.ts +582 -0
- package/src/plural-registry.ts +12 -0
- package/src/postgres/connection.ts +48 -0
- package/src/postgres/migration-generator.ts +370 -0
- package/src/postgres/migration-runner.ts +115 -0
- package/src/postgres/postgres-db.ts +616 -0
- package/src/postgres/query-builder.ts +148 -0
- package/src/postgres/schema-introspector.ts +360 -0
- package/src/postgres/type-map.ts +61 -0
- package/src/record.ts +186 -0
- package/src/relationships.ts +54 -0
- package/src/schema-helpers.ts +59 -0
- package/src/serializer.ts +161 -0
- package/src/setup-rest-server.ts +62 -0
- package/src/standalone-db.ts +185 -0
- package/src/store.ts +373 -0
- package/src/timescale/query-builder.ts +174 -0
- package/src/timescale/timescale-db.ts +119 -0
- package/src/transforms.ts +20 -0
- package/src/types/mysql2.d.ts +49 -0
- package/src/types/orm-types.ts +158 -0
- package/src/types/pg.d.ts +32 -0
- package/src/types/stonyx-cron.d.ts +5 -0
- package/src/types/stonyx-events.d.ts +4 -0
- package/src/types/stonyx-rest-server.d.ts +16 -0
- package/src/types/stonyx-utils.d.ts +33 -0
- package/src/types/stonyx.d.ts +21 -0
- package/src/utils.ts +22 -0
- package/src/view-resolver.ts +211 -0
- package/src/view.ts +22 -0
- package/.claude/project-structure.md +0 -578
- package/.github/workflows/ci.yml +0 -36
- package/.github/workflows/publish.yml +0 -143
- package/src/belongs-to.js +0 -63
- package/src/db.js +0 -80
- package/src/has-many.js +0 -61
- package/src/main.js +0 -119
- package/src/manage-record.js +0 -103
- package/src/model-property.js +0 -29
- package/src/model.js +0 -9
- package/src/orm-request.js +0 -249
- package/src/record.js +0 -100
- package/src/relationships.js +0 -43
- package/src/serializer.js +0 -138
- package/src/setup-rest-server.js +0 -57
- package/src/store.js +0 -211
- package/src/transforms.js +0 -20
- package/stonyx-bootstrap.cjs +0 -30
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import Orm, { store } from '@stonyx/orm';
|
|
2
|
+
import OrmRecord from './record.js';
|
|
3
|
+
import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
|
|
4
|
+
import type Serializer from './serializer.js';
|
|
5
|
+
import { isOrmRecord } from './utils.js';
|
|
6
|
+
|
|
7
|
+
interface CreateRecordOptions {
|
|
8
|
+
isDbRecord?: boolean;
|
|
9
|
+
serialize?: boolean;
|
|
10
|
+
transform?: boolean;
|
|
11
|
+
update?: boolean;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface PendingBelongsToEntry {
|
|
16
|
+
sourceRecord: OrmRecord;
|
|
17
|
+
sourceModelName: string;
|
|
18
|
+
relationshipKey: string;
|
|
19
|
+
relationshipId: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const defaultOptions: CreateRecordOptions = {
|
|
23
|
+
isDbRecord: false,
|
|
24
|
+
serialize: true,
|
|
25
|
+
transform: true
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function createRecord(modelName: string, rawData: { [key: string]: unknown } = {}, userOptions: CreateRecordOptions = {}): OrmRecord {
|
|
29
|
+
const orm = Orm.instance;
|
|
30
|
+
const { initialized } = Orm;
|
|
31
|
+
const options = { ...defaultOptions, ...userOptions };
|
|
32
|
+
|
|
33
|
+
if (!initialized && !options.isDbRecord) throw new Error('ORM is not ready');
|
|
34
|
+
|
|
35
|
+
// Guard: read-only views cannot have records created directly
|
|
36
|
+
if (orm?.isView?.(modelName) && !options.isDbRecord) {
|
|
37
|
+
throw new Error(`Cannot create records for read-only view '${modelName}'`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const modelStore = store.get(modelName);
|
|
41
|
+
const globalRelationships = getGlobalRegistry();
|
|
42
|
+
const pendingRelationships = getPendingRegistry();
|
|
43
|
+
|
|
44
|
+
if (!modelStore) throw new Error(`Model store for '${modelName}' is not registered. Ensure the model is defined before creating records.`);
|
|
45
|
+
|
|
46
|
+
assignRecordId(modelName, rawData);
|
|
47
|
+
const existingRecord = modelStore.get(rawData.id as number | string);
|
|
48
|
+
|
|
49
|
+
if (existingRecord instanceof OrmRecord) {
|
|
50
|
+
// Update the existing record with new data so the last entry wins
|
|
51
|
+
updateRecord(existingRecord, rawData, { ...options, update: true });
|
|
52
|
+
return existingRecord;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const recordClasses = orm.getRecordClasses(modelName);
|
|
56
|
+
const modelClass = recordClasses.modelClass as (new (name: string) => { __name: string; [key: string]: unknown }) | undefined;
|
|
57
|
+
const serializerClass = recordClasses.serializerClass as new (model: { [key: string]: unknown }) => Serializer;
|
|
58
|
+
|
|
59
|
+
if (!modelClass) throw new Error(`A model named '${modelName}' does not exist`);
|
|
60
|
+
|
|
61
|
+
const model = new modelClass(modelName);
|
|
62
|
+
const serializer = new serializerClass(model);
|
|
63
|
+
const record = new OrmRecord(model, serializer);
|
|
64
|
+
|
|
65
|
+
record.serialize(rawData, options);
|
|
66
|
+
modelStore.set(record.id as number | string, record);
|
|
67
|
+
|
|
68
|
+
// populate global hasMany relationships
|
|
69
|
+
const globalHasMany = globalRelationships.get(modelName);
|
|
70
|
+
if (globalHasMany) for (const relationship of globalHasMany) relationship.push(record);
|
|
71
|
+
|
|
72
|
+
// populate pending hasMany relationships and clear the queue
|
|
73
|
+
const pendingHasMany = pendingRelationships.get(modelName)?.get(record.id);
|
|
74
|
+
if (pendingHasMany) {
|
|
75
|
+
for (const relationship of pendingHasMany) relationship.push(record);
|
|
76
|
+
pendingHasMany.splice(0);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Fulfill pending belongsTo relationships
|
|
80
|
+
const pendingBelongsToQueue = getPendingBelongsToRegistry();
|
|
81
|
+
const pendingBelongsToRaw = pendingBelongsToQueue.get(modelName)?.get(record.id);
|
|
82
|
+
const pendingBelongsTo = Array.isArray(pendingBelongsToRaw) ? pendingBelongsToRaw as PendingBelongsToEntry[] : undefined;
|
|
83
|
+
|
|
84
|
+
if (pendingBelongsTo) {
|
|
85
|
+
const belongsToReg = getBelongsToRegistry();
|
|
86
|
+
const hasManyReg = getHasManyRegistry();
|
|
87
|
+
|
|
88
|
+
for (const { sourceRecord, sourceModelName, relationshipKey, relationshipId } of pendingBelongsTo) {
|
|
89
|
+
// Update the belongsTo relationship on the source record
|
|
90
|
+
sourceRecord.__relationships[relationshipKey] = record;
|
|
91
|
+
sourceRecord[relationshipKey] = record; // Also update the direct property
|
|
92
|
+
|
|
93
|
+
// Update the belongsTo relationship registry
|
|
94
|
+
const sourceModelReg = belongsToReg.get(sourceModelName);
|
|
95
|
+
if (sourceModelReg) {
|
|
96
|
+
const targetModelReg = sourceModelReg.get(modelName);
|
|
97
|
+
if (targetModelReg) {
|
|
98
|
+
targetModelReg.set(relationshipId, record);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Wire inverse hasMany if it exists
|
|
103
|
+
const inverseHasMany = hasManyReg.get(modelName)?.get(sourceModelName)?.get(record.id);
|
|
104
|
+
|
|
105
|
+
if (inverseHasMany && !inverseHasMany.includes(sourceRecord)) {
|
|
106
|
+
inverseHasMany.push(sourceRecord);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Clear the pending queue
|
|
111
|
+
pendingBelongsTo.length = 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return record;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: CreateRecordOptions = {}): void {
|
|
118
|
+
if (!rawData) throw new Error('rawData must be passed in to updateRecord call');
|
|
119
|
+
|
|
120
|
+
// Guard: read-only views cannot be updated
|
|
121
|
+
const modelName = record?.__model?.__name;
|
|
122
|
+
if (modelName && Orm.instance?.isView?.(modelName)) {
|
|
123
|
+
throw new Error(`Cannot update records for read-only view '${modelName}'`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const options = { ...defaultOptions, ...userOptions, update: true };
|
|
127
|
+
|
|
128
|
+
record.serialize(rawData, options);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* gets the next available id based on last record entry.
|
|
133
|
+
*
|
|
134
|
+
* In MySQL mode with numeric IDs, assigns a temporary pending ID.
|
|
135
|
+
* MySQL's AUTO_INCREMENT provides the real ID after INSERT.
|
|
136
|
+
*/
|
|
137
|
+
function assignRecordId(modelName: string, rawData: { [key: string]: unknown }): void {
|
|
138
|
+
if (rawData.id) return;
|
|
139
|
+
|
|
140
|
+
// In SQL mode with numeric IDs, defer to database auto-increment
|
|
141
|
+
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
142
|
+
rawData.id = `__pending_${Date.now()}_${Math.random()}`;
|
|
143
|
+
rawData.__pendingSqlId = true;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const storeMap = store.get(modelName);
|
|
148
|
+
if (!storeMap) throw new Error(`Cannot assign record ID: model "${modelName}" not found in store`);
|
|
149
|
+
const modelStore = Array.from(storeMap.values()).filter(isOrmRecord);
|
|
150
|
+
const lastRecord = modelStore.at(-1);
|
|
151
|
+
rawData.id = lastRecord ? (lastRecord.id as number) + 1 : 1;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function isStringIdModel(modelName: string): boolean {
|
|
155
|
+
const modelClass = Orm.instance.getRecordClasses(modelName).modelClass as (new (name: string) => { [key: string]: unknown }) | undefined;
|
|
156
|
+
if (!modelClass) return false;
|
|
157
|
+
|
|
158
|
+
const model = new modelClass(modelName);
|
|
159
|
+
|
|
160
|
+
return (model.id as { type?: string } | undefined)?.type === 'string';
|
|
161
|
+
}
|
|
@@ -1,37 +1,40 @@
|
|
|
1
1
|
import { Request } from '@stonyx/rest-server';
|
|
2
|
+
import ModelProperty from './model-property.js';
|
|
2
3
|
import Orm from '@stonyx/orm';
|
|
3
4
|
import config from 'stonyx/config';
|
|
4
5
|
import { dbKey } from './db.js';
|
|
5
6
|
|
|
6
7
|
export default class MetaRequest extends Request {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
handlers: Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
constructor(...args: unknown[]) {
|
|
11
|
+
super(...args);
|
|
12
|
+
|
|
10
13
|
this.handlers = {
|
|
11
14
|
get: {
|
|
12
15
|
'/meta': () => {
|
|
13
16
|
try {
|
|
14
|
-
const { models } = Orm.instance;
|
|
15
|
-
const metadata = {};
|
|
17
|
+
const { models } = Orm.instance as Orm;
|
|
18
|
+
const metadata: Record<string, Record<string, unknown>> = {};
|
|
16
19
|
|
|
17
20
|
for (const [modelName, modelClass] of Object.entries(models)) {
|
|
18
21
|
const name = modelName.slice(0, -5).toLowerCase();
|
|
19
22
|
|
|
20
23
|
if (name === dbKey) continue;
|
|
21
24
|
|
|
22
|
-
const model = new modelClass(modelName);
|
|
23
|
-
const properties = {};
|
|
25
|
+
const model = new (modelClass as new (name: string) => Record<string, unknown>)(modelName);
|
|
26
|
+
const properties: Record<string, unknown> = {};
|
|
24
27
|
|
|
25
28
|
// Get regular properties and relationships
|
|
26
29
|
for (const [key, property] of Object.entries(model)) {
|
|
27
30
|
// Skip internal properties
|
|
28
31
|
if (key.startsWith('__')) continue;
|
|
29
32
|
|
|
30
|
-
if (property
|
|
31
|
-
properties[key] = { type: property.type };
|
|
33
|
+
if (property instanceof ModelProperty) {
|
|
34
|
+
properties[key] = { type: (property as { type: string }).type };
|
|
32
35
|
} else if (typeof property === 'function') {
|
|
33
|
-
const isBelongsTo = property
|
|
34
|
-
const isHasMany = property
|
|
36
|
+
const isBelongsTo = (property as { __relationshipType?: string }).__relationshipType === 'belongsTo';
|
|
37
|
+
const isHasMany = (property as { __relationshipType?: string }).__relationshipType === 'hasMany';
|
|
35
38
|
|
|
36
39
|
if (isBelongsTo || isHasMany) properties[key] = { [isBelongsTo ? 'belongsTo' : 'hasMany']: name };
|
|
37
40
|
}
|
|
@@ -40,16 +43,16 @@ export default class MetaRequest extends Request {
|
|
|
40
43
|
metadata[name] = properties;
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
return metadata;
|
|
46
|
+
return metadata;
|
|
44
47
|
} catch (error) {
|
|
45
|
-
return { error: error.message };
|
|
48
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
46
49
|
}
|
|
47
50
|
},
|
|
48
51
|
},
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
54
|
|
|
52
|
-
auth() {
|
|
55
|
+
auth(): number | undefined {
|
|
53
56
|
if (!config.orm.restServer.metaRoute) return 403;
|
|
54
57
|
}
|
|
55
58
|
}
|
package/src/migrate.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import config from 'stonyx/config';
|
|
2
|
+
import Orm from '@stonyx/orm';
|
|
3
|
+
import { createFile, createDirectory, readFile, updateFile, deleteDirectory } from '@stonyx/utils/file';
|
|
4
|
+
import { dbKey } from './db.js';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
|
|
7
|
+
function getCollectionKeys(): string[] {
|
|
8
|
+
const SchemaClass = (Orm.instance as Orm).models[`${dbKey}Model`] as new () => Record<string, unknown>;
|
|
9
|
+
const instance = new SchemaClass();
|
|
10
|
+
const keys: string[] = [];
|
|
11
|
+
|
|
12
|
+
for (const key of Object.keys(instance)) {
|
|
13
|
+
if (key === '__name' || key === 'id') continue;
|
|
14
|
+
if (typeof instance[key] === 'function') keys.push(key);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return keys;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getDirPath(): string {
|
|
21
|
+
const { rootPath } = config;
|
|
22
|
+
const { file, directory } = config.orm.db;
|
|
23
|
+
const dbDir = path.dirname(path.resolve(`${rootPath}/${file}`));
|
|
24
|
+
|
|
25
|
+
return path.join(dbDir, directory);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function fileToDirectory(): Promise<void> {
|
|
29
|
+
const { rootPath } = config;
|
|
30
|
+
const { file } = config.orm.db;
|
|
31
|
+
const dbFilePath = path.resolve(`${rootPath}/${file}`);
|
|
32
|
+
const collectionKeys = getCollectionKeys();
|
|
33
|
+
const dirPath = getDirPath();
|
|
34
|
+
|
|
35
|
+
// Read full data from db.json
|
|
36
|
+
const data = await readFile(dbFilePath, { json: true }) as Record<string, unknown[]>;
|
|
37
|
+
|
|
38
|
+
// Create directory and write each collection
|
|
39
|
+
await createDirectory(dirPath);
|
|
40
|
+
|
|
41
|
+
await Promise.all(collectionKeys.map(key =>
|
|
42
|
+
createFile(path.join(dirPath, `${key}.json`), data[key] || [], { json: true })
|
|
43
|
+
));
|
|
44
|
+
|
|
45
|
+
// Overwrite db.json with empty-array skeleton
|
|
46
|
+
const skeleton: Record<string, unknown[]> = {};
|
|
47
|
+
for (const key of collectionKeys) skeleton[key] = [];
|
|
48
|
+
|
|
49
|
+
await updateFile(dbFilePath, skeleton, { json: true });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function directoryToFile(): Promise<void> {
|
|
53
|
+
const { rootPath } = config;
|
|
54
|
+
const { file } = config.orm.db;
|
|
55
|
+
const dbFilePath = path.resolve(`${rootPath}/${file}`);
|
|
56
|
+
const collectionKeys = getCollectionKeys();
|
|
57
|
+
const dirPath = getDirPath();
|
|
58
|
+
|
|
59
|
+
// Read each collection from the directory
|
|
60
|
+
const assembled: Record<string, unknown> = {};
|
|
61
|
+
|
|
62
|
+
await Promise.all(collectionKeys.map(async key => {
|
|
63
|
+
const filePath = path.join(dirPath, `${key}.json`);
|
|
64
|
+
assembled[key] = await readFile(filePath, { json: true });
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
// Overwrite db.json with full assembled data
|
|
68
|
+
await updateFile(dbFilePath, assembled, { json: true });
|
|
69
|
+
|
|
70
|
+
// Remove the directory
|
|
71
|
+
await deleteDirectory(dirPath);
|
|
72
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import Orm from '@stonyx/orm';
|
|
2
|
+
|
|
3
|
+
function validType(type: string): boolean {
|
|
4
|
+
return Object.keys(Orm.instance.transforms).includes(type);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default class ModelProperty {
|
|
8
|
+
readonly __kind = 'ModelProperty' as const;
|
|
9
|
+
type: string;
|
|
10
|
+
private _value: unknown;
|
|
11
|
+
ignoreFirstTransform?: boolean;
|
|
12
|
+
|
|
13
|
+
constructor(type: string = 'passthrough', defaultValue?: unknown) {
|
|
14
|
+
if (!validType(type)) throw new Error(`Invalid model property type: ${type}`);
|
|
15
|
+
|
|
16
|
+
this.type = type;
|
|
17
|
+
this.value = defaultValue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get value(): unknown {
|
|
21
|
+
return this._value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
set value(newValue: unknown) {
|
|
25
|
+
if (this.ignoreFirstTransform) {
|
|
26
|
+
delete this.ignoreFirstTransform;
|
|
27
|
+
this._value = newValue;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (newValue === undefined) return;
|
|
32
|
+
|
|
33
|
+
this._value = newValue === null ? null : Orm.instance.transforms[this.type](newValue);
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import attr from './attr.js';
|
|
2
|
+
|
|
3
|
+
export default class Model {
|
|
4
|
+
/**
|
|
5
|
+
* Controls whether records of this model are loaded into memory on startup.
|
|
6
|
+
*
|
|
7
|
+
* - true → loaded on boot, kept in store
|
|
8
|
+
* - false → never cached; find() always queries MySQL (default)
|
|
9
|
+
*
|
|
10
|
+
* Override in subclass: static memory = true;
|
|
11
|
+
*/
|
|
12
|
+
static memory: boolean = false;
|
|
13
|
+
static pluralName: string | undefined = undefined;
|
|
14
|
+
|
|
15
|
+
id = attr('number');
|
|
16
|
+
__name: string;
|
|
17
|
+
|
|
18
|
+
constructor(name: string) {
|
|
19
|
+
this.__name = name;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Pool } from 'mysql2/promise';
|
|
2
|
+
|
|
3
|
+
interface MysqlConfig {
|
|
4
|
+
host: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
user: string;
|
|
7
|
+
password: string;
|
|
8
|
+
database: string;
|
|
9
|
+
connectionLimit?: number;
|
|
10
|
+
migrationsTable?: string;
|
|
11
|
+
migrationsDir?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let pool: Pool | null = null;
|
|
15
|
+
|
|
16
|
+
export async function getPool(mysqlConfig: MysqlConfig): Promise<Pool> {
|
|
17
|
+
if (pool) return pool;
|
|
18
|
+
|
|
19
|
+
const mysql = await import('mysql2/promise');
|
|
20
|
+
|
|
21
|
+
pool = mysql.createPool({
|
|
22
|
+
host: mysqlConfig.host,
|
|
23
|
+
port: mysqlConfig.port,
|
|
24
|
+
user: mysqlConfig.user,
|
|
25
|
+
password: mysqlConfig.password,
|
|
26
|
+
database: mysqlConfig.database,
|
|
27
|
+
connectionLimit: mysqlConfig.connectionLimit,
|
|
28
|
+
waitForConnections: true,
|
|
29
|
+
enableKeepAlive: true,
|
|
30
|
+
keepAliveInitialDelay: 10000,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return pool;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function closePool(): Promise<void> {
|
|
37
|
+
if (!pool) return;
|
|
38
|
+
|
|
39
|
+
await pool.end();
|
|
40
|
+
pool = null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type { MysqlConfig };
|