@jayfong/x-server 2.1.1 → 2.1.3

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,5 +1,5 @@
1
1
  import { x, HttpError } from '@jayfong/x-server'
2
- import { PrismaClient } from '@prisma/client'
2
+ import { PrismaClient, Prisma } from '@prisma/client'
3
3
 
4
4
  export const prismaClient = new PrismaClient({
5
5
  rejectOnNotFound: err => new HttpError.NotFound(err.message),
@@ -15,6 +15,27 @@ type ModelName =
15
15
  function makeBaseModel<TModelName extends ModelName>(name: TModelName) {
16
16
  return class BaseModel {
17
17
  public query = prismaClient[name]
18
+
19
+ public transactionClient: Prisma.TransactionClient | null = null
20
+
21
+ public transactionify = (tx: Prisma.TransactionClient) => {
22
+ // @ts-ignore
23
+ return new (class extends this.constructor {
24
+ public query = tx[name]
25
+
26
+ public transactionClient = tx
27
+ })() as typeof this
28
+ }
29
+
30
+ public getModel = <T extends ModelName>(
31
+ name: T,
32
+ ): typeof import('../../src/models')[`${T}Model`] => {
33
+ return this.transactionClient
34
+ ? require('../../src/models')[`${name}Model`].transactionify(
35
+ this.transactionClient,
36
+ )
37
+ : require('../../src/models')[`${name}Model`]
38
+ }
18
39
  }
19
40
  }
20
41
 
@@ -1,5 +1,5 @@
1
1
  import { x, HttpError } from '@jayfong/x-server'
2
- import { PrismaClient } from '@prisma/client'
2
+ import { PrismaClient, Prisma } from '@prisma/client'
3
3
 
4
4
  export const prismaClient = new PrismaClient({
5
5
  rejectOnNotFound: err => new HttpError.NotFound(err.message),
@@ -15,6 +15,27 @@ type ModelName =
15
15
  function makeBaseModel<TModelName extends ModelName>(name: TModelName) {
16
16
  return class BaseModel {
17
17
  public query = prismaClient[name]
18
+
19
+ public transactionClient: Prisma.TransactionClient | null = null
20
+
21
+ public transactionify = (tx: Prisma.TransactionClient) => {
22
+ // @ts-ignore
23
+ return new (class extends this.constructor {
24
+ public query = tx[name]
25
+
26
+ public transactionClient = tx
27
+ })() as typeof this
28
+ }
29
+
30
+ public getModel = <T extends ModelName>(
31
+ name: T,
32
+ ): typeof import('../../src/models')[`${T}Model`] => {
33
+ return this.transactionClient
34
+ ? require('../../src/models')[`${name}Model`].transactionify(
35
+ this.transactionClient,
36
+ )
37
+ : require('../../src/models')[`${name}Model`]
38
+ }
18
39
  }
19
40
  }
20
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",