|
| 1 | +/// <reference types="@cloudflare/workers-types" /> |
1 | 2 | import type { PGlite } from '@electric-sql/pglite'; |
2 | 3 | import type { SQLiteCloudRowset } from '@sqlitecloud/drivers'; |
3 | 4 | import type { AwsDataApiPgQueryResult, AwsDataApiSessionOptions } from 'drizzle-orm/aws-data-api/pg'; |
@@ -1702,7 +1703,7 @@ const prepareSqliteParams = (params: any[], driver?: string) => { |
1702 | 1703 | ? JSON.stringify(param.value) |
1703 | 1704 | : (param.value as string); |
1704 | 1705 |
|
1705 | | - if (driver === 'd1-http') { |
| 1706 | + if (driver === 'd1-http' || driver === 'd1') { |
1706 | 1707 | return value; |
1707 | 1708 | } |
1708 | 1709 |
|
@@ -1731,6 +1732,84 @@ const preparePGliteParams = (params: any[]) => { |
1731 | 1732 | }); |
1732 | 1733 | }; |
1733 | 1734 |
|
| 1735 | +export type D1Credentials = { |
| 1736 | + driver: 'd1'; |
| 1737 | + binding: D1Database; |
| 1738 | +}; |
| 1739 | + |
| 1740 | +export const connectToD1 = async ( |
| 1741 | + d1: D1Database, |
| 1742 | +): Promise< |
| 1743 | + & SQLiteDB |
| 1744 | + & { |
| 1745 | + packageName: 'd1'; |
| 1746 | + migrate: (config: MigrationConfig) => Promise<void | MigratorInitFailResponse>; |
| 1747 | + proxy: Proxy; |
| 1748 | + transactionProxy: TransactionProxy; |
| 1749 | + } |
| 1750 | +> => { |
| 1751 | + const db: SQLiteDB = { |
| 1752 | + query: async <T>(sql: string, params?: any[]) => { |
| 1753 | + const stmt = d1.prepare(sql); |
| 1754 | + const boundStmt = params && params.length > 0 ? stmt.bind(...params) : stmt; |
| 1755 | + const result = await boundStmt.all<T>(); |
| 1756 | + return (result.results ?? []) as T[]; |
| 1757 | + }, |
| 1758 | + run: async (query: string) => { |
| 1759 | + const stmt = d1.prepare(query); |
| 1760 | + await stmt.run(); |
| 1761 | + }, |
| 1762 | + batch: async (statements: string[]) => { |
| 1763 | + await d1.batch(statements.map((s) => d1.prepare(s))); |
| 1764 | + }, |
| 1765 | + }; |
| 1766 | + |
| 1767 | + const proxy: Proxy = async (params) => { |
| 1768 | + const preparedParams = prepareSqliteParams(params.params || [], 'd1'); |
| 1769 | + const stmt = d1.prepare(params.sql); |
| 1770 | + const boundStmt = preparedParams.length > 0 ? stmt.bind(...preparedParams) : stmt; |
| 1771 | + |
| 1772 | + try { |
| 1773 | + if (params.mode === 'array') { |
| 1774 | + return await boundStmt.raw(); |
| 1775 | + } |
| 1776 | + const result = await boundStmt.all(); |
| 1777 | + return result.results ?? []; |
| 1778 | + } catch (error: any) { |
| 1779 | + // D1 doesn't allow certain introspection queries (sqlite_master with pragma functions) |
| 1780 | + // Return empty array for SQLITE_AUTH errors on these system queries |
| 1781 | + if (error?.message?.includes('SQLITE_AUTH') || error?.message?.includes('not authorized')) { |
| 1782 | + return []; |
| 1783 | + } |
| 1784 | + throw error; |
| 1785 | + } |
| 1786 | + }; |
| 1787 | + |
| 1788 | + const transactionProxy: TransactionProxy = async (queries) => { |
| 1789 | + const results: any[] = []; |
| 1790 | + try { |
| 1791 | + // D1 doesn't support true transactions via binding, use batch instead |
| 1792 | + const statements = queries.map((q) => d1.prepare(q.sql)); |
| 1793 | + const batchResults = await d1.batch(statements); |
| 1794 | + for (const result of batchResults) { |
| 1795 | + results.push(result.results ?? []); |
| 1796 | + } |
| 1797 | + } catch (error) { |
| 1798 | + results.push(error as Error); |
| 1799 | + } |
| 1800 | + return results; |
| 1801 | + }; |
| 1802 | + |
| 1803 | + const { drizzle } = await import('drizzle-orm/d1'); |
| 1804 | + const { migrate } = await import('drizzle-orm/d1/migrator'); |
| 1805 | + const drzl = drizzle(d1); |
| 1806 | + const migrateFn = async (config: MigrationConfig) => { |
| 1807 | + return migrate(drzl, config); |
| 1808 | + }; |
| 1809 | + |
| 1810 | + return { ...db, packageName: 'd1', proxy, transactionProxy, migrate: migrateFn }; |
| 1811 | +}; |
| 1812 | + |
1734 | 1813 | export const connectToSQLite = async ( |
1735 | 1814 | credentials: SqliteCredentials, |
1736 | 1815 | ): Promise< |
|
0 commit comments