@@ -5,7 +5,7 @@ import fetch from 'node-fetch';
55import ws from 'ws' ;
66import { assertUnreachable } from '../global' ;
77import type { ProxyParams } from '../serializer/studio' ;
8- import { type DB , normaliseSQLiteUrl , type Proxy , type SQLiteDB , type SqliteProxy } from '../utils' ;
8+ import { type DB , normalisePGliteUrl , normaliseSQLiteUrl , type Proxy , type SQLiteDB , type SqliteProxy } from '../utils' ;
99import { assertPackages , checkPackage } from './utils' ;
1010import type { MysqlCredentials } from './validations/mysql' ;
1111import { withStyle } from './validations/outputs' ;
@@ -21,7 +21,8 @@ export const preparePostgresDB = async (
2121 }
2222> => {
2323 if ( 'driver' in credentials ) {
24- if ( credentials . driver === 'aws-data-api' ) {
24+ const { driver } = credentials ;
25+ if ( driver === 'aws-data-api' ) {
2526 assertPackages ( '@aws-sdk/client-rds-data' ) ;
2627 const { RDSDataClient, ExecuteStatementCommand, TypeHint } = await import (
2728 '@aws-sdk/client-rds-data'
@@ -92,7 +93,45 @@ export const preparePostgresDB = async (
9293 } ;
9394 }
9495
95- assertUnreachable ( credentials . driver ) ;
96+ if ( driver === 'pglite' ) {
97+ assertPackages ( '@electric-sql/pglite' ) ;
98+ const { PGlite } = await import ( '@electric-sql/pglite' ) ;
99+ const { drizzle } = await import ( 'drizzle-orm/pglite' ) ;
100+ const { migrate } = await import ( 'drizzle-orm/pglite/migrator' ) ;
101+
102+ const pglite = new PGlite ( normalisePGliteUrl ( credentials . url ) ) ;
103+ await pglite . waitReady ;
104+ const drzl = drizzle ( pglite ) ;
105+ const migrateFn = async ( config : MigrationConfig ) => {
106+ return migrate ( drzl , config ) ;
107+ } ;
108+
109+ const query = async < T > ( sql : string , params : any [ ] = [ ] ) => {
110+ const result = await pglite . query ( sql , params ) ;
111+ return result . rows as T [ ] ;
112+ } ;
113+
114+ const proxy = async ( params : ProxyParams ) => {
115+ const preparedParams = preparePGliteParams ( params . params ) ;
116+ if (
117+ params . method === 'values'
118+ || params . method === 'get'
119+ || params . method === 'all'
120+ ) {
121+ const result = await pglite . query ( params . sql , preparedParams , {
122+ rowMode : params . mode ,
123+ } ) ;
124+ return result . rows ;
125+ }
126+
127+ const result = await pglite . query ( params . sql , preparedParams ) ;
128+ return result . rows ;
129+ } ;
130+
131+ return { query, proxy, migrate : migrateFn } ;
132+ }
133+
134+ assertUnreachable ( driver ) ;
96135 }
97136
98137 if ( await checkPackage ( 'pg' ) ) {
@@ -415,6 +454,25 @@ const prepareSqliteParams = (params: any[], driver?: string) => {
415454 } ) ;
416455} ;
417456
457+ const preparePGliteParams = ( params : any [ ] ) => {
458+ return params . map ( ( param ) => {
459+ if (
460+ param
461+ && typeof param === 'object'
462+ && 'type' in param
463+ && 'value' in param
464+ && param . type === 'binary'
465+ ) {
466+ const value = typeof param . value === 'object'
467+ ? JSON . stringify ( param . value )
468+ : ( param . value as string ) ;
469+
470+ return value ;
471+ }
472+ return param ;
473+ } ) ;
474+ } ;
475+
418476export const connectToSQLite = async (
419477 credentials : SqliteCredentials ,
420478) : Promise <
0 commit comments