@apibara/plugin-drizzle 2.1.0-beta.21 → 2.1.0-beta.23
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 +35 -26
- package/dist/index.mjs +35 -22
- package/package.json +12 -3
- package/src/helper.ts +48 -31
package/dist/index.cjs
CHANGED
|
@@ -5,20 +5,10 @@ const plugins = require('@apibara/indexer/plugins');
|
|
|
5
5
|
const internal = require('@apibara/indexer/internal');
|
|
6
6
|
const plugins$1 = require('@apibara/indexer/internal/plugins');
|
|
7
7
|
const constants = require('./shared/plugin-drizzle.cae20704.cjs');
|
|
8
|
-
const pglite$1 = require('@electric-sql/pglite');
|
|
9
|
-
const nodePostgres = require('drizzle-orm/node-postgres');
|
|
10
|
-
const migrator$1 = require('drizzle-orm/node-postgres/migrator');
|
|
11
|
-
const pglite = require('drizzle-orm/pglite');
|
|
12
|
-
const migrator = require('drizzle-orm/pglite/migrator');
|
|
13
|
-
const pg = require('pg');
|
|
14
|
-
const protocol = require('@apibara/protocol');
|
|
15
8
|
const drizzleOrm = require('drizzle-orm');
|
|
9
|
+
const protocol = require('@apibara/protocol');
|
|
16
10
|
const pgCore = require('drizzle-orm/pg-core');
|
|
17
11
|
|
|
18
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
19
|
-
|
|
20
|
-
const pg__default = /*#__PURE__*/_interopDefaultCompat(pg);
|
|
21
|
-
|
|
22
12
|
class DrizzleStorageError extends Error {
|
|
23
13
|
constructor(message, options) {
|
|
24
14
|
super(message, options);
|
|
@@ -61,15 +51,9 @@ function drizzle(options) {
|
|
|
61
51
|
config,
|
|
62
52
|
poolConfig
|
|
63
53
|
} = options ?? {};
|
|
64
|
-
if (
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
...poolConfig || {}
|
|
68
|
-
});
|
|
69
|
-
return nodePostgres.drizzle(pool, { schema, ...config || {} });
|
|
70
|
-
}
|
|
71
|
-
if (type === "pglite") {
|
|
72
|
-
return pglite.drizzle({
|
|
54
|
+
if (isPgliteConnectionString(connectionString) && type === "pglite") {
|
|
55
|
+
const { drizzle: drizzlePGLite } = require("drizzle-orm/pglite");
|
|
56
|
+
return drizzlePGLite({
|
|
73
57
|
schema,
|
|
74
58
|
connection: {
|
|
75
59
|
dataDir: connectionString || "memory://pglite"
|
|
@@ -77,15 +61,25 @@ function drizzle(options) {
|
|
|
77
61
|
...config || {}
|
|
78
62
|
});
|
|
79
63
|
}
|
|
80
|
-
|
|
64
|
+
const { Pool } = require("pg");
|
|
65
|
+
const { drizzle: drizzleNode } = require("drizzle-orm/node-postgres");
|
|
66
|
+
const pool = new Pool({
|
|
67
|
+
connectionString,
|
|
68
|
+
...poolConfig || {}
|
|
69
|
+
});
|
|
70
|
+
return drizzleNode(pool, { schema, ...config || {} });
|
|
81
71
|
}
|
|
82
72
|
async function migrate(db, options) {
|
|
83
|
-
const isPglite =
|
|
73
|
+
const isPglite = isDrizzleKind(db, "PgliteDatabase");
|
|
84
74
|
try {
|
|
85
75
|
if (isPglite) {
|
|
86
|
-
|
|
76
|
+
const { migrate: migratePGLite } = require("drizzle-orm/pglite/migrator");
|
|
77
|
+
await migratePGLite(db, options);
|
|
87
78
|
} else {
|
|
88
|
-
|
|
79
|
+
const {
|
|
80
|
+
migrate: migrateNode
|
|
81
|
+
} = require("drizzle-orm/node-postgres/migrator");
|
|
82
|
+
await migrateNode(db, options);
|
|
89
83
|
}
|
|
90
84
|
} catch (error) {
|
|
91
85
|
throw new DrizzleStorageError(
|
|
@@ -96,8 +90,23 @@ async function migrate(db, options) {
|
|
|
96
90
|
);
|
|
97
91
|
}
|
|
98
92
|
}
|
|
99
|
-
function
|
|
100
|
-
return conn.startsWith("
|
|
93
|
+
function isPgliteConnectionString(conn) {
|
|
94
|
+
return conn.startsWith("memory://") || conn.startsWith("file://") || conn.startsWith("idb://");
|
|
95
|
+
}
|
|
96
|
+
function isDrizzleKind(value, entityKindValue) {
|
|
97
|
+
if (!value || typeof value !== "object") {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
let cls = Object.getPrototypeOf(value).constructor;
|
|
101
|
+
if (cls) {
|
|
102
|
+
while (cls) {
|
|
103
|
+
if (drizzleOrm.entityKind in cls && cls[drizzleOrm.entityKind] === entityKindValue) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
cls = Object.getPrototypeOf(cls);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
101
110
|
}
|
|
102
111
|
|
|
103
112
|
const CHECKPOINTS_TABLE_NAME = "checkpoints";
|
package/dist/index.mjs
CHANGED
|
@@ -3,14 +3,8 @@ import { defineIndexerPlugin, useLogger } from '@apibara/indexer/plugins';
|
|
|
3
3
|
import { generateIndexerId } from '@apibara/indexer/internal';
|
|
4
4
|
import { useInternalContext } from '@apibara/indexer/internal/plugins';
|
|
5
5
|
import { S as SCHEMA_NAME, D as DRIZZLE_PROPERTY, a as DRIZZLE_STORAGE_DB_PROPERTY } from './shared/plugin-drizzle.2d226351.mjs';
|
|
6
|
-
import {
|
|
7
|
-
import { drizzle as drizzle$1 } from 'drizzle-orm/node-postgres';
|
|
8
|
-
import { migrate as migrate$2 } from 'drizzle-orm/node-postgres/migrator';
|
|
9
|
-
import { drizzle as drizzle$2 } from 'drizzle-orm/pglite';
|
|
10
|
-
import { migrate as migrate$1 } from 'drizzle-orm/pglite/migrator';
|
|
11
|
-
import pg from 'pg';
|
|
6
|
+
import { entityKind, sql, eq, and, isNull, gt, lt } from 'drizzle-orm';
|
|
12
7
|
import { normalizeCursor } from '@apibara/protocol';
|
|
13
|
-
import { sql, eq, and, isNull, gt, lt } from 'drizzle-orm';
|
|
14
8
|
import { pgSchema, text, integer, primaryKey, serial, char, jsonb } from 'drizzle-orm/pg-core';
|
|
15
9
|
|
|
16
10
|
class DrizzleStorageError extends Error {
|
|
@@ -55,15 +49,9 @@ function drizzle(options) {
|
|
|
55
49
|
config,
|
|
56
50
|
poolConfig
|
|
57
51
|
} = options ?? {};
|
|
58
|
-
if (
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
...poolConfig || {}
|
|
62
|
-
});
|
|
63
|
-
return drizzle$1(pool, { schema, ...config || {} });
|
|
64
|
-
}
|
|
65
|
-
if (type === "pglite") {
|
|
66
|
-
return drizzle$2({
|
|
52
|
+
if (isPgliteConnectionString(connectionString) && type === "pglite") {
|
|
53
|
+
const { drizzle: drizzlePGLite } = require("drizzle-orm/pglite");
|
|
54
|
+
return drizzlePGLite({
|
|
67
55
|
schema,
|
|
68
56
|
connection: {
|
|
69
57
|
dataDir: connectionString || "memory://pglite"
|
|
@@ -71,15 +59,25 @@ function drizzle(options) {
|
|
|
71
59
|
...config || {}
|
|
72
60
|
});
|
|
73
61
|
}
|
|
74
|
-
|
|
62
|
+
const { Pool } = require("pg");
|
|
63
|
+
const { drizzle: drizzleNode } = require("drizzle-orm/node-postgres");
|
|
64
|
+
const pool = new Pool({
|
|
65
|
+
connectionString,
|
|
66
|
+
...poolConfig || {}
|
|
67
|
+
});
|
|
68
|
+
return drizzleNode(pool, { schema, ...config || {} });
|
|
75
69
|
}
|
|
76
70
|
async function migrate(db, options) {
|
|
77
|
-
const isPglite =
|
|
71
|
+
const isPglite = isDrizzleKind(db, "PgliteDatabase");
|
|
78
72
|
try {
|
|
79
73
|
if (isPglite) {
|
|
80
|
-
|
|
74
|
+
const { migrate: migratePGLite } = require("drizzle-orm/pglite/migrator");
|
|
75
|
+
await migratePGLite(db, options);
|
|
81
76
|
} else {
|
|
82
|
-
|
|
77
|
+
const {
|
|
78
|
+
migrate: migrateNode
|
|
79
|
+
} = require("drizzle-orm/node-postgres/migrator");
|
|
80
|
+
await migrateNode(db, options);
|
|
83
81
|
}
|
|
84
82
|
} catch (error) {
|
|
85
83
|
throw new DrizzleStorageError(
|
|
@@ -90,8 +88,23 @@ async function migrate(db, options) {
|
|
|
90
88
|
);
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
|
-
function
|
|
94
|
-
return conn.startsWith("
|
|
91
|
+
function isPgliteConnectionString(conn) {
|
|
92
|
+
return conn.startsWith("memory://") || conn.startsWith("file://") || conn.startsWith("idb://");
|
|
93
|
+
}
|
|
94
|
+
function isDrizzleKind(value, entityKindValue) {
|
|
95
|
+
if (!value || typeof value !== "object") {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
let cls = Object.getPrototypeOf(value).constructor;
|
|
99
|
+
if (cls) {
|
|
100
|
+
while (cls) {
|
|
101
|
+
if (entityKind in cls && cls[entityKind] === entityKindValue) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
cls = Object.getPrototypeOf(cls);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
95
108
|
}
|
|
96
109
|
|
|
97
110
|
const CHECKPOINTS_TABLE_NAME = "checkpoints";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apibara/plugin-drizzle",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.23",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -32,9 +32,18 @@
|
|
|
32
32
|
"test:ci": "vitest run"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
+
"@electric-sql/pglite": ">=0.2.0",
|
|
35
36
|
"drizzle-orm": "<1",
|
|
36
37
|
"pg": ">=8"
|
|
37
38
|
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"@electric-sql/pglite": {
|
|
41
|
+
"optional": true
|
|
42
|
+
},
|
|
43
|
+
"pg": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
38
47
|
"devDependencies": {
|
|
39
48
|
"@electric-sql/pglite": "^0.2.17",
|
|
40
49
|
"@types/node": "^20.14.0",
|
|
@@ -45,8 +54,8 @@
|
|
|
45
54
|
"vitest": "^1.6.0"
|
|
46
55
|
},
|
|
47
56
|
"dependencies": {
|
|
48
|
-
"@apibara/indexer": "2.1.0-beta.
|
|
49
|
-
"@apibara/protocol": "2.1.0-beta.
|
|
57
|
+
"@apibara/indexer": "2.1.0-beta.23",
|
|
58
|
+
"@apibara/protocol": "2.1.0-beta.23",
|
|
50
59
|
"postgres-range": "^1.1.4"
|
|
51
60
|
}
|
|
52
61
|
}
|
package/src/helper.ts
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import { PGlite,
|
|
1
|
+
import type { PGlite, PGliteOptions } from "@electric-sql/pglite";
|
|
2
2
|
import type { DrizzleConfig } from "drizzle-orm";
|
|
3
|
+
import { entityKind } from "drizzle-orm";
|
|
3
4
|
import type { MigrationConfig } from "drizzle-orm/migrator";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "drizzle-orm/node-postgres";
|
|
8
|
-
import { migrate as migrateNode } from "drizzle-orm/node-postgres/migrator";
|
|
9
|
-
import {} from "drizzle-orm/pg-core";
|
|
10
|
-
import {
|
|
11
|
-
type PgliteDatabase as OriginalPgliteDatabase,
|
|
12
|
-
drizzle as drizzlePGLite,
|
|
13
|
-
} from "drizzle-orm/pglite";
|
|
14
|
-
import { migrate as migratePGLite } from "drizzle-orm/pglite/migrator";
|
|
15
|
-
import pg from "pg";
|
|
5
|
+
import type { NodePgDatabase as OriginalNodePgDatabase } from "drizzle-orm/node-postgres";
|
|
6
|
+
import type { PgliteDatabase as OriginalPgliteDatabase } from "drizzle-orm/pglite";
|
|
7
|
+
import type pg from "pg";
|
|
16
8
|
import { DrizzleStorageError } from "./utils";
|
|
17
9
|
|
|
18
10
|
/**
|
|
@@ -129,21 +121,9 @@ export function drizzle<
|
|
|
129
121
|
poolConfig,
|
|
130
122
|
} = options ?? {};
|
|
131
123
|
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
type === "node-postgres"
|
|
135
|
-
) {
|
|
136
|
-
const pool = new pg.Pool({
|
|
137
|
-
connectionString,
|
|
138
|
-
...(poolConfig || {}),
|
|
139
|
-
});
|
|
140
|
-
return drizzleNode(pool, { schema, ...(config || {}) }) as Database<
|
|
141
|
-
TOptions,
|
|
142
|
-
TSchema
|
|
143
|
-
>;
|
|
144
|
-
}
|
|
124
|
+
if (isPgliteConnectionString(connectionString) && type === "pglite") {
|
|
125
|
+
const { drizzle: drizzlePGLite } = require("drizzle-orm/pglite");
|
|
145
126
|
|
|
146
|
-
if (type === "pglite") {
|
|
147
127
|
return drizzlePGLite({
|
|
148
128
|
schema: schema as TSchema,
|
|
149
129
|
connection: {
|
|
@@ -153,7 +133,16 @@ export function drizzle<
|
|
|
153
133
|
}) as Database<TOptions, TSchema>;
|
|
154
134
|
}
|
|
155
135
|
|
|
156
|
-
|
|
136
|
+
const { Pool } = require("pg");
|
|
137
|
+
const { drizzle: drizzleNode } = require("drizzle-orm/node-postgres");
|
|
138
|
+
const pool = new Pool({
|
|
139
|
+
connectionString,
|
|
140
|
+
...(poolConfig || {}),
|
|
141
|
+
});
|
|
142
|
+
return drizzleNode(pool, { schema, ...(config || {}) }) as Database<
|
|
143
|
+
TOptions,
|
|
144
|
+
TSchema
|
|
145
|
+
>;
|
|
157
146
|
}
|
|
158
147
|
|
|
159
148
|
/**
|
|
@@ -179,12 +168,16 @@ export async function migrate<TSchema extends Record<string, unknown>>(
|
|
|
179
168
|
db: PgliteDatabase<TSchema> | NodePgDatabase<TSchema>,
|
|
180
169
|
options: MigrateOptions,
|
|
181
170
|
) {
|
|
182
|
-
const isPglite =
|
|
171
|
+
const isPglite = isDrizzleKind(db, "PgliteDatabase");
|
|
183
172
|
|
|
184
173
|
try {
|
|
185
174
|
if (isPglite) {
|
|
175
|
+
const { migrate: migratePGLite } = require("drizzle-orm/pglite/migrator");
|
|
186
176
|
await migratePGLite(db as PgliteDatabase<TSchema>, options);
|
|
187
177
|
} else {
|
|
178
|
+
const {
|
|
179
|
+
migrate: migrateNode,
|
|
180
|
+
} = require("drizzle-orm/node-postgres/migrator");
|
|
188
181
|
await migrateNode(db as NodePgDatabase<TSchema>, options);
|
|
189
182
|
}
|
|
190
183
|
} catch (error) {
|
|
@@ -197,6 +190,30 @@ export async function migrate<TSchema extends Record<string, unknown>>(
|
|
|
197
190
|
}
|
|
198
191
|
}
|
|
199
192
|
|
|
200
|
-
function
|
|
201
|
-
return
|
|
193
|
+
function isPgliteConnectionString(conn: string) {
|
|
194
|
+
return (
|
|
195
|
+
conn.startsWith("memory://") ||
|
|
196
|
+
conn.startsWith("file://") ||
|
|
197
|
+
conn.startsWith("idb://")
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function isDrizzleKind(value: unknown, entityKindValue: string) {
|
|
202
|
+
if (!value || typeof value !== "object") {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
// https://github.com/drizzle-team/drizzle-orm/blob/f39f885779800982e90dd3c89aba6df3217a6fd2/drizzle-orm/src/entity.ts#L29-L41
|
|
206
|
+
let cls = Object.getPrototypeOf(value).constructor;
|
|
207
|
+
if (cls) {
|
|
208
|
+
// Traverse the prototype chain to find the entityKind
|
|
209
|
+
while (cls) {
|
|
210
|
+
// https://github.com/drizzle-team/drizzle-orm/blob/f39f885779800982e90dd3c89aba6df3217a6fd2/drizzle-orm/src/pglite/driver.ts#L41
|
|
211
|
+
if (entityKind in cls && cls[entityKind] === entityKindValue) {
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
cls = Object.getPrototypeOf(cls);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return false;
|
|
202
219
|
}
|