@@ -10,11 +10,60 @@ function getCommentsDatabaseUrl(): string {
1010 return `mysql://${ user } :${ password } @${ host } :${ port } /${ database } ` ;
1111}
1212
13+ function serializeBigInt ( obj : any ) : any {
14+ if ( obj === null || obj === undefined ) return obj ;
15+ if ( typeof obj === 'bigint' ) return obj . toString ( ) ;
16+ if ( Array . isArray ( obj ) ) return obj . map ( serializeBigInt ) ;
17+ if ( typeof obj === 'object' && ! ( obj instanceof Date ) ) {
18+ return Object . fromEntries (
19+ Object . entries ( obj ) . map ( ( [ key , value ] ) => [ key , serializeBigInt ( value ) ] )
20+ ) ;
21+ }
22+ return obj ;
23+ }
24+
25+ function autoParseJson ( obj : any ) : any {
26+ if ( obj === null || obj === undefined ) return obj ;
27+ if ( typeof obj === 'string' || Buffer . isBuffer ( obj ) ) {
28+ const str = obj . toString ( ) . trim ( ) ;
29+ if ( ( str . startsWith ( '{' ) && str . endsWith ( '}' ) ) || ( str . startsWith ( '[' ) && str . endsWith ( ']' ) ) ) {
30+ try {
31+ const parsed = JSON . parse ( str ) ;
32+ return autoParseJson ( parsed ) ;
33+ } catch {
34+ return obj ;
35+ }
36+ }
37+ }
38+
39+ if ( Array . isArray ( obj ) ) return obj . map ( autoParseJson ) ;
40+ if ( typeof obj === 'object' && ! ( obj instanceof Date ) ) {
41+ return Object . fromEntries (
42+ Object . entries ( obj ) . map ( ( [ key , value ] ) => [ key , autoParseJson ( value ) ] )
43+ ) ;
44+ }
45+ return obj ;
46+ }
47+
48+ const globalTransform = async ( { args, query } : any ) => {
49+ const result = await query ( args ) ;
50+ const parsed = autoParseJson ( result ) ;
51+ return serializeBigInt ( parsed ) ;
52+ } ;
53+
1354const adapter = new PrismaMariaDb ( getCommentsDatabaseUrl ( ) ) ;
55+ const baseClient = new PrismaClient ( { adapter } ) ;
56+
57+ export const commentsDb = baseClient . $extends ( {
58+ query : {
59+ $allModels : { $allOperations : globalTransform } ,
60+ $queryRaw : globalTransform ,
61+ $executeRaw : globalTransform ,
62+ } ,
63+ } ) ;
1464
15- const globalForPrisma = globalThis as unknown as { commentsDb : PrismaClient } ;
16- export const commentsDb =
17- globalForPrisma . commentsDb ?? new PrismaClient ( { adapter } ) ;
65+ type ExtendedPrismaClient = typeof commentsDb ;
66+ const globalForPrisma = globalThis as unknown as { commentsDb : ExtendedPrismaClient } ;
1867if ( process . env . NEXT_PUBLIC_SITE_VERSION !== 'production' ) {
1968 globalForPrisma . commentsDb = commentsDb ;
2069}
0 commit comments