@drizzle-adapter/d1 1.0.0 → 1.0.1
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-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 +3 -3
- 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/package.json +3 -3
- package/src/Adapter/D1DrizzleAdapter.ts +9 -10
- package/src/Adapter/D1DrizzleConnection.ts +8 -6
- package/src/Adapter/D1DrizzleMigrator.ts +19 -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/worker.ts +10 -10
- package/vitest.config.ts +3 -1
package/README.md
CHANGED
|
@@ -77,9 +77,9 @@ While D1 operates within the Workers environment and doesn't require traditional
|
|
|
77
77
|
```typescript
|
|
78
78
|
// These connection operations are no-ops for D1 but ensure
|
|
79
79
|
// your code remains portable if you switch to another database
|
|
80
|
-
const connection = adapter.getConnection();
|
|
81
|
-
|
|
82
|
-
const client =
|
|
80
|
+
const connection = await adapter.getConnection();
|
|
81
|
+
|
|
82
|
+
const client = connection.getClient();
|
|
83
83
|
|
|
84
84
|
// Your database operations
|
|
85
85
|
|
|
@@ -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
|
+
}
|
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.1",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"devDependencies": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"@drizzle-adapter/sqlite-core": "1.0.0"
|
|
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
|
+
}
|
|
@@ -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
|
+
});
|
package/tests/worker.ts
CHANGED
|
@@ -25,8 +25,8 @@ export default {
|
|
|
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();
|
|
@@ -69,8 +69,8 @@ export default {
|
|
|
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();
|
|
@@ -120,8 +120,8 @@ export default {
|
|
|
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();
|
|
@@ -178,8 +178,8 @@ export default {
|
|
|
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();
|
|
@@ -281,8 +281,8 @@ export default {
|
|
|
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
|
}
|