ponder 0.11.21 → 0.11.22
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/CHANGELOG.md +6 -0
- package/dist/esm/bin/commands/createViews.js +9 -20
- package/dist/esm/bin/commands/createViews.js.map +1 -1
- package/dist/esm/bin/commands/dev.js +1 -1
- package/dist/esm/bin/commands/dev.js.map +1 -1
- package/dist/esm/bin/commands/list.js +4 -7
- package/dist/esm/bin/commands/list.js.map +1 -1
- package/dist/esm/bin/commands/prune.js +9 -21
- package/dist/esm/bin/commands/prune.js.map +1 -1
- package/dist/esm/bin/commands/serve.js +1 -1
- package/dist/esm/bin/commands/serve.js.map +1 -1
- package/dist/esm/bin/commands/start.js +3 -3
- package/dist/esm/bin/commands/start.js.map +1 -1
- package/dist/esm/bin/utils/run.js +159 -180
- package/dist/esm/bin/utils/run.js.map +1 -1
- package/dist/esm/build/index.js +1 -48
- package/dist/esm/build/index.js.map +1 -1
- package/dist/esm/build/plugin.js +1 -1
- package/dist/esm/client/index.js +9 -13
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/database/index.js +429 -141
- package/dist/esm/database/index.js.map +1 -1
- package/dist/esm/drizzle/index.js.map +1 -1
- package/dist/esm/drizzle/kit/index.js.map +1 -1
- package/dist/esm/drizzle/onchain.js +1 -8
- package/dist/esm/drizzle/onchain.js.map +1 -1
- package/dist/esm/graphql/index.js +16 -19
- package/dist/esm/graphql/index.js.map +1 -1
- package/dist/esm/graphql/middleware.js +7 -3
- package/dist/esm/graphql/middleware.js.map +1 -1
- package/dist/esm/indexing-store/cache.js +32 -26
- package/dist/esm/indexing-store/cache.js.map +1 -1
- package/dist/esm/indexing-store/historical.js +32 -23
- package/dist/esm/indexing-store/historical.js.map +1 -1
- package/dist/esm/indexing-store/index.js +18 -1
- package/dist/esm/indexing-store/index.js.map +1 -1
- package/dist/esm/indexing-store/realtime.js +140 -89
- package/dist/esm/indexing-store/realtime.js.map +1 -1
- package/dist/esm/internal/errors.js +0 -12
- package/dist/esm/internal/errors.js.map +1 -1
- package/dist/esm/server/index.js +2 -10
- package/dist/esm/server/index.js.map +1 -1
- package/dist/esm/sync-store/index.js +432 -403
- package/dist/esm/sync-store/index.js.map +1 -1
- package/dist/esm/utils/wait.js +0 -2
- package/dist/esm/utils/wait.js.map +1 -1
- package/dist/types/bin/commands/createViews.d.ts.map +1 -1
- package/dist/types/bin/commands/list.d.ts.map +1 -1
- package/dist/types/bin/commands/prune.d.ts.map +1 -1
- package/dist/types/bin/commands/start.d.ts +0 -2
- package/dist/types/bin/commands/start.d.ts.map +1 -1
- package/dist/types/bin/utils/run.d.ts +1 -1
- package/dist/types/bin/utils/run.d.ts.map +1 -1
- package/dist/types/build/index.d.ts +1 -1
- package/dist/types/build/index.d.ts.map +1 -1
- package/dist/types/client/index.d.ts.map +1 -1
- package/dist/types/database/index.d.ts +73 -25
- package/dist/types/database/index.d.ts.map +1 -1
- package/dist/types/drizzle/index.d.ts +3 -2
- package/dist/types/drizzle/index.d.ts.map +1 -1
- package/dist/types/drizzle/kit/index.d.ts +4 -3
- package/dist/types/drizzle/kit/index.d.ts.map +1 -1
- package/dist/types/drizzle/onchain.d.ts +5 -12
- package/dist/types/drizzle/onchain.d.ts.map +1 -1
- package/dist/types/graphql/index.d.ts +4 -2
- package/dist/types/graphql/index.d.ts.map +1 -1
- package/dist/types/graphql/middleware.d.ts +1 -1
- package/dist/types/graphql/middleware.d.ts.map +1 -1
- package/dist/types/indexing-store/cache.d.ts +12 -5
- package/dist/types/indexing-store/cache.d.ts.map +1 -1
- package/dist/types/indexing-store/historical.d.ts +7 -2
- package/dist/types/indexing-store/historical.d.ts.map +1 -1
- package/dist/types/indexing-store/index.d.ts +2 -4
- package/dist/types/indexing-store/index.d.ts.map +1 -1
- package/dist/types/indexing-store/realtime.d.ts +3 -1
- package/dist/types/indexing-store/realtime.d.ts.map +1 -1
- package/dist/types/internal/errors.d.ts +0 -4
- package/dist/types/internal/errors.d.ts.map +1 -1
- package/dist/types/server/index.d.ts +1 -1
- package/dist/types/server/index.d.ts.map +1 -1
- package/dist/types/sync/index.d.ts +1 -1
- package/dist/types/sync-store/index.d.ts.map +1 -1
- package/dist/types/utils/wait.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/bin/commands/createViews.ts +26 -37
- package/src/bin/commands/dev.ts +1 -1
- package/src/bin/commands/list.ts +4 -7
- package/src/bin/commands/prune.ts +17 -31
- package/src/bin/commands/serve.ts +1 -1
- package/src/bin/commands/start.ts +3 -4
- package/src/bin/utils/run.ts +210 -256
- package/src/build/index.ts +2 -53
- package/src/build/plugin.ts +1 -1
- package/src/client/index.ts +10 -21
- package/src/database/index.ts +742 -331
- package/src/drizzle/index.ts +3 -2
- package/src/drizzle/kit/index.ts +5 -2
- package/src/drizzle/onchain.ts +2 -26
- package/src/graphql/index.ts +26 -31
- package/src/graphql/middleware.ts +7 -5
- package/src/indexing-store/cache.ts +52 -35
- package/src/indexing-store/historical.ts +40 -28
- package/src/indexing-store/index.ts +27 -2
- package/src/indexing-store/realtime.ts +220 -176
- package/src/internal/errors.ts +0 -9
- package/src/server/index.ts +3 -14
- package/src/sync-store/index.ts +997 -870
- package/src/utils/wait.ts +0 -1
- package/dist/esm/database/queryBuilder.js +0 -206
- package/dist/esm/database/queryBuilder.js.map +0 -1
- package/dist/esm/database/utils.js +0 -100
- package/dist/esm/database/utils.js.map +0 -1
- package/dist/esm/drizzle/json.js +0 -119
- package/dist/esm/drizzle/json.js.map +0 -1
- package/dist/types/database/queryBuilder.d.ts +0 -37
- package/dist/types/database/queryBuilder.d.ts.map +0 -1
- package/dist/types/database/utils.d.ts +0 -25
- package/dist/types/database/utils.d.ts.map +0 -1
- package/dist/types/drizzle/json.d.ts +0 -51
- package/dist/types/drizzle/json.d.ts.map +0 -1
- package/src/database/queryBuilder.ts +0 -319
- package/src/database/utils.ts +0 -140
- package/src/drizzle/json.ts +0 -154
package/src/drizzle/index.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { getTableColumns, getTableName } from "drizzle-orm";
|
|
1
|
+
import { type TableConfig, getTableColumns, getTableName } from "drizzle-orm";
|
|
2
2
|
import {
|
|
3
3
|
type PgColumn,
|
|
4
4
|
type PgTable,
|
|
5
|
+
type PgTableWithColumns,
|
|
5
6
|
getTableConfig,
|
|
6
7
|
} from "drizzle-orm/pg-core";
|
|
7
8
|
import { getColumnCasing, sqlToReorgTableName } from "./kit/index.js";
|
|
8
9
|
|
|
9
|
-
export const getTableNames = (table:
|
|
10
|
+
export const getTableNames = (table: PgTableWithColumns<TableConfig>) => {
|
|
10
11
|
const sql = getTableName(table);
|
|
11
12
|
|
|
12
13
|
return {
|
package/src/drizzle/kit/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SQL, getTableName, is } from "drizzle-orm";
|
|
1
|
+
import { SQL, type TableConfig, getTableName, is } from "drizzle-orm";
|
|
2
2
|
import { CasingCache, toCamelCase, toSnakeCase } from "drizzle-orm/casing";
|
|
3
3
|
import {
|
|
4
4
|
type AnyPgTable,
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
PgSchema,
|
|
10
10
|
type PgSequence,
|
|
11
11
|
PgTable,
|
|
12
|
+
type PgTableWithColumns,
|
|
12
13
|
PgView,
|
|
13
14
|
getTableConfig,
|
|
14
15
|
index,
|
|
@@ -39,7 +40,9 @@ export type SqlStatements = {
|
|
|
39
40
|
export const sqlToReorgTableName = (tableName: string) =>
|
|
40
41
|
`_reorg__${tableName}`;
|
|
41
42
|
|
|
42
|
-
export const getReorgTable =
|
|
43
|
+
export const getReorgTable = <config extends TableConfig>(
|
|
44
|
+
table: PgTableWithColumns<config>,
|
|
45
|
+
) => {
|
|
43
46
|
const schema = getTableConfig(table).schema;
|
|
44
47
|
|
|
45
48
|
if (schema && schema !== "public") {
|
package/src/drizzle/onchain.ts
CHANGED
|
@@ -26,12 +26,6 @@ import {
|
|
|
26
26
|
import { PgBigintBuilder, type PgBigintBuilderInitial } from "./bigint.js";
|
|
27
27
|
import { PgBytesBuilder, type PgBytesBuilderInitial } from "./bytes.js";
|
|
28
28
|
import { PgHexBuilder, type PgHexBuilderInitial } from "./hex.js";
|
|
29
|
-
import {
|
|
30
|
-
PgJsonBuilder,
|
|
31
|
-
type PgJsonBuilderInitial,
|
|
32
|
-
PgJsonbBuilder,
|
|
33
|
-
type PgJsonbBuilderInitial,
|
|
34
|
-
} from "./json.js";
|
|
35
29
|
import { PgTextBuilder, type PgTextBuilderInitial } from "./text.js";
|
|
36
30
|
|
|
37
31
|
/** @internal */
|
|
@@ -62,22 +56,6 @@ export function bigint(columnName?: string) {
|
|
|
62
56
|
return new PgBigintBuilder(columnName ?? "");
|
|
63
57
|
}
|
|
64
58
|
|
|
65
|
-
export function json(): PgJsonBuilderInitial<"">;
|
|
66
|
-
export function json<name extends string>(
|
|
67
|
-
name: name,
|
|
68
|
-
): PgJsonBuilderInitial<name>;
|
|
69
|
-
export function json(name?: string) {
|
|
70
|
-
return new PgJsonBuilder(name ?? "");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function jsonb(): PgJsonbBuilderInitial<"">;
|
|
74
|
-
export function jsonb<name extends string>(
|
|
75
|
-
name: name,
|
|
76
|
-
): PgJsonbBuilderInitial<name>;
|
|
77
|
-
export function jsonb(name?: string) {
|
|
78
|
-
return new PgJsonbBuilder(name ?? "");
|
|
79
|
-
}
|
|
80
|
-
|
|
81
59
|
// @ts-ignore
|
|
82
60
|
export function bytes(): PgBytesBuilderInitial<"">;
|
|
83
61
|
export function bytes<name extends string>(
|
|
@@ -143,7 +121,7 @@ export type BuildExtraConfigColumns<
|
|
|
143
121
|
|
|
144
122
|
export type PgColumnsBuilders = Omit<
|
|
145
123
|
_PgColumnsBuilders,
|
|
146
|
-
"bigint" | "serial" | "smallserial" | "bigserial"
|
|
124
|
+
"bigint" | "serial" | "smallserial" | "bigserial"
|
|
147
125
|
> & {
|
|
148
126
|
/**
|
|
149
127
|
* Create an 8 byte number column.
|
|
@@ -188,8 +166,6 @@ export type PgColumnsBuilders = Omit<
|
|
|
188
166
|
* }));
|
|
189
167
|
*/
|
|
190
168
|
bytes: typeof bytes;
|
|
191
|
-
json: typeof json;
|
|
192
|
-
jsonb: typeof jsonb;
|
|
193
169
|
};
|
|
194
170
|
|
|
195
171
|
/**
|
|
@@ -297,7 +273,7 @@ function pgTableWithSchema<
|
|
|
297
273
|
|
|
298
274
|
const parsedColumns: columns =
|
|
299
275
|
typeof columns === "function"
|
|
300
|
-
? columns({ ...restColumns, int8, hex, bigint, bytes, text
|
|
276
|
+
? columns({ ...restColumns, int8, hex, bigint, bytes, text })
|
|
301
277
|
: columns;
|
|
302
278
|
|
|
303
279
|
const builtColumns = Object.fromEntries(
|
package/src/graphql/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Database } from "@/database/index.js";
|
|
2
2
|
import type { OnchainTable } from "@/drizzle/onchain.js";
|
|
3
3
|
import { normalizeColumn } from "@/indexing-store/utils.js";
|
|
4
4
|
import type { Schema, Status } from "@/internal/types.js";
|
|
5
|
+
import type { Drizzle, ReadonlyDrizzle } from "@/types/db.js";
|
|
5
6
|
import { decodeCheckpoint } from "@/utils/checkpoint.js";
|
|
6
7
|
import { never } from "@/utils/never.js";
|
|
7
8
|
import { deserialize, serialize } from "@/utils/serialize.js";
|
|
@@ -36,7 +37,6 @@ import {
|
|
|
36
37
|
notInArray,
|
|
37
38
|
notLike,
|
|
38
39
|
or,
|
|
39
|
-
sql,
|
|
40
40
|
} from "drizzle-orm";
|
|
41
41
|
import {
|
|
42
42
|
type PgEnum,
|
|
@@ -68,8 +68,9 @@ import { GraphQLJSON } from "./json.js";
|
|
|
68
68
|
|
|
69
69
|
type Parent = Record<string, any>;
|
|
70
70
|
type Context = {
|
|
71
|
-
qb: QB<{ [key: string]: OnchainTable }>;
|
|
72
71
|
getDataLoader: ReturnType<typeof buildDataLoaderCache>;
|
|
72
|
+
getCheckpoints: Database["getCheckpoints"];
|
|
73
|
+
drizzle: Drizzle<{ [key: string]: OnchainTable }>;
|
|
73
74
|
};
|
|
74
75
|
|
|
75
76
|
type PluralArgs = {
|
|
@@ -334,7 +335,7 @@ export function buildGraphQLSchema({
|
|
|
334
335
|
|
|
335
336
|
return executePluralQuery(
|
|
336
337
|
referencedTable,
|
|
337
|
-
context.
|
|
338
|
+
context.drizzle,
|
|
338
339
|
args,
|
|
339
340
|
includeTotalCount,
|
|
340
341
|
relationalConditions,
|
|
@@ -414,7 +415,12 @@ export function buildGraphQLSchema({
|
|
|
414
415
|
resolve: async (_parent, args: PluralArgs, context, info) => {
|
|
415
416
|
const includeTotalCount = selectionIncludesField(info, "totalCount");
|
|
416
417
|
|
|
417
|
-
return executePluralQuery(
|
|
418
|
+
return executePluralQuery(
|
|
419
|
+
table,
|
|
420
|
+
context.drizzle,
|
|
421
|
+
args,
|
|
422
|
+
includeTotalCount,
|
|
423
|
+
);
|
|
418
424
|
},
|
|
419
425
|
};
|
|
420
426
|
}
|
|
@@ -422,27 +428,15 @@ export function buildGraphQLSchema({
|
|
|
422
428
|
queryFields._meta = {
|
|
423
429
|
type: GraphQLMeta,
|
|
424
430
|
resolve: async (_source, _args, context) => {
|
|
425
|
-
|
|
426
|
-
const checkpoints = (await context
|
|
427
|
-
.qb()
|
|
428
|
-
.execute(
|
|
429
|
-
sql`SELECT chain_name, chain_id, latest_checkpoint, safe_checkpoint from _ponder_checkpoint`,
|
|
430
|
-
)
|
|
431
|
-
.then((res) => res.rows)) as {
|
|
432
|
-
chain_name: string;
|
|
433
|
-
chain_id: number;
|
|
434
|
-
latest_checkpoint: string;
|
|
435
|
-
safe_checkpoint: string;
|
|
436
|
-
}[];
|
|
437
|
-
|
|
431
|
+
const checkpoints = await context.getCheckpoints();
|
|
438
432
|
const status: Status = {};
|
|
439
|
-
for (const {
|
|
440
|
-
status[
|
|
441
|
-
id:
|
|
433
|
+
for (const { chainName, chainId, latestCheckpoint } of checkpoints) {
|
|
434
|
+
status[chainName] = {
|
|
435
|
+
id: chainId,
|
|
442
436
|
block: {
|
|
443
|
-
number: Number(decodeCheckpoint(
|
|
437
|
+
number: Number(decodeCheckpoint(latestCheckpoint).blockNumber),
|
|
444
438
|
timestamp: Number(
|
|
445
|
-
decodeCheckpoint(
|
|
439
|
+
decodeCheckpoint(latestCheckpoint).blockTimestamp,
|
|
446
440
|
),
|
|
447
441
|
},
|
|
448
442
|
};
|
|
@@ -566,13 +560,13 @@ const innerType = (
|
|
|
566
560
|
|
|
567
561
|
async function executePluralQuery(
|
|
568
562
|
table: TableRelationalConfig,
|
|
569
|
-
|
|
563
|
+
drizzle: Drizzle<{ [key: string]: OnchainTable }>,
|
|
570
564
|
args: PluralArgs,
|
|
571
565
|
includeTotalCount: boolean,
|
|
572
566
|
extraConditions: (SQL | undefined)[] = [],
|
|
573
567
|
) {
|
|
574
|
-
const rawTable =
|
|
575
|
-
const baseQuery =
|
|
568
|
+
const rawTable = drizzle._.fullSchema[table.tsName];
|
|
569
|
+
const baseQuery = drizzle.query[table.tsName];
|
|
576
570
|
if (rawTable === undefined || baseQuery === undefined)
|
|
577
571
|
throw new Error(`Internal error: Table "${table.tsName}" not found in RQB`);
|
|
578
572
|
|
|
@@ -616,7 +610,7 @@ async function executePluralQuery(
|
|
|
616
610
|
let hasNextPage = false;
|
|
617
611
|
|
|
618
612
|
const totalCountPromise = includeTotalCount
|
|
619
|
-
?
|
|
613
|
+
? drizzle
|
|
620
614
|
.select({ count: count() })
|
|
621
615
|
.from(rawTable)
|
|
622
616
|
.where(and(...whereConditions, ...extraConditions))
|
|
@@ -1019,15 +1013,16 @@ function buildCursorCondition(
|
|
|
1019
1013
|
return buildCondition(0);
|
|
1020
1014
|
}
|
|
1021
1015
|
|
|
1022
|
-
export function buildDataLoaderCache(
|
|
1016
|
+
export function buildDataLoaderCache({
|
|
1017
|
+
drizzle,
|
|
1018
|
+
}: { drizzle: ReadonlyDrizzle<Schema> }) {
|
|
1023
1019
|
const dataLoaderMap = new Map<
|
|
1024
1020
|
TableRelationalConfig,
|
|
1025
1021
|
DataLoader<string, any> | undefined
|
|
1026
1022
|
>();
|
|
1027
1023
|
return ({ table }: { table: TableRelationalConfig }) => {
|
|
1028
|
-
const baseQuery = (
|
|
1029
|
-
table.tsName
|
|
1030
|
-
];
|
|
1024
|
+
const baseQuery = (drizzle as Drizzle<{ [key: string]: OnchainTable }>)
|
|
1025
|
+
.query[table.tsName];
|
|
1031
1026
|
if (baseQuery === undefined)
|
|
1032
1027
|
throw new Error(
|
|
1033
1028
|
`Internal error: Unknown table "${table.tsName}" in data loader cache`,
|
|
@@ -28,7 +28,7 @@ import { buildDataLoaderCache, buildGraphQLSchema } from "./index.js";
|
|
|
28
28
|
*
|
|
29
29
|
*/
|
|
30
30
|
export const graphql = (
|
|
31
|
-
{ schema }: { db: ReadonlyDrizzle<Schema>; schema: Schema },
|
|
31
|
+
{ db, schema }: { db: ReadonlyDrizzle<Schema>; schema: Schema },
|
|
32
32
|
{
|
|
33
33
|
maxOperationTokens = 1000,
|
|
34
34
|
maxOperationDepth = 100,
|
|
@@ -53,11 +53,13 @@ export const graphql = (
|
|
|
53
53
|
graphqlEndpoint: "*", // Disable built-in route validation, use Hono routing instead
|
|
54
54
|
schema: graphqlSchema,
|
|
55
55
|
context: () => {
|
|
56
|
-
const getDataLoader = buildDataLoaderCache(
|
|
57
|
-
globalThis.PONDER_DATABASE.readonlyQB,
|
|
58
|
-
);
|
|
56
|
+
const getDataLoader = buildDataLoaderCache({ drizzle: db });
|
|
59
57
|
|
|
60
|
-
return {
|
|
58
|
+
return {
|
|
59
|
+
getDataLoader,
|
|
60
|
+
getCheckpoints: () => globalThis.PONDER_DATABASE.getCheckpoints(),
|
|
61
|
+
drizzle: db,
|
|
62
|
+
};
|
|
61
63
|
},
|
|
62
64
|
maskedErrors: process.env.NODE_ENV === "production",
|
|
63
65
|
logging: false,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Readable } from "node:stream";
|
|
2
2
|
import { pipeline } from "node:stream/promises";
|
|
3
|
-
import type { QB } from "@/database/queryBuilder.js";
|
|
4
3
|
import { getPrimaryKeyColumns } from "@/drizzle/index.js";
|
|
5
4
|
import { getColumnCasing } from "@/drizzle/kit/index.js";
|
|
6
5
|
import { addErrorMeta, toErrorMeta } from "@/indexing/index.js";
|
|
@@ -9,11 +8,14 @@ import { FlushError } from "@/internal/errors.js";
|
|
|
9
8
|
import type {
|
|
10
9
|
CrashRecoveryCheckpoint,
|
|
11
10
|
Event,
|
|
11
|
+
Schema,
|
|
12
12
|
SchemaBuild,
|
|
13
13
|
} from "@/internal/types.js";
|
|
14
|
+
import type { Drizzle } from "@/types/db.js";
|
|
14
15
|
import { dedupe } from "@/utils/dedupe.js";
|
|
15
16
|
import { prettyPrint } from "@/utils/print.js";
|
|
16
17
|
import { startClock } from "@/utils/timer.js";
|
|
18
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
17
19
|
import {
|
|
18
20
|
type Column,
|
|
19
21
|
type Table,
|
|
@@ -22,13 +24,13 @@ import {
|
|
|
22
24
|
getTableName,
|
|
23
25
|
is,
|
|
24
26
|
or,
|
|
25
|
-
sql,
|
|
26
27
|
} from "drizzle-orm";
|
|
27
28
|
import {
|
|
28
29
|
PgTable,
|
|
29
30
|
type PgTableWithColumns,
|
|
30
31
|
getTableConfig,
|
|
31
32
|
} from "drizzle-orm/pg-core";
|
|
33
|
+
import type { PoolClient } from "pg";
|
|
32
34
|
import copy from "pg-copy-streams";
|
|
33
35
|
import {
|
|
34
36
|
getProfilePatternKey,
|
|
@@ -45,10 +47,11 @@ export type IndexingCache = {
|
|
|
45
47
|
/**
|
|
46
48
|
* Returns the entry for `table` with `key`.
|
|
47
49
|
*/
|
|
48
|
-
get: (params: {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
get: (params: {
|
|
51
|
+
table: Table;
|
|
52
|
+
key: object;
|
|
53
|
+
db: Drizzle<Schema>;
|
|
54
|
+
}) => Row | null | Promise<Row | null>;
|
|
52
55
|
/**
|
|
53
56
|
* Sets the entry for `table` with `key` to `row`.
|
|
54
57
|
*/
|
|
@@ -61,17 +64,27 @@ export type IndexingCache = {
|
|
|
61
64
|
/**
|
|
62
65
|
* Deletes the entry for `table` with `key`.
|
|
63
66
|
*/
|
|
64
|
-
delete: (params: {
|
|
67
|
+
delete: (params: {
|
|
68
|
+
table: Table;
|
|
69
|
+
key: object;
|
|
70
|
+
db: Drizzle<Schema>;
|
|
71
|
+
}) => boolean | Promise<boolean>;
|
|
65
72
|
/**
|
|
66
73
|
* Writes all temporary data to the database.
|
|
67
74
|
*
|
|
68
75
|
* @param params.tableNames - If provided, only flush the tables in the set.
|
|
69
76
|
*/
|
|
70
|
-
flush: (params
|
|
77
|
+
flush: (params: {
|
|
78
|
+
client: PoolClient | PGlite;
|
|
79
|
+
tableNames?: Set<string>;
|
|
80
|
+
}) => Promise<void>;
|
|
71
81
|
/**
|
|
72
82
|
* Predict and load rows that will be accessed in the next event batch.
|
|
73
83
|
*/
|
|
74
|
-
prefetch: (params: {
|
|
84
|
+
prefetch: (params: {
|
|
85
|
+
events: Event[];
|
|
86
|
+
db: Drizzle<Schema>;
|
|
87
|
+
}) => Promise<void>;
|
|
75
88
|
/**
|
|
76
89
|
* Remove spillover and buffer entries.
|
|
77
90
|
*/
|
|
@@ -85,7 +98,6 @@ export type IndexingCache = {
|
|
|
85
98
|
*/
|
|
86
99
|
clear: () => void;
|
|
87
100
|
event: Event | undefined;
|
|
88
|
-
qb: QB;
|
|
89
101
|
};
|
|
90
102
|
|
|
91
103
|
const SAMPLING_RATE = 10;
|
|
@@ -219,15 +231,15 @@ export const getCopyText = (table: Table, rows: Row[]) => {
|
|
|
219
231
|
return results.join("\n");
|
|
220
232
|
};
|
|
221
233
|
|
|
222
|
-
export const getCopyHelper = (
|
|
223
|
-
if (
|
|
234
|
+
export const getCopyHelper = ({ client }: { client: PoolClient | PGlite }) => {
|
|
235
|
+
if (client instanceof PGlite) {
|
|
224
236
|
return async (table: Table, text: string, includeSchema = true) => {
|
|
225
237
|
const target = includeSchema
|
|
226
238
|
? `"${getTableConfig(table).schema ?? "public"}"."${getTableName(
|
|
227
239
|
table,
|
|
228
240
|
)}"`
|
|
229
241
|
: `"${getTableName(table)}"`;
|
|
230
|
-
await
|
|
242
|
+
await client.query(`COPY ${target} FROM '/dev/blob'`, [], {
|
|
231
243
|
blob: new Blob([text]),
|
|
232
244
|
});
|
|
233
245
|
};
|
|
@@ -240,7 +252,7 @@ export const getCopyHelper = (qb: QB) => {
|
|
|
240
252
|
: `"${getTableName(table)}"`;
|
|
241
253
|
await pipeline(
|
|
242
254
|
Readable.from(text),
|
|
243
|
-
|
|
255
|
+
client.query(copy.from(`COPY ${target} FROM STDIN`)),
|
|
244
256
|
);
|
|
245
257
|
};
|
|
246
258
|
}
|
|
@@ -291,7 +303,6 @@ export const createIndexingCache = ({
|
|
|
291
303
|
*/
|
|
292
304
|
let cacheBytes = 0;
|
|
293
305
|
let event: Event | undefined;
|
|
294
|
-
let qb: QB = undefined!;
|
|
295
306
|
let isCacheComplete = crashRecoveryCheckpoint === undefined;
|
|
296
307
|
const primaryKeyCache = new Map<Table, [string, Column][]>();
|
|
297
308
|
|
|
@@ -330,7 +341,7 @@ export const createIndexingCache = ({
|
|
|
330
341
|
updateBuffer.get(table)!.has(ck)
|
|
331
342
|
);
|
|
332
343
|
},
|
|
333
|
-
async get({ table, key }) {
|
|
344
|
+
async get({ table, key, db }) {
|
|
334
345
|
if (event && eventCount[event.name]! % SAMPLING_RATE === 1) {
|
|
335
346
|
if (profile.has(event.name) === false) {
|
|
336
347
|
profile.set(event.name, new Map());
|
|
@@ -402,7 +413,7 @@ export const createIndexingCache = ({
|
|
|
402
413
|
|
|
403
414
|
const endClock = startClock();
|
|
404
415
|
|
|
405
|
-
const result = await
|
|
416
|
+
const result = await db
|
|
406
417
|
.select()
|
|
407
418
|
.from(table)
|
|
408
419
|
.where(getWhereCondition(table, key))
|
|
@@ -443,7 +454,7 @@ export const createIndexingCache = ({
|
|
|
443
454
|
|
|
444
455
|
return row;
|
|
445
456
|
},
|
|
446
|
-
async delete({ table, key }) {
|
|
457
|
+
async delete({ table, key, db }) {
|
|
447
458
|
const ck = getCacheKey(table, key);
|
|
448
459
|
|
|
449
460
|
const inInsertBuffer = insertBuffer.get(table)!.delete(ck);
|
|
@@ -451,7 +462,7 @@ export const createIndexingCache = ({
|
|
|
451
462
|
|
|
452
463
|
cache.get(table)!.delete(ck);
|
|
453
464
|
|
|
454
|
-
const inDb = await
|
|
465
|
+
const inDb = await db
|
|
455
466
|
.delete(table)
|
|
456
467
|
.where(getWhereCondition(table, key))
|
|
457
468
|
.returning()
|
|
@@ -459,8 +470,8 @@ export const createIndexingCache = ({
|
|
|
459
470
|
|
|
460
471
|
return inInsertBuffer || inUpdateBuffer || inDb;
|
|
461
472
|
},
|
|
462
|
-
async flush({ tableNames }
|
|
463
|
-
const copy = getCopyHelper(
|
|
473
|
+
async flush({ client, tableNames }) {
|
|
474
|
+
const copy = getCopyHelper({ client });
|
|
464
475
|
|
|
465
476
|
const shouldRecordBytes = isCacheComplete;
|
|
466
477
|
|
|
@@ -480,7 +491,8 @@ export const createIndexingCache = ({
|
|
|
480
491
|
if (insertValues.length > 0) {
|
|
481
492
|
const endClock = startClock();
|
|
482
493
|
|
|
483
|
-
|
|
494
|
+
// @ts-ignore
|
|
495
|
+
await client.query("SAVEPOINT flush");
|
|
484
496
|
|
|
485
497
|
try {
|
|
486
498
|
const text = getCopyText(
|
|
@@ -496,13 +508,15 @@ export const createIndexingCache = ({
|
|
|
496
508
|
const result = await recoverBatchError(
|
|
497
509
|
insertValues,
|
|
498
510
|
async (values) => {
|
|
499
|
-
|
|
511
|
+
// @ts-ignore
|
|
512
|
+
await client.query("ROLLBACK to flush");
|
|
500
513
|
const text = getCopyText(
|
|
501
514
|
table,
|
|
502
515
|
values.map(({ row }) => row),
|
|
503
516
|
);
|
|
504
517
|
await copy(table, text);
|
|
505
|
-
|
|
518
|
+
// @ts-ignore
|
|
519
|
+
await client.query("SAVEPOINT flush");
|
|
506
520
|
},
|
|
507
521
|
);
|
|
508
522
|
|
|
@@ -615,8 +629,10 @@ export const createIndexingCache = ({
|
|
|
615
629
|
|
|
616
630
|
const endClock = startClock();
|
|
617
631
|
|
|
618
|
-
|
|
619
|
-
await
|
|
632
|
+
// @ts-ignore
|
|
633
|
+
await client.query(createTempTableQuery);
|
|
634
|
+
// @ts-ignore
|
|
635
|
+
await client.query("SAVEPOINT flush");
|
|
620
636
|
|
|
621
637
|
try {
|
|
622
638
|
const text = getCopyText(
|
|
@@ -632,13 +648,15 @@ export const createIndexingCache = ({
|
|
|
632
648
|
const result = await recoverBatchError(
|
|
633
649
|
updateValues,
|
|
634
650
|
async (values) => {
|
|
635
|
-
|
|
651
|
+
// @ts-ignore
|
|
652
|
+
await client.query("ROLLBACK to flush");
|
|
636
653
|
const text = getCopyText(
|
|
637
654
|
table,
|
|
638
655
|
values.map(({ row }) => row),
|
|
639
656
|
);
|
|
640
657
|
await copy(table, text, false);
|
|
641
|
-
|
|
658
|
+
// @ts-ignore
|
|
659
|
+
await client.query("SAVEPOINT flush");
|
|
642
660
|
},
|
|
643
661
|
);
|
|
644
662
|
|
|
@@ -691,7 +709,8 @@ export const createIndexingCache = ({
|
|
|
691
709
|
);
|
|
692
710
|
}
|
|
693
711
|
|
|
694
|
-
|
|
712
|
+
// @ts-ignore
|
|
713
|
+
await client.query(updateQuery);
|
|
695
714
|
|
|
696
715
|
common.metrics.ponder_indexing_cache_query_duration.observe(
|
|
697
716
|
{
|
|
@@ -718,11 +737,12 @@ export const createIndexingCache = ({
|
|
|
718
737
|
}
|
|
719
738
|
|
|
720
739
|
if (insertValues.length > 0 || updateValues.length > 0) {
|
|
721
|
-
|
|
740
|
+
// @ts-ignore
|
|
741
|
+
await client.query("RELEASE flush");
|
|
722
742
|
}
|
|
723
743
|
}
|
|
724
744
|
},
|
|
725
|
-
async prefetch({ events }) {
|
|
745
|
+
async prefetch({ events, db }) {
|
|
726
746
|
if (isCacheComplete) {
|
|
727
747
|
if (cacheBytes < common.options.indexingCacheMaxBytes) {
|
|
728
748
|
return;
|
|
@@ -803,7 +823,7 @@ export const createIndexingCache = ({
|
|
|
803
823
|
if (conditions.length === 0) return;
|
|
804
824
|
const endClock = startClock();
|
|
805
825
|
|
|
806
|
-
await
|
|
826
|
+
await db
|
|
807
827
|
.select()
|
|
808
828
|
.from(table)
|
|
809
829
|
.where(or(...conditions))
|
|
@@ -878,8 +898,5 @@ export const createIndexingCache = ({
|
|
|
878
898
|
set event(_event: Event | undefined) {
|
|
879
899
|
event = _event;
|
|
880
900
|
},
|
|
881
|
-
set qb(_qb: QB) {
|
|
882
|
-
qb = _qb;
|
|
883
|
-
},
|
|
884
901
|
};
|
|
885
902
|
};
|