( credentials?: GelCredentials, )
| 534 | }; |
| 535 | |
| 536 | export const prepareGelDB = async ( |
| 537 | credentials?: GelCredentials, |
| 538 | ): Promise< |
| 539 | DB & { |
| 540 | packageName: 'gel'; |
| 541 | proxy: Proxy; |
| 542 | transactionProxy: TransactionProxy; |
| 543 | } |
| 544 | > => { |
| 545 | if (await checkPackage('gel')) { |
| 546 | const gel = await import('gel'); |
| 547 | |
| 548 | let client: ReturnType<typeof gel.createClient>; |
| 549 | if (!credentials) { |
| 550 | client = gel.createClient(); |
| 551 | try { |
| 552 | await client.querySQL(`select 1;`); |
| 553 | } catch (error: any) { |
| 554 | if (error instanceof gel.ClientConnectionError) { |
| 555 | console.error( |
| 556 | `It looks like you forgot to link the Gel project or provide the database credentials. |
| 557 | To link your project, please refer https://docs.geldata.com/reference/cli/gel_instance/gel_instance_link, or add the dbCredentials to your configuration file.`, |
| 558 | ); |
| 559 | process.exit(1); |
| 560 | } |
| 561 | |
| 562 | throw error; |
| 563 | } |
| 564 | } else if ('url' in credentials) { |
| 565 | 'tlsSecurity' in credentials |
| 566 | ? client = gel.createClient({ dsn: credentials.url, tlsSecurity: credentials.tlsSecurity, concurrency: 1 }) |
| 567 | : client = gel.createClient({ dsn: credentials.url, concurrency: 1 }); |
| 568 | } else { |
| 569 | gel.createClient({ ...credentials, concurrency: 1 }); |
| 570 | } |
| 571 | |
| 572 | const query = async (sql: string, params?: any[]) => { |
| 573 | const result = params?.length ? await client.querySQL(sql, params) : await client.querySQL(sql); |
| 574 | return result as any[]; |
| 575 | }; |
| 576 | |
| 577 | const proxy: Proxy = async (params: ProxyParams) => { |
| 578 | const { method, mode, params: sqlParams, sql, typings } = params; |
| 579 | |
| 580 | let result: any[]; |
| 581 | switch (mode) { |
| 582 | case 'array': |
| 583 | result = sqlParams?.length |
| 584 | ? await client.withSQLRowMode('array').querySQL(sql, sqlParams) |
| 585 | : await client.withSQLRowMode('array').querySQL(sql); |
| 586 | break; |
| 587 | case 'object': |
| 588 | result = sqlParams?.length ? await client.querySQL(sql, sqlParams) : await client.querySQL(sql); |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | return result; |
| 593 | }; |
no test coverage detected