@drizzle-adapter/d1 1.0.3 → 1.0.5
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/dist/Adapter/D1DrizzleMigrator.d.ts +9 -4
- package/dist/Adapter/D1DrizzleMigrator.js +15 -3
- package/dist/Adapter/index.d.ts +1 -0
- package/dist/Adapter/index.js +1 -0
- package/dist/Factory/D1DrizzleMigratorFactory.d.ts +11 -0
- package/dist/Factory/D1DrizzleMigratorFactory.js +12 -0
- package/dist/Factory/index.d.ts +1 -0
- package/dist/Factory/index.js +1 -0
- package/dist/index.js +2 -2
- package/package.json +3 -2
- package/src/Adapter/D1DrizzleMigrator.ts +14 -5
- package/src/Adapter/index.ts +1 -0
- package/src/Factory/D1DrizzleMigratorFactory.ts +16 -0
- package/src/Factory/index.ts +1 -0
- package/src/index.ts +2 -2
- package/tests/Adapter/D1DrizzleMigrator.test.ts +18 -10
- package/tests/Factory/D1DrizzleMigratorFactory.test.ts +36 -0
|
Binary file
|
|
Binary file
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import type { DrizzleMigratorInterface } from '@drizzle-adapter/core';
|
|
2
|
-
import type { MigrationConfig } from 'drizzle-orm/migrator';
|
|
1
|
+
import type { DrizzleMigratorInterface, TypeDrizzleMigratorConfig } from '@drizzle-adapter/core';
|
|
3
2
|
import type { D1DrizzleClientInterface } from '../Interface/index.js';
|
|
4
3
|
export declare class D1DrizzleMigrator implements DrizzleMigratorInterface {
|
|
4
|
+
private readonly config;
|
|
5
|
+
/**
|
|
6
|
+
* Constructor.
|
|
7
|
+
*
|
|
8
|
+
* @param {TypeDrizzleMigratorConfig} config The migrator configuration.
|
|
9
|
+
*/
|
|
10
|
+
constructor(config: TypeDrizzleMigratorConfig);
|
|
5
11
|
/**
|
|
6
12
|
* Migrate the database.
|
|
7
13
|
*
|
|
8
14
|
* @param {DrizzleClientInterface} client The Drizzle client.
|
|
9
|
-
* @param {MigrationConfig} config The migration configuration.
|
|
10
15
|
*/
|
|
11
|
-
migrate(client: D1DrizzleClientInterface
|
|
16
|
+
migrate(client: D1DrizzleClientInterface): Promise<void>;
|
|
12
17
|
}
|
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
import { DrizzleD1Database } from 'drizzle-orm/d1/driver';
|
|
2
2
|
import { migrate } from 'drizzle-orm/d1/migrator';
|
|
3
3
|
export class D1DrizzleMigrator {
|
|
4
|
+
config;
|
|
5
|
+
/**
|
|
6
|
+
* Constructor.
|
|
7
|
+
*
|
|
8
|
+
* @param {TypeDrizzleMigratorConfig} config The migrator configuration.
|
|
9
|
+
*/
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
}
|
|
4
13
|
/**
|
|
5
14
|
* Migrate the database.
|
|
6
15
|
*
|
|
7
16
|
* @param {DrizzleClientInterface} client The Drizzle client.
|
|
8
|
-
* @param {MigrationConfig} config The migration configuration.
|
|
9
17
|
*/
|
|
10
|
-
async migrate(client
|
|
11
|
-
await migrate(client,
|
|
18
|
+
async migrate(client) {
|
|
19
|
+
await migrate(client, {
|
|
20
|
+
migrationsFolder: this.config.DATABASE_MIGRATIONS_FOLDER,
|
|
21
|
+
migrationsTable: this.config.DATABASE_MIGRATIONS_TABLE,
|
|
22
|
+
migrationsSchema: this.config.DATABASE_MIGRATIONS_SCHEMA
|
|
23
|
+
});
|
|
12
24
|
}
|
|
13
25
|
}
|
package/dist/Adapter/index.d.ts
CHANGED
package/dist/Adapter/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DrizzleMigratorFactoryInterface, TypeDrizzleMigratorConfig } from '@drizzle-adapter/core';
|
|
2
|
+
import { D1DrizzleMigrator } from '../Adapter/index.js';
|
|
3
|
+
export declare class D1DrizzleMigratorFactory implements DrizzleMigratorFactoryInterface {
|
|
4
|
+
/**
|
|
5
|
+
* Returns a new migrator.
|
|
6
|
+
*
|
|
7
|
+
* @param {TypeDrizzleMigratorConfig} config
|
|
8
|
+
* @returns {D1DrizzleMigrator}
|
|
9
|
+
*/
|
|
10
|
+
create(config: TypeDrizzleMigratorConfig): D1DrizzleMigrator;
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { D1DrizzleMigrator } from '../Adapter/index.js';
|
|
2
|
+
export class D1DrizzleMigratorFactory {
|
|
3
|
+
/**
|
|
4
|
+
* Returns a new migrator.
|
|
5
|
+
*
|
|
6
|
+
* @param {TypeDrizzleMigratorConfig} config
|
|
7
|
+
* @returns {D1DrizzleMigrator}
|
|
8
|
+
*/
|
|
9
|
+
create(config) {
|
|
10
|
+
return new D1DrizzleMigrator(config);
|
|
11
|
+
}
|
|
12
|
+
}
|
package/dist/Factory/index.d.ts
CHANGED
package/dist/Factory/index.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ export * from './Factory/index.js';
|
|
|
3
3
|
export * from './Interface/index.js';
|
|
4
4
|
export * from './Type/index.js';
|
|
5
5
|
// default export so that core factory can use it directly
|
|
6
|
-
import { D1DrizzleAdapterFactory } from './Factory/index.js';
|
|
6
|
+
import { D1DrizzleAdapterFactory, D1DrizzleMigratorFactory } from './Factory/index.js';
|
|
7
7
|
export default D1DrizzleAdapterFactory;
|
|
8
8
|
import { DrizzleAdapterFactoryRegistry } from '@drizzle-adapter/core';
|
|
9
|
-
DrizzleAdapterFactoryRegistry.register('d1', D1DrizzleAdapterFactory);
|
|
9
|
+
DrizzleAdapterFactoryRegistry.register('d1', D1DrizzleAdapterFactory, D1DrizzleMigratorFactory);
|
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.5",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"devDependencies": {
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"wrangler": "^3.91.0"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@drizzle-adapter/
|
|
21
|
+
"@drizzle-adapter/core": ">=1.0.5",
|
|
22
|
+
"@drizzle-adapter/sqlite-core": ">=1.0.5",
|
|
22
23
|
"@libsql/client": "^0.5.6",
|
|
23
24
|
"drizzle-orm": "^0.36.4"
|
|
24
25
|
},
|
|
@@ -1,19 +1,28 @@
|
|
|
1
|
-
import type { DrizzleMigratorInterface } from '@drizzle-adapter/core';
|
|
2
|
-
import type { MigrationConfig } from 'drizzle-orm/migrator';
|
|
1
|
+
import type { DrizzleMigratorInterface, TypeDrizzleMigratorConfig } from '@drizzle-adapter/core';
|
|
3
2
|
import { DrizzleD1Database } from 'drizzle-orm/d1/driver';
|
|
4
3
|
import { migrate } from 'drizzle-orm/d1/migrator';
|
|
5
4
|
import type { D1DrizzleClientInterface } from '../Interface';
|
|
6
5
|
|
|
7
6
|
export class D1DrizzleMigrator implements DrizzleMigratorInterface
|
|
8
7
|
{
|
|
8
|
+
/**
|
|
9
|
+
* Constructor.
|
|
10
|
+
*
|
|
11
|
+
* @param {TypeDrizzleMigratorConfig} config The migrator configuration.
|
|
12
|
+
*/
|
|
13
|
+
public constructor(private readonly config: TypeDrizzleMigratorConfig) {}
|
|
14
|
+
|
|
9
15
|
/**
|
|
10
16
|
* Migrate the database.
|
|
11
17
|
*
|
|
12
18
|
* @param {DrizzleClientInterface} client The Drizzle client.
|
|
13
|
-
* @param {MigrationConfig} config The migration configuration.
|
|
14
19
|
*/
|
|
15
|
-
public async migrate(client: D1DrizzleClientInterface
|
|
20
|
+
public async migrate(client: D1DrizzleClientInterface): Promise<void>
|
|
16
21
|
{
|
|
17
|
-
await migrate(client as unknown as DrizzleD1Database<Record<string, unknown>>,
|
|
22
|
+
await migrate(client as unknown as DrizzleD1Database<Record<string, unknown>>, {
|
|
23
|
+
migrationsFolder: this.config.DATABASE_MIGRATIONS_FOLDER,
|
|
24
|
+
migrationsTable: this.config.DATABASE_MIGRATIONS_TABLE,
|
|
25
|
+
migrationsSchema: this.config.DATABASE_MIGRATIONS_SCHEMA
|
|
26
|
+
});
|
|
18
27
|
}
|
|
19
28
|
}
|
package/src/Adapter/index.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DrizzleMigratorFactoryInterface, TypeDrizzleMigratorConfig } from '@drizzle-adapter/core';
|
|
2
|
+
import { D1DrizzleMigrator } from '../Adapter';
|
|
3
|
+
|
|
4
|
+
export class D1DrizzleMigratorFactory implements DrizzleMigratorFactoryInterface
|
|
5
|
+
{
|
|
6
|
+
/**
|
|
7
|
+
* Returns a new migrator.
|
|
8
|
+
*
|
|
9
|
+
* @param {TypeDrizzleMigratorConfig} config
|
|
10
|
+
* @returns {D1DrizzleMigrator}
|
|
11
|
+
*/
|
|
12
|
+
public create(config: TypeDrizzleMigratorConfig): D1DrizzleMigrator
|
|
13
|
+
{
|
|
14
|
+
return new D1DrizzleMigrator(config);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/Factory/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,8 +4,8 @@ export * from './Interface';
|
|
|
4
4
|
export * from './Type';
|
|
5
5
|
|
|
6
6
|
// default export so that core factory can use it directly
|
|
7
|
-
import { D1DrizzleAdapterFactory } from './Factory';
|
|
7
|
+
import { D1DrizzleAdapterFactory, D1DrizzleMigratorFactory } from './Factory';
|
|
8
8
|
export default D1DrizzleAdapterFactory;
|
|
9
9
|
|
|
10
10
|
import { DrizzleAdapterFactoryRegistry } from '@drizzle-adapter/core';
|
|
11
|
-
DrizzleAdapterFactoryRegistry.register('d1', D1DrizzleAdapterFactory);
|
|
11
|
+
DrizzleAdapterFactoryRegistry.register('d1', D1DrizzleAdapterFactory, D1DrizzleMigratorFactory);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { D1DrizzleMigrator } from '../../src/Adapter/D1DrizzleMigrator';
|
|
3
3
|
import * as migrator from 'drizzle-orm/d1/migrator';
|
|
4
|
-
import type {
|
|
4
|
+
import type { TypeDrizzleMigratorConfig } from '@drizzle-adapter/core';
|
|
5
5
|
import type { D1DrizzleClientInterface } from '../../src/Interface';
|
|
6
6
|
|
|
7
7
|
// Mock the migrate function from drizzle-orm/d1/migrator
|
|
@@ -19,39 +19,47 @@ describe('D1DrizzleMigrator', () => {
|
|
|
19
19
|
mockedClient: true
|
|
20
20
|
} as unknown as D1DrizzleClientInterface;
|
|
21
21
|
|
|
22
|
-
const mockConfig:
|
|
23
|
-
|
|
22
|
+
const mockConfig: TypeDrizzleMigratorConfig = {
|
|
23
|
+
DATABASE_DRIVER: 'd1',
|
|
24
|
+
DATABASE_URL: 'file:./test.db',
|
|
25
|
+
DATABASE_MIGRATIONS_FOLDER: './drizzle',
|
|
26
|
+
DATABASE_MIGRATIONS_TABLE: 'drizzle_migrations',
|
|
27
|
+
DATABASE_MIGRATIONS_SCHEMA: 'public'
|
|
24
28
|
};
|
|
25
29
|
|
|
26
30
|
beforeEach(() => {
|
|
27
31
|
vi.clearAllMocks();
|
|
28
32
|
});
|
|
29
33
|
|
|
30
|
-
it('should create an instance with
|
|
31
|
-
const migratorInstance = new D1DrizzleMigrator();
|
|
34
|
+
it('should create an instance with config', () => {
|
|
35
|
+
const migratorInstance = new D1DrizzleMigrator(mockConfig);
|
|
32
36
|
expect(migratorInstance).toBeInstanceOf(D1DrizzleMigrator);
|
|
33
37
|
});
|
|
34
38
|
|
|
35
39
|
it('should call migrate with correct parameters', async () => {
|
|
36
|
-
const migratorInstance = new D1DrizzleMigrator();
|
|
37
|
-
await migratorInstance.migrate(mockClient
|
|
40
|
+
const migratorInstance = new D1DrizzleMigrator(mockConfig);
|
|
41
|
+
await migratorInstance.migrate(mockClient);
|
|
38
42
|
|
|
39
43
|
expect(migrator.migrate).toHaveBeenCalledWith(
|
|
40
44
|
expect.objectContaining({ mockedClient: true }),
|
|
41
|
-
|
|
45
|
+
{
|
|
46
|
+
migrationsFolder: mockConfig.DATABASE_MIGRATIONS_FOLDER,
|
|
47
|
+
migrationsTable: mockConfig.DATABASE_MIGRATIONS_TABLE,
|
|
48
|
+
migrationsSchema: mockConfig.DATABASE_MIGRATIONS_SCHEMA,
|
|
49
|
+
}
|
|
42
50
|
);
|
|
43
51
|
expect(migrator.migrate).toHaveBeenCalledTimes(1);
|
|
44
52
|
});
|
|
45
53
|
|
|
46
54
|
it('should handle migration errors', async () => {
|
|
47
|
-
const migratorInstance = new D1DrizzleMigrator();
|
|
55
|
+
const migratorInstance = new D1DrizzleMigrator(mockConfig);
|
|
48
56
|
|
|
49
57
|
// Mock migrate to throw an error
|
|
50
58
|
vi.mocked(migrator.migrate).mockImplementationOnce(async () => {
|
|
51
59
|
throw new Error('Migration failed');
|
|
52
60
|
});
|
|
53
61
|
|
|
54
|
-
await expect(migratorInstance.migrate(mockClient
|
|
62
|
+
await expect(migratorInstance.migrate(mockClient))
|
|
55
63
|
.rejects
|
|
56
64
|
.toThrow('Migration failed');
|
|
57
65
|
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { D1DrizzleMigratorFactory } from '../../src/Factory';
|
|
3
|
+
import { D1DrizzleMigrator } from '../../src/Adapter';
|
|
4
|
+
import type { TypeDrizzleMigratorConfig } from '@drizzle-adapter/core';
|
|
5
|
+
|
|
6
|
+
describe('D1DrizzleMigratorFactory', () => {
|
|
7
|
+
const mockMigratorConfig: TypeDrizzleMigratorConfig = {
|
|
8
|
+
DATABASE_DRIVER: 'd1',
|
|
9
|
+
DATABASE_URL: 'file:./test.db',
|
|
10
|
+
DATABASE_MIGRATIONS_FOLDER: './drizzle',
|
|
11
|
+
DATABASE_MIGRATIONS_TABLE: 'drizzle_migrations',
|
|
12
|
+
DATABASE_MIGRATIONS_SCHEMA: 'public'
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
it('should be instantiable', () => {
|
|
16
|
+
const factory = new D1DrizzleMigratorFactory();
|
|
17
|
+
expect(factory).toBeInstanceOf(D1DrizzleMigratorFactory);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should create a D1DrizzleMigrator instance', () => {
|
|
21
|
+
const factory = new D1DrizzleMigratorFactory();
|
|
22
|
+
const migrator = factory.create(mockMigratorConfig);
|
|
23
|
+
|
|
24
|
+
expect(migrator).toBeInstanceOf(D1DrizzleMigrator);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should pass the config to the migrator', () => {
|
|
28
|
+
const factory = new D1DrizzleMigratorFactory();
|
|
29
|
+
const migrator = factory.create(mockMigratorConfig);
|
|
30
|
+
|
|
31
|
+
// Verify the migrator was created with the correct config
|
|
32
|
+
expect(migrator).toBeInstanceOf(D1DrizzleMigrator);
|
|
33
|
+
// @ts-expect-error Accessing private property for testing
|
|
34
|
+
expect(migrator.config).toEqual(mockMigratorConfig);
|
|
35
|
+
});
|
|
36
|
+
});
|