@cheetah.js/orm 0.1.141 → 0.1.142
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/domain/entities.js
CHANGED
|
@@ -12,6 +12,7 @@ var EntityStorage_1;
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.EntityStorage = void 0;
|
|
14
14
|
const core_1 = require("@cheetah.js/core");
|
|
15
|
+
const orm_session_context_1 = require("../orm-session-context");
|
|
15
16
|
const utils_1 = require("../utils");
|
|
16
17
|
let EntityStorage = EntityStorage_1 = class EntityStorage {
|
|
17
18
|
constructor() {
|
|
@@ -69,6 +70,10 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
|
|
|
69
70
|
return this.entities.entries();
|
|
70
71
|
}
|
|
71
72
|
static getInstance() {
|
|
73
|
+
const scoped = orm_session_context_1.ormSessionContext.getStorage();
|
|
74
|
+
if (scoped) {
|
|
75
|
+
return scoped;
|
|
76
|
+
}
|
|
72
77
|
return EntityStorage_1.instance;
|
|
73
78
|
}
|
|
74
79
|
async snapshot(values) {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Orm } from './orm';
|
|
2
|
+
import type { EntityStorage } from './domain/entities';
|
|
3
|
+
type OrmSession = {
|
|
4
|
+
orm: Orm<any>;
|
|
5
|
+
storage: EntityStorage;
|
|
6
|
+
};
|
|
7
|
+
declare class OrmSessionContext {
|
|
8
|
+
private storage;
|
|
9
|
+
constructor();
|
|
10
|
+
run<T>(session: OrmSession, routine: () => Promise<T>): Promise<T>;
|
|
11
|
+
getOrm(): Orm<any> | undefined;
|
|
12
|
+
getStorage(): EntityStorage | undefined;
|
|
13
|
+
hasContext(): boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const ormSessionContext: OrmSessionContext;
|
|
16
|
+
export type { OrmSession };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ormSessionContext = void 0;
|
|
4
|
+
const async_hooks_1 = require("async_hooks");
|
|
5
|
+
class OrmSessionContext {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.storage = new async_hooks_1.AsyncLocalStorage();
|
|
8
|
+
}
|
|
9
|
+
run(session, routine) {
|
|
10
|
+
return this.storage.run(session, routine);
|
|
11
|
+
}
|
|
12
|
+
getOrm() {
|
|
13
|
+
return this.storage.getStore()?.orm;
|
|
14
|
+
}
|
|
15
|
+
getStorage() {
|
|
16
|
+
return this.storage.getStore()?.storage;
|
|
17
|
+
}
|
|
18
|
+
hasContext() {
|
|
19
|
+
return this.storage.getStore() !== undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.ormSessionContext = new OrmSessionContext();
|
package/dist/orm.js
CHANGED
|
@@ -15,6 +15,7 @@ const core_1 = require("@cheetah.js/core");
|
|
|
15
15
|
const SqlBuilder_1 = require("./SqlBuilder");
|
|
16
16
|
const query_cache_manager_1 = require("./cache/query-cache-manager");
|
|
17
17
|
const transaction_context_1 = require("./transaction/transaction-context");
|
|
18
|
+
const orm_session_context_1 = require("./orm-session-context");
|
|
18
19
|
let Orm = Orm_1 = class Orm {
|
|
19
20
|
constructor(logger, cacheService) {
|
|
20
21
|
this.logger = logger;
|
|
@@ -28,6 +29,10 @@ let Orm = Orm_1 = class Orm {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
static getInstance() {
|
|
32
|
+
const scoped = orm_session_context_1.ormSessionContext.getOrm();
|
|
33
|
+
if (scoped) {
|
|
34
|
+
return scoped;
|
|
35
|
+
}
|
|
31
36
|
return Orm_1.instance;
|
|
32
37
|
}
|
|
33
38
|
setConnection(connection) {
|
|
@@ -45,6 +45,8 @@ const entities_1 = require("../domain/entities");
|
|
|
45
45
|
const orm_1 = require("../orm");
|
|
46
46
|
const orm_service_1 = require("../orm.service");
|
|
47
47
|
const bun_pg_driver_1 = require("../driver/bun-pg.driver");
|
|
48
|
+
const orm_session_context_1 = require("../orm-session-context");
|
|
49
|
+
const constants_1 = require("../constants");
|
|
48
50
|
const DEFAULT_SCHEMA = 'public';
|
|
49
51
|
const DEFAULT_CONNECTION = {
|
|
50
52
|
host: 'localhost',
|
|
@@ -57,6 +59,7 @@ const DEFAULT_CONNECTION = {
|
|
|
57
59
|
const sessionCache = new Map();
|
|
58
60
|
function getCacheKey(options) {
|
|
59
61
|
const connection = resolveConnection(options.connection);
|
|
62
|
+
const entitySignature = resolveEntitySignature();
|
|
60
63
|
return JSON.stringify({
|
|
61
64
|
host: connection.host,
|
|
62
65
|
port: connection.port,
|
|
@@ -64,8 +67,20 @@ function getCacheKey(options) {
|
|
|
64
67
|
schema: options.schema ?? DEFAULT_SCHEMA,
|
|
65
68
|
entityFile: options.entityFile,
|
|
66
69
|
migrationPath: connection.migrationPath,
|
|
70
|
+
entitySignature,
|
|
67
71
|
});
|
|
68
72
|
}
|
|
73
|
+
function resolveEntitySignature() {
|
|
74
|
+
const entities = core_1.Metadata.get(constants_1.ENTITIES, Reflect) || [];
|
|
75
|
+
return buildEntitySignature(entities);
|
|
76
|
+
}
|
|
77
|
+
function buildEntitySignature(entities) {
|
|
78
|
+
if (entities.length < 1) {
|
|
79
|
+
return 'none';
|
|
80
|
+
}
|
|
81
|
+
const names = entities.map((entity) => entity.target?.name ?? 'unknown');
|
|
82
|
+
return names.sort().join('|');
|
|
83
|
+
}
|
|
69
84
|
async function withDatabase(arg1, arg2, arg3) {
|
|
70
85
|
const { routine: targetRoutine, options: targetOptions, statements } = await normalizeArgs(arg1, arg2, arg3);
|
|
71
86
|
const cacheKey = getCacheKey(targetOptions);
|
|
@@ -80,12 +95,13 @@ async function withDatabase(arg1, arg2, arg3) {
|
|
|
80
95
|
};
|
|
81
96
|
sessionCache.set(cacheKey, cachedSession);
|
|
82
97
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
98
|
+
await runWithSession(cachedSession, async () => {
|
|
99
|
+
const context = buildContext(cachedSession.orm);
|
|
100
|
+
await dropAndRecreateSchema(context, cachedSession.schema);
|
|
101
|
+
await prepareSchema(context, cachedSession.schema);
|
|
102
|
+
await createTables(context, schemaStatements);
|
|
103
|
+
await targetRoutine(context);
|
|
104
|
+
});
|
|
89
105
|
}
|
|
90
106
|
async function createSession(options) {
|
|
91
107
|
const logger = selectLogger(options);
|
|
@@ -112,9 +128,8 @@ async function initializeOrm(orm, storage, options) {
|
|
|
112
128
|
const connection = resolveConnection(options.connection);
|
|
113
129
|
await service.onInit(connection);
|
|
114
130
|
}
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
entities_1.EntityStorage.instance = session.storage;
|
|
131
|
+
async function runWithSession(session, routine) {
|
|
132
|
+
await orm_session_context_1.ormSessionContext.run({ orm: session.orm, storage: session.storage }, routine);
|
|
118
133
|
}
|
|
119
134
|
function resolveConnection(overrides) {
|
|
120
135
|
if (!overrides) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheetah.js/orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.142",
|
|
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": "eb70114c61e3b2467bf063bade66ba6cba9ed4cf"
|
|
59
59
|
}
|