duron 0.1.1 → 0.2.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.
@@ -1,9 +1,14 @@
1
+ import { join } from 'node:path'
2
+
1
3
  import { drizzle } from 'drizzle-orm/pglite'
4
+ import { migrate } from 'drizzle-orm/pglite/migrator'
5
+
6
+ import { type AdapterOptions, PostgresBaseAdapter } from './base.js'
7
+ import type createSchema from './schema.js'
2
8
 
3
- import type { PostgresAdapterOptions } from './postgres.js'
4
- import { type DB, PostgresAdapter } from './postgres.js'
9
+ type Schema = ReturnType<typeof createSchema>
5
10
 
6
- type PGLiteDB = ReturnType<typeof drizzle>
11
+ export type DB = ReturnType<typeof drizzle<Schema>>
7
12
 
8
13
  /**
9
14
  * PGLite adapter implementation for Duron.
@@ -11,9 +16,27 @@ type PGLiteDB = ReturnType<typeof drizzle>
11
16
  *
12
17
  * @template Options - The adapter options type
13
18
  */
14
- export class PGLiteAdapter extends PostgresAdapter {
15
- override async _stop() {
16
- await (this.db as unknown as PGLiteDB).$client.close()
19
+ export class PGLiteAdapter extends PostgresBaseAdapter<DB, string | undefined> {
20
+ /**
21
+ * Start the adapter.
22
+ * Runs migrations if enabled and sets up database listeners.
23
+ *
24
+ * @returns Promise resolving to `true` if started successfully, `false` otherwise
25
+ */
26
+ protected override async _start() {
27
+ if (this.migrateOnStart) {
28
+ await migrate(this.db, {
29
+ migrationsFolder: join(import.meta.dirname, '..', '..', '..', 'migrations', 'postgres'),
30
+ migrationsTable: 'migrations',
31
+ migrationsSchema: 'duron',
32
+ })
33
+ }
34
+
35
+ await super._start()
36
+ }
37
+
38
+ protected override async _stop() {
39
+ await this.db?.$client.close()
17
40
  }
18
41
 
19
42
  /**
@@ -34,8 +57,8 @@ export class PGLiteAdapter extends PostgresAdapter {
34
57
  protected override _initDb() {
35
58
  let connection = ':memory:'
36
59
  // it means that the user is using a file path, so we need to use the file path
37
- if (typeof this.options.connection === 'string' && !this.options.connection.startsWith('postgres://')) {
38
- connection = this.options.connection
60
+ if (typeof this.connection === 'string' && !this.connection.startsWith('postgres://')) {
61
+ connection = this.connection
39
62
  }
40
63
  if (connection === ':memory:') {
41
64
  this.db = drizzle() as unknown as DB
@@ -52,9 +75,7 @@ export class PGLiteAdapter extends PostgresAdapter {
52
75
  * @returns Promise resolving to `void`
53
76
  */
54
77
  protected override async _notify(event: string, data: any): Promise<void> {
55
- await (this.db as unknown as PGLiteDB).$client.query(
56
- `NOTIFY "${this.options.schema}.${event}", '${JSON.stringify(data)}'`,
57
- )
78
+ await this.db?.$client.query(`NOTIFY "${this.schema}.${event}", '${JSON.stringify(data)}'`)
58
79
  }
59
80
 
60
81
  /**
@@ -68,12 +89,9 @@ export class PGLiteAdapter extends PostgresAdapter {
68
89
  event: string,
69
90
  callback: (payload: string) => void,
70
91
  ): Promise<{ unlisten: () => void }> {
71
- const unlisten = await (this.db as unknown as PGLiteDB).$client.listen(
72
- `"${this.options.schema}.${event}"`,
73
- (payload: string) => {
74
- callback(payload)
75
- },
76
- )
92
+ const unlisten = await this.db?.$client.listen(`"${this.schema}.${event}"`, (payload: string) => {
93
+ callback(payload)
94
+ })
77
95
 
78
96
  return {
79
97
  unlisten,
@@ -81,6 +99,6 @@ export class PGLiteAdapter extends PostgresAdapter {
81
99
  }
82
100
  }
83
101
 
84
- export const pgliteAdapter = (options: PostgresAdapterOptions) => {
102
+ export const pgliteAdapter = (options: AdapterOptions<string | undefined>) => {
85
103
  return new PGLiteAdapter(options)
86
104
  }