@apibara/plugin-drizzle 2.1.0-beta.3 → 2.1.0-beta.30
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.cjs +250 -64
- package/dist/index.d.cts +165 -4
- package/dist/index.d.mts +165 -4
- package/dist/index.d.ts +165 -4
- package/dist/index.mjs +239 -55
- package/dist/shared/plugin-drizzle.2d226351.mjs +5 -0
- package/dist/shared/plugin-drizzle.cae20704.cjs +9 -0
- package/dist/testing.cjs +13 -0
- package/dist/testing.d.cts +6 -0
- package/dist/testing.d.mts +6 -0
- package/dist/testing.d.ts +6 -0
- package/dist/testing.mjs +11 -0
- package/package.json +21 -6
- package/src/constants.ts +3 -0
- package/src/helper.ts +219 -0
- package/src/index.ts +142 -17
- package/src/persistence.ts +60 -18
- package/src/storage.ts +88 -23
- package/src/testing.ts +13 -0
- package/src/utils.ts +19 -0
package/dist/index.d.cts
CHANGED
|
@@ -1,17 +1,177 @@
|
|
|
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>;
|
|
115
|
+
|
|
116
|
+
interface IdColumnMap extends Record<string, string> {
|
|
117
|
+
/**
|
|
118
|
+
* Wildcard mapping for all tables.
|
|
119
|
+
*/
|
|
120
|
+
"*": string;
|
|
121
|
+
}
|
|
4
122
|
|
|
5
123
|
type DrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> = {
|
|
6
124
|
db: PgTransaction<TQueryResult, TFullSchema, TSchema>;
|
|
7
125
|
};
|
|
8
126
|
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
127
|
interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
|
|
128
|
+
/**
|
|
129
|
+
* The Drizzle database instance.
|
|
130
|
+
*/
|
|
10
131
|
db: PgDatabase<TQueryResult, TFullSchema, TSchema>;
|
|
132
|
+
/**
|
|
133
|
+
* Whether to persist the indexer's state. Defaults to true.
|
|
134
|
+
*/
|
|
11
135
|
persistState?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* The name of the indexer. Default value is 'default'.
|
|
138
|
+
*/
|
|
12
139
|
indexerName?: string;
|
|
140
|
+
/**
|
|
141
|
+
* The schema of the database.
|
|
142
|
+
*/
|
|
13
143
|
schema?: Record<string, unknown>;
|
|
14
|
-
|
|
144
|
+
/**
|
|
145
|
+
* The column to use as the primary identifier for each table.
|
|
146
|
+
*
|
|
147
|
+
* This identifier is used for tracking changes during reorgs and rollbacks.
|
|
148
|
+
*
|
|
149
|
+
* Can be specified in two ways:
|
|
150
|
+
*
|
|
151
|
+
* 1. As a single string that applies to all tables:
|
|
152
|
+
* ```ts
|
|
153
|
+
* idColumn: "_id" // Uses "_id" column for all tables
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* 2. As an object mapping table names to their ID columns:
|
|
157
|
+
* ```ts
|
|
158
|
+
* idColumn: {
|
|
159
|
+
* transfers: "transaction_hash", // Use "transaction_hash" for transfers table
|
|
160
|
+
* blocks: "block_number", // Use "block_number" for blocks table
|
|
161
|
+
* "*": "_id" // Use "_id" for all other tables | defaults to "id"
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* The special "*" key acts as a fallback for any tables not explicitly mapped.
|
|
166
|
+
*
|
|
167
|
+
* @default "id"
|
|
168
|
+
* @type {string | Partial<IdColumnMap>}
|
|
169
|
+
*/
|
|
170
|
+
idColumn?: string | Partial<IdColumnMap>;
|
|
171
|
+
/**
|
|
172
|
+
* The options for the database migration. When provided, the database will automatically run migrations before the indexer runs.
|
|
173
|
+
*/
|
|
174
|
+
migrate?: MigrateOptions;
|
|
15
175
|
}
|
|
16
176
|
/**
|
|
17
177
|
* Creates a plugin that uses Drizzle as the storage layer.
|
|
@@ -22,7 +182,8 @@ interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSche
|
|
|
22
182
|
* @param options.indexerName - The name of the indexer. Defaults value is 'default'.
|
|
23
183
|
* @param options.schema - The schema of the database.
|
|
24
184
|
* @param options.idColumn - The column to use as the id. Defaults to 'id'.
|
|
185
|
+
* @param options.migrate - The options for the database migration. when provided, the database will automatically run migrations before the indexer runs.
|
|
25
186
|
*/
|
|
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>;
|
|
187
|
+
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: _schema, idColumn, migrate: migrateOptions, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
|
|
27
188
|
|
|
28
|
-
export { type DrizzleStorage, type DrizzleStorageOptions, drizzleStorage, useDrizzleStorage };
|
|
189
|
+
export { type Database, type DrizzleOptions, type DrizzleStorage, type DrizzleStorageOptions, type IdColumnMap, type MigrateOptions, type NodePgDatabase, type NodePgDrizzleOptions, type PgliteDatabase, type PgliteDrizzleOptions, drizzle, drizzleStorage, migrate, useDrizzleStorage };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,17 +1,177 @@
|
|
|
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>;
|
|
115
|
+
|
|
116
|
+
interface IdColumnMap extends Record<string, string> {
|
|
117
|
+
/**
|
|
118
|
+
* Wildcard mapping for all tables.
|
|
119
|
+
*/
|
|
120
|
+
"*": string;
|
|
121
|
+
}
|
|
4
122
|
|
|
5
123
|
type DrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> = {
|
|
6
124
|
db: PgTransaction<TQueryResult, TFullSchema, TSchema>;
|
|
7
125
|
};
|
|
8
126
|
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
127
|
interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
|
|
128
|
+
/**
|
|
129
|
+
* The Drizzle database instance.
|
|
130
|
+
*/
|
|
10
131
|
db: PgDatabase<TQueryResult, TFullSchema, TSchema>;
|
|
132
|
+
/**
|
|
133
|
+
* Whether to persist the indexer's state. Defaults to true.
|
|
134
|
+
*/
|
|
11
135
|
persistState?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* The name of the indexer. Default value is 'default'.
|
|
138
|
+
*/
|
|
12
139
|
indexerName?: string;
|
|
140
|
+
/**
|
|
141
|
+
* The schema of the database.
|
|
142
|
+
*/
|
|
13
143
|
schema?: Record<string, unknown>;
|
|
14
|
-
|
|
144
|
+
/**
|
|
145
|
+
* The column to use as the primary identifier for each table.
|
|
146
|
+
*
|
|
147
|
+
* This identifier is used for tracking changes during reorgs and rollbacks.
|
|
148
|
+
*
|
|
149
|
+
* Can be specified in two ways:
|
|
150
|
+
*
|
|
151
|
+
* 1. As a single string that applies to all tables:
|
|
152
|
+
* ```ts
|
|
153
|
+
* idColumn: "_id" // Uses "_id" column for all tables
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* 2. As an object mapping table names to their ID columns:
|
|
157
|
+
* ```ts
|
|
158
|
+
* idColumn: {
|
|
159
|
+
* transfers: "transaction_hash", // Use "transaction_hash" for transfers table
|
|
160
|
+
* blocks: "block_number", // Use "block_number" for blocks table
|
|
161
|
+
* "*": "_id" // Use "_id" for all other tables | defaults to "id"
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* The special "*" key acts as a fallback for any tables not explicitly mapped.
|
|
166
|
+
*
|
|
167
|
+
* @default "id"
|
|
168
|
+
* @type {string | Partial<IdColumnMap>}
|
|
169
|
+
*/
|
|
170
|
+
idColumn?: string | Partial<IdColumnMap>;
|
|
171
|
+
/**
|
|
172
|
+
* The options for the database migration. When provided, the database will automatically run migrations before the indexer runs.
|
|
173
|
+
*/
|
|
174
|
+
migrate?: MigrateOptions;
|
|
15
175
|
}
|
|
16
176
|
/**
|
|
17
177
|
* Creates a plugin that uses Drizzle as the storage layer.
|
|
@@ -22,7 +182,8 @@ interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSche
|
|
|
22
182
|
* @param options.indexerName - The name of the indexer. Defaults value is 'default'.
|
|
23
183
|
* @param options.schema - The schema of the database.
|
|
24
184
|
* @param options.idColumn - The column to use as the id. Defaults to 'id'.
|
|
185
|
+
* @param options.migrate - The options for the database migration. when provided, the database will automatically run migrations before the indexer runs.
|
|
25
186
|
*/
|
|
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>;
|
|
187
|
+
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: _schema, idColumn, migrate: migrateOptions, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
|
|
27
188
|
|
|
28
|
-
export { type DrizzleStorage, type DrizzleStorageOptions, drizzleStorage, useDrizzleStorage };
|
|
189
|
+
export { type Database, type DrizzleOptions, type DrizzleStorage, type DrizzleStorageOptions, type IdColumnMap, type MigrateOptions, type NodePgDatabase, type NodePgDrizzleOptions, type PgliteDatabase, type PgliteDrizzleOptions, drizzle, drizzleStorage, migrate, useDrizzleStorage };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,177 @@
|
|
|
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>;
|
|
115
|
+
|
|
116
|
+
interface IdColumnMap extends Record<string, string> {
|
|
117
|
+
/**
|
|
118
|
+
* Wildcard mapping for all tables.
|
|
119
|
+
*/
|
|
120
|
+
"*": string;
|
|
121
|
+
}
|
|
4
122
|
|
|
5
123
|
type DrizzleStorage<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> = {
|
|
6
124
|
db: PgTransaction<TQueryResult, TFullSchema, TSchema>;
|
|
7
125
|
};
|
|
8
126
|
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
127
|
interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
|
|
128
|
+
/**
|
|
129
|
+
* The Drizzle database instance.
|
|
130
|
+
*/
|
|
10
131
|
db: PgDatabase<TQueryResult, TFullSchema, TSchema>;
|
|
132
|
+
/**
|
|
133
|
+
* Whether to persist the indexer's state. Defaults to true.
|
|
134
|
+
*/
|
|
11
135
|
persistState?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* The name of the indexer. Default value is 'default'.
|
|
138
|
+
*/
|
|
12
139
|
indexerName?: string;
|
|
140
|
+
/**
|
|
141
|
+
* The schema of the database.
|
|
142
|
+
*/
|
|
13
143
|
schema?: Record<string, unknown>;
|
|
14
|
-
|
|
144
|
+
/**
|
|
145
|
+
* The column to use as the primary identifier for each table.
|
|
146
|
+
*
|
|
147
|
+
* This identifier is used for tracking changes during reorgs and rollbacks.
|
|
148
|
+
*
|
|
149
|
+
* Can be specified in two ways:
|
|
150
|
+
*
|
|
151
|
+
* 1. As a single string that applies to all tables:
|
|
152
|
+
* ```ts
|
|
153
|
+
* idColumn: "_id" // Uses "_id" column for all tables
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* 2. As an object mapping table names to their ID columns:
|
|
157
|
+
* ```ts
|
|
158
|
+
* idColumn: {
|
|
159
|
+
* transfers: "transaction_hash", // Use "transaction_hash" for transfers table
|
|
160
|
+
* blocks: "block_number", // Use "block_number" for blocks table
|
|
161
|
+
* "*": "_id" // Use "_id" for all other tables | defaults to "id"
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* The special "*" key acts as a fallback for any tables not explicitly mapped.
|
|
166
|
+
*
|
|
167
|
+
* @default "id"
|
|
168
|
+
* @type {string | Partial<IdColumnMap>}
|
|
169
|
+
*/
|
|
170
|
+
idColumn?: string | Partial<IdColumnMap>;
|
|
171
|
+
/**
|
|
172
|
+
* The options for the database migration. When provided, the database will automatically run migrations before the indexer runs.
|
|
173
|
+
*/
|
|
174
|
+
migrate?: MigrateOptions;
|
|
15
175
|
}
|
|
16
176
|
/**
|
|
17
177
|
* Creates a plugin that uses Drizzle as the storage layer.
|
|
@@ -22,7 +182,8 @@ interface DrizzleStorageOptions<TQueryResult extends PgQueryResultHKT, TFullSche
|
|
|
22
182
|
* @param options.indexerName - The name of the indexer. Defaults value is 'default'.
|
|
23
183
|
* @param options.schema - The schema of the database.
|
|
24
184
|
* @param options.idColumn - The column to use as the id. Defaults to 'id'.
|
|
185
|
+
* @param options.migrate - The options for the database migration. when provided, the database will automatically run migrations before the indexer runs.
|
|
25
186
|
*/
|
|
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>;
|
|
187
|
+
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: _schema, idColumn, migrate: migrateOptions, }: DrizzleStorageOptions<TQueryResult, TFullSchema, TSchema>): _apibara_indexer_plugins.IndexerPlugin<TFilter, TBlock>;
|
|
27
188
|
|
|
28
|
-
export { type DrizzleStorage, type DrizzleStorageOptions, drizzleStorage, useDrizzleStorage };
|
|
189
|
+
export { type Database, type DrizzleOptions, type DrizzleStorage, type DrizzleStorageOptions, type IdColumnMap, type MigrateOptions, type NodePgDatabase, type NodePgDrizzleOptions, type PgliteDatabase, type PgliteDrizzleOptions, drizzle, drizzleStorage, migrate, useDrizzleStorage };
|