@@ -29,23 +29,34 @@ export class LiteREST {
2929 * @param tableName - The name of the table.
3030 * @returns An array of primary key column names.
3131 */
32- private async getPrimaryKeyColumns ( tableName : string ) : Promise < string [ ] > {
32+ private async getPrimaryKeyColumns ( tableName : string , schemaName ?: string ) : Promise < string [ ] > {
3333 let query = `PRAGMA table_info(${ tableName } );` ;
34-
34+
3535 if ( this . dataSource . source === Source . external ) {
3636 if ( this . env . EXTERNAL_DB_TYPE ?. toLowerCase ( ) === "postgres" ) {
37- query = `SELECT * FROM information_schema.table_constraints WHERE table_name = '${ tableName } ' AND constraint_type = 'PRIMARY KEY';` ;
37+ query = `
38+ SELECT kcu.column_name AS name
39+ FROM information_schema.table_constraints tc
40+ JOIN information_schema.key_column_usage kcu
41+ ON tc.constraint_name = kcu.constraint_name
42+ AND tc.table_schema = kcu.table_schema
43+ AND tc.table_name = kcu.table_name
44+ WHERE tc.constraint_type = 'PRIMARY KEY'
45+ AND tc.table_name = '${ tableName } '
46+ AND tc.table_schema = '${ schemaName ?? 'public' } ';` ;
3847 } else if ( this . env . EXTERNAL_DB_TYPE ?. toLowerCase ( ) === "mysql" ) {
39- query = `SELECT COLUMN_NAME FROM information_schema.key_column_usage
40- WHERE table_name = '${ tableName } '
41- AND constraint_name = 'PRIMARY'
42- AND table_schema = DATABASE();` ;
48+ query = `
49+ SELECT COLUMN_NAME AS name FROM information_schema.key_column_usage
50+ WHERE table_name = '${ tableName } '
51+ AND constraint_name = 'PRIMARY'
52+ AND table_schema = ${ schemaName ?? 'DATABASE()' } ;
53+ ` ;
4354 }
4455 }
4556
46- const schemaInfo = ( await executeQuery ( query , [ ] , false , this . dataSource ) ) as any [ ] ;
57+ const schemaInfo = ( await executeQuery ( query , this . dataSource . source === Source . external ? { } : [ ] , false , this . dataSource , this . env ) ) as any [ ] ;
4758 const pkColumns = schemaInfo
48- . filter ( col => typeof col . pk === 'number' && col . pk > 0 && col . name !== null )
59+ // .filter(col => typeof col.pk === 'number' && col.pk > 0 && col.name !== null)
4960 . map ( col => col . name as string ) ;
5061 return pkColumns ;
5162 }
@@ -90,7 +101,7 @@ export class LiteREST {
90101 const conditions : string [ ] = [ ] ;
91102 const params : any [ ] = [ ] ;
92103
93- if ( pkColumns . length === 1 ) {
104+ if ( pkColumns ? .length === 1 ) {
94105 const pk = pkColumns [ 0 ] ;
95106 const pkValue = id || data [ pk ] || searchParams . get ( pk ) ;
96107 if ( ! pkValue ) {
@@ -118,7 +129,7 @@ export class LiteREST {
118129 * @param queries - The operations to execute.
119130 */
120131 private async executeOperation ( queries : { sql : string , params : any [ ] } [ ] ) : Promise < { result ?: any , error ?: string | undefined , status : number } > {
121- const results : any [ ] = ( await executeTransaction ( queries , false , this . dataSource ) ) as any [ ] ;
132+ const results : any [ ] = ( await executeTransaction ( queries , false , this . dataSource , this . env ) ) as any [ ] ;
122133 return { result : results ?. length > 0 ? results [ 0 ] : undefined , status : 200 } ;
123134 }
124135
@@ -128,20 +139,20 @@ export class LiteREST {
128139 * @returns The response to the request.
129140 */
130141 async handleRequest ( request : Request ) : Promise < Response > {
131- const { method, tableName, id, searchParams, body } = await this . parseRequest ( request ) ;
142+ const { method, tableName, schemaName , id, searchParams, body } = await this . parseRequest ( request ) ;
132143
133144 try {
134145 switch ( method ) {
135146 case 'GET' :
136- return await this . handleGet ( tableName , id , searchParams ) ;
147+ return await this . handleGet ( tableName , schemaName , id , searchParams ) ;
137148 case 'POST' :
138- return await this . handlePost ( tableName , body ) ;
149+ return await this . handlePost ( tableName , schemaName , body ) ;
139150 case 'PATCH' :
140- return await this . handlePatch ( tableName , id , body ) ;
151+ return await this . handlePatch ( tableName , schemaName , id , body ) ;
141152 case 'PUT' :
142- return await this . handlePut ( tableName , id , body ) ;
153+ return await this . handlePut ( tableName , schemaName , id , body ) ;
143154 case 'DELETE' :
144- return await this . handleDelete ( tableName , id ) ;
155+ return await this . handleDelete ( tableName , schemaName , id ) ;
145156 default :
146157 return createResponse ( undefined , 'Method not allowed' , 405 ) ;
147158 }
@@ -156,7 +167,7 @@ export class LiteREST {
156167 * @param request - The incoming request.
157168 * @returns An object containing the method, table name, id, search parameters, and body.
158169 */
159- private async parseRequest ( request : Request ) : Promise < { method : string , tableName : string , id ?: string , searchParams : URLSearchParams , body ?: any } > {
170+ private async parseRequest ( request : Request ) : Promise < { method : string , tableName : string , schemaName : string | undefined , id ?: string , searchParams : URLSearchParams , body ?: any } > {
160171 const liteRequest = new Request ( request . url . replace ( '/rest' , '' ) , request ) ;
161172 const url = new URL ( liteRequest . url ) ;
162173 const pathParts = url . pathname . split ( '/' ) . filter ( Boolean ) ;
@@ -165,24 +176,26 @@ export class LiteREST {
165176 throw new Error ( 'Invalid route' ) ;
166177 }
167178
168- const tableName = this . sanitizeIdentifier ( pathParts [ 0 ] ) ;
169- const id = pathParts [ 1 ] ;
179+ const tableName = this . sanitizeIdentifier ( pathParts . length === 1 ? pathParts [ 0 ] : pathParts [ 1 ] ) ;
180+ const schemaName = pathParts . length === 1 ? undefined : this . sanitizeIdentifier ( pathParts [ 0 ] )
181+ const id = pathParts . length === 1 ? pathParts [ 1 ] : pathParts [ 2 ] ;
170182 const body = [ 'POST' , 'PUT' , 'PATCH' ] . includes ( liteRequest . method ) ? await liteRequest . json ( ) : undefined ;
171183
172184 return {
173185 method : liteRequest . method ,
174186 tableName,
187+ schemaName,
175188 id,
176189 searchParams : url . searchParams ,
177190 body
178191 } ;
179192 }
180193
181- private async buildSelectQuery ( tableName : string , id : string | undefined , searchParams : URLSearchParams ) : Promise < { query : string , params : any [ ] } > {
182- let query = `SELECT * FROM ${ tableName } ` ;
194+ private async buildSelectQuery ( tableName : string , schemaName : string | undefined , id : string | undefined , searchParams : URLSearchParams ) : Promise < { query : string , params : any [ ] } > {
195+ let query = `SELECT * FROM ${ schemaName ? ` ${ schemaName } .` : '' } ${ tableName } ` ;
183196 const params : any [ ] = [ ] ;
184197 const conditions : string [ ] = [ ] ;
185- const pkColumns = await this . getPrimaryKeyColumns ( tableName ) ;
198+ const pkColumns = await this . getPrimaryKeyColumns ( tableName , schemaName ) ;
186199 const { conditions : pkConditions , params : pkParams , error } = this . getPrimaryKeyConditions ( pkColumns , id , { } , searchParams ) ;
187200
188201 if ( ! error ) {
@@ -249,8 +262,8 @@ export class LiteREST {
249262 return { query, params } ;
250263 }
251264
252- private async handleGet ( tableName : string , id : string | undefined , searchParams : URLSearchParams ) : Promise < Response > {
253- const { query, params } = await this . buildSelectQuery ( tableName , id , searchParams ) ;
265+ private async handleGet ( tableName : string , schemaName : string | undefined , id : string | undefined , searchParams : URLSearchParams ) : Promise < Response > {
266+ const { query, params } = await this . buildSelectQuery ( tableName , schemaName , id , searchParams ) ;
254267
255268 try {
256269 const response = await this . executeOperation ( [ { sql : query , params } ] ) ;
@@ -262,7 +275,7 @@ export class LiteREST {
262275 }
263276 }
264277
265- private async handlePost ( tableName : string , data : any ) : Promise < Response > {
278+ private async handlePost ( tableName : string , schemaName : string | undefined , data : any ) : Promise < Response > {
266279 if ( ! this . isDataValid ( data ) ) {
267280 console . error ( 'Invalid data format for POST:' , data ) ;
268281 return createResponse ( undefined , 'Invalid data format' , 400 ) ;
@@ -293,8 +306,8 @@ export class LiteREST {
293306 }
294307 }
295308
296- private async handlePatch ( tableName : string , id : string | undefined , data : any ) : Promise < Response > {
297- const pkColumns = await this . getPrimaryKeyColumns ( tableName ) ;
309+ private async handlePatch ( tableName : string , schemaName : string | undefined , id : string | undefined , data : any ) : Promise < Response > {
310+ const pkColumns = await this . getPrimaryKeyColumns ( tableName , schemaName ) ;
298311
299312 const { conditions : pkConditions , params : pkParams , error } = this . getPrimaryKeyConditions ( pkColumns , id , data , new URLSearchParams ( ) ) ;
300313
@@ -342,8 +355,8 @@ export class LiteREST {
342355 }
343356 }
344357
345- private async handlePut ( tableName : string , id : string | undefined , data : any ) : Promise < Response > {
346- const pkColumns = await this . getPrimaryKeyColumns ( tableName ) ;
358+ private async handlePut ( tableName : string , schemaName : string | undefined , id : string | undefined , data : any ) : Promise < Response > {
359+ const pkColumns = await this . getPrimaryKeyColumns ( tableName , schemaName ) ;
347360
348361 const { conditions : pkConditions , params : pkParams , error } = this . getPrimaryKeyConditions ( pkColumns , id , data , new URLSearchParams ( ) ) ;
349362
@@ -383,8 +396,8 @@ export class LiteREST {
383396 }
384397 }
385398
386- private async handleDelete ( tableName : string , id : string | undefined ) : Promise < Response > {
387- const pkColumns = await this . getPrimaryKeyColumns ( tableName ) ;
399+ private async handleDelete ( tableName : string , schemaName : string | undefined , id : string | undefined ) : Promise < Response > {
400+ const pkColumns = await this . getPrimaryKeyColumns ( tableName , schemaName ) ;
388401
389402 const { conditions : pkConditions , params : pkParams , error } = this . getPrimaryKeyConditions ( pkColumns , id , { } , new URLSearchParams ( ) ) ;
390403
0 commit comments