@@ -28,9 +28,8 @@ import { compress } from 'hono/compress';
28
28
import { cors } from 'hono/cors' ;
29
29
import { createServer } from 'node:https' ;
30
30
import { LibSQLCredentials } from 'src/cli/validations/libsql' ;
31
- import superjson from 'superjson' ;
32
31
import { z } from 'zod' ;
33
- import { assertUnreachable } from '../../utils' ;
32
+ import { assertUnreachable , Proxy , TransactionProxy } from '../../utils' ;
34
33
import { safeRegister } from '../../utils/utils-node' ;
35
34
import { prepareFilenames } from '../../utils/utils-node' ;
36
35
import type { MysqlCredentials } from '../validations/mysql' ;
@@ -54,7 +53,8 @@ export type Setup = {
54
53
dbHash : string ;
55
54
dialect : 'postgresql' | 'mysql' | 'sqlite' | 'singlestore' ;
56
55
driver ?: 'aws-data-api' | 'd1-http' | 'turso' | 'pglite' ;
57
- proxy : ( params : ProxyParams ) => Promise < any [ ] | any > ;
56
+ proxy : Proxy ;
57
+ transactionProxy : TransactionProxy ;
58
58
customDefaults : CustomDefault [ ] ;
59
59
schema : Record < string , Record < string , AnyTable < any > > > ;
60
60
relations : Record < string , Relations > ;
@@ -63,7 +63,7 @@ export type Setup = {
63
63
64
64
export type ProxyParams = {
65
65
sql : string ;
66
- params : any [ ] ;
66
+ params ? : any [ ] ;
67
67
typings ?: any [ ] ;
68
68
mode : 'array' | 'object' ;
69
69
method : 'values' | 'get' | 'all' | 'run' | 'execute' ;
@@ -325,6 +325,7 @@ export const drizzleForPostgres = async (
325
325
dialect : 'postgresql' ,
326
326
driver : 'driver' in credentials ? credentials . driver : undefined ,
327
327
proxy : db . proxy ,
328
+ transactionProxy : db . transactionProxy ,
328
329
customDefaults,
329
330
schema : pgSchema ,
330
331
relations,
@@ -339,7 +340,7 @@ export const drizzleForMySQL = async (
339
340
schemaFiles ?: SchemaFile [ ] ,
340
341
) : Promise < Setup > => {
341
342
const { connectToMySQL } = await import ( '../connections' ) ;
342
- const { proxy } = await connectToMySQL ( credentials ) ;
343
+ const { proxy, transactionProxy } = await connectToMySQL ( credentials ) ;
343
344
344
345
const customDefaults = getCustomDefaults ( mysqlSchema ) ;
345
346
@@ -358,6 +359,7 @@ export const drizzleForMySQL = async (
358
359
dbHash,
359
360
dialect : 'mysql' ,
360
361
proxy,
362
+ transactionProxy,
361
363
customDefaults,
362
364
schema : mysqlSchema ,
363
365
relations,
@@ -430,6 +432,7 @@ export const drizzleForSQLite = async (
430
432
dialect : 'sqlite' ,
431
433
driver : 'driver' in credentials ? credentials . driver : undefined ,
432
434
proxy : sqliteDB . proxy ,
435
+ transactionProxy : sqliteDB . transactionProxy ,
433
436
customDefaults,
434
437
schema : sqliteSchema ,
435
438
relations,
@@ -456,6 +459,7 @@ export const drizzleForLibSQL = async (
456
459
dialect : 'sqlite' ,
457
460
driver : undefined ,
458
461
proxy : sqliteDB . proxy ,
462
+ transactionProxy : sqliteDB . transactionProxy ,
459
463
customDefaults,
460
464
schema : sqliteSchema ,
461
465
relations,
@@ -470,7 +474,7 @@ export const drizzleForSingleStore = async (
470
474
schemaFiles ?: SchemaFile [ ] ,
471
475
) : Promise < Setup > => {
472
476
const { connectToSingleStore } = await import ( '../connections' ) ;
473
- const { proxy } = await connectToSingleStore ( credentials ) ;
477
+ const { proxy, transactionProxy } = await connectToSingleStore ( credentials ) ;
474
478
475
479
const customDefaults = getCustomDefaults ( singlestoreSchema ) ;
476
480
@@ -489,6 +493,7 @@ export const drizzleForSingleStore = async (
489
493
dbHash,
490
494
dialect : 'singlestore' ,
491
495
proxy,
496
+ transactionProxy,
492
497
customDefaults,
493
498
schema : singlestoreSchema ,
494
499
relations,
@@ -573,6 +578,25 @@ const proxySchema = z.object({
573
578
} ) ,
574
579
} ) ;
575
580
581
+ const transactionProxySchema = z . object ( {
582
+ type : z . literal ( 'tproxy' ) ,
583
+ data : z
584
+ . object ( {
585
+ sql : z . string ( ) ,
586
+ params : z . array ( z . any ( ) ) . optional ( ) ,
587
+ typings : z . string ( ) . array ( ) . optional ( ) ,
588
+ mode : z . enum ( [ 'array' , 'object' ] ) . default ( 'object' ) ,
589
+ method : z . union ( [
590
+ z . literal ( 'values' ) ,
591
+ z . literal ( 'get' ) ,
592
+ z . literal ( 'all' ) ,
593
+ z . literal ( 'run' ) ,
594
+ z . literal ( 'execute' ) ,
595
+ ] ) ,
596
+ } )
597
+ . array ( ) ,
598
+ } ) ;
599
+
576
600
const defaultsSchema = z . object ( {
577
601
type : z . literal ( 'defaults' ) ,
578
602
data : z
@@ -586,19 +610,18 @@ const defaultsSchema = z.object({
586
610
. min ( 1 ) ,
587
611
} ) ;
588
612
589
- const schema = z . union ( [ init , proxySchema , defaultsSchema ] ) ;
590
-
591
- superjson . registerCustom < Buffer , number [ ] > (
592
- {
593
- isApplicable : ( v ) : v is Buffer => v instanceof Buffer ,
594
- serialize : ( v ) => [ ...v ] ,
595
- deserialize : ( v ) => Buffer . from ( v ) ,
596
- } ,
597
- 'buffer' ,
598
- ) ;
613
+ const schema = z . union ( [ init , proxySchema , transactionProxySchema , defaultsSchema ] ) ;
599
614
600
615
const jsonStringify = ( data : any ) => {
601
616
return JSON . stringify ( data , ( _key , value ) => {
617
+ // Convert Error to object
618
+ if ( value instanceof Error ) {
619
+ return {
620
+ error : value . message ,
621
+ } ;
622
+ }
623
+
624
+ // Convert BigInt to string
602
625
if ( typeof value === 'bigint' ) {
603
626
return value . toString ( ) ;
604
627
}
@@ -635,6 +658,7 @@ export const prepareServer = async (
635
658
dialect,
636
659
driver,
637
660
proxy,
661
+ transactionProxy,
638
662
customDefaults,
639
663
schema : drizzleSchema ,
640
664
relations,
@@ -714,6 +738,11 @@ export const prepareServer = async (
714
738
return c . json ( JSON . parse ( jsonStringify ( result ) ) ) ;
715
739
}
716
740
741
+ if ( type === 'tproxy' ) {
742
+ const result = await transactionProxy ( body . data ) ;
743
+ return c . json ( JSON . parse ( jsonStringify ( result ) ) ) ;
744
+ }
745
+
717
746
if ( type === 'defaults' ) {
718
747
const columns = body . data ;
719
748
0 commit comments