@apibara/plugin-drizzle 2.1.0-beta.2 → 2.1.0-beta.20

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/dist/index.d.mts CHANGED
@@ -1,17 +1,147 @@
1
1
  import * as _apibara_indexer_plugins from '@apibara/indexer/plugins';
2
- import { TablesRelationalConfig, ExtractTablesWithRelations } from 'drizzle-orm';
2
+ import { DrizzleConfig, TablesRelationalConfig, ExtractTablesWithRelations } from 'drizzle-orm';
3
3
  import { PgQueryResultHKT, PgTransaction, PgDatabase } from 'drizzle-orm/pg-core';
4
+ import { PGliteOptions, PGlite } from '@electric-sql/pglite';
5
+ import { MigrationConfig } from 'drizzle-orm/migrator';
6
+ import { NodePgDatabase as NodePgDatabase$1 } from 'drizzle-orm/node-postgres';
7
+ import { PgliteDatabase as PgliteDatabase$1 } from 'drizzle-orm/pglite';
8
+ import pg from 'pg';
9
+
10
+ /**
11
+ * Union type of all possible drizzle database options
12
+ */
13
+ type DrizzleOptions = PgliteDrizzleOptions | NodePgDrizzleOptions;
14
+ /**
15
+ * Configuration options for Node-Postgres database connection
16
+ */
17
+ type NodePgDrizzleOptions = {
18
+ /**
19
+ * Type of database to use -
20
+ * - "pglite" - PGLite database
21
+ * - "node-postgres" - Node-Postgres database
22
+ * @default "pglite"
23
+ */
24
+ type: "node-postgres";
25
+ /**
26
+ * Connection string to use for the database
27
+ * @default ""
28
+ */
29
+ connectionString?: string;
30
+ /**
31
+ * Pool configuration options for Node-Postgres
32
+ */
33
+ poolConfig?: pg.PoolConfig;
34
+ /**
35
+ * Additional drizzle configuration options
36
+ */
37
+ config?: Omit<DrizzleConfig, "schema">;
38
+ };
39
+ /**
40
+ * Configuration options for PGLite database connection
41
+ */
42
+ type PgliteDrizzleOptions = {
43
+ /**
44
+ * Type of database to use -
45
+ * - "pglite" - PGLite database
46
+ * - "node-postgres" - Node-Postgres database
47
+ */
48
+ type?: "pglite";
49
+ /**
50
+ * Connection string to use for the database
51
+ * @default process.env["POSTGRES_CONNECTION_STRING"] ?? "memory://pglite"
52
+ */
53
+ connectionString?: string;
54
+ /**
55
+ * Pool configuration is not supported for PGLite
56
+ */
57
+ poolConfig?: never;
58
+ /**
59
+ * Additional drizzle configuration options with PGLite specific connection options
60
+ */
61
+ config?: Omit<DrizzleConfig, "schema"> & {
62
+ connection?: (PGliteOptions & {
63
+ dataDir?: string;
64
+ }) | string;
65
+ };
66
+ };
67
+ /**
68
+ * Extended PGLite database type with client information
69
+ */
70
+ type PgliteDatabase<TSchema extends Record<string, unknown>> = PgliteDatabase$1<TSchema> & {
71
+ $client: PGlite;
72
+ };
73
+ /**
74
+ * Extended Node-Postgres database type with client information
75
+ */
76
+ type NodePgDatabase<TSchema extends Record<string, unknown>> = NodePgDatabase$1<TSchema> & {
77
+ $client: pg.Pool;
78
+ };
79
+ type Database<TOptions extends DrizzleOptions, TSchema extends Record<string, unknown>> = TOptions extends PgliteDrizzleOptions ? PgliteDatabase<TSchema> : NodePgDatabase<TSchema>;
80
+ /**
81
+ * Creates a new Drizzle database instance based on the provided options
82
+ *
83
+ * @important connectionString defaults to process.env["POSTGRES_CONNECTION_STRING"], if not set, it defaults to "memory://" (in-memory pglite)
84
+ *
85
+ * @param options - Configuration options for the database connection
86
+ * @returns A configured Drizzle database instance
87
+ * @throws {Error} If an invalid database type is specified
88
+ */
89
+ declare function drizzle<TSchema extends Record<string, unknown>, TOptions extends DrizzleOptions>(options?: TOptions & {
90
+ /**
91
+ * Schema to use for the database
92
+ * @default {}
93
+ */
94
+ schema?: TSchema;
95
+ }): Database<TOptions, TSchema>;
96
+ /**
97
+ * Options for database migration
98
+ */
99
+ type MigrateOptions = MigrationConfig;
100
+ /**
101
+ * Performs database migration based on the provided configuration
102
+ * @param db - The database instance to migrate
103
+ * @param options - Migration configuration options
104
+ *
105
+ * @important This function runs migrations on the database instance provided to the `drizzleStorage` plugin.
106
+ * It automatically detects the type of database and runs the appropriate migrate function
107
+ * (PGLite or Node-Postgres).
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * await migrate(db, { migrationsFolder: "./drizzle" });
112
+ * ```
113
+ */
114
+ declare function migrate<TSchema extends Record<string, unknown>>(db: PgliteDatabase<TSchema> | NodePgDatabase<TSchema>, options: MigrateOptions): Promise<void>;
4
115
 
5
116
  type DrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> = {
6
117
  db: PgTransaction<TQueryResult, TFullSchema, TSchema>;
7
118
  };
8
119
  declare function useDrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>(_db?: PgDatabase<TQueryResult, TFullSchema, TSchema>): DrizzleStorage<TQueryResult, TFullSchema, TSchema>;
9
120
  interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
121
+ /**
122
+ * The Drizzle database instance.
123
+ */
10
124
  db: PgDatabase<TQueryResult, TFullSchema, TSchema>;
125
+ /**
126
+ * Whether to persist the indexer's state. Defaults to true.
127
+ */
11
128
  persistState?: boolean;
129
+ /**
130
+ * The name of the indexer. Default value is 'default'.
131
+ */
12
132
  indexerName?: string;
133
+ /**
134
+ * The schema of the database.
135
+ */
13
136
  schema?: Record<string, unknown>;
137
+ /**
138
+ * The column to use as the id. Defaults to 'id'.
139
+ */
14
140
  idColumn?: string;
141
+ /**
142
+ * The options for the database migration. When provided, the database will automatically run migrations before the indexer runs.
143
+ */
144
+ migrate?: MigrateOptions;
15
145
  }
16
146
  /**
17
147
  * Creates a plugin that uses Drizzle as the storage layer.
@@ -22,7 +152,8 @@ interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSche
22
152
  * @param options.indexerName - The name of the indexer. Defaults value is 'default'.
23
153
  * @param options.schema - The schema of the database.
24
154
  * @param options.idColumn - The column to use as the id. Defaults to 'id'.
155
+ * @param options.migrate - The options for the database migration. when provided, the database will automatically run migrations before the indexer runs.
25
156
  */
26
- declare function drizzleStorage<TFilter, TBlock, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>({ db, persistState: enablePersistence, indexerName: identifier, schema, idColumn, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
157
+ declare function drizzleStorage<TFilter, TBlock, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>({ db, persistState: enablePersistence, indexerName: identifier, schema, idColumn, migrate: migrateOptions, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
27
158
 
28
- export { type DrizzleStorage, type DrizzleStorageOptions, drizzleStorage, useDrizzleStorage };
159
+ export { type Database, type DrizzleOptions, type DrizzleStorage, type DrizzleStorageOptions, type MigrateOptions, type NodePgDatabase, type NodePgDrizzleOptions, type PgliteDatabase, type PgliteDrizzleOptions, drizzle, drizzleStorage, migrate, useDrizzleStorage };
package/dist/index.d.ts CHANGED
@@ -1,17 +1,147 @@
1
1
  import * as _apibara_indexer_plugins from '@apibara/indexer/plugins';
2
- import { TablesRelationalConfig, ExtractTablesWithRelations } from 'drizzle-orm';
2
+ import { DrizzleConfig, TablesRelationalConfig, ExtractTablesWithRelations } from 'drizzle-orm';
3
3
  import { PgQueryResultHKT, PgTransaction, PgDatabase } from 'drizzle-orm/pg-core';
4
+ import { PGliteOptions, PGlite } from '@electric-sql/pglite';
5
+ import { MigrationConfig } from 'drizzle-orm/migrator';
6
+ import { NodePgDatabase as NodePgDatabase$1 } from 'drizzle-orm/node-postgres';
7
+ import { PgliteDatabase as PgliteDatabase$1 } from 'drizzle-orm/pglite';
8
+ import pg from 'pg';
9
+
10
+ /**
11
+ * Union type of all possible drizzle database options
12
+ */
13
+ type DrizzleOptions = PgliteDrizzleOptions | NodePgDrizzleOptions;
14
+ /**
15
+ * Configuration options for Node-Postgres database connection
16
+ */
17
+ type NodePgDrizzleOptions = {
18
+ /**
19
+ * Type of database to use -
20
+ * - "pglite" - PGLite database
21
+ * - "node-postgres" - Node-Postgres database
22
+ * @default "pglite"
23
+ */
24
+ type: "node-postgres";
25
+ /**
26
+ * Connection string to use for the database
27
+ * @default ""
28
+ */
29
+ connectionString?: string;
30
+ /**
31
+ * Pool configuration options for Node-Postgres
32
+ */
33
+ poolConfig?: pg.PoolConfig;
34
+ /**
35
+ * Additional drizzle configuration options
36
+ */
37
+ config?: Omit<DrizzleConfig, "schema">;
38
+ };
39
+ /**
40
+ * Configuration options for PGLite database connection
41
+ */
42
+ type PgliteDrizzleOptions = {
43
+ /**
44
+ * Type of database to use -
45
+ * - "pglite" - PGLite database
46
+ * - "node-postgres" - Node-Postgres database
47
+ */
48
+ type?: "pglite";
49
+ /**
50
+ * Connection string to use for the database
51
+ * @default process.env["POSTGRES_CONNECTION_STRING"] ?? "memory://pglite"
52
+ */
53
+ connectionString?: string;
54
+ /**
55
+ * Pool configuration is not supported for PGLite
56
+ */
57
+ poolConfig?: never;
58
+ /**
59
+ * Additional drizzle configuration options with PGLite specific connection options
60
+ */
61
+ config?: Omit<DrizzleConfig, "schema"> & {
62
+ connection?: (PGliteOptions & {
63
+ dataDir?: string;
64
+ }) | string;
65
+ };
66
+ };
67
+ /**
68
+ * Extended PGLite database type with client information
69
+ */
70
+ type PgliteDatabase<TSchema extends Record<string, unknown>> = PgliteDatabase$1<TSchema> & {
71
+ $client: PGlite;
72
+ };
73
+ /**
74
+ * Extended Node-Postgres database type with client information
75
+ */
76
+ type NodePgDatabase<TSchema extends Record<string, unknown>> = NodePgDatabase$1<TSchema> & {
77
+ $client: pg.Pool;
78
+ };
79
+ type Database<TOptions extends DrizzleOptions, TSchema extends Record<string, unknown>> = TOptions extends PgliteDrizzleOptions ? PgliteDatabase<TSchema> : NodePgDatabase<TSchema>;
80
+ /**
81
+ * Creates a new Drizzle database instance based on the provided options
82
+ *
83
+ * @important connectionString defaults to process.env["POSTGRES_CONNECTION_STRING"], if not set, it defaults to "memory://" (in-memory pglite)
84
+ *
85
+ * @param options - Configuration options for the database connection
86
+ * @returns A configured Drizzle database instance
87
+ * @throws {Error} If an invalid database type is specified
88
+ */
89
+ declare function drizzle<TSchema extends Record<string, unknown>, TOptions extends DrizzleOptions>(options?: TOptions & {
90
+ /**
91
+ * Schema to use for the database
92
+ * @default {}
93
+ */
94
+ schema?: TSchema;
95
+ }): Database<TOptions, TSchema>;
96
+ /**
97
+ * Options for database migration
98
+ */
99
+ type MigrateOptions = MigrationConfig;
100
+ /**
101
+ * Performs database migration based on the provided configuration
102
+ * @param db - The database instance to migrate
103
+ * @param options - Migration configuration options
104
+ *
105
+ * @important This function runs migrations on the database instance provided to the `drizzleStorage` plugin.
106
+ * It automatically detects the type of database and runs the appropriate migrate function
107
+ * (PGLite or Node-Postgres).
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * await migrate(db, { migrationsFolder: "./drizzle" });
112
+ * ```
113
+ */
114
+ declare function migrate<TSchema extends Record<string, unknown>>(db: PgliteDatabase<TSchema> | NodePgDatabase<TSchema>, options: MigrateOptions): Promise<void>;
4
115
 
5
116
  type DrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> = {
6
117
  db: PgTransaction<TQueryResult, TFullSchema, TSchema>;
7
118
  };
8
119
  declare function useDrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>(_db?: PgDatabase<TQueryResult, TFullSchema, TSchema>): DrizzleStorage<TQueryResult, TFullSchema, TSchema>;
9
120
  interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
121
+ /**
122
+ * The Drizzle database instance.
123
+ */
10
124
  db: PgDatabase<TQueryResult, TFullSchema, TSchema>;
125
+ /**
126
+ * Whether to persist the indexer's state. Defaults to true.
127
+ */
11
128
  persistState?: boolean;
129
+ /**
130
+ * The name of the indexer. Default value is 'default'.
131
+ */
12
132
  indexerName?: string;
133
+ /**
134
+ * The schema of the database.
135
+ */
13
136
  schema?: Record<string, unknown>;
137
+ /**
138
+ * The column to use as the id. Defaults to 'id'.
139
+ */
14
140
  idColumn?: string;
141
+ /**
142
+ * The options for the database migration. When provided, the database will automatically run migrations before the indexer runs.
143
+ */
144
+ migrate?: MigrateOptions;
15
145
  }
16
146
  /**
17
147
  * Creates a plugin that uses Drizzle as the storage layer.
@@ -22,7 +152,8 @@ interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSche
22
152
  * @param options.indexerName - The name of the indexer. Defaults value is 'default'.
23
153
  * @param options.schema - The schema of the database.
24
154
  * @param options.idColumn - The column to use as the id. Defaults to 'id'.
155
+ * @param options.migrate - The options for the database migration. when provided, the database will automatically run migrations before the indexer runs.
25
156
  */
26
- declare function drizzleStorage<TFilter, TBlock, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>({ db, persistState: enablePersistence, indexerName: identifier, schema, idColumn, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
157
+ declare function drizzleStorage<TFilter, TBlock, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>({ db, persistState: enablePersistence, indexerName: identifier, schema, idColumn, migrate: migrateOptions, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
27
158
 
28
- export { type DrizzleStorage, type DrizzleStorageOptions, drizzleStorage, useDrizzleStorage };
159
+ export { type Database, type DrizzleOptions, type DrizzleStorage, type DrizzleStorageOptions, type MigrateOptions, type NodePgDatabase, type NodePgDrizzleOptions, type PgliteDatabase, type PgliteDrizzleOptions, drizzle, drizzleStorage, migrate, useDrizzleStorage };