@windrun-huaiin/backend-core 26.0.2 → 28.0.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.
Files changed (191) hide show
  1. package/dist/app/api/ai/route.d.ts +2 -0
  2. package/dist/app/api/ai/route.d.ts.map +1 -0
  3. package/dist/app/api/ai/route.js +10 -0
  4. package/dist/app/api/ai/route.mjs +1 -0
  5. package/dist/app/api/stripe/checkout/route.js +1 -1
  6. package/dist/app/api/stripe/checkout/route.mjs +1 -1
  7. package/dist/app/api/stripe/customer-portal/route.js +1 -1
  8. package/dist/app/api/stripe/customer-portal/route.mjs +1 -1
  9. package/dist/app/api/webhook/clerk/user/route.js +1 -1
  10. package/dist/app/api/webhook/clerk/user/route.mjs +1 -1
  11. package/dist/auth/auth-utils.js +1 -1
  12. package/dist/auth/auth-utils.mjs +1 -1
  13. package/dist/config/fingerprint.d.ts +2 -0
  14. package/dist/config/fingerprint.d.ts.map +1 -0
  15. package/dist/config/fingerprint.js +7 -0
  16. package/dist/config/fingerprint.mjs +1 -0
  17. package/dist/config/money-price.d.ts +2 -0
  18. package/dist/config/money-price.d.ts.map +1 -0
  19. package/dist/config/money-price.js +10 -0
  20. package/dist/config/money-price.mjs +1 -0
  21. package/dist/core-prisma/browser.d.ts +40 -0
  22. package/dist/core-prisma/browser.d.ts.map +1 -0
  23. package/dist/core-prisma/client.d.ts +57 -0
  24. package/dist/core-prisma/client.d.ts.map +1 -0
  25. package/dist/core-prisma/client.js +55 -0
  26. package/dist/core-prisma/client.mjs +33 -0
  27. package/dist/core-prisma/commonInputTypes.d.ts +495 -0
  28. package/dist/core-prisma/commonInputTypes.d.ts.map +1 -0
  29. package/dist/core-prisma/enums.d.ts +2 -0
  30. package/dist/core-prisma/enums.d.ts.map +1 -0
  31. package/dist/core-prisma/internal/class.d.ts +193 -0
  32. package/dist/core-prisma/internal/class.d.ts.map +1 -0
  33. package/dist/core-prisma/internal/class.js +108 -0
  34. package/dist/core-prisma/internal/class.mjs +87 -0
  35. package/dist/core-prisma/internal/prismaNamespace.d.ts +1180 -0
  36. package/dist/core-prisma/internal/prismaNamespace.d.ts.map +1 -0
  37. package/dist/core-prisma/internal/prismaNamespace.js +278 -0
  38. package/dist/core-prisma/internal/prismaNamespace.mjs +226 -0
  39. package/dist/core-prisma/internal/prismaNamespaceBrowser.d.ts +233 -0
  40. package/dist/core-prisma/internal/prismaNamespaceBrowser.d.ts.map +1 -0
  41. package/dist/core-prisma/models/Apilog.d.ts +1088 -0
  42. package/dist/core-prisma/models/Apilog.d.ts.map +1 -0
  43. package/dist/core-prisma/models/Credit.d.ts +1554 -0
  44. package/dist/core-prisma/models/Credit.d.ts.map +1 -0
  45. package/dist/core-prisma/models/CreditAuditLog.d.ts +1362 -0
  46. package/dist/core-prisma/models/CreditAuditLog.d.ts.map +1 -0
  47. package/dist/core-prisma/models/Subscription.d.ts +1441 -0
  48. package/dist/core-prisma/models/Subscription.d.ts.map +1 -0
  49. package/dist/core-prisma/models/Transaction.d.ts +2222 -0
  50. package/dist/core-prisma/models/Transaction.d.ts.map +1 -0
  51. package/dist/core-prisma/models/User.d.ts +1775 -0
  52. package/dist/core-prisma/models/User.d.ts.map +1 -0
  53. package/dist/core-prisma/models/UserBackup.d.ts +1228 -0
  54. package/dist/core-prisma/models/UserBackup.d.ts.map +1 -0
  55. package/dist/core-prisma/models.d.ts +9 -0
  56. package/dist/core-prisma/models.d.ts.map +1 -0
  57. package/dist/credit/server.d.ts +2 -0
  58. package/dist/credit/server.d.ts.map +1 -0
  59. package/dist/credit/server.js +11 -0
  60. package/dist/credit/server.mjs +1 -0
  61. package/dist/index.d.ts +0 -1
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +7 -98
  64. package/dist/index.mjs +5 -14
  65. package/dist/lib/money-price-helper.d.ts.map +1 -1
  66. package/dist/lib/money-price-helper.js +1 -0
  67. package/dist/lib/money-price-helper.mjs +2 -1
  68. package/dist/lib/stripe-config.js +1 -1
  69. package/dist/lib/stripe-config.mjs +1 -1
  70. package/dist/pricing/server.d.ts +2 -0
  71. package/dist/pricing/server.d.ts.map +1 -0
  72. package/dist/pricing/server.js +7 -0
  73. package/dist/pricing/server.mjs +1 -0
  74. package/dist/prisma/index.d.ts +4 -1
  75. package/dist/prisma/index.d.ts.map +1 -1
  76. package/dist/prisma/index.js +6 -0
  77. package/dist/prisma/index.mjs +4 -1
  78. package/dist/prisma/prisma-transaction-util.d.ts +1 -1
  79. package/dist/prisma/prisma-transaction-util.d.ts.map +1 -1
  80. package/dist/prisma/prisma-transaction-util.js +1 -1
  81. package/dist/prisma/prisma-transaction-util.mjs +2 -2
  82. package/dist/prisma/prisma.d.ts +22 -3
  83. package/dist/prisma/prisma.d.ts.map +1 -1
  84. package/dist/prisma/prisma.js +83 -20
  85. package/dist/prisma/prisma.mjs +81 -21
  86. package/dist/services/aggregate/anonymous.aggregate.service.d.ts +2 -2
  87. package/dist/services/aggregate/anonymous.aggregate.service.d.ts.map +1 -1
  88. package/dist/services/aggregate/anonymous.aggregate.service.js +3 -2
  89. package/dist/services/aggregate/anonymous.aggregate.service.mjs +3 -2
  90. package/dist/services/aggregate/billing.aggregate.service.d.ts +2 -2
  91. package/dist/services/aggregate/billing.aggregate.service.d.ts.map +1 -1
  92. package/dist/services/aggregate/billing.aggregate.service.js +1 -1
  93. package/dist/services/aggregate/billing.aggregate.service.mjs +1 -1
  94. package/dist/services/aggregate/user.aggregate.service.d.ts +1 -1
  95. package/dist/services/aggregate/user.aggregate.service.d.ts.map +1 -1
  96. package/dist/services/aggregate/user.aggregate.service.js +3 -3
  97. package/dist/services/aggregate/user.aggregate.service.mjs +3 -3
  98. package/dist/services/context/index.js +1 -0
  99. package/dist/services/context/index.mjs +1 -1
  100. package/dist/services/context/user-context-finalizer.d.ts +1 -0
  101. package/dist/services/context/user-context-finalizer.d.ts.map +1 -1
  102. package/dist/services/context/user-context-finalizer.js +17 -0
  103. package/dist/services/context/user-context-finalizer.mjs +17 -1
  104. package/dist/services/context/user-context-service.js +1 -1
  105. package/dist/services/context/user-context-service.mjs +1 -1
  106. package/dist/services/database/apilog.service.d.ts +2 -2
  107. package/dist/services/database/apilog.service.d.ts.map +1 -1
  108. package/dist/services/database/apilog.service.js +1 -0
  109. package/dist/services/database/apilog.service.mjs +1 -0
  110. package/dist/services/database/credit.service.d.ts +2 -2
  111. package/dist/services/database/credit.service.d.ts.map +1 -1
  112. package/dist/services/database/credit.service.js +5 -4
  113. package/dist/services/database/credit.service.mjs +3 -2
  114. package/dist/services/database/creditAuditLog.service.d.ts +2 -2
  115. package/dist/services/database/creditAuditLog.service.d.ts.map +1 -1
  116. package/dist/services/database/creditAuditLog.service.js +5 -4
  117. package/dist/services/database/creditAuditLog.service.mjs +3 -2
  118. package/dist/services/database/prisma-model-type.d.ts +2 -2
  119. package/dist/services/database/prisma-model-type.d.ts.map +1 -1
  120. package/dist/services/database/subscription.service.d.ts +1 -1
  121. package/dist/services/database/subscription.service.d.ts.map +1 -1
  122. package/dist/services/database/subscription.service.js +1 -0
  123. package/dist/services/database/subscription.service.mjs +1 -0
  124. package/dist/services/database/transaction.service.d.ts +3 -3
  125. package/dist/services/database/transaction.service.d.ts.map +1 -1
  126. package/dist/services/database/transaction.service.js +5 -4
  127. package/dist/services/database/transaction.service.mjs +3 -2
  128. package/dist/services/database/user.service.d.ts +2 -2
  129. package/dist/services/database/user.service.d.ts.map +1 -1
  130. package/dist/services/database/user.service.js +1 -0
  131. package/dist/services/database/user.service.mjs +1 -0
  132. package/dist/services/database/userBackup.service.d.ts +2 -2
  133. package/dist/services/database/userBackup.service.d.ts.map +1 -1
  134. package/dist/services/database/userBackup.service.js +1 -0
  135. package/dist/services/database/userBackup.service.mjs +1 -0
  136. package/dist/services/stripe/webhook-handler.js +1 -1
  137. package/dist/services/stripe/webhook-handler.mjs +1 -1
  138. package/dist/upstash/server.d.ts +3 -0
  139. package/dist/upstash/server.d.ts.map +1 -0
  140. package/dist/{lib/index.js → upstash/server.js} +7 -32
  141. package/dist/upstash/server.mjs +7 -0
  142. package/package.json +44 -12
  143. package/prisma/schema.prisma +3 -1
  144. package/src/app/api/ai/route.ts +6 -0
  145. package/src/app/api/stripe/checkout/route.ts +4 -4
  146. package/src/app/api/stripe/customer-portal/route.ts +3 -3
  147. package/src/app/api/user/anonymous/init/route.ts +5 -5
  148. package/src/app/api/webhook/clerk/user/route.ts +3 -3
  149. package/src/app/api/webhook/stripe/route.ts +3 -3
  150. package/src/config/fingerprint.ts +1 -0
  151. package/src/config/money-price.ts +1 -0
  152. package/src/core-prisma/browser.ts +54 -0
  153. package/src/core-prisma/client.ts +78 -0
  154. package/src/core-prisma/commonInputTypes.ts +565 -0
  155. package/src/core-prisma/enums.ts +15 -0
  156. package/src/core-prisma/internal/class.ts +293 -0
  157. package/src/core-prisma/internal/prismaNamespace.ts +1419 -0
  158. package/src/core-prisma/internal/prismaNamespaceBrowser.ts +259 -0
  159. package/src/core-prisma/models/Apilog.ts +1244 -0
  160. package/src/core-prisma/models/Credit.ts +1722 -0
  161. package/src/core-prisma/models/CreditAuditLog.ts +1536 -0
  162. package/src/core-prisma/models/Subscription.ts +1610 -0
  163. package/src/core-prisma/models/Transaction.ts +2398 -0
  164. package/src/core-prisma/models/User.ts +1982 -0
  165. package/src/core-prisma/models/UserBackup.ts +1384 -0
  166. package/src/core-prisma/models.ts +18 -0
  167. package/src/credit/server.ts +1 -0
  168. package/src/index.ts +0 -1
  169. package/src/lib/money-price-helper.ts +3 -0
  170. package/src/pricing/server.ts +1 -0
  171. package/src/prisma/index.ts +18 -1
  172. package/src/prisma/prisma-transaction-util.ts +3 -3
  173. package/src/prisma/prisma.ts +130 -21
  174. package/src/services/aggregate/anonymous.aggregate.service.ts +7 -7
  175. package/src/services/aggregate/billing.aggregate.service.ts +3 -3
  176. package/src/services/aggregate/user.aggregate.service.ts +7 -6
  177. package/src/services/context/user-context-finalizer.ts +16 -0
  178. package/src/services/database/apilog.service.ts +3 -3
  179. package/src/services/database/credit.service.ts +7 -7
  180. package/src/services/database/creditAuditLog.service.ts +4 -4
  181. package/src/services/database/prisma-model-type.ts +2 -2
  182. package/src/services/database/subscription.service.ts +3 -3
  183. package/src/services/database/transaction.service.ts +4 -4
  184. package/src/services/database/user.service.ts +4 -4
  185. package/src/services/database/userBackup.service.ts +4 -4
  186. package/src/services/stripe/webhook-handler.ts +6 -6
  187. package/src/upstash/server.ts +2 -0
  188. package/dist/lib/index.d.ts +0 -8
  189. package/dist/lib/index.d.ts.map +0 -1
  190. package/dist/lib/index.mjs +0 -12
  191. package/src/lib/index.ts +0 -7
@@ -0,0 +1,18 @@
1
+
2
+ /* !!! This is code generated by Prisma. Do not edit directly. !!! */
3
+ /* eslint-disable */
4
+ // biome-ignore-all lint: generated file
5
+ // @ts-nocheck
6
+ /*
7
+ * This is a barrel export file for all models and their related types.
8
+ *
9
+ * 🟢 You can import this file directly.
10
+ */
11
+ export type * from './models/User'
12
+ export type * from './models/Subscription'
13
+ export type * from './models/Credit'
14
+ export type * from './models/Transaction'
15
+ export type * from './models/CreditAuditLog'
16
+ export type * from './models/UserBackup'
17
+ export type * from './models/Apilog'
18
+ export type * from './commonInputTypes'
@@ -0,0 +1 @@
1
+ export * from '../lib/credit-init';
package/src/index.ts CHANGED
@@ -4,4 +4,3 @@ export * from './services/aggregate';
4
4
  export * from './services/context';
5
5
  export * from './services/stripe';
6
6
  export * from './services/ai';
7
- export * from './lib';
@@ -5,6 +5,7 @@ import {
5
5
  import type { InitUserContext } from '@windrun-huaiin/third-ui/main/server';
6
6
  import { getOptionalServerAuthIdentity } from '../auth/auth-utils';
7
7
  import {
8
+ applyMoneyPriceMockUserDelay,
8
9
  buildInitUserContextFromEntities,
9
10
  fetchUserContextByClerkUserId,
10
11
  finalizeUserContext,
@@ -21,6 +22,8 @@ async function readFingerprintIdFromRequest(): Promise<string | null> {
21
22
  }
22
23
 
23
24
  export async function getMoneyPriceInitUserContext(): Promise<InitUserContext> {
25
+ await applyMoneyPriceMockUserDelay();
26
+
24
27
  const authIdentity = await getOptionalServerAuthIdentity();
25
28
  const clerkUserId = authIdentity?.providerUserId ?? null;
26
29
 
@@ -0,0 +1 @@
1
+ export * from '../lib/money-price-helper';
@@ -1,2 +1,19 @@
1
- export { prisma, checkAndFallbackWithNonTCClient } from './prisma';
1
+ export {
2
+ prisma,
3
+ createPrismaClient,
4
+ configureBackendCorePrisma,
5
+ getBackendCorePrisma,
6
+ checkAndFallbackWithNonTCClient,
7
+ } from './prisma';
8
+ export type { BackendCorePrismaClient, BackendCoreHostPrismaClient } from './prisma';
2
9
  export { runInTransaction } from './prisma-transaction-util';
10
+ export type {
11
+ User,
12
+ Subscription,
13
+ Credit,
14
+ CreditAuditLog,
15
+ Transaction,
16
+ Apilog,
17
+ UserBackup,
18
+ } from '../core-prisma/client';
19
+ export { Prisma } from '../core-prisma/client';
@@ -1,5 +1,5 @@
1
- import { prisma } from './prisma';
2
- import { Prisma } from '@prisma/client';
1
+ import { getBackendCorePrisma } from './prisma';
2
+ import { Prisma } from '../core-prisma/client';
3
3
 
4
4
  // 事务工具类,回滚时打印回滚日志
5
5
  export async function runInTransaction<T>(
@@ -8,7 +8,7 @@ export async function runInTransaction<T>(
8
8
  ): Promise<T> {
9
9
  const start = Date.now();
10
10
  try {
11
- return await prisma.$transaction(fn);
11
+ return await getBackendCorePrisma().$transaction(fn);
12
12
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
13
  } catch (error: any) {
14
14
  const duration = Date.now() - start;
@@ -1,22 +1,57 @@
1
- import { PrismaClient, Prisma } from '@prisma/client';
1
+ import { PrismaPg } from '@prisma/adapter-pg';
2
+ import { PrismaClient, Prisma } from '../core-prisma/client';
3
+
4
+ type AppPrismaClient = PrismaClient<'query' | 'info' | 'warn' | 'error'>;
5
+ export type BackendCorePrismaClient = AppPrismaClient;
6
+ export type BackendCoreHostPrismaClient = {
7
+ // Deliberately loose: host applications generate their own Prisma Client,
8
+ // so transaction overloads are structurally compatible at runtime but not
9
+ // nominally identical to backend-core's generated client types.
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ $transaction: any;
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ $on?: any;
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ $executeRaw: any;
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ $queryRaw: any;
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ user: any;
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ subscription: any;
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ credit: any;
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ transaction: any;
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ creditAuditLog: any;
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
+ userBackup: any;
30
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
+ apilog: any;
32
+ };
2
33
 
3
34
  const globalForPrisma = globalThis as unknown as {
4
- prisma?: PrismaClient;
35
+ prisma?: BackendCoreHostPrismaClient;
5
36
  __prisma_query_logger_registered?: boolean;
6
37
  __prisma_query_logger_id?: string;
38
+ __prisma_instance_id?: string;
39
+ __prisma_ssl_warning_logged?: boolean;
7
40
  };
8
41
 
9
42
  // ==================== 日志配置 ====================
10
43
  const getLogConfig = () => {
44
+ if (process.env.PRISMA_DEBUG === 'true') {
45
+ return [
46
+ { emit: 'event' as const, level: 'query' as const },
47
+ { emit: 'stdout' as const, level: 'info' as const },
48
+ { emit: 'stdout' as const, level: 'warn' as const },
49
+ { emit: 'stdout' as const, level: 'error' as const },
50
+ ];
51
+ }
52
+
11
53
  const env = process.env.NODE_ENV || 'development';
12
54
  switch (env) {
13
- case 'development':
14
- return [
15
- { emit: 'event' as const, level: 'query' as const },
16
- { emit: 'stdout' as const, level: 'info' as const },
17
- { emit: 'stdout' as const, level: 'warn' as const },
18
- { emit: 'stdout' as const, level: 'error' as const },
19
- ];
20
55
  case 'test':
21
56
  return [
22
57
  { emit: 'stdout' as const, level: 'warn' as const },
@@ -29,26 +64,99 @@ const getLogConfig = () => {
29
64
 
30
65
  const logConfig = getLogConfig();
31
66
 
32
- // ==================== 创建 Prisma 全局单例 ====================
33
- export const prisma =
34
- globalForPrisma.prisma ??
35
- new PrismaClient<Prisma.PrismaClientOptions, 'query' | 'info' | 'warn' | 'error'>({
67
+ function isPrismaDebugEnabled() {
68
+ return process.env.PRISMA_DEBUG === 'true';
69
+ }
70
+
71
+ function createPrismaInstanceId(prefix = 'core-prisma') {
72
+ return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`;
73
+ }
74
+
75
+ function createPrismaPgConfig(databaseUrl: string) {
76
+ const ca = process.env.SUPABASE_DB_CA_CERT;
77
+
78
+ if (!ca && !globalForPrisma.__prisma_ssl_warning_logged) {
79
+ console.warn(
80
+ 'SUPABASE_DB_CA_CERT is not configured. Prisma will request TLS without certificate verification unless DATABASE_URL SSL parameters override this behavior. Configure SUPABASE_DB_CA_CERT for certificate verification.',
81
+ );
82
+ globalForPrisma.__prisma_ssl_warning_logged = true;
83
+ }
84
+
85
+ return {
86
+ connectionString: databaseUrl,
87
+ ssl: ca
88
+ ? {
89
+ ca,
90
+ rejectUnauthorized: true,
91
+ }
92
+ : {
93
+ rejectUnauthorized: false,
94
+ },
95
+ };
96
+ }
97
+
98
+ export function createPrismaClient(databaseUrl = process.env.DATABASE_URL): AppPrismaClient {
99
+ if (!databaseUrl) {
100
+ throw new Error('DATABASE_URL is required to create PrismaClient');
101
+ }
102
+
103
+ const adapter = new PrismaPg(createPrismaPgConfig(databaseUrl));
104
+
105
+ const instanceId = createPrismaInstanceId();
106
+ if (isPrismaDebugEnabled()) {
107
+ console.log(`Prisma Client Created | ID: ${instanceId}`);
108
+ }
109
+
110
+ const client = new PrismaClient({
111
+ adapter,
36
112
  log: logConfig,
37
113
  });
38
114
 
39
- if (process.env.NODE_ENV !== 'production') {
40
- globalForPrisma.prisma = prisma;
115
+ return configureBackendCorePrisma(client, instanceId) as AppPrismaClient;
116
+ }
117
+
118
+ export function configureBackendCorePrisma(
119
+ prismaClient: BackendCoreHostPrismaClient,
120
+ instanceId = globalForPrisma.__prisma_instance_id ?? createPrismaInstanceId(),
121
+ ): BackendCoreHostPrismaClient {
122
+ globalForPrisma.prisma = prismaClient;
123
+ globalForPrisma.__prisma_instance_id = instanceId;
124
+ registerDevelopmentQueryLogger(prismaClient, instanceId);
125
+ return prismaClient;
41
126
  }
42
127
 
43
- if (process.env.NODE_ENV === 'development') {
128
+ export function getBackendCorePrisma(): BackendCorePrismaClient {
129
+ if (!globalForPrisma.prisma) {
130
+ configureBackendCorePrisma(createPrismaClient());
131
+ }
132
+
133
+ return globalForPrisma.prisma as unknown as BackendCorePrismaClient;
134
+ }
135
+
136
+ // Backward-compatible lazy export. Accessing a property creates the client,
137
+ // importing this module does not.
138
+ export const prisma = new Proxy({} as BackendCorePrismaClient, {
139
+ get(_target, property, receiver) {
140
+ return Reflect.get(getBackendCorePrisma(), property, receiver);
141
+ },
142
+ });
143
+
144
+ function registerDevelopmentQueryLogger(prismaClient: BackendCoreHostPrismaClient, instanceId: string) {
145
+ if (!isPrismaDebugEnabled()) {
146
+ return;
147
+ }
148
+
44
149
  const REGISTERED_KEY = '__prisma_query_logger_registered';
45
150
  const ID_KEY = '__prisma_query_logger_id';
46
151
 
47
152
  if (globalForPrisma[REGISTERED_KEY]) {
48
- console.log(`Prisma Query Logger Already Registered | ID: ${globalForPrisma[ID_KEY]}`);
153
+ console.log(
154
+ `Prisma Query Logger Already Registered | Listener ID: ${globalForPrisma[ID_KEY]} | Instance ID: ${globalForPrisma.__prisma_instance_id}`,
155
+ );
49
156
  } else {
50
157
  const listenerId = `listener_${Date.now()}_${Math.random().toString(36).substr(2, 5)}`;
51
158
  globalForPrisma[ID_KEY] = listenerId;
159
+ console.log(`Prisma Query Logger Registered | Listener ID: ${listenerId} | Instance ID: ${instanceId}`);
52
160
 
53
161
  // --- 自定义SQL拼接 ---
54
162
  const interpolate = (query: string, params: string) => {
@@ -95,7 +203,7 @@ if (process.env.NODE_ENV === 'development') {
95
203
  };
96
204
 
97
205
  const wrappedHandler = (event: Prisma.QueryEvent) => {
98
- const ms = event.duration;
206
+ const ms = Math.round(event.duration);
99
207
  const slow = ms >= 200 ? '🐌 SLOW SQL ' : '🚀 SQL';
100
208
 
101
209
  const interpolatedSql = interpolate(event.query, event.params);
@@ -106,17 +214,18 @@ if (process.env.NODE_ENV === 'development') {
106
214
  .replace(/"/g, ''); // 彻底灭双引号
107
215
 
108
216
  console.log('─'.repeat(60));
217
+ console.log(`Prisma Instance ID: ${instanceId} | Listener ID: ${listenerId}`);
109
218
  console.log(`${clean};`);
110
219
  console.log(`⏰ 耗时: ${ms}ms, ${slow}`);
111
220
  };
112
221
  // 注册包装后的 handler
113
- prisma.$on('query' as never, wrappedHandler);
222
+ prismaClient.$on?.('query' as never, wrappedHandler);
114
223
 
115
224
  globalForPrisma[REGISTERED_KEY] = true;
116
225
  }
117
226
  }
118
227
 
119
228
  // ==================== 便捷方法, 入参事务客户端不存在或者不传, 就返回全局非事务客户端 ====================
120
- export function checkAndFallbackWithNonTCClient(tx?: Prisma.TransactionClient): Prisma.TransactionClient | PrismaClient {
121
- return tx ?? prisma;
229
+ export function checkAndFallbackWithNonTCClient(tx?: Prisma.TransactionClient): Prisma.TransactionClient | BackendCorePrismaClient {
230
+ return tx ?? getBackendCorePrisma();
122
231
  }
@@ -1,10 +1,10 @@
1
- import { creditService, subscriptionService, userService } from '@/db';
2
- import { UserStatus } from '@/db/constants';
3
- import type { Credit, Subscription, User } from '@/db/prisma-model-type';
4
- import { freeAmount } from '@/lib/credit-init';
5
- import { runInTransaction } from '@/prisma/prisma-transaction-util';
6
- import { CreditType, OperationType } from '@/db/constants';
7
- import { Prisma } from '@/db/prisma-model-type';
1
+ import { creditService, subscriptionService, userService } from '@core/db';
2
+ import { UserStatus } from '@core/db/constants';
3
+ import type { Credit, Subscription, User } from '@core/db/prisma-model-type';
4
+ import { freeAmount } from '@core/lib/credit-init';
5
+ import { runInTransaction } from '@core/prisma/prisma-transaction-util';
6
+ import { CreditType, OperationType } from '@core/db/constants';
7
+ import { Prisma } from '@core/db/prisma-model-type';
8
8
 
9
9
  const ANONYMOUS_INIT_LOCK_NAMESPACE = 92831;
10
10
 
@@ -9,9 +9,9 @@ import {
9
9
  SubscriptionStatus,
10
10
  transactionService,
11
11
  TransactionType,
12
- } from '@/db/index';
13
- import type { Subscription, Transaction } from '@/db/prisma-model-type';
14
- import { runInTransaction } from '@/prisma/prisma-transaction-util';
12
+ } from '@core/db/index';
13
+ import type { Subscription, Transaction } from '@core/db/prisma-model-type';
14
+ import { runInTransaction } from '@core/prisma/prisma-transaction-util';
15
15
 
16
16
  type NullableString = string | null | undefined;
17
17
 
@@ -1,8 +1,8 @@
1
- import { CreditType, OperationType, UserStatus } from '@/db/constants';
2
- import { creditService, subscriptionService, userService } from '@/db/index';
3
- import type { Credit, Prisma, User } from '@/db/prisma-model-type';
4
- import { freeAmount, freeRegisterAmount } from '@/lib/credit-init';
5
- import { runInTransaction } from '@/prisma/prisma-transaction-util';
1
+ import { CreditType, OperationType, UserStatus } from '@core/db/constants';
2
+ import { creditService, subscriptionService, userService } from '@core/db/index';
3
+ import type { Credit, Prisma, User } from '@core/db/prisma-model-type';
4
+ import { freeAmount, freeRegisterAmount } from '@core/lib/credit-init';
5
+ import { runInTransaction } from '@core/prisma/prisma-transaction-util';
6
6
 
7
7
 
8
8
  export class UserAggregateService {
@@ -81,6 +81,7 @@ export class UserAggregateService {
81
81
  operationReferId: newUser.userId,
82
82
  creditsChange: freeRegisterAmount,
83
83
  },
84
+ tx
84
85
  );
85
86
 
86
87
  await subscriptionService.initializeSubscription(newUser.userId, tx);
@@ -135,7 +136,7 @@ export class UserAggregateService {
135
136
  }
136
137
  const userId = user.userId;
137
138
  // 更改用户状态,保留user信息尤其是FingerprintId,防止反复注册薅羊毛
138
- await userService.unregister(user.userId);
139
+ await userService.unregister(user.userId, tx);
139
140
  // 清空积分
140
141
  await creditService.purgeCredit(userId, 'soft_delete_user', userId, tx);
141
142
 
@@ -6,6 +6,22 @@ type FinalizableUserContext = {
6
6
  xSubscription: XSubscription | null;
7
7
  };
8
8
 
9
+ const MAX_MONEY_PRICE_MOCK_DELAY_MS = 10000;
10
+
11
+ export async function applyMoneyPriceMockUserDelay(): Promise<void> {
12
+ if (process.env.MONEY_PRICE_MOCK_USER_ENABLED !== 'true') {
13
+ return;
14
+ }
15
+
16
+ const delaySeconds = Number(process.env.MONEY_PRICE_MOCK_USER_DELAY_SECONDS ?? 0);
17
+ if (!Number.isFinite(delaySeconds) || delaySeconds <= 0) {
18
+ return;
19
+ }
20
+
21
+ const delayMs = Math.min(Math.floor(delaySeconds * 1000), MAX_MONEY_PRICE_MOCK_DELAY_MS);
22
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
23
+ }
24
+
9
25
  /**
10
26
  * Output finalizer for user-context payloads.
11
27
  * Real data assembly should stay in user-context-service; any optional test-only
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import type { Prisma } from '@/db/prisma-model-type';
4
- import type { Apilog } from '@/db/prisma-model-type';
5
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
3
+ import type { Prisma } from '@core/db/prisma-model-type';
4
+ import type { Apilog } from '@core/db/prisma-model-type';
5
+ import { checkAndFallbackWithNonTCClient } from '@core/prisma/index';
6
6
 
7
7
  export type ApiType = 'from_clerk_in' | 'to_clerk_out' | 'from_stripe_in' | 'to_stripe_out';
8
8
 
@@ -1,9 +1,9 @@
1
- import { Prisma } from '@/db/prisma-model-type';
2
- import type { Credit, CreditAuditLog } from '@/db/prisma-model-type';
3
- import { CreditType, OperationType } from '@/db/constants';
4
- import { freeExpiredDays } from '@/lib/credit-init';
5
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
6
- import { creditAuditLogService } from '@/db/creditAuditLog.service';
1
+ import { Prisma } from '@core/db/prisma-model-type';
2
+ import type { Credit, CreditAuditLog } from '@core/db/prisma-model-type';
3
+ import { CreditType, OperationType } from '@core/db/constants';
4
+ import { freeExpiredDays } from '@core/lib/credit-init';
5
+ import { checkAndFallbackWithNonTCClient, type BackendCorePrismaClient } from '@core/prisma/index';
6
+ import { creditAuditLogService } from '@core/db/creditAuditLog.service';
7
7
 
8
8
  type CreditAmounts = {
9
9
  free?: number;
@@ -222,7 +222,7 @@ export class CreditService {
222
222
  }
223
223
 
224
224
  private async recordCreditAuditLog(
225
- client: Prisma.TransactionClient,
225
+ client: Prisma.TransactionClient | BackendCorePrismaClient,
226
226
  userId: string,
227
227
  operationType: string,
228
228
  amounts: Required<CreditAmounts>,
@@ -1,9 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import { Prisma } from '@/db/prisma-model-type';
4
- import type { CreditAuditLog } from '@/db/prisma-model-type';
5
- import { CreditType, OperationType } from '@/db/constants';
6
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
3
+ import { Prisma } from '@core/db/prisma-model-type';
4
+ import type { CreditAuditLog } from '@core/db/prisma-model-type';
5
+ import { CreditType, OperationType } from '@core/db/constants';
6
+ import { checkAndFallbackWithNonTCClient } from '@core/prisma/index';
7
7
 
8
8
  export class CreditAuditLogService {
9
9
 
@@ -7,7 +7,7 @@ export type {
7
7
  Transaction,
8
8
  Apilog,
9
9
  UserBackup,
10
- } from '@prisma/client';
10
+ } from '../../core-prisma/client';
11
11
 
12
12
  // Prisma既是类型也是值
13
- export { Prisma } from '@prisma/client';
13
+ export { Prisma } from '../../core-prisma/client';
@@ -1,6 +1,6 @@
1
- import { SubscriptionStatus } from '@/db/constants';
2
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
3
- import type { Prisma, Subscription } from '@/db/prisma-model-type';
1
+ import { SubscriptionStatus } from '@core/db/constants';
2
+ import { checkAndFallbackWithNonTCClient } from '@core/prisma/index';
3
+ import type { Prisma, Subscription } from '@core/db/prisma-model-type';
4
4
 
5
5
  export class SubscriptionService {
6
6
 
@@ -1,9 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import { Prisma } from '@/db/prisma-model-type';
4
- import type { Transaction } from '@/db/prisma-model-type';
5
- import { OrderStatus, TransactionType, PaySupplier, PaymentStatus } from '@/db/constants';
6
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
3
+ import { Prisma } from '@core/db/prisma-model-type';
4
+ import type { Transaction } from '@core/db/prisma-model-type';
5
+ import { OrderStatus, TransactionType, PaySupplier, PaymentStatus } from '@core/db/constants';
6
+ import { checkAndFallbackWithNonTCClient } from '@core/prisma/index';
7
7
 
8
8
  export class TransactionService {
9
9
 
@@ -1,9 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import type { Prisma } from '@/db/prisma-model-type';
4
- import type { User } from '@/db/prisma-model-type';
5
- import { UserStatus } from '@/db/constants';
6
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
3
+ import type { Prisma } from '@core/db/prisma-model-type';
4
+ import type { User } from '@core/db/prisma-model-type';
5
+ import { UserStatus } from '@core/db/constants';
6
+ import { checkAndFallbackWithNonTCClient } from '@core/prisma/index';
7
7
 
8
8
  export class UserService {
9
9
 
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import type { Prisma } from '@/db/prisma-model-type';
4
- import type { UserBackup } from '@/db/prisma-model-type';
5
- import { checkAndFallbackWithNonTCClient } from '@/prisma/index';
3
+ import type { Prisma } from '@core/db/prisma-model-type';
4
+ import type { UserBackup } from '@core/db/prisma-model-type';
5
+ import { checkAndFallbackWithNonTCClient, type BackendCorePrismaClient } from '@core/prisma/index';
6
6
 
7
7
  export class UserBackupService {
8
8
 
@@ -135,7 +135,7 @@ export class UserBackupService {
135
135
  throw new Error('No backup data available');
136
136
  }
137
137
 
138
- const restore = async (client: Prisma.TransactionClient) => {
138
+ const restore = async (client: Prisma.TransactionClient | BackendCorePrismaClient) => {
139
139
  const user = await client.user.create({
140
140
  data: {
141
141
  fingerprintId: backupData.fingerprintId,
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import { billingAggregateService } from '@/aggregate/billing.aggregate.service';
3
+ import { billingAggregateService } from '@core/aggregate/billing.aggregate.service';
4
4
  import {
5
5
  Apilogger,
6
6
  BillingReason,
@@ -9,11 +9,11 @@ import {
9
9
  subscriptionService,
10
10
  transactionService,
11
11
  TransactionType
12
- } from '@/db/index';
13
- import { Transaction } from '@/db/prisma-model-type';
14
- import { oneTimeExpiredDays } from '@/lib/credit-init';
15
- import { getCreditsFromPriceId } from '@/lib/money-price-config';
16
- import { fetchPaymentId, getStripe } from '@/lib/stripe-config';
12
+ } from '@core/db/index';
13
+ import { Transaction } from '@core/db/prisma-model-type';
14
+ import { oneTimeExpiredDays } from '@core/lib/credit-init';
15
+ import { getCreditsFromPriceId } from '@core/lib/money-price-config';
16
+ import { fetchPaymentId, getStripe } from '@core/lib/stripe-config';
17
17
  import Stripe from 'stripe';
18
18
  import { viewLocalTime } from '@windrun-huaiin/lib/utils';
19
19
 
@@ -0,0 +1,2 @@
1
+ export * from '../lib/upstash-config';
2
+ export * from '../lib/upstash';
@@ -1,8 +0,0 @@
1
- export * from './money-price-config';
2
- export * from './money-price-helper';
3
- export * from './fingerprint-config';
4
- export * from './credit-init';
5
- export * from './stripe-config';
6
- export * from './upstash-config';
7
- export * from './upstash';
8
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC"}
@@ -1,12 +0,0 @@
1
- export { getActiveProviderConfig, getCreditsFromPriceId, getPriceConfig, moneyPriceConfig } from './money-price-config.mjs';
2
- export { getMoneyPriceInitUserContext } from './money-price-helper.mjs';
3
- export { fingerprintConfig } from './fingerprint-config.mjs';
4
- export { creditsConfig, freeAmount, freeExpiredDays, freeRegisterAmount, oneTimeExpiredDays } from './credit-init.mjs';
5
- export { ActiveSubscriptionExistsError, cancelSubscription, createCheckoutSession, createCustomerPortalSession, createOrGetCustomer, fetchPaymentId, getStripe, updateSubscription, validateStripeWebhook } from './stripe-config.mjs';
6
- export { getPrefixedQstashQueueName, getPrefixedRedisKey, getPrefixedRedisKeys, getQstash, getQstashNamePrefix, getRedis, withQstash, withRedis } from './upstash-config.mjs';
7
- export { acquireLock, releaseLock, withLock } from './upstash/redis-lock.mjs';
8
- export { getTargetLikeCount, getUserLikedTargets, isTargetLiked, likeTarget, unlikeTarget } from './upstash/redis-like.mjs';
9
- export { addFavorite, getFavoriteCount, getUserFavorites, isFavorited, removeFavorite } from './upstash/redis-favorite.mjs';
10
- export { getCounter, getUniqueCounter, incrCounter, incrUniqueCounter } from './upstash/redis-counter.mjs';
11
- export { del, deleteHashField, deleteKey, exists, expire, getHashAll, getHashField, getHashJson, getJson, getString, hexists, hkeys, hlen, hmget, hmset, listLength, mget, mgetJson, mset, msetJson, pipeline, popList, pushList, rangeList, sadd, scard, setHashField, setHashJson, setJson, setString, sismember, smembers, srem, ttl } from './upstash/redis-structures.mjs';
12
- export { cancelSchedule, publishBroadcastMessage, publishDelayedMessage, publishFIFOQueueMessage, publishMessage, scheduleMessage, verifyQstashSignature } from './upstash/qstash.mjs';
package/src/lib/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export * from './money-price-config';
2
- export * from './money-price-helper';
3
- export * from './fingerprint-config';
4
- export * from './credit-init';
5
- export * from './stripe-config';
6
- export * from './upstash-config';
7
- export * from './upstash';