@cheetah.js/orm 0.1.63 → 0.1.65
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);
|
|
@@ -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;
|
|
@@ -54,12 +54,37 @@ const DEFAULT_CONNECTION = {
|
|
|
54
54
|
password: 'postgres',
|
|
55
55
|
driver: bun_pg_driver_1.BunPgDriver,
|
|
56
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
|
+
}
|
|
57
69
|
async function withDatabase(arg1, arg2, arg3) {
|
|
58
70
|
const { routine: targetRoutine, options: targetOptions, statements } = await normalizeArgs(arg1, arg2, arg3);
|
|
59
|
-
const
|
|
60
|
-
|
|
71
|
+
const cacheKey = getCacheKey(targetOptions);
|
|
72
|
+
let cachedSession = sessionCache.get(cacheKey);
|
|
61
73
|
const schemaStatements = await resolveSchemaStatements(statements, targetOptions);
|
|
62
|
-
|
|
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);
|
|
63
88
|
}
|
|
64
89
|
async function createSession(options) {
|
|
65
90
|
const logger = selectLogger(options);
|
|
@@ -109,16 +134,6 @@ async function executeSql(orm, sql) {
|
|
|
109
134
|
const result = await orm.driverInstance.executeSql(sql);
|
|
110
135
|
return { rows: Array.isArray(result) ? result : [] };
|
|
111
136
|
}
|
|
112
|
-
async function executeWithinSession(session, context, statements, routine) {
|
|
113
|
-
try {
|
|
114
|
-
await prepareSchema(context, session.schema);
|
|
115
|
-
await createTables(context, statements);
|
|
116
|
-
await routine(context);
|
|
117
|
-
}
|
|
118
|
-
finally {
|
|
119
|
-
await resetSession(session, context);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
137
|
async function createTables(context, statements) {
|
|
123
138
|
const payload = statements.filter(Boolean);
|
|
124
139
|
if (payload.length < 1) {
|
|
@@ -131,17 +146,8 @@ async function executeStatements(context, statements) {
|
|
|
131
146
|
await context.executeSql(statement);
|
|
132
147
|
}
|
|
133
148
|
}
|
|
134
|
-
async function
|
|
135
|
-
await
|
|
136
|
-
await ensureSearchPath(context, session.schema);
|
|
137
|
-
await session.orm.disconnect();
|
|
138
|
-
}
|
|
139
|
-
async function dropSchema(session, context) {
|
|
140
|
-
const statement = buildDropStatement(session.schema);
|
|
141
|
-
await context.executeSql(statement);
|
|
142
|
-
}
|
|
143
|
-
function buildDropStatement(schema) {
|
|
144
|
-
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};`);
|
|
145
151
|
}
|
|
146
152
|
async function normalizeArgs(tablesOrRoutine, routineOrOptions, optionsOrStatements) {
|
|
147
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.65",
|
|
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": "8792dea14d85bab1370809ad79060103e35bf0e3"
|
|
59
59
|
}
|