@drizzle-adapter/d1 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/6e148680931b1bfcb993e0463aced3b0a6d6cf50af24dea2dcc2c3b2e9ae9e3b.sqlite +0 -0
- package/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/6e148680931b1bfcb993e0463aced3b0a6d6cf50af24dea2dcc2c3b2e9ae9e3b.sqlite-shm +0 -0
- package/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/6e148680931b1bfcb993e0463aced3b0a6d6cf50af24dea2dcc2c3b2e9ae9e3b.sqlite-wal +0 -0
- package/.wrangler/tmp/bundle-5POwQi/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-5POwQi/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-5POwQi/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-Awe26S/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-Awe26S/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-Awe26S/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-CHi1ri/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-CHi1ri/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-CHi1ri/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-G5DpmU/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-G5DpmU/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-G5DpmU/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-L3XGWN/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-L3XGWN/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-L3XGWN/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-MycZGG/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-MycZGG/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-MycZGG/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-PJMwtq/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-PJMwtq/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-PJMwtq/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-SQq7Tu/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-SQq7Tu/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-SQq7Tu/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-UNiTDE/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-UNiTDE/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-UNiTDE/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-WqbqXV/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-WqbqXV/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-WqbqXV/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-YhqsYC/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-YhqsYC/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-YhqsYC/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-Z9Ytbr/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-Z9Ytbr/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-Z9Ytbr/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-agEOGG/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-agEOGG/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-agEOGG/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-bKzVij/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-bKzVij/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-bKzVij/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-ehd4TO/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-ehd4TO/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-ehd4TO/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-fU4auK/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-fU4auK/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-fU4auK/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-iLarYT/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-iLarYT/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-iLarYT/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-kxse6R/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-kxse6R/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-kxse6R/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-lMGAOP/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-lMGAOP/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-lMGAOP/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-mVsIPC/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-mVsIPC/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-mVsIPC/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-mlXKeI/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-mlXKeI/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-mlXKeI/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-oqR7ot/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-oqR7ot/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-oqR7ot/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-r6Dv1e/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-r6Dv1e/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-r6Dv1e/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-wLgVka/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-wLgVka/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-wLgVka/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/bundle-xVaLLy/checked-fetch.js +30 -0
- package/.wrangler/tmp/bundle-xVaLLy/middleware-insertion-facade.js +11 -0
- package/.wrangler/tmp/bundle-xVaLLy/middleware-loader.entry.ts +134 -0
- package/.wrangler/tmp/dev-0nPmAG/worker.js +6194 -0
- package/.wrangler/tmp/dev-0nPmAG/worker.js.map +8 -0
- package/.wrangler/tmp/dev-1aTYmG/worker.js +6611 -0
- package/.wrangler/tmp/dev-1aTYmG/worker.js.map +8 -0
- package/.wrangler/tmp/dev-1osU6W/worker.js +6610 -0
- package/.wrangler/tmp/dev-1osU6W/worker.js.map +8 -0
- package/.wrangler/tmp/dev-21Njjx/worker.js +6611 -0
- package/.wrangler/tmp/dev-21Njjx/worker.js.map +8 -0
- package/.wrangler/tmp/dev-26GSGQ/worker.js +6253 -0
- package/.wrangler/tmp/dev-26GSGQ/worker.js.map +8 -0
- package/.wrangler/tmp/dev-31oRGo/worker.js +6194 -0
- package/.wrangler/tmp/dev-31oRGo/worker.js.map +8 -0
- package/.wrangler/tmp/dev-3PUe1f/worker.js +6194 -0
- package/.wrangler/tmp/dev-3PUe1f/worker.js.map +8 -0
- package/.wrangler/tmp/dev-49noGf/worker.js +6268 -0
- package/.wrangler/tmp/dev-49noGf/worker.js.map +8 -0
- package/.wrangler/tmp/dev-4IHUrW/worker.js +6194 -0
- package/.wrangler/tmp/dev-4IHUrW/worker.js.map +8 -0
- package/.wrangler/tmp/dev-A9zJjT/worker.js +6194 -0
- package/.wrangler/tmp/dev-A9zJjT/worker.js.map +8 -0
- package/.wrangler/tmp/dev-IAjPvs/worker.js +6194 -0
- package/.wrangler/tmp/dev-IAjPvs/worker.js.map +8 -0
- package/.wrangler/tmp/dev-RAb75q/worker.js +6194 -0
- package/.wrangler/tmp/dev-RAb75q/worker.js.map +8 -0
- package/.wrangler/tmp/dev-SzA1fi/worker.js +6610 -0
- package/.wrangler/tmp/dev-SzA1fi/worker.js.map +8 -0
- package/.wrangler/tmp/dev-Z6uo4O/worker.js +6194 -0
- package/.wrangler/tmp/dev-Z6uo4O/worker.js.map +8 -0
- package/.wrangler/tmp/dev-b6RHui/worker.js +6253 -0
- package/.wrangler/tmp/dev-b6RHui/worker.js.map +8 -0
- package/.wrangler/tmp/dev-eD5RAO/worker.js +6284 -0
- package/.wrangler/tmp/dev-eD5RAO/worker.js.map +8 -0
- package/.wrangler/tmp/dev-h1SJ6S/worker.js +6253 -0
- package/.wrangler/tmp/dev-h1SJ6S/worker.js.map +8 -0
- package/.wrangler/tmp/dev-ivThQ6/worker.js +6268 -0
- package/.wrangler/tmp/dev-ivThQ6/worker.js.map +8 -0
- package/.wrangler/tmp/dev-iwU6dw/worker.js +6611 -0
- package/.wrangler/tmp/dev-iwU6dw/worker.js.map +8 -0
- package/.wrangler/tmp/dev-iwvsJt/worker.js +6284 -0
- package/.wrangler/tmp/dev-iwvsJt/worker.js.map +8 -0
- package/.wrangler/tmp/dev-kYVP9x/worker.js +6268 -0
- package/.wrangler/tmp/dev-kYVP9x/worker.js.map +8 -0
- package/.wrangler/tmp/dev-l39YAw/worker.js +6194 -0
- package/.wrangler/tmp/dev-l39YAw/worker.js.map +8 -0
- package/.wrangler/tmp/dev-vT7ODU/worker.js +6253 -0
- package/.wrangler/tmp/dev-vT7ODU/worker.js.map +8 -0
- package/.wrangler/tmp/dev-w6wgcm/worker.js +6611 -0
- package/.wrangler/tmp/dev-w6wgcm/worker.js.map +8 -0
- package/.wrangler/tmp/dev-yxONWu/worker.js +6194 -0
- package/.wrangler/tmp/dev-yxONWu/worker.js.map +8 -0
- package/README.md +17 -5
- package/dist/Adapter/D1DrizzleAdapter.d.ts +5 -6
- package/dist/Adapter/D1DrizzleAdapter.js +6 -10
- package/dist/Adapter/D1DrizzleConnection.d.ts +6 -5
- package/dist/Adapter/D1DrizzleConnection.js +4 -2
- package/dist/Adapter/D1DrizzleMigrator.d.ts +12 -0
- package/dist/Adapter/D1DrizzleMigrator.js +13 -0
- package/dist/Factory/D1DrizzleAdapterFactory.d.ts +2 -2
- package/dist/Factory/D1DrizzleAdapterFactory.js +2 -2
- package/dist/index.js +2 -0
- package/package.json +5 -5
- package/src/Adapter/D1DrizzleAdapter.ts +9 -10
- package/src/Adapter/D1DrizzleConnection.ts +8 -6
- package/src/Adapter/D1DrizzleMigrator.ts +19 -0
- package/src/Factory/D1DrizzleAdapterFactory.ts +2 -2
- package/src/index.ts +3 -0
- package/tests/Adapter/D1DrizzleAdapter.test.ts +1 -2
- package/tests/Adapter/D1DrizzleConnection.test.ts +3 -3
- package/tests/Adapter/D1DrizzleMigrator.test.ts +58 -0
- package/tests/Factory/D1DrizzleAdapterFactory.test.ts +1 -1
- package/tests/worker.ts +15 -15
- package/vitest.config.ts +3 -1
package/README.md
CHANGED
|
@@ -34,6 +34,18 @@ D1 offers unique advantages for Cloudflare Workers:
|
|
|
34
34
|
pnpm install @drizzle-adapter/core @drizzle-adapter/d1
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
## Important: Adapter Registration
|
|
38
|
+
|
|
39
|
+
For the adapter to work correctly with the DrizzleAdapterFactory, you must import it for its self-registration side effects:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
// Import for side effects - adapter will self-register
|
|
43
|
+
import '@drizzle-adapter/d1';
|
|
44
|
+
|
|
45
|
+
// Now you can use the factory
|
|
46
|
+
import { DrizzleAdapterFactory } from '@drizzle-adapter/core';
|
|
47
|
+
```
|
|
48
|
+
|
|
37
49
|
## Usage
|
|
38
50
|
|
|
39
51
|
### Cloudflare Workers Setup
|
|
@@ -67,7 +79,7 @@ const config: TypeDrizzleDatabaseConfig = {
|
|
|
67
79
|
};
|
|
68
80
|
|
|
69
81
|
const factory = new DrizzleAdapterFactory();
|
|
70
|
-
const adapter =
|
|
82
|
+
const adapter = factory.create(config);
|
|
71
83
|
```
|
|
72
84
|
|
|
73
85
|
### Important Note About Connections
|
|
@@ -77,9 +89,9 @@ While D1 operates within the Workers environment and doesn't require traditional
|
|
|
77
89
|
```typescript
|
|
78
90
|
// These connection operations are no-ops for D1 but ensure
|
|
79
91
|
// your code remains portable if you switch to another database
|
|
80
|
-
const connection = adapter.getConnection();
|
|
81
|
-
|
|
82
|
-
const client =
|
|
92
|
+
const connection = await adapter.getConnection();
|
|
93
|
+
|
|
94
|
+
const client = connection.getClient();
|
|
83
95
|
|
|
84
96
|
// Your database operations
|
|
85
97
|
|
|
@@ -201,7 +213,7 @@ interface Env {
|
|
|
201
213
|
export default {
|
|
202
214
|
async fetch(request: Request, env: Env): Promise<Response> {
|
|
203
215
|
const factory = new DrizzleAdapterFactory();
|
|
204
|
-
const adapter =
|
|
216
|
+
const adapter = factory.create({
|
|
205
217
|
DATABASE_DRIVER: 'd1',
|
|
206
218
|
DATABASE: env.DB
|
|
207
219
|
});
|
|
@@ -3,14 +3,13 @@ import { D1DrizzleConnection } from './D1DrizzleConnection.js';
|
|
|
3
3
|
import { D1DrizzleDataTypes } from './D1DrizzleDataTypes.js';
|
|
4
4
|
import type { DrizzleAdapterInterface } from '@drizzle-adapter/core';
|
|
5
5
|
export declare class D1DrizzleAdapter implements DrizzleAdapterInterface {
|
|
6
|
-
private
|
|
7
|
-
private connectionInstance;
|
|
6
|
+
private connection;
|
|
8
7
|
/**
|
|
9
8
|
* Constructor.
|
|
10
9
|
*
|
|
11
|
-
* @param {D1Database}
|
|
10
|
+
* @param {D1Database} driver
|
|
12
11
|
*/
|
|
13
|
-
constructor(
|
|
12
|
+
constructor(driver: D1Database);
|
|
14
13
|
/**
|
|
15
14
|
* Returns the dialect.
|
|
16
15
|
*
|
|
@@ -26,7 +25,7 @@ export declare class D1DrizzleAdapter implements DrizzleAdapterInterface {
|
|
|
26
25
|
/**
|
|
27
26
|
* Returns the connection.
|
|
28
27
|
*
|
|
29
|
-
* @returns {D1DrizzleConnection}
|
|
28
|
+
* @returns {Promise<D1DrizzleConnection>}
|
|
30
29
|
*/
|
|
31
|
-
getConnection(): D1DrizzleConnection
|
|
30
|
+
getConnection(): Promise<D1DrizzleConnection>;
|
|
32
31
|
}
|
|
@@ -2,14 +2,13 @@ import { D1DrizzleConnection } from './D1DrizzleConnection.js';
|
|
|
2
2
|
import { D1DrizzleDataTypes } from './D1DrizzleDataTypes.js';
|
|
3
3
|
export class D1DrizzleAdapter {
|
|
4
4
|
connection;
|
|
5
|
-
connectionInstance = null;
|
|
6
5
|
/**
|
|
7
6
|
* Constructor.
|
|
8
7
|
*
|
|
9
|
-
* @param {D1Database}
|
|
8
|
+
* @param {D1Database} driver
|
|
10
9
|
*/
|
|
11
|
-
constructor(
|
|
12
|
-
this.connection =
|
|
10
|
+
constructor(driver) {
|
|
11
|
+
this.connection = new D1DrizzleConnection(driver);
|
|
13
12
|
}
|
|
14
13
|
/**
|
|
15
14
|
* Returns the dialect.
|
|
@@ -30,12 +29,9 @@ export class D1DrizzleAdapter {
|
|
|
30
29
|
/**
|
|
31
30
|
* Returns the connection.
|
|
32
31
|
*
|
|
33
|
-
* @returns {D1DrizzleConnection}
|
|
32
|
+
* @returns {Promise<D1DrizzleConnection>}
|
|
34
33
|
*/
|
|
35
|
-
getConnection() {
|
|
36
|
-
|
|
37
|
-
this.connectionInstance = new D1DrizzleConnection(this.connection);
|
|
38
|
-
}
|
|
39
|
-
return this.connectionInstance;
|
|
34
|
+
async getConnection() {
|
|
35
|
+
return this.connection.connect();
|
|
40
36
|
}
|
|
41
37
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
-
import type {
|
|
2
|
+
import type { DrizzleConnectionInterface } from '@drizzle-adapter/core';
|
|
3
|
+
import type { D1DrizzleClientInterface } from 'src/Interface';
|
|
3
4
|
export declare class D1DrizzleConnection implements DrizzleConnectionInterface {
|
|
4
5
|
private client;
|
|
5
6
|
/**
|
|
@@ -13,17 +14,17 @@ export declare class D1DrizzleConnection implements DrizzleConnectionInterface {
|
|
|
13
14
|
*
|
|
14
15
|
* @returns {Promise<void>}
|
|
15
16
|
*/
|
|
16
|
-
connect(): Promise<
|
|
17
|
+
connect(): Promise<D1DrizzleConnection>;
|
|
17
18
|
/**
|
|
18
19
|
* Disconnects from the database.
|
|
19
20
|
*
|
|
20
21
|
* @returns {Promise<void>}
|
|
21
22
|
*/
|
|
22
|
-
disconnect(): Promise<
|
|
23
|
+
disconnect(): Promise<D1DrizzleConnection>;
|
|
23
24
|
/**
|
|
24
25
|
* Returns the client.
|
|
25
26
|
*
|
|
26
|
-
* @returns {
|
|
27
|
+
* @returns {D1DrizzleClientInterface}
|
|
27
28
|
*/
|
|
28
|
-
getClient():
|
|
29
|
+
getClient(): D1DrizzleClientInterface;
|
|
29
30
|
}
|
|
@@ -19,6 +19,7 @@ export class D1DrizzleConnection {
|
|
|
19
19
|
*/
|
|
20
20
|
async connect() {
|
|
21
21
|
// D1 is serverless, no need to explicitly connect
|
|
22
|
+
return this;
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
25
|
* Disconnects from the database.
|
|
@@ -27,13 +28,14 @@ export class D1DrizzleConnection {
|
|
|
27
28
|
*/
|
|
28
29
|
async disconnect() {
|
|
29
30
|
// D1 is serverless, no need to explicitly disconnect
|
|
31
|
+
return this;
|
|
30
32
|
}
|
|
31
33
|
/**
|
|
32
34
|
* Returns the client.
|
|
33
35
|
*
|
|
34
|
-
* @returns {
|
|
36
|
+
* @returns {D1DrizzleClientInterface}
|
|
35
37
|
*/
|
|
36
|
-
|
|
38
|
+
getClient() {
|
|
37
39
|
return this.client;
|
|
38
40
|
}
|
|
39
41
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DrizzleMigratorInterface } from '@drizzle-adapter/core';
|
|
2
|
+
import type { MigrationConfig } from 'drizzle-orm/migrator';
|
|
3
|
+
import type { D1DrizzleClientInterface } from '../Interface/index.js';
|
|
4
|
+
export declare class D1DrizzleMigrator implements DrizzleMigratorInterface {
|
|
5
|
+
/**
|
|
6
|
+
* Migrate the database.
|
|
7
|
+
*
|
|
8
|
+
* @param {DrizzleClientInterface} client The Drizzle client.
|
|
9
|
+
* @param {MigrationConfig} config The migration configuration.
|
|
10
|
+
*/
|
|
11
|
+
migrate(client: D1DrizzleClientInterface, config: MigrationConfig): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DrizzleD1Database } from 'drizzle-orm/d1/driver';
|
|
2
|
+
import { migrate } from 'drizzle-orm/d1/migrator';
|
|
3
|
+
export class D1DrizzleMigrator {
|
|
4
|
+
/**
|
|
5
|
+
* Migrate the database.
|
|
6
|
+
*
|
|
7
|
+
* @param {DrizzleClientInterface} client The Drizzle client.
|
|
8
|
+
* @param {MigrationConfig} config The migration configuration.
|
|
9
|
+
*/
|
|
10
|
+
async migrate(client, config) {
|
|
11
|
+
await migrate(client, config);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -6,7 +6,7 @@ export declare class D1DrizzleAdapterFactory implements DrizzleAdapterFactoryInt
|
|
|
6
6
|
* Returns a adapter.
|
|
7
7
|
*
|
|
8
8
|
* @param {TypeD1DatabaseConfig} databaseConfig
|
|
9
|
-
* @returns {
|
|
9
|
+
* @returns {D1DrizzleAdapter}
|
|
10
10
|
*/
|
|
11
|
-
create(databaseConfig: TypeD1DatabaseConfig):
|
|
11
|
+
create(databaseConfig: TypeD1DatabaseConfig): D1DrizzleAdapter;
|
|
12
12
|
}
|
|
@@ -4,9 +4,9 @@ export class D1DrizzleAdapterFactory {
|
|
|
4
4
|
* Returns a adapter.
|
|
5
5
|
*
|
|
6
6
|
* @param {TypeD1DatabaseConfig} databaseConfig
|
|
7
|
-
* @returns {
|
|
7
|
+
* @returns {D1DrizzleAdapter}
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
create(databaseConfig) {
|
|
10
10
|
return new D1DrizzleAdapter(databaseConfig.DATABASE);
|
|
11
11
|
}
|
|
12
12
|
}
|
package/dist/index.js
CHANGED
|
@@ -5,3 +5,5 @@ export * from './Type/index.js';
|
|
|
5
5
|
// default export so that core factory can use it directly
|
|
6
6
|
import { D1DrizzleAdapterFactory } from './Factory/index.js';
|
|
7
7
|
export default D1DrizzleAdapterFactory;
|
|
8
|
+
import { DrizzleAdapterFactoryRegistry } from '@drizzle-adapter/core';
|
|
9
|
+
DrizzleAdapterFactoryRegistry.register('d1', D1DrizzleAdapterFactory);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drizzle-adapter/d1",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"devDependencies": {
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"wrangler": "^3.91.0"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@drizzle-adapter/sqlite-core": ">=1.0.3",
|
|
21
22
|
"@libsql/client": "^0.5.6",
|
|
22
|
-
"drizzle-orm": "^0.36.4"
|
|
23
|
-
"@drizzle-adapter/sqlite-core": "1.0.0"
|
|
23
|
+
"drizzle-orm": "^0.36.4"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
|
-
"lint": "eslint
|
|
27
|
-
"format": "eslint --fix
|
|
26
|
+
"lint": "eslint src/",
|
|
27
|
+
"format": "eslint --fix src/",
|
|
28
28
|
"test": "vitest run",
|
|
29
29
|
"check": "tsc --noEmit",
|
|
30
30
|
"build": "shx rm -rf dist && tsc && fix-esm-import-path dist",
|
|
@@ -5,14 +5,17 @@ import type { DrizzleAdapterInterface } from '@drizzle-adapter/core';
|
|
|
5
5
|
|
|
6
6
|
export class D1DrizzleAdapter implements DrizzleAdapterInterface
|
|
7
7
|
{
|
|
8
|
-
private
|
|
8
|
+
private connection: D1DrizzleConnection;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Constructor.
|
|
12
12
|
*
|
|
13
|
-
* @param {D1Database}
|
|
13
|
+
* @param {D1Database} driver
|
|
14
14
|
*/
|
|
15
|
-
public constructor(
|
|
15
|
+
public constructor(driver: D1Database)
|
|
16
|
+
{
|
|
17
|
+
this.connection = new D1DrizzleConnection(driver);
|
|
18
|
+
}
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* Returns the dialect.
|
|
@@ -37,14 +40,10 @@ export class D1DrizzleAdapter implements DrizzleAdapterInterface
|
|
|
37
40
|
/**
|
|
38
41
|
* Returns the connection.
|
|
39
42
|
*
|
|
40
|
-
* @returns {D1DrizzleConnection}
|
|
43
|
+
* @returns {Promise<D1DrizzleConnection>}
|
|
41
44
|
*/
|
|
42
|
-
public getConnection(): D1DrizzleConnection
|
|
45
|
+
public async getConnection(): Promise<D1DrizzleConnection>
|
|
43
46
|
{
|
|
44
|
-
|
|
45
|
-
{
|
|
46
|
-
this.connectionInstance = new D1DrizzleConnection(this.connection);
|
|
47
|
-
}
|
|
48
|
-
return this.connectionInstance;
|
|
47
|
+
return this.connection.connect();
|
|
49
48
|
}
|
|
50
49
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
2
|
import { drizzle } from 'drizzle-orm/d1';
|
|
3
|
-
import type {
|
|
3
|
+
import type { DrizzleConnectionInterface } from '@drizzle-adapter/core';
|
|
4
4
|
import type { D1DrizzleClientInterface } from 'src/Interface';
|
|
5
5
|
|
|
6
6
|
export class D1DrizzleConnection implements DrizzleConnectionInterface
|
|
7
7
|
{
|
|
8
|
-
private client:
|
|
8
|
+
private client: D1DrizzleClientInterface;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Constructor.
|
|
@@ -26,9 +26,10 @@ export class D1DrizzleConnection implements DrizzleConnectionInterface
|
|
|
26
26
|
*
|
|
27
27
|
* @returns {Promise<void>}
|
|
28
28
|
*/
|
|
29
|
-
public async connect(): Promise<
|
|
29
|
+
public async connect(): Promise<D1DrizzleConnection>
|
|
30
30
|
{
|
|
31
31
|
// D1 is serverless, no need to explicitly connect
|
|
32
|
+
return this;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
/**
|
|
@@ -36,17 +37,18 @@ export class D1DrizzleConnection implements DrizzleConnectionInterface
|
|
|
36
37
|
*
|
|
37
38
|
* @returns {Promise<void>}
|
|
38
39
|
*/
|
|
39
|
-
public async disconnect(): Promise<
|
|
40
|
+
public async disconnect(): Promise<D1DrizzleConnection>
|
|
40
41
|
{
|
|
41
42
|
// D1 is serverless, no need to explicitly disconnect
|
|
43
|
+
return this;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
47
|
* Returns the client.
|
|
46
48
|
*
|
|
47
|
-
* @returns {
|
|
49
|
+
* @returns {D1DrizzleClientInterface}
|
|
48
50
|
*/
|
|
49
|
-
public
|
|
51
|
+
public getClient(): D1DrizzleClientInterface
|
|
50
52
|
{
|
|
51
53
|
return this.client;
|
|
52
54
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DrizzleMigratorInterface } from '@drizzle-adapter/core';
|
|
2
|
+
import type { MigrationConfig } from 'drizzle-orm/migrator';
|
|
3
|
+
import { DrizzleD1Database } from 'drizzle-orm/d1/driver';
|
|
4
|
+
import { migrate } from 'drizzle-orm/d1/migrator';
|
|
5
|
+
import type { D1DrizzleClientInterface } from '../Interface';
|
|
6
|
+
|
|
7
|
+
export class D1DrizzleMigrator implements DrizzleMigratorInterface
|
|
8
|
+
{
|
|
9
|
+
/**
|
|
10
|
+
* Migrate the database.
|
|
11
|
+
*
|
|
12
|
+
* @param {DrizzleClientInterface} client The Drizzle client.
|
|
13
|
+
* @param {MigrationConfig} config The migration configuration.
|
|
14
|
+
*/
|
|
15
|
+
public async migrate(client: D1DrizzleClientInterface, config: MigrationConfig): Promise<void>
|
|
16
|
+
{
|
|
17
|
+
await migrate(client as unknown as DrizzleD1Database<Record<string, unknown>>, config);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -8,9 +8,9 @@ export class D1DrizzleAdapterFactory implements DrizzleAdapterFactoryInterface
|
|
|
8
8
|
* Returns a adapter.
|
|
9
9
|
*
|
|
10
10
|
* @param {TypeD1DatabaseConfig} databaseConfig
|
|
11
|
-
* @returns {
|
|
11
|
+
* @returns {D1DrizzleAdapter}
|
|
12
12
|
*/
|
|
13
|
-
public
|
|
13
|
+
public create(databaseConfig: TypeD1DatabaseConfig): D1DrizzleAdapter
|
|
14
14
|
{
|
|
15
15
|
return new D1DrizzleAdapter(databaseConfig.DATABASE);
|
|
16
16
|
}
|
package/src/index.ts
CHANGED
|
@@ -6,3 +6,6 @@ export * from './Type';
|
|
|
6
6
|
// default export so that core factory can use it directly
|
|
7
7
|
import { D1DrizzleAdapterFactory } from './Factory';
|
|
8
8
|
export default D1DrizzleAdapterFactory;
|
|
9
|
+
|
|
10
|
+
import { DrizzleAdapterFactoryRegistry } from '@drizzle-adapter/core';
|
|
11
|
+
DrizzleAdapterFactoryRegistry.register('d1', D1DrizzleAdapterFactory);
|
|
@@ -30,7 +30,6 @@ describe('D1DrizzleAdapter', () => {
|
|
|
30
30
|
|
|
31
31
|
it('should return D1DrizzleConnection instance with the provided database', () => {
|
|
32
32
|
const adapter = new D1DrizzleAdapter(mockD1Database);
|
|
33
|
-
|
|
34
|
-
expect(connection).toBeInstanceOf(D1DrizzleConnection);
|
|
33
|
+
expect(adapter.getConnection()).resolves.toBeInstanceOf(D1DrizzleConnection);
|
|
35
34
|
});
|
|
36
35
|
});
|
|
@@ -24,17 +24,17 @@ describe('D1DrizzleConnection', () => {
|
|
|
24
24
|
|
|
25
25
|
it('should not perform any operation on connect (serverless)', async () => {
|
|
26
26
|
const connection = new D1DrizzleConnection(mockD1Database);
|
|
27
|
-
await expect(connection.connect()).resolves.
|
|
27
|
+
await expect(connection.connect()).resolves.toBeInstanceOf(D1DrizzleConnection);
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
it('should not perform any operation on disconnect (serverless)', async () => {
|
|
31
31
|
const connection = new D1DrizzleConnection(mockD1Database);
|
|
32
|
-
await expect(connection.disconnect()).resolves.
|
|
32
|
+
await expect(connection.disconnect()).resolves.toBeInstanceOf(D1DrizzleConnection);
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
it('should return the query client', async () => {
|
|
36
36
|
const connection = new D1DrizzleConnection(mockD1Database);
|
|
37
|
-
const client =
|
|
37
|
+
const client = connection.getClient();
|
|
38
38
|
expect(client).toEqual({ mockedClient: true });
|
|
39
39
|
});
|
|
40
40
|
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { D1DrizzleMigrator } from '../../src/Adapter/D1DrizzleMigrator';
|
|
3
|
+
import * as migrator from 'drizzle-orm/d1/migrator';
|
|
4
|
+
import type { MigrationConfig } from 'drizzle-orm/migrator';
|
|
5
|
+
import type { D1DrizzleClientInterface } from '../../src/Interface';
|
|
6
|
+
|
|
7
|
+
// Mock the migrate function from drizzle-orm/d1/migrator
|
|
8
|
+
vi.mock('drizzle-orm/d1/migrator', () => ({
|
|
9
|
+
migrate: vi.fn().mockImplementation(async (client, config) => {
|
|
10
|
+
if (!client || !config) {
|
|
11
|
+
throw new Error('Invalid arguments');
|
|
12
|
+
}
|
|
13
|
+
return Promise.resolve();
|
|
14
|
+
})
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
describe('D1DrizzleMigrator', () => {
|
|
18
|
+
const mockClient: D1DrizzleClientInterface = {
|
|
19
|
+
mockedClient: true
|
|
20
|
+
} as unknown as D1DrizzleClientInterface;
|
|
21
|
+
|
|
22
|
+
const mockConfig: MigrationConfig = {
|
|
23
|
+
migrationsFolder: './drizzle'
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
vi.clearAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should create an instance with a D1 client', () => {
|
|
31
|
+
const migratorInstance = new D1DrizzleMigrator();
|
|
32
|
+
expect(migratorInstance).toBeInstanceOf(D1DrizzleMigrator);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should call migrate with correct parameters', async () => {
|
|
36
|
+
const migratorInstance = new D1DrizzleMigrator();
|
|
37
|
+
await migratorInstance.migrate(mockClient, mockConfig);
|
|
38
|
+
|
|
39
|
+
expect(migrator.migrate).toHaveBeenCalledWith(
|
|
40
|
+
expect.objectContaining({ mockedClient: true }),
|
|
41
|
+
mockConfig
|
|
42
|
+
);
|
|
43
|
+
expect(migrator.migrate).toHaveBeenCalledTimes(1);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should handle migration errors', async () => {
|
|
47
|
+
const migratorInstance = new D1DrizzleMigrator();
|
|
48
|
+
|
|
49
|
+
// Mock migrate to throw an error
|
|
50
|
+
vi.mocked(migrator.migrate).mockImplementationOnce(async () => {
|
|
51
|
+
throw new Error('Migration failed');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await expect(migratorInstance.migrate(mockClient, mockConfig))
|
|
55
|
+
.rejects
|
|
56
|
+
.toThrow('Migration failed');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -34,7 +34,7 @@ describe('D1DrizzleAdapterFactory', () => {
|
|
|
34
34
|
|
|
35
35
|
it('should create a D1DrizzleAdapter instance', async () => {
|
|
36
36
|
const factory = new D1DrizzleAdapterFactory();
|
|
37
|
-
const adapter =
|
|
37
|
+
const adapter = factory.create(mockDatabaseConfig);
|
|
38
38
|
|
|
39
39
|
expect(adapter).toBeInstanceOf(D1DrizzleAdapter);
|
|
40
40
|
});
|
package/tests/worker.ts
CHANGED
|
@@ -21,12 +21,12 @@ export default {
|
|
|
21
21
|
case '/test/create-record': {
|
|
22
22
|
try {
|
|
23
23
|
const factory = new D1DrizzleAdapterFactory();
|
|
24
|
-
const adapter =
|
|
24
|
+
const adapter = factory.create({
|
|
25
25
|
DATABASE: env.DB
|
|
26
26
|
}) as D1DrizzleAdapter;
|
|
27
27
|
|
|
28
|
-
const connection = adapter.getConnection();
|
|
29
|
-
const client =
|
|
28
|
+
const connection = await adapter.getConnection();
|
|
29
|
+
const client = connection.getClient();
|
|
30
30
|
|
|
31
31
|
// Create table definition
|
|
32
32
|
const dataTypes = adapter.getDataTypes();
|
|
@@ -65,12 +65,12 @@ export default {
|
|
|
65
65
|
case '/test/update-record': {
|
|
66
66
|
try {
|
|
67
67
|
const factory = new D1DrizzleAdapterFactory();
|
|
68
|
-
const adapter =
|
|
68
|
+
const adapter = factory.create({
|
|
69
69
|
DATABASE: env.DB
|
|
70
70
|
}) as D1DrizzleAdapter;
|
|
71
71
|
|
|
72
|
-
const connection = adapter.getConnection();
|
|
73
|
-
const client =
|
|
72
|
+
const connection = await adapter.getConnection();
|
|
73
|
+
const client = connection.getClient();
|
|
74
74
|
|
|
75
75
|
// Create table definition
|
|
76
76
|
const dataTypes = adapter.getDataTypes();
|
|
@@ -116,12 +116,12 @@ export default {
|
|
|
116
116
|
case '/test/select-records': {
|
|
117
117
|
try {
|
|
118
118
|
const factory = new D1DrizzleAdapterFactory();
|
|
119
|
-
const adapter =
|
|
119
|
+
const adapter = factory.create({
|
|
120
120
|
DATABASE: env.DB
|
|
121
121
|
}) as D1DrizzleAdapter;
|
|
122
122
|
|
|
123
|
-
const connection = adapter.getConnection();
|
|
124
|
-
const client =
|
|
123
|
+
const connection = await adapter.getConnection();
|
|
124
|
+
const client = connection.getClient();
|
|
125
125
|
|
|
126
126
|
// Create table definition
|
|
127
127
|
const dataTypes = adapter.getDataTypes();
|
|
@@ -174,12 +174,12 @@ export default {
|
|
|
174
174
|
case '/test/migrations': {
|
|
175
175
|
try {
|
|
176
176
|
const factory = new D1DrizzleAdapterFactory();
|
|
177
|
-
const adapter =
|
|
177
|
+
const adapter = factory.create({
|
|
178
178
|
DATABASE: env.DB
|
|
179
179
|
}) as D1DrizzleAdapter;
|
|
180
180
|
|
|
181
|
-
const connection = adapter.getConnection();
|
|
182
|
-
const client =
|
|
181
|
+
const connection = await adapter.getConnection();
|
|
182
|
+
const client = connection.getClient();
|
|
183
183
|
|
|
184
184
|
// Create table definition
|
|
185
185
|
const dataTypes = adapter.getDataTypes();
|
|
@@ -277,12 +277,12 @@ export default {
|
|
|
277
277
|
case '/test/text-type': {
|
|
278
278
|
try {
|
|
279
279
|
const factory = new D1DrizzleAdapterFactory();
|
|
280
|
-
const adapter =
|
|
280
|
+
const adapter = factory.create({
|
|
281
281
|
DATABASE: env.DB
|
|
282
282
|
}) as D1DrizzleAdapter;
|
|
283
283
|
|
|
284
|
-
const connection = adapter.getConnection();
|
|
285
|
-
const client =
|
|
284
|
+
const connection = await adapter.getConnection();
|
|
285
|
+
const client = connection.getClient();
|
|
286
286
|
|
|
287
287
|
// Create table definition
|
|
288
288
|
const dataTypes = adapter.getDataTypes();
|
package/vitest.config.ts
CHANGED
|
@@ -3,12 +3,14 @@ import { defineConfig } from 'vitest/config';
|
|
|
3
3
|
export default defineConfig({
|
|
4
4
|
test: {
|
|
5
5
|
globals: true,
|
|
6
|
+
passWithNoTests: true,
|
|
6
7
|
environment: 'node',
|
|
7
8
|
include: ['tests/**/*.test.ts'],
|
|
8
9
|
// Run tests sequentially
|
|
9
10
|
threads: false,
|
|
10
11
|
// Increase timeout for integration tests
|
|
11
|
-
testTimeout:
|
|
12
|
+
testTimeout: 30000,
|
|
13
|
+
hookTimeout: 30000,
|
|
12
14
|
// Ensure test isolation
|
|
13
15
|
isolate: true
|
|
14
16
|
}
|