@jayfong/x-server 2.108.6 → 2.109.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.
@@ -1,3 +1,4 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
1
2
  import { URL } from 'node:url';
2
3
  import { x } from '@jayfong/x-server';
3
4
  import { PrismaMariaDb } from '@prisma/adapter-mariadb';
@@ -22,7 +23,10 @@ type ModelName =
22
23
 
23
24
  function _makeBaseModel<TModelName extends ModelName>(name: TModelName) {
24
25
  return class BaseModel {
25
- public query = prismaClient[name];
26
+ public get query() {
27
+ const tx = transactionStorage.getStore();
28
+ return tx?.[name] || prismaClient[name];
29
+ }
26
30
 
27
31
  public transactionClient: Prisma.TransactionClient | null = null;
28
32
 
@@ -52,6 +56,9 @@ function _makeBaseModel<TModelName extends ModelName>(name: TModelName) {
52
56
 
53
57
  export * from './prisma/client';
54
58
 
59
+ /**
60
+ * @deprecated 使用 runInTransaction 代替
61
+ */
55
62
  export const startTransaction = <T>(
56
63
  cb: (
57
64
  models: {
@@ -68,3 +75,14 @@ export const startTransaction = <T>(
68
75
  });
69
76
  });
70
77
  };
78
+
79
+ /** 事务上下文存储 */
80
+ export const transactionStorage = new AsyncLocalStorage<any>();
81
+ /** 在事务上下文中运行 */
82
+ export const runInTransaction = <T>(cb: () => Promise<T>): Promise<T> => {
83
+ const tx = transactionStorage.getStore();
84
+ if (!tx) {
85
+ return prismaClient.$transaction(tx => transactionStorage.run(tx, cb));
86
+ }
87
+ return cb();
88
+ };
@@ -1,3 +1,4 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
1
2
  import { URL } from 'node:url';
2
3
  import { x } from '@jayfong/x-server';
3
4
  import { PrismaMariaDb } from '@prisma/adapter-mariadb';
@@ -22,7 +23,10 @@ type ModelName =
22
23
 
23
24
  function _makeBaseModel<TModelName extends ModelName>(name: TModelName) {
24
25
  return class BaseModel {
25
- public query = prismaClient[name];
26
+ public get query() {
27
+ const tx = transactionStorage.getStore();
28
+ return tx?.[name] || prismaClient[name];
29
+ }
26
30
 
27
31
  public transactionClient: Prisma.TransactionClient | null = null;
28
32
 
@@ -52,6 +56,9 @@ function _makeBaseModel<TModelName extends ModelName>(name: TModelName) {
52
56
 
53
57
  export * from './prisma/client';
54
58
 
59
+ /**
60
+ * @deprecated 使用 runInTransaction 代替
61
+ */
55
62
  export const startTransaction = <T>(
56
63
  cb: (
57
64
  models: {
@@ -68,3 +75,14 @@ export const startTransaction = <T>(
68
75
  });
69
76
  });
70
77
  };
78
+
79
+ /** 事务上下文存储 */
80
+ export const transactionStorage = new AsyncLocalStorage<any>();
81
+ /** 在事务上下文中运行 */
82
+ export const runInTransaction = <T>(cb: () => Promise<T>): Promise<T> => {
83
+ const tx = transactionStorage.getStore();
84
+ if (!tx) {
85
+ return prismaClient.$transaction(tx => transactionStorage.run(tx, cb));
86
+ }
87
+ return cb();
88
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.108.6",
3
+ "version": "2.109.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -78,7 +78,7 @@
78
78
  "unplugin-macros": "^0.16.0",
79
79
  "utf-8-validate": "^5.0.9",
80
80
  "vscode-generate-index-standalone": "^1.7.1",
81
- "vtils": "^4.135.0",
81
+ "vtils": "^4.136.0",
82
82
  "yaml": "^2.3.1",
83
83
  "yargs": "^17.4.1"
84
84
  },