Skip to content

Commit 45b3c52

Browse files
add transaction proxies for studio
1 parent b5a2614 commit 45b3c52

File tree

5 files changed

+17528
-9991
lines changed

5 files changed

+17528
-9991
lines changed

drizzle-kit/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
"postgres": "^3.4.4",
109109
"prettier": "^3.5.3",
110110
"semver": "^7.7.2",
111-
"superjson": "^2.2.1",
112111
"tsup": "^8.3.5",
113112
"tsx": "^3.12.1",
114113
"typescript": "^5.6.3",

drizzle-kit/src/cli/commands/studio.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import { compress } from 'hono/compress';
2828
import { cors } from 'hono/cors';
2929
import { createServer } from 'node:https';
3030
import { LibSQLCredentials } from 'src/cli/validations/libsql';
31-
import superjson from 'superjson';
3231
import { z } from 'zod';
33-
import { assertUnreachable } from '../../utils';
32+
import { assertUnreachable, Proxy, TransactionProxy } from '../../utils';
3433
import { safeRegister } from '../../utils/utils-node';
3534
import { prepareFilenames } from '../../utils/utils-node';
3635
import type { MysqlCredentials } from '../validations/mysql';
@@ -54,7 +53,8 @@ export type Setup = {
5453
dbHash: string;
5554
dialect: 'postgresql' | 'mysql' | 'sqlite' | 'singlestore';
5655
driver?: 'aws-data-api' | 'd1-http' | 'turso' | 'pglite';
57-
proxy: (params: ProxyParams) => Promise<any[] | any>;
56+
proxy: Proxy;
57+
transactionProxy: TransactionProxy;
5858
customDefaults: CustomDefault[];
5959
schema: Record<string, Record<string, AnyTable<any>>>;
6060
relations: Record<string, Relations>;
@@ -63,7 +63,7 @@ export type Setup = {
6363

6464
export type ProxyParams = {
6565
sql: string;
66-
params: any[];
66+
params?: any[];
6767
typings?: any[];
6868
mode: 'array' | 'object';
6969
method: 'values' | 'get' | 'all' | 'run' | 'execute';
@@ -325,6 +325,7 @@ export const drizzleForPostgres = async (
325325
dialect: 'postgresql',
326326
driver: 'driver' in credentials ? credentials.driver : undefined,
327327
proxy: db.proxy,
328+
transactionProxy: db.transactionProxy,
328329
customDefaults,
329330
schema: pgSchema,
330331
relations,
@@ -339,7 +340,7 @@ export const drizzleForMySQL = async (
339340
schemaFiles?: SchemaFile[],
340341
): Promise<Setup> => {
341342
const { connectToMySQL } = await import('../connections');
342-
const { proxy } = await connectToMySQL(credentials);
343+
const { proxy, transactionProxy } = await connectToMySQL(credentials);
343344

344345
const customDefaults = getCustomDefaults(mysqlSchema);
345346

@@ -358,6 +359,7 @@ export const drizzleForMySQL = async (
358359
dbHash,
359360
dialect: 'mysql',
360361
proxy,
362+
transactionProxy,
361363
customDefaults,
362364
schema: mysqlSchema,
363365
relations,
@@ -430,6 +432,7 @@ export const drizzleForSQLite = async (
430432
dialect: 'sqlite',
431433
driver: 'driver' in credentials ? credentials.driver : undefined,
432434
proxy: sqliteDB.proxy,
435+
transactionProxy: sqliteDB.transactionProxy,
433436
customDefaults,
434437
schema: sqliteSchema,
435438
relations,
@@ -456,6 +459,7 @@ export const drizzleForLibSQL = async (
456459
dialect: 'sqlite',
457460
driver: undefined,
458461
proxy: sqliteDB.proxy,
462+
transactionProxy: sqliteDB.transactionProxy,
459463
customDefaults,
460464
schema: sqliteSchema,
461465
relations,
@@ -470,7 +474,7 @@ export const drizzleForSingleStore = async (
470474
schemaFiles?: SchemaFile[],
471475
): Promise<Setup> => {
472476
const { connectToSingleStore } = await import('../connections');
473-
const { proxy } = await connectToSingleStore(credentials);
477+
const { proxy, transactionProxy } = await connectToSingleStore(credentials);
474478

475479
const customDefaults = getCustomDefaults(singlestoreSchema);
476480

@@ -489,6 +493,7 @@ export const drizzleForSingleStore = async (
489493
dbHash,
490494
dialect: 'singlestore',
491495
proxy,
496+
transactionProxy,
492497
customDefaults,
493498
schema: singlestoreSchema,
494499
relations,
@@ -573,6 +578,25 @@ const proxySchema = z.object({
573578
}),
574579
});
575580

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+
576600
const defaultsSchema = z.object({
577601
type: z.literal('defaults'),
578602
data: z
@@ -586,19 +610,18 @@ const defaultsSchema = z.object({
586610
.min(1),
587611
});
588612

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]);
599614

600615
const jsonStringify = (data: any) => {
601616
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
602625
if (typeof value === 'bigint') {
603626
return value.toString();
604627
}
@@ -635,6 +658,7 @@ export const prepareServer = async (
635658
dialect,
636659
driver,
637660
proxy,
661+
transactionProxy,
638662
customDefaults,
639663
schema: drizzleSchema,
640664
relations,
@@ -714,6 +738,11 @@ export const prepareServer = async (
714738
return c.json(JSON.parse(jsonStringify(result)));
715739
}
716740

741+
if (type === 'tproxy') {
742+
const result = await transactionProxy(body.data);
743+
return c.json(JSON.parse(jsonStringify(result)));
744+
}
745+
717746
if (type === 'defaults') {
718747
const columns = body.data;
719748

0 commit comments

Comments
 (0)