@drizzle-adapter/d1 1.0.0
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/LICENSE +674 -0
- package/README.md +260 -0
- package/dist/Adapter/D1DrizzleAdapter.d.ts +32 -0
- package/dist/Adapter/D1DrizzleAdapter.js +41 -0
- package/dist/Adapter/D1DrizzleConnection.d.ts +29 -0
- package/dist/Adapter/D1DrizzleConnection.js +39 -0
- package/dist/Adapter/D1DrizzleDataTypes.d.ts +3 -0
- package/dist/Adapter/D1DrizzleDataTypes.js +3 -0
- package/dist/Adapter/index.d.ts +2 -0
- package/dist/Adapter/index.js +2 -0
- package/dist/Factory/D1DrizzleAdapterFactory.d.ts +12 -0
- package/dist/Factory/D1DrizzleAdapterFactory.js +12 -0
- package/dist/Factory/index.d.ts +1 -0
- package/dist/Factory/index.js +1 -0
- package/dist/Interface/D1DrizzleClientInterface.d.ts +13 -0
- package/dist/Interface/D1DrizzleClientInterface.js +1 -0
- package/dist/Interface/index.d.ts +1 -0
- package/dist/Interface/index.js +1 -0
- package/dist/Type/Types.d.ts +5 -0
- package/dist/Type/Types.js +1 -0
- package/dist/Type/index.d.ts +1 -0
- package/dist/Type/index.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/eslint.config.mjs +6 -0
- package/package.json +34 -0
- package/src/Adapter/D1DrizzleAdapter.ts +50 -0
- package/src/Adapter/D1DrizzleConnection.ts +53 -0
- package/src/Adapter/D1DrizzleDataTypes.ts +3 -0
- package/src/Adapter/index.ts +2 -0
- package/src/Factory/D1DrizzleAdapterFactory.ts +17 -0
- package/src/Factory/index.ts +1 -0
- package/src/Interface/D1DrizzleClientInterface.ts +15 -0
- package/src/Interface/index.ts +1 -0
- package/src/Type/Types.ts +7 -0
- package/src/Type/index.ts +1 -0
- package/src/index.ts +8 -0
- package/tests/Adapter/D1DrizzleAdapter.test.ts +36 -0
- package/tests/Adapter/D1DrizzleConnection.test.ts +40 -0
- package/tests/Factory/D1DrizzleAdapterFactory.test.ts +41 -0
- package/tests/Integration/D1DrizzleAdapter.integration.test.ts +101 -0
- package/tests/Integration/D1DrizzleMigrations.integration.test.ts +100 -0
- package/tests/test-runner.ts +51 -0
- package/tests/worker.ts +334 -0
- package/tsconfig.eslint.json +4 -0
- package/tsconfig.json +38 -0
- package/vitest.config.ts +15 -0
- package/wrangler.toml +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# @drizzle-adapter/d1
|
|
2
|
+
|
|
3
|
+
Cloudflare D1 adapter implementation for the Drizzle Adapter ecosystem.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `@drizzle-adapter/d1` package provides the Cloudflare D1 implementation for the Drizzle Adapter interface. While you don't interact with this package directly (you use `@drizzle-adapter/core` instead), it enables D1 support in the Drizzle Adapter ecosystem.
|
|
8
|
+
|
|
9
|
+
## Why D1?
|
|
10
|
+
|
|
11
|
+
D1 offers unique advantages for Cloudflare Workers:
|
|
12
|
+
|
|
13
|
+
- **Edge SQLite**: Distributed SQLite database built for the edge
|
|
14
|
+
- **Zero Configuration**: Automatic replication and distribution
|
|
15
|
+
- **Global Performance**: Data served from the closest edge location
|
|
16
|
+
- **Cloudflare Workers Integration**: Native integration with Workers
|
|
17
|
+
- **Automatic Scaling**: Built-in scalability and reliability
|
|
18
|
+
- **Cost Effective**: Included in Workers pricing with generous free tier
|
|
19
|
+
- **SQLite Compatibility**: Familiar SQLite interface and features
|
|
20
|
+
- **Automatic Backups**: Built-in backup and restore capabilities
|
|
21
|
+
|
|
22
|
+
### Perfect For:
|
|
23
|
+
|
|
24
|
+
1. **Edge Applications**: Data served from the closest location
|
|
25
|
+
2. **Cloudflare Workers**: Native database for Workers applications
|
|
26
|
+
3. **Global Applications**: Automatic global distribution
|
|
27
|
+
4. **Cost-Sensitive Projects**: Included in Workers pricing
|
|
28
|
+
5. **Simple Deployments**: Zero configuration required
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Install both the core package and the D1 adapter
|
|
34
|
+
pnpm install @drizzle-adapter/core @drizzle-adapter/d1
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Cloudflare Workers Setup
|
|
40
|
+
|
|
41
|
+
First, set up your D1 database:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# Create a new D1 database
|
|
45
|
+
wrangler d1 create my-database
|
|
46
|
+
|
|
47
|
+
# Add to wrangler.toml
|
|
48
|
+
[[d1_databases]]
|
|
49
|
+
binding = "DB"
|
|
50
|
+
database_name = "my-database"
|
|
51
|
+
database_id = "your-database-id"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Configuration
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { DrizzleAdapterFactory, TypeDrizzleDatabaseConfig } from '@drizzle-adapter/core';
|
|
58
|
+
import { D1Database } from '@cloudflare/workers-types';
|
|
59
|
+
|
|
60
|
+
interface Env {
|
|
61
|
+
DB: D1Database;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const config: TypeDrizzleDatabaseConfig = {
|
|
65
|
+
DATABASE_DRIVER: 'd1',
|
|
66
|
+
DATABASE: env.DB
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const factory = new DrizzleAdapterFactory();
|
|
70
|
+
const adapter = await factory.create(config);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Important Note About Connections
|
|
74
|
+
|
|
75
|
+
While D1 operates within the Workers environment and doesn't require traditional database connections, this adapter implements the standard connection interface for compatibility. This means your code remains portable across different database adapters:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
// These connection operations are no-ops for D1 but ensure
|
|
79
|
+
// your code remains portable if you switch to another database
|
|
80
|
+
const connection = adapter.getConnection();
|
|
81
|
+
await connection.connect(); // No-op for D1
|
|
82
|
+
const client = await connection.getClient();
|
|
83
|
+
|
|
84
|
+
// Your database operations
|
|
85
|
+
|
|
86
|
+
await connection.disconnect(); // No-op for D1
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Schema Definition
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const dataTypes = adapter.getDataTypes();
|
|
93
|
+
|
|
94
|
+
const users = dataTypes.dbTable('users', {
|
|
95
|
+
id: dataTypes.dbInteger('id').primaryKey().autoincrement(),
|
|
96
|
+
name: dataTypes.dbText('name').notNull(),
|
|
97
|
+
email: dataTypes.dbText('email').notNull().unique(),
|
|
98
|
+
metadata: dataTypes.dbText('metadata'), // JSON stored as text
|
|
99
|
+
createdAt: dataTypes.dbInteger('created_at')
|
|
100
|
+
.default(sql`(unixepoch())`)
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const posts = dataTypes.dbTable('posts', {
|
|
104
|
+
id: dataTypes.dbInteger('id').primaryKey().autoincrement(),
|
|
105
|
+
userId: dataTypes.dbInteger('user_id')
|
|
106
|
+
.references(() => users.id),
|
|
107
|
+
title: dataTypes.dbText('title').notNull(),
|
|
108
|
+
content: dataTypes.dbText('content').notNull(),
|
|
109
|
+
published: dataTypes.dbInteger('published').default(0),
|
|
110
|
+
createdAt: dataTypes.dbInteger('created_at')
|
|
111
|
+
.default(sql`(unixepoch())`)
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Basic CRUD Operations
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { eq, and, or, desc, sql } from 'drizzle-orm';
|
|
119
|
+
|
|
120
|
+
const client = await adapter.getConnection().getClient();
|
|
121
|
+
|
|
122
|
+
// INSERT
|
|
123
|
+
// Single insert with returning
|
|
124
|
+
const [newUser] = await client
|
|
125
|
+
.insert(users)
|
|
126
|
+
.values({
|
|
127
|
+
name: 'John Doe',
|
|
128
|
+
email: 'john@example.com',
|
|
129
|
+
metadata: JSON.stringify({ role: 'user' })
|
|
130
|
+
})
|
|
131
|
+
.returning();
|
|
132
|
+
|
|
133
|
+
// Bulk insert
|
|
134
|
+
await client
|
|
135
|
+
.insert(posts)
|
|
136
|
+
.values([
|
|
137
|
+
{
|
|
138
|
+
userId: newUser.id,
|
|
139
|
+
title: 'First Post',
|
|
140
|
+
content: 'Hello, world!'
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
userId: newUser.id,
|
|
144
|
+
title: 'Second Post',
|
|
145
|
+
content: 'Another post'
|
|
146
|
+
}
|
|
147
|
+
]);
|
|
148
|
+
|
|
149
|
+
// SELECT
|
|
150
|
+
// Select all
|
|
151
|
+
const allUsers = await client
|
|
152
|
+
.select()
|
|
153
|
+
.from(users);
|
|
154
|
+
|
|
155
|
+
// Select with conditions
|
|
156
|
+
const user = await client
|
|
157
|
+
.select()
|
|
158
|
+
.from(users)
|
|
159
|
+
.where(eq(users.email, 'john@example.com'));
|
|
160
|
+
|
|
161
|
+
// Select with join
|
|
162
|
+
const userPosts = await client
|
|
163
|
+
.select({
|
|
164
|
+
userName: users.name,
|
|
165
|
+
postTitle: posts.title,
|
|
166
|
+
content: posts.content,
|
|
167
|
+
metadata: users.metadata
|
|
168
|
+
})
|
|
169
|
+
.from(posts)
|
|
170
|
+
.leftJoin(users, eq(posts.userId, users.id))
|
|
171
|
+
.where(eq(posts.published, 1))
|
|
172
|
+
.orderBy(desc(posts.createdAt));
|
|
173
|
+
|
|
174
|
+
// UPDATE
|
|
175
|
+
await client
|
|
176
|
+
.update(users)
|
|
177
|
+
.set({
|
|
178
|
+
name: 'John Smith',
|
|
179
|
+
metadata: JSON.stringify({ role: 'admin' })
|
|
180
|
+
})
|
|
181
|
+
.where(eq(users.id, newUser.id));
|
|
182
|
+
|
|
183
|
+
// DELETE
|
|
184
|
+
await client
|
|
185
|
+
.delete(posts)
|
|
186
|
+
.where(
|
|
187
|
+
and(
|
|
188
|
+
eq(posts.userId, newUser.id),
|
|
189
|
+
eq(posts.published, 0)
|
|
190
|
+
)
|
|
191
|
+
);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Worker Implementation
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
interface Env {
|
|
198
|
+
DB: D1Database;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export default {
|
|
202
|
+
async fetch(request: Request, env: Env): Promise<Response> {
|
|
203
|
+
const factory = new DrizzleAdapterFactory();
|
|
204
|
+
const adapter = await factory.create({
|
|
205
|
+
DATABASE_DRIVER: 'd1',
|
|
206
|
+
DATABASE: env.DB
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const client = await adapter.getConnection().getClient();
|
|
210
|
+
const url = new URL(request.url);
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
switch (url.pathname) {
|
|
214
|
+
case '/users': {
|
|
215
|
+
const users = await client
|
|
216
|
+
.select()
|
|
217
|
+
.from(users);
|
|
218
|
+
return Response.json(users);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
case '/posts': {
|
|
222
|
+
const posts = await client
|
|
223
|
+
.select({
|
|
224
|
+
title: posts.title,
|
|
225
|
+
content: posts.content,
|
|
226
|
+
author: users.name
|
|
227
|
+
})
|
|
228
|
+
.from(posts)
|
|
229
|
+
.leftJoin(users, eq(posts.userId, users.id))
|
|
230
|
+
.where(eq(posts.published, 1))
|
|
231
|
+
.orderBy(desc(posts.createdAt));
|
|
232
|
+
return Response.json(posts);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
default:
|
|
236
|
+
return new Response('Not Found', { status: 404 });
|
|
237
|
+
}
|
|
238
|
+
} catch (error) {
|
|
239
|
+
return new Response('Error', { status: 500 });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Best Practices
|
|
246
|
+
|
|
247
|
+
1. **Edge Optimization**: Queries are automatically optimized for edge execution
|
|
248
|
+
2. **JSON Handling**: Store JSON data as TEXT and parse/stringify as needed
|
|
249
|
+
3. **Error Handling**: Implement proper error handling for edge environments
|
|
250
|
+
4. **Query Optimization**: Minimize the number of queries per request
|
|
251
|
+
5. **Portability**: Use the connection interface for database portability
|
|
252
|
+
|
|
253
|
+
## Contributing
|
|
254
|
+
|
|
255
|
+
We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
256
|
+
|
|
257
|
+
## Related Packages
|
|
258
|
+
|
|
259
|
+
- [@drizzle-adapter/core](../core) - Core interfaces and types
|
|
260
|
+
- [@drizzle-adapter/sqlite-core](../sqlite-core) - Shared SQLite functionality
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
+
import { D1DrizzleConnection } from './D1DrizzleConnection.js';
|
|
3
|
+
import { D1DrizzleDataTypes } from './D1DrizzleDataTypes.js';
|
|
4
|
+
import type { DrizzleAdapterInterface } from '@drizzle-adapter/core';
|
|
5
|
+
export declare class D1DrizzleAdapter implements DrizzleAdapterInterface {
|
|
6
|
+
private readonly connection;
|
|
7
|
+
private connectionInstance;
|
|
8
|
+
/**
|
|
9
|
+
* Constructor.
|
|
10
|
+
*
|
|
11
|
+
* @param {D1Database} connectionDriver
|
|
12
|
+
*/
|
|
13
|
+
constructor(connection: D1Database);
|
|
14
|
+
/**
|
|
15
|
+
* Returns the dialect.
|
|
16
|
+
*
|
|
17
|
+
* @returns {string}
|
|
18
|
+
*/
|
|
19
|
+
getDialect(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the data types.
|
|
22
|
+
*
|
|
23
|
+
* @returns {D1DrizzleDataTypes}
|
|
24
|
+
*/
|
|
25
|
+
getDataTypes(): D1DrizzleDataTypes;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the connection.
|
|
28
|
+
*
|
|
29
|
+
* @returns {D1DrizzleConnection}
|
|
30
|
+
*/
|
|
31
|
+
getConnection(): D1DrizzleConnection;
|
|
32
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { D1DrizzleConnection } from './D1DrizzleConnection.js';
|
|
2
|
+
import { D1DrizzleDataTypes } from './D1DrizzleDataTypes.js';
|
|
3
|
+
export class D1DrizzleAdapter {
|
|
4
|
+
connection;
|
|
5
|
+
connectionInstance = null;
|
|
6
|
+
/**
|
|
7
|
+
* Constructor.
|
|
8
|
+
*
|
|
9
|
+
* @param {D1Database} connectionDriver
|
|
10
|
+
*/
|
|
11
|
+
constructor(connection) {
|
|
12
|
+
this.connection = connection;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns the dialect.
|
|
16
|
+
*
|
|
17
|
+
* @returns {string}
|
|
18
|
+
*/
|
|
19
|
+
getDialect() {
|
|
20
|
+
return 'sqlite';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the data types.
|
|
24
|
+
*
|
|
25
|
+
* @returns {D1DrizzleDataTypes}
|
|
26
|
+
*/
|
|
27
|
+
getDataTypes() {
|
|
28
|
+
return new D1DrizzleDataTypes();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns the connection.
|
|
32
|
+
*
|
|
33
|
+
* @returns {D1DrizzleConnection}
|
|
34
|
+
*/
|
|
35
|
+
getConnection() {
|
|
36
|
+
if (!this.connectionInstance) {
|
|
37
|
+
this.connectionInstance = new D1DrizzleConnection(this.connection);
|
|
38
|
+
}
|
|
39
|
+
return this.connectionInstance;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
+
import type { DrizzleClientInterface, DrizzleConnectionInterface } from '@drizzle-adapter/core';
|
|
3
|
+
export declare class D1DrizzleConnection implements DrizzleConnectionInterface {
|
|
4
|
+
private client;
|
|
5
|
+
/**
|
|
6
|
+
* Constructor.
|
|
7
|
+
*
|
|
8
|
+
* @param {D1Database} driver
|
|
9
|
+
*/
|
|
10
|
+
constructor(driver: D1Database);
|
|
11
|
+
/**
|
|
12
|
+
* Establishes a connection.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Promise<void>}
|
|
15
|
+
*/
|
|
16
|
+
connect(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Disconnects from the database.
|
|
19
|
+
*
|
|
20
|
+
* @returns {Promise<void>}
|
|
21
|
+
*/
|
|
22
|
+
disconnect(): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the client.
|
|
25
|
+
*
|
|
26
|
+
* @returns {Promise<DrizzleClientInterface>}
|
|
27
|
+
*/
|
|
28
|
+
getClient(): Promise<DrizzleClientInterface>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { drizzle } from 'drizzle-orm/d1';
|
|
2
|
+
export class D1DrizzleConnection {
|
|
3
|
+
client;
|
|
4
|
+
/**
|
|
5
|
+
* Constructor.
|
|
6
|
+
*
|
|
7
|
+
* @param {D1Database} driver
|
|
8
|
+
*/
|
|
9
|
+
constructor(driver) {
|
|
10
|
+
// We want to add the execute method as an alias for run
|
|
11
|
+
const database = drizzle(driver);
|
|
12
|
+
database.execute = database.run;
|
|
13
|
+
this.client = database;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Establishes a connection.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<void>}
|
|
19
|
+
*/
|
|
20
|
+
async connect() {
|
|
21
|
+
// D1 is serverless, no need to explicitly connect
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Disconnects from the database.
|
|
25
|
+
*
|
|
26
|
+
* @returns {Promise<void>}
|
|
27
|
+
*/
|
|
28
|
+
async disconnect() {
|
|
29
|
+
// D1 is serverless, no need to explicitly disconnect
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns the client.
|
|
33
|
+
*
|
|
34
|
+
* @returns {Promise<DrizzleClientInterface>}
|
|
35
|
+
*/
|
|
36
|
+
async getClient() {
|
|
37
|
+
return this.client;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TypeD1DatabaseConfig } from '../Type/Types.js';
|
|
2
|
+
import { D1DrizzleAdapter } from '../Adapter/D1DrizzleAdapter.js';
|
|
3
|
+
import type { DrizzleAdapterFactoryInterface } from '@drizzle-adapter/core';
|
|
4
|
+
export declare class D1DrizzleAdapterFactory implements DrizzleAdapterFactoryInterface {
|
|
5
|
+
/**
|
|
6
|
+
* Returns a adapter.
|
|
7
|
+
*
|
|
8
|
+
* @param {TypeD1DatabaseConfig} databaseConfig
|
|
9
|
+
* @returns {Promise<D1DrizzleAdapter>}
|
|
10
|
+
*/
|
|
11
|
+
create(databaseConfig: TypeD1DatabaseConfig): Promise<D1DrizzleAdapter>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { D1DrizzleAdapter } from '../Adapter/D1DrizzleAdapter.js';
|
|
2
|
+
export class D1DrizzleAdapterFactory {
|
|
3
|
+
/**
|
|
4
|
+
* Returns a adapter.
|
|
5
|
+
*
|
|
6
|
+
* @param {TypeD1DatabaseConfig} databaseConfig
|
|
7
|
+
* @returns {Promise<D1DrizzleAdapter>}
|
|
8
|
+
*/
|
|
9
|
+
async create(databaseConfig) {
|
|
10
|
+
return new D1DrizzleAdapter(databaseConfig.DATABASE);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './D1DrizzleAdapterFactory.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './D1DrizzleAdapterFactory.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AnyD1Database } from 'drizzle-orm/d1';
|
|
2
|
+
import type { DrizzleClientInterface } from '@drizzle-adapter/core';
|
|
3
|
+
export interface D1DrizzleClientInterface extends DrizzleClientInterface {
|
|
4
|
+
execute(...args: any[]): ReturnType<AnyD1Database['run']>;
|
|
5
|
+
$with(...args: any[]): ReturnType<AnyD1Database['$with']>;
|
|
6
|
+
$count(...args: any[]): ReturnType<AnyD1Database['$count']>;
|
|
7
|
+
with(...args: any[]): ReturnType<AnyD1Database['with']>;
|
|
8
|
+
select(...args: any[]): ReturnType<AnyD1Database['select']>;
|
|
9
|
+
selectDistinct(...args: any[]): ReturnType<AnyD1Database['selectDistinct']>;
|
|
10
|
+
update(...args: any[]): ReturnType<AnyD1Database['update']>;
|
|
11
|
+
insert(...args: any[]): ReturnType<AnyD1Database['insert']>;
|
|
12
|
+
transaction(...args: any[]): ReturnType<AnyD1Database['transaction']>;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './D1DrizzleClientInterface.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './D1DrizzleClientInterface.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Types.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Types.js';
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './Adapter/index.js';
|
|
2
|
+
export * from './Factory/index.js';
|
|
3
|
+
export * from './Interface/index.js';
|
|
4
|
+
export * from './Type/index.js';
|
|
5
|
+
// default export so that core factory can use it directly
|
|
6
|
+
import { D1DrizzleAdapterFactory } from './Factory/index.js';
|
|
7
|
+
export default D1DrizzleAdapterFactory;
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@drizzle-adapter/d1",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"@cloudflare/workers-types": "^4.20240208.0",
|
|
9
|
+
"@dmitryrechkin/eslint-standard": "^1.0.7",
|
|
10
|
+
"dotenv": "^16.4.5",
|
|
11
|
+
"eslint": "^8.0.0",
|
|
12
|
+
"eslint-plugin-unused-imports": "^3.0.0",
|
|
13
|
+
"fix-esm-import-path": "^1.0.1",
|
|
14
|
+
"shx": "^0.3.4",
|
|
15
|
+
"tsconfig-paths": "^4.2.0",
|
|
16
|
+
"typescript": "^5.5.3",
|
|
17
|
+
"vitest": "^0.24.5",
|
|
18
|
+
"wrangler": "^3.91.0"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@libsql/client": "^0.5.6",
|
|
22
|
+
"drizzle-orm": "^0.36.4",
|
|
23
|
+
"@drizzle-adapter/sqlite-core": "1.0.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"format": "eslint --fix .",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"check": "tsc --noEmit",
|
|
30
|
+
"build": "shx rm -rf dist && tsc && fix-esm-import-path dist",
|
|
31
|
+
"package:tgz": "pnpm pack",
|
|
32
|
+
"package:publish": "pnpm publish --access public"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
+
import { D1DrizzleConnection } from './D1DrizzleConnection';
|
|
3
|
+
import { D1DrizzleDataTypes } from './D1DrizzleDataTypes';
|
|
4
|
+
import type { DrizzleAdapterInterface } from '@drizzle-adapter/core';
|
|
5
|
+
|
|
6
|
+
export class D1DrizzleAdapter implements DrizzleAdapterInterface
|
|
7
|
+
{
|
|
8
|
+
private connectionInstance: D1DrizzleConnection | null = null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Constructor.
|
|
12
|
+
*
|
|
13
|
+
* @param {D1Database} connectionDriver
|
|
14
|
+
*/
|
|
15
|
+
public constructor(private readonly connection: D1Database) {}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns the dialect.
|
|
19
|
+
*
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
public getDialect(): string
|
|
23
|
+
{
|
|
24
|
+
return 'sqlite';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Returns the data types.
|
|
29
|
+
*
|
|
30
|
+
* @returns {D1DrizzleDataTypes}
|
|
31
|
+
*/
|
|
32
|
+
public getDataTypes(): D1DrizzleDataTypes
|
|
33
|
+
{
|
|
34
|
+
return new D1DrizzleDataTypes();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns the connection.
|
|
39
|
+
*
|
|
40
|
+
* @returns {D1DrizzleConnection}
|
|
41
|
+
*/
|
|
42
|
+
public getConnection(): D1DrizzleConnection
|
|
43
|
+
{
|
|
44
|
+
if (!this.connectionInstance)
|
|
45
|
+
{
|
|
46
|
+
this.connectionInstance = new D1DrizzleConnection(this.connection);
|
|
47
|
+
}
|
|
48
|
+
return this.connectionInstance;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
+
import { drizzle } from 'drizzle-orm/d1';
|
|
3
|
+
import type { DrizzleClientInterface, DrizzleConnectionInterface } from '@drizzle-adapter/core';
|
|
4
|
+
import type { D1DrizzleClientInterface } from 'src/Interface';
|
|
5
|
+
|
|
6
|
+
export class D1DrizzleConnection implements DrizzleConnectionInterface
|
|
7
|
+
{
|
|
8
|
+
private client: DrizzleClientInterface;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Constructor.
|
|
12
|
+
*
|
|
13
|
+
* @param {D1Database} driver
|
|
14
|
+
*/
|
|
15
|
+
public constructor(driver: D1Database)
|
|
16
|
+
{
|
|
17
|
+
// We want to add the execute method as an alias for run
|
|
18
|
+
const database: ReturnType<typeof drizzle> | D1DrizzleClientInterface = drizzle(driver);
|
|
19
|
+
(database as unknown as D1DrizzleClientInterface).execute = database.run;
|
|
20
|
+
|
|
21
|
+
this.client = database as unknown as D1DrizzleClientInterface;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Establishes a connection.
|
|
26
|
+
*
|
|
27
|
+
* @returns {Promise<void>}
|
|
28
|
+
*/
|
|
29
|
+
public async connect(): Promise<void>
|
|
30
|
+
{
|
|
31
|
+
// D1 is serverless, no need to explicitly connect
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Disconnects from the database.
|
|
36
|
+
*
|
|
37
|
+
* @returns {Promise<void>}
|
|
38
|
+
*/
|
|
39
|
+
public async disconnect(): Promise<void>
|
|
40
|
+
{
|
|
41
|
+
// D1 is serverless, no need to explicitly disconnect
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns the client.
|
|
46
|
+
*
|
|
47
|
+
* @returns {Promise<DrizzleClientInterface>}
|
|
48
|
+
*/
|
|
49
|
+
public async getClient(): Promise<DrizzleClientInterface>
|
|
50
|
+
{
|
|
51
|
+
return this.client;
|
|
52
|
+
}
|
|
53
|
+
}
|