@cheetah.js/orm 0.1.113 → 0.1.115

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.
@@ -12,6 +12,7 @@ export declare abstract class BunDriverBase implements Partial<DriverInterface>
12
12
  protected validateConnection(): Promise<void>;
13
13
  disconnect(): Promise<void>;
14
14
  executeSql(sqlString: string): Promise<any>;
15
+ private getExecutionContext;
15
16
  transaction<T>(callback: (tx: SQL) => Promise<T>): Promise<T>;
16
17
  protected toDatabaseValue(value: unknown): string | number | boolean;
17
18
  protected escapeString(value: string): string;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BunDriverBase = void 0;
4
4
  const bun_1 = require("bun");
5
+ const transaction_context_1 = require("../transaction/transaction-context");
5
6
  class BunDriverBase {
6
7
  constructor(options) {
7
8
  this.connectionString = this.buildConnectionString(options);
@@ -31,10 +32,18 @@ class BunDriverBase {
31
32
  await this.sql.close();
32
33
  }
33
34
  async executeSql(sqlString) {
35
+ const context = this.getExecutionContext();
36
+ return await context.unsafe(sqlString);
37
+ }
38
+ getExecutionContext() {
39
+ const txContext = transaction_context_1.transactionContext.getContext();
40
+ if (txContext) {
41
+ return txContext;
42
+ }
34
43
  if (!this.sql) {
35
44
  throw new Error('Database not connected');
36
45
  }
37
- return await this.sql.unsafe(sqlString);
46
+ return this.sql;
38
47
  }
39
48
  async transaction(callback) {
40
49
  if (!this.sql) {
@@ -119,13 +128,14 @@ class BunDriverBase {
119
128
  }
120
129
  async executeStatement(statement) {
121
130
  const startTime = Date.now();
131
+ const context = this.getExecutionContext();
122
132
  if (statement.statement === 'insert') {
123
133
  const sql = this.buildInsertSqlWithReturn(statement);
124
- const result = await this.sql.unsafe(sql);
134
+ const result = await context.unsafe(sql);
125
135
  return this.handleInsertReturn(statement, result, sql, startTime);
126
136
  }
127
137
  const sql = this.buildStatementSql(statement);
128
- const result = await this.sql.unsafe(sql);
138
+ const result = await context.unsafe(sql);
129
139
  return {
130
140
  query: { rows: Array.isArray(result) ? result : [] },
131
141
  startTime,
package/dist/index.d.ts CHANGED
@@ -22,3 +22,4 @@ export * from './common/value-object';
22
22
  export * from './common/email.vo';
23
23
  export * from './common/uuid';
24
24
  export * from './repository/Repository';
25
+ export { transactionContext } from './transaction/transaction-context';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.EntityStorage = void 0;
17
+ exports.transactionContext = exports.EntityStorage = void 0;
18
18
  __exportStar(require("./decorators/entity.decorator"), exports);
19
19
  __exportStar(require("./decorators/property.decorator"), exports);
20
20
  __exportStar(require("./decorators/primary-key.decorator"), exports);
@@ -39,3 +39,5 @@ __exportStar(require("./common/value-object"), exports);
39
39
  __exportStar(require("./common/email.vo"), exports);
40
40
  __exportStar(require("./common/uuid"), exports);
41
41
  __exportStar(require("./repository/Repository"), exports);
42
+ var transaction_context_1 = require("./transaction/transaction-context");
43
+ Object.defineProperty(exports, "transactionContext", { enumerable: true, get: function () { return transaction_context_1.transactionContext; } });
package/dist/orm.js CHANGED
@@ -14,6 +14,7 @@ exports.Orm = void 0;
14
14
  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
+ const transaction_context_1 = require("./transaction/transaction-context");
17
18
  let Orm = Orm_1 = class Orm {
18
19
  constructor(logger, cacheService) {
19
20
  this.logger = logger;
@@ -47,7 +48,9 @@ let Orm = Orm_1 = class Orm {
47
48
  if (!this.driverInstance) {
48
49
  throw new Error('Driver instance not initialized');
49
50
  }
50
- return this.driverInstance.transaction(operation);
51
+ return this.driverInstance.transaction(async (tx) => {
52
+ return transaction_context_1.transactionContext.run(tx, () => operation(tx));
53
+ });
51
54
  }
52
55
  };
53
56
  exports.Orm = Orm;
@@ -0,0 +1,10 @@
1
+ import { SQL } from 'bun';
2
+ declare class TransactionContextManager {
3
+ private storage;
4
+ constructor();
5
+ run<T>(tx: SQL, callback: () => Promise<T>): Promise<T>;
6
+ getContext(): SQL | undefined;
7
+ hasContext(): boolean;
8
+ }
9
+ export declare const transactionContext: TransactionContextManager;
10
+ export {};
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transactionContext = void 0;
4
+ const async_hooks_1 = require("async_hooks");
5
+ class TransactionContextManager {
6
+ constructor() {
7
+ this.storage = new async_hooks_1.AsyncLocalStorage();
8
+ }
9
+ run(tx, callback) {
10
+ return this.storage.run({ tx }, callback);
11
+ }
12
+ getContext() {
13
+ return this.storage.getStore()?.tx;
14
+ }
15
+ hasContext() {
16
+ return this.storage.getStore() !== undefined;
17
+ }
18
+ }
19
+ exports.transactionContext = new TransactionContextManager();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.113",
3
+ "version": "0.1.115",
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": "439b0bb37b4f7b81d2aebf4171ba27792062d762"
58
+ "gitHead": "90003dc77de481a78a13fa7dedd8f62bde43b4cc"
59
59
  }