11import { LinkedQLClient } from './LinkedQLClient.js' ;
22import { RealtimeClient } from '../../proc/realtime/RealtimeClient.js' ;
33import { MainstreamSchemaInference } from './MainstreamSchemaInference.js' ;
4- import { MainstreamWalEngine } from './MainstreamWalEngine.js' ;
54import { SQLParser } from '../../lang/SQLParser.js' ;
65import { registry } from '../../lang/registry.js' ;
76import { normalizeQueryArgs } from './util.js' ;
87import { Result } from '../Result.js' ;
98
10- export class MainstreamDBClient extends LinkedQLClient {
9+ export class MainstreamClient extends LinkedQLClient {
1110
12- // Standard getters: parsers, resolver, wal
11+ // Standard getters: parsers, resolver
1312
1413 #parser;
15- #wal;
1614 #live;
1715
1816 get parser ( ) { return this . #parser; }
1917 get resolver ( ) {
2018 return super . resolveGetResolver ( ( ) =>
21- new MainstreamSchemaInference ( { client : this , dialect : this . dialect } ) ) ;
19+ new MainstreamSchemaInference ( { mainstreamClient : this , dialect : this . dialect } ) ) ;
2220 }
23- get wal ( ) { return this . #wal; }
2421 get live ( ) { return this . #live; }
2522
2623 // Internal
@@ -33,64 +30,54 @@ export class MainstreamDBClient extends LinkedQLClient {
3330 super ( options ) ;
3431
3532 this . #parser = new SQLParser ( { dialect : this . dialect } ) ;
36- this . #wal = new MainstreamWalEngine ( {
37- client : this ,
38- drainMode : 'drain' ,
39- lifecycleHook : async ( status ) => {
40- await this . setCapability ( { realtime : ! ! status } ) ;
41- }
42- } ) ;
4333
4434 this . #realtimeClient = new RealtimeClient ( this ) ;
4535 this . #live = {
4636 forget : async ( id ) => await this . #realtimeClient. forget ( id ) ,
4737 } ;
4838 }
4939
50- async disconnect ( ) {
51- await this . #wal. close ( { destroy : true } ) ;
52- await super . disconnect ( ) ;
53- }
54-
5540 // ------------
5641
57- async transaction ( cb ) {
42+ async begin ( options = { } ) {
43+ if ( options . parentTx ) {
44+ throw new Error ( `Nested transactions are not supported on mainstream databasew for now` ) ;
45+ }
46+ return await this . _begin ( options ) ;
47+ }
48+
49+ async transaction ( cb , options = { } ) {
5850 if ( typeof cb !== 'function' ) {
5951 throw new TypeError ( 'transaction(cb): cb must be a function' ) ;
6052 }
61- if ( typeof this . _beginTransaction !== 'function'
62- || typeof this . _commitTransaction !== 'function'
63- || typeof this . _rollbackTransaction !== 'function' ) {
64- throw new Error ( 'Transaction not supported by this client implementation' ) ;
65- }
66-
67- const tx = await this . _beginTransaction ( ) ;
53+
54+ const tx = await this . begin ( options ) ;
6855
6956 try {
7057 const result = await cb ( tx ) ;
71- await this . _commitTransaction ( tx ) ;
58+ await tx . commit ( ) ;
7259 return result ;
7360 } catch ( e ) {
74- await this . _rollbackTransaction ( tx ) ;
61+ await tx . rollback ( ) ;
7562 throw e ;
7663 }
7764 }
7865
7966 // ------------
8067
8168 async query ( ...args ) {
82- const [ _query , options ] = normalizeQueryArgs ( ...args ) ;
69+ const [ _query , { tx : inputTx , ... options } ] = normalizeQueryArgs ( ...args ) ;
8370 const query = await this . #parser. parse ( _query , options ) ;
8471
85- const resolveQuery = async ( query , tx = null , ifHasSugars = false ) => {
72+ const resolveQuery = async ( query , tx , ifHasSugars = false ) => {
8673 const schemaInference = this . resolver ;
8774 return await schemaInference . resolveQuery ( query , { tx, ifHasSugars } ) ;
8875 } ;
8976
9077 // Realtime query?
9178 if ( options . live ) {
92- const resolvedQuery = await resolveQuery ( query ) ;
93- return await this . #realtimeClient. query ( resolvedQuery , options ) ;
79+ const resolvedQuery = await resolveQuery ( query , inputTx ) ;
80+ return await this . #realtimeClient. query ( resolvedQuery , { ... options , tx : inputTx } ) ;
9481 }
9582
9683 let result ;
@@ -108,22 +95,25 @@ export class MainstreamDBClient extends LinkedQLClient {
10895 { ...options , tx }
10996 ) ;
11097 }
111- } ) ;
98+ } , { parentTx : inputTx } ) ;
11299 } else {
113- result = await this . _query ( await resolveQuery ( query , null , true ) , options ) ;
100+ result = await this . _query (
101+ await resolveQuery ( query , inputTx , true ) ,
102+ { ...options , tx : inputTx }
103+ ) ;
114104 }
115105
116106 // The result instance
117107 return new Result ( { rows : result . rows , rowCount : result . rowCount } ) ;
118108 }
119109
120110 async stream ( ...args ) {
121- const [ _query , options ] = normalizeQueryArgs ( ...args ) ;
111+ const [ _query , { tx : inputTx , ... options } ] = normalizeQueryArgs ( ...args ) ;
122112 const query = await this . #parser. parse ( _query , options ) ;
123113
124114 const schemaInference = this . resolver ;
125- const resolvedQuery = await schemaInference . resolveQuery ( query , { ...options , ifHasSugars : true } ) ;
115+ const resolvedQuery = await schemaInference . resolveQuery ( query , { ...options , tx : inputTx , ifHasSugars : true } ) ;
126116
127- return await this . _stream ( resolvedQuery , options ) ;
117+ return await this . _stream ( resolvedQuery , { ... options , tx : inputTx } ) ;
128118 }
129119}
0 commit comments