@cheetah.js/orm 0.1.62 → 0.1.64
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/SqlBuilder.js
CHANGED
|
@@ -285,9 +285,10 @@ class SqlBuilder {
|
|
|
285
285
|
}
|
|
286
286
|
}
|
|
287
287
|
setDefaultValue(values, key, property) {
|
|
288
|
-
|
|
288
|
+
const columnName = property.options.columnName;
|
|
289
|
+
if (typeof values[columnName] !== 'undefined')
|
|
289
290
|
return;
|
|
290
|
-
values[
|
|
291
|
+
values[columnName] = typeof property.options.default === 'function'
|
|
291
292
|
? property.options.default()
|
|
292
293
|
: property.options.default;
|
|
293
294
|
}
|
|
@@ -296,8 +297,9 @@ class SqlBuilder {
|
|
|
296
297
|
properties.forEach(([key, property]) => this.applyOnInsert(values, key, property));
|
|
297
298
|
}
|
|
298
299
|
applyOnInsert(values, key, property) {
|
|
299
|
-
|
|
300
|
-
|
|
300
|
+
const columnName = property.options.columnName;
|
|
301
|
+
values[columnName] = property.options.onInsert();
|
|
302
|
+
this.updatedColumns.push(`${this.statements.alias}."${columnName}" as "${this.statements.alias}_${columnName}"`);
|
|
301
303
|
}
|
|
302
304
|
withUpdatedValues(values, entityOptions) {
|
|
303
305
|
const properties = Object.entries(entityOptions.properties).filter(([_, value]) => value.options.onUpdate);
|
package/dist/orm.service.js
CHANGED
|
@@ -237,22 +237,43 @@ let OrmService = class OrmService {
|
|
|
237
237
|
});
|
|
238
238
|
}
|
|
239
239
|
async onInit(customConfig = {}) {
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
240
|
+
const hasCustomConfig = Object.keys(customConfig).length > 0;
|
|
241
|
+
let config = null;
|
|
242
|
+
let setConfig;
|
|
243
|
+
if (!hasCustomConfig) {
|
|
244
|
+
const configFile = globby.sync('cheetah.config.ts', { absolute: true });
|
|
245
|
+
if (configFile.length === 0) {
|
|
246
|
+
console.log('No config file found!');
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
config = await Promise.resolve(`${configFile[0]}`).then(s => __importStar(require(s)));
|
|
250
|
+
setConfig = config.default;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
setConfig = customConfig;
|
|
244
254
|
}
|
|
245
|
-
const config = await Promise.resolve(`${configFile[0]}`).then(s => __importStar(require(s)));
|
|
246
|
-
const setConfig = Object.keys(customConfig).length > 0 ? customConfig : config.default;
|
|
247
255
|
this.orm.setConnection(setConfig);
|
|
248
256
|
await this.orm.connect();
|
|
249
|
-
if (typeof config.default.entities === 'string') {
|
|
257
|
+
if (config && typeof config.default.entities === 'string') {
|
|
250
258
|
const files = globby.sync([config.default.entities, '!node_modules'], { gitignore: true, absolute: true });
|
|
251
259
|
for (const file of files) {
|
|
252
260
|
await Promise.resolve(`${file}`).then(s => __importStar(require(s)));
|
|
253
261
|
}
|
|
254
262
|
}
|
|
255
|
-
|
|
263
|
+
let entities = core_1.Metadata.get(constants_1.ENTITIES, Reflect);
|
|
264
|
+
if (!entities) {
|
|
265
|
+
const entityPaths = this.getSourceFilePaths();
|
|
266
|
+
const entityFiles = globby.sync(entityPaths.filter(path => path.includes('.entity.') || path.includes('entities/')));
|
|
267
|
+
for (const file of entityFiles) {
|
|
268
|
+
try {
|
|
269
|
+
await Promise.resolve(`${file}`).then(s => __importStar(require(s)));
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
console.warn(`Failed to import entity file ${file}:`, error);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
entities = core_1.Metadata.get(constants_1.ENTITIES, Reflect);
|
|
276
|
+
}
|
|
256
277
|
if (!entities) {
|
|
257
278
|
console.log('No entities found!');
|
|
258
279
|
return;
|
|
@@ -12,6 +12,7 @@ class ModelTransformer {
|
|
|
12
12
|
const optionsMap = this.buildOptionsMap(instanceMap);
|
|
13
13
|
this.populateProperties(data, instanceMap, optionsMap);
|
|
14
14
|
this.linkJoinedEntities(statement, instanceMap, optionsMap);
|
|
15
|
+
this.resetChangedValues(instanceMap);
|
|
15
16
|
return instanceMap[statement.alias];
|
|
16
17
|
}
|
|
17
18
|
createInstances(model, statement) {
|
|
@@ -118,5 +119,17 @@ class ModelTransformer {
|
|
|
118
119
|
appendToArray(existingArray, newItem) {
|
|
119
120
|
return existingArray ? [...existingArray, newItem] : [newItem];
|
|
120
121
|
}
|
|
122
|
+
resetChangedValues(instanceMap) {
|
|
123
|
+
Object.values(instanceMap).forEach(instance => {
|
|
124
|
+
const currentValues = {};
|
|
125
|
+
for (const key in instance) {
|
|
126
|
+
if (!key.startsWith('_') && !key.startsWith('$')) {
|
|
127
|
+
currentValues[key] = instance[key];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
instance._oldValues = currentValues;
|
|
131
|
+
instance._changedValues = {};
|
|
132
|
+
});
|
|
133
|
+
}
|
|
121
134
|
}
|
|
122
135
|
exports.ModelTransformer = ModelTransformer;
|
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
37
|
};
|
|
@@ -21,12 +54,37 @@ const DEFAULT_CONNECTION = {
|
|
|
21
54
|
password: 'postgres',
|
|
22
55
|
driver: bun_pg_driver_1.BunPgDriver,
|
|
23
56
|
};
|
|
57
|
+
const sessionCache = new Map();
|
|
58
|
+
function getCacheKey(options) {
|
|
59
|
+
const connection = resolveConnection(options.connection);
|
|
60
|
+
return JSON.stringify({
|
|
61
|
+
host: connection.host,
|
|
62
|
+
port: connection.port,
|
|
63
|
+
database: connection.database,
|
|
64
|
+
schema: options.schema ?? DEFAULT_SCHEMA,
|
|
65
|
+
entityFile: options.entityFile,
|
|
66
|
+
migrationPath: connection.migrationPath,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
24
69
|
async function withDatabase(arg1, arg2, arg3) {
|
|
25
70
|
const { routine: targetRoutine, options: targetOptions, statements } = await normalizeArgs(arg1, arg2, arg3);
|
|
26
|
-
const
|
|
27
|
-
|
|
71
|
+
const cacheKey = getCacheKey(targetOptions);
|
|
72
|
+
let cachedSession = sessionCache.get(cacheKey);
|
|
28
73
|
const schemaStatements = await resolveSchemaStatements(statements, targetOptions);
|
|
29
|
-
|
|
74
|
+
if (!cachedSession) {
|
|
75
|
+
const session = await createSession(targetOptions);
|
|
76
|
+
cachedSession = {
|
|
77
|
+
orm: session.orm,
|
|
78
|
+
tables: [],
|
|
79
|
+
schema: session.schema,
|
|
80
|
+
};
|
|
81
|
+
sessionCache.set(cacheKey, cachedSession);
|
|
82
|
+
}
|
|
83
|
+
const context = buildContext(cachedSession.orm);
|
|
84
|
+
await dropAndRecreateSchema(context, cachedSession.schema);
|
|
85
|
+
await prepareSchema(context, cachedSession.schema);
|
|
86
|
+
await createTables(context, schemaStatements);
|
|
87
|
+
await targetRoutine(context);
|
|
30
88
|
}
|
|
31
89
|
async function createSession(options) {
|
|
32
90
|
const logger = selectLogger(options);
|
|
@@ -43,6 +101,12 @@ function selectLogger(options) {
|
|
|
43
101
|
}
|
|
44
102
|
async function initializeOrm(orm, options) {
|
|
45
103
|
const storage = new entities_1.EntityStorage();
|
|
104
|
+
if (options.entityFile) {
|
|
105
|
+
const entityFiles = await (0, globby_1.default)(options.entityFile, { absolute: true });
|
|
106
|
+
for (const file of entityFiles) {
|
|
107
|
+
await Promise.resolve(`${file}`).then(s => __importStar(require(s)));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
46
110
|
const service = new orm_service_1.OrmService(orm, storage, options.entityFile);
|
|
47
111
|
const connection = resolveConnection(options.connection);
|
|
48
112
|
await service.onInit(connection);
|
|
@@ -70,16 +134,6 @@ async function executeSql(orm, sql) {
|
|
|
70
134
|
const result = await orm.driverInstance.executeSql(sql);
|
|
71
135
|
return { rows: Array.isArray(result) ? result : [] };
|
|
72
136
|
}
|
|
73
|
-
async function executeWithinSession(session, context, statements, routine) {
|
|
74
|
-
try {
|
|
75
|
-
await prepareSchema(context, session.schema);
|
|
76
|
-
await createTables(context, statements);
|
|
77
|
-
await routine(context);
|
|
78
|
-
}
|
|
79
|
-
finally {
|
|
80
|
-
await resetSession(session, context);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
137
|
async function createTables(context, statements) {
|
|
84
138
|
const payload = statements.filter(Boolean);
|
|
85
139
|
if (payload.length < 1) {
|
|
@@ -92,17 +146,8 @@ async function executeStatements(context, statements) {
|
|
|
92
146
|
await context.executeSql(statement);
|
|
93
147
|
}
|
|
94
148
|
}
|
|
95
|
-
async function
|
|
96
|
-
await
|
|
97
|
-
await ensureSearchPath(context, session.schema);
|
|
98
|
-
await session.orm.disconnect();
|
|
99
|
-
}
|
|
100
|
-
async function dropSchema(session, context) {
|
|
101
|
-
const statement = buildDropStatement(session.schema);
|
|
102
|
-
await context.executeSql(statement);
|
|
103
|
-
}
|
|
104
|
-
function buildDropStatement(schema) {
|
|
105
|
-
return `DROP SCHEMA IF EXISTS ${schema} CASCADE; CREATE SCHEMA ${schema};`;
|
|
149
|
+
async function dropAndRecreateSchema(context, schema) {
|
|
150
|
+
await context.executeSql(`DROP SCHEMA IF EXISTS ${schema} CASCADE; CREATE SCHEMA ${schema};`);
|
|
106
151
|
}
|
|
107
152
|
async function normalizeArgs(tablesOrRoutine, routineOrOptions, optionsOrStatements) {
|
|
108
153
|
if (Array.isArray(tablesOrRoutine)) {
|
|
@@ -65,8 +65,9 @@ class ValueProcessor {
|
|
|
65
65
|
if (property.options.onUpdate && moment === 'update') {
|
|
66
66
|
instance[key] = property.options.onUpdate();
|
|
67
67
|
}
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
const columnName = property.options.columnName;
|
|
69
|
+
if (columnName in values) {
|
|
70
|
+
instance[key] = values[columnName];
|
|
70
71
|
}
|
|
71
72
|
});
|
|
72
73
|
if (relations) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheetah.js/orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.64",
|
|
4
4
|
"description": "A simple ORM for Cheetah.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"bun",
|
|
56
56
|
"value-object"
|
|
57
57
|
],
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "91b624454730ef136975e456b365aa220378f4d2"
|
|
59
59
|
}
|