( credentials: SqliteCredentials, )
| 996 | }; |
| 997 | |
| 998 | export const connectToSQLite = async ( |
| 999 | credentials: SqliteCredentials, |
| 1000 | ): Promise< |
| 1001 | & SQLiteDB |
| 1002 | & { |
| 1003 | packageName: 'd1-http' | '@libsql/client' | 'better-sqlite3'; |
| 1004 | migrate: (config: MigrationConfig) => Promise<void>; |
| 1005 | proxy: Proxy; |
| 1006 | transactionProxy: TransactionProxy; |
| 1007 | } |
| 1008 | > => { |
| 1009 | if ('driver' in credentials) { |
| 1010 | const { driver } = credentials; |
| 1011 | if (driver === 'd1-http') { |
| 1012 | const { drizzle } = await import('drizzle-orm/sqlite-proxy'); |
| 1013 | const { migrate } = await import('drizzle-orm/sqlite-proxy/migrator'); |
| 1014 | |
| 1015 | type D1Response = |
| 1016 | | { |
| 1017 | success: true; |
| 1018 | result: { |
| 1019 | results: |
| 1020 | | any[] |
| 1021 | | { |
| 1022 | columns: string[]; |
| 1023 | rows: any[][]; |
| 1024 | }; |
| 1025 | }[]; |
| 1026 | } |
| 1027 | | { |
| 1028 | success: false; |
| 1029 | errors: { code: number; message: string }[]; |
| 1030 | }; |
| 1031 | |
| 1032 | const remoteCallback: Parameters<typeof drizzle>[0] = async ( |
| 1033 | sql, |
| 1034 | params, |
| 1035 | method, |
| 1036 | ) => { |
| 1037 | const res = await fetch( |
| 1038 | `https://api.cloudflare.com/client/v4/accounts/${credentials.accountId}/d1/database/${credentials.databaseId}/${ |
| 1039 | method === 'values' ? 'raw' : 'query' |
| 1040 | }`, |
| 1041 | { |
| 1042 | method: 'POST', |
| 1043 | body: JSON.stringify({ sql, params }), |
| 1044 | headers: { |
| 1045 | 'Content-Type': 'application/json', |
| 1046 | Authorization: `Bearer ${credentials.token}`, |
| 1047 | }, |
| 1048 | }, |
| 1049 | ); |
| 1050 | |
| 1051 | const data = (await res.json()) as D1Response; |
| 1052 | |
| 1053 | if (!data.success) { |
| 1054 | throw new Error( |
| 1055 | data.errors.map((it) => `${it.code}: ${it.message}`).join('\n'), |
no test coverage detected