@rocicorp/zero 0.18.2025032000 → 0.18.2025032002

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.
Files changed (36) hide show
  1. package/out/chunk-KRZMF5WQ.js +6472 -0
  2. package/out/chunk-KRZMF5WQ.js.map +7 -0
  3. package/out/{chunk-6AOMML7A.js → chunk-XTAWYWZ4.js} +3671 -9885
  4. package/out/chunk-XTAWYWZ4.js.map +7 -0
  5. package/out/inspector-SVHRQAQN.js +691 -0
  6. package/out/inspector-SVHRQAQN.js.map +7 -0
  7. package/out/solid.js +6 -6
  8. package/out/solid.js.map +2 -2
  9. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  10. package/out/zero-cache/src/server/syncer.js +1 -1
  11. package/out/zero-cache/src/server/syncer.js.map +1 -1
  12. package/out/zero-cache/src/services/mutagen/pusher.d.ts +3 -1
  13. package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
  14. package/out/zero-cache/src/services/mutagen/pusher.js +14 -4
  15. package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
  16. package/out/zero-client/src/client/inspector/inspector.d.ts +8 -0
  17. package/out/zero-client/src/client/inspector/inspector.d.ts.map +1 -0
  18. package/out/zero-client/src/client/inspector/types.d.ts +34 -0
  19. package/out/zero-client/src/client/inspector/types.d.ts.map +1 -0
  20. package/out/zero-client/src/client/keys.d.ts +1 -0
  21. package/out/zero-client/src/client/keys.d.ts.map +1 -1
  22. package/out/zero-client/src/client/zero.d.ts +6 -0
  23. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  24. package/out/zero-pg/src/custom.d.ts +1 -1
  25. package/out/zero-pg/src/custom.d.ts.map +1 -1
  26. package/out/zero-pg/src/custom.js +3 -6
  27. package/out/zero-pg/src/custom.js.map +1 -1
  28. package/out/zero-pg/src/web.d.ts +7 -4
  29. package/out/zero-pg/src/web.d.ts.map +1 -1
  30. package/out/zero-pg/src/web.js +10 -13
  31. package/out/zero-pg/src/web.js.map +1 -1
  32. package/out/zero.js +2 -1
  33. package/out/zql/src/query/ast-to-zql.d.ts +13 -0
  34. package/out/zql/src/query/ast-to-zql.d.ts.map +1 -0
  35. package/package.json +13 -1
  36. package/out/chunk-6AOMML7A.js.map +0 -7
@@ -0,0 +1,691 @@
1
+ import {
2
+ ENTITIES_KEY_PREFIX,
3
+ Latest,
4
+ SUBQ_PREFIX,
5
+ astSchema,
6
+ clientToServer,
7
+ desiredQueriesPrefixForClient,
8
+ getClient,
9
+ getClientGroup,
10
+ getClientGroups,
11
+ getClients,
12
+ parse,
13
+ readFromHash,
14
+ toGotQueriesKey,
15
+ valita_exports,
16
+ withRead
17
+ } from "./chunk-KRZMF5WQ.js";
18
+ import "./chunk-2B2QE5AT.js";
19
+ import {
20
+ assert,
21
+ must,
22
+ unreachable
23
+ } from "./chunk-2NCGIK6G.js";
24
+ import "./chunk-424PT5DM.js";
25
+
26
+ // ../z2s/src/sql.ts
27
+ import baseSql from "@databases/sql";
28
+ import {
29
+ escapePostgresIdentifier,
30
+ escapeSQLiteIdentifier
31
+ } from "@databases/escape-identifier";
32
+ var pgFormat = {
33
+ escapeIdentifier: (str) => escapePostgresIdentifier(str),
34
+ formatValue: (value, index) => ({ placeholder: `$${index + 1}`, value })
35
+ };
36
+ function formatPg(sql2) {
37
+ return sql2.format(pgFormat);
38
+ }
39
+ var sql = baseSql.default;
40
+
41
+ // ../z2s/src/compiler.ts
42
+ function compile(ast, tables, format) {
43
+ const compiler = new Compiler(tables);
44
+ return compiler.compile(ast, format);
45
+ }
46
+ var Compiler = class {
47
+ #tables;
48
+ #nameMapper;
49
+ constructor(tables) {
50
+ this.#tables = tables;
51
+ this.#nameMapper = clientToServer(tables);
52
+ }
53
+ compile(ast, format) {
54
+ return this.select(ast, format, void 0);
55
+ }
56
+ select(ast, format, correlation) {
57
+ const selectionSet = this.related(ast.related ?? [], format, ast.table);
58
+ const table = this.#tables[ast.table];
59
+ for (const column of Object.keys(table.columns)) {
60
+ selectionSet.push(
61
+ sql`${sql.ident(ast.table)}.${this.#mapColumn(ast.table, column)}`
62
+ );
63
+ }
64
+ return sql`SELECT ${sql.join(selectionSet, ",")} FROM ${this.#mapTable(
65
+ ast.table
66
+ )} ${ast.where ? sql`WHERE ${this.where(ast.where, ast.table)}` : sql``} ${correlation ? sql`${ast.where ? sql`AND` : sql`WHERE`} (${correlation})` : sql``} ${this.orderBy(ast.orderBy, ast.table)} ${format?.singular ? this.limit(1) : this.limit(ast.limit)}`;
67
+ }
68
+ orderBy(orderBy, table) {
69
+ if (!orderBy) {
70
+ return sql``;
71
+ }
72
+ return sql`ORDER BY ${sql.join(
73
+ orderBy.map(
74
+ ([col, dir]) => dir === "asc" ? (
75
+ // Oh postgres. The table must be referred to be client name but the column by server name.
76
+ // E.g., `SELECT server_col as client_col FROM server_table as client_table ORDER BY client_Table.server_col`
77
+ sql`${sql.ident(table)}.${this.#mapColumnNoAlias(table, col)} ASC`
78
+ ) : sql`${sql.ident(table)}.${this.#mapColumnNoAlias(table, col)} DESC`
79
+ ),
80
+ ", "
81
+ )}`;
82
+ }
83
+ limit(limit) {
84
+ if (!limit) {
85
+ return sql``;
86
+ }
87
+ return sql`LIMIT ${sql.value(limit)}`;
88
+ }
89
+ related(relationships, format, parentTable) {
90
+ return relationships.map(
91
+ (relationship) => this.relationshipSubquery(
92
+ relationship,
93
+ format?.relationships[must(relationship.subquery.alias)],
94
+ parentTable
95
+ )
96
+ );
97
+ }
98
+ relationshipSubquery(relationship, format, parentTable) {
99
+ if (relationship.hidden) {
100
+ const [join, lastAlias, lastLimit, lastTable] = this.makeJunctionJoin(relationship);
101
+ const lastClientColumns = Object.keys(this.#tables[lastTable].columns);
102
+ return sql`(
103
+ SELECT ${format?.singular ? sql`` : sql`COALESCE(array_agg`}(row_to_json(${sql.ident(`inner_${relationship.subquery.alias}`)})) ${format?.singular ? sql`` : sql`, ARRAY[]::json[])`} FROM (SELECT ${sql.join(
104
+ lastClientColumns.map(
105
+ (c) => sql`${sql.ident(lastAlias)}.${this.#mapColumn(lastTable, c)}`
106
+ ),
107
+ ","
108
+ )} FROM ${join} WHERE (${this.correlate(
109
+ parentTable,
110
+ parentTable,
111
+ relationship.correlation.parentField,
112
+ relationship.subquery.table,
113
+ relationship.subquery.table,
114
+ relationship.correlation.childField
115
+ )}) ${relationship.subquery.where ? sql`AND ${this.where(
116
+ relationship.subquery.where,
117
+ relationship.subquery.table
118
+ )}` : sql``} ${this.orderBy(
119
+ relationship.subquery.orderBy,
120
+ relationship.subquery.table
121
+ )} ${format?.singular ? this.limit(1) : this.limit(lastLimit)} ) ${sql.ident(`inner_${relationship.subquery.alias}`)}
122
+ ) as ${sql.ident(relationship.subquery.alias)}`;
123
+ }
124
+ return sql`(
125
+ SELECT ${format?.singular ? sql`` : sql`COALESCE(array_agg`}(row_to_json(${sql.ident(`inner_${relationship.subquery.alias}`)})) ${format?.singular ? sql`` : sql`, ARRAY[]::json[])`} FROM (${this.select(
126
+ relationship.subquery,
127
+ format,
128
+ this.correlate(
129
+ parentTable,
130
+ parentTable,
131
+ relationship.correlation.parentField,
132
+ relationship.subquery.table,
133
+ relationship.subquery.table,
134
+ relationship.correlation.childField
135
+ )
136
+ )}) ${sql.ident(`inner_${relationship.subquery.alias}`)}
137
+ ) as ${sql.ident(relationship.subquery.alias)}`;
138
+ }
139
+ pullTablesForJunction(relationship, tables = []) {
140
+ tables.push([
141
+ relationship.subquery.table,
142
+ relationship.correlation,
143
+ relationship.subquery.limit
144
+ ]);
145
+ assert(
146
+ relationship.subquery.related?.length || 0 <= 1,
147
+ "Too many related tables for a junction edge"
148
+ );
149
+ for (const subRelationship of relationship.subquery.related ?? []) {
150
+ this.pullTablesForJunction(subRelationship, tables);
151
+ }
152
+ return tables;
153
+ }
154
+ makeJunctionJoin(relationship) {
155
+ const participatingTables = this.pullTablesForJunction(relationship);
156
+ const joins = [];
157
+ function alias(index) {
158
+ if (index === 0) {
159
+ return participatingTables[0][0];
160
+ }
161
+ return `table_${index}`;
162
+ }
163
+ for (const [table, _correlation] of participatingTables) {
164
+ if (joins.length === 0) {
165
+ joins.push(this.#mapTable(table));
166
+ continue;
167
+ }
168
+ joins.push(
169
+ sql` JOIN ${this.#mapTableNoAlias(table)} as ${sql.ident(
170
+ alias(joins.length)
171
+ )} ON ${this.correlate(
172
+ participatingTables[joins.length - 1][0],
173
+ alias(joins.length - 1),
174
+ participatingTables[joins.length][1].parentField,
175
+ participatingTables[joins.length][0],
176
+ alias(joins.length),
177
+ participatingTables[joins.length][1].childField
178
+ )}`
179
+ );
180
+ }
181
+ return [
182
+ sql.join(joins, ""),
183
+ alias(joins.length - 1),
184
+ participatingTables[participatingTables.length - 1][2],
185
+ participatingTables[participatingTables.length - 1][0]
186
+ ];
187
+ }
188
+ where(condition, table) {
189
+ if (!condition) {
190
+ return sql``;
191
+ }
192
+ switch (condition.type) {
193
+ case "and":
194
+ return sql`(${sql.join(
195
+ condition.conditions.map((c) => this.where(c, table)),
196
+ " AND "
197
+ )})`;
198
+ case "or":
199
+ return sql`(${sql.join(
200
+ condition.conditions.map((c) => this.where(c, table)),
201
+ " OR "
202
+ )})`;
203
+ case "correlatedSubquery":
204
+ return this.exists(condition, table);
205
+ case "simple":
206
+ return this.simple(condition, table);
207
+ }
208
+ }
209
+ simple(condition, table) {
210
+ switch (condition.op) {
211
+ case "!=":
212
+ case "<":
213
+ case "<=":
214
+ case "=":
215
+ case ">":
216
+ case ">=":
217
+ case "ILIKE":
218
+ case "LIKE":
219
+ case "NOT ILIKE":
220
+ case "NOT LIKE":
221
+ return sql`${this.valuePosition(
222
+ condition.left,
223
+ table
224
+ )} ${sql.__dangerous__rawValue(condition.op)} ${this.valuePosition(
225
+ condition.right,
226
+ table
227
+ )}`;
228
+ case "NOT IN":
229
+ case "IN":
230
+ return this.any(condition, table);
231
+ case "IS":
232
+ case "IS NOT":
233
+ return this.distinctFrom(condition, table);
234
+ }
235
+ }
236
+ distinctFrom(condition, table) {
237
+ return sql`${this.valuePosition(condition.left, table)} ${condition.op === "IS" ? sql`IS NOT DISTINCT FROM` : sql`IS DISTINCT FROM`} ${this.valuePosition(condition.right, table)}`;
238
+ }
239
+ any(condition, table) {
240
+ return sql`${this.valuePosition(condition.left, table)} ${condition.op === "IN" ? sql`= ANY` : sql`!= ANY`} (${this.valuePosition(condition.right, table)})`;
241
+ }
242
+ valuePosition(value, table) {
243
+ switch (value.type) {
244
+ case "column":
245
+ return this.#mapColumnNoAlias(table, value.name);
246
+ case "literal":
247
+ return sql.value(value.value);
248
+ case "static":
249
+ throw new Error(
250
+ "Static parameters must be bound to a value before compiling to SQL"
251
+ );
252
+ }
253
+ }
254
+ exists(condition, parentTable) {
255
+ switch (condition.op) {
256
+ case "EXISTS":
257
+ return sql`EXISTS (${this.select(
258
+ condition.related.subquery,
259
+ void 0,
260
+ this.correlate(
261
+ parentTable,
262
+ parentTable,
263
+ condition.related.correlation.parentField,
264
+ condition.related.subquery.table,
265
+ condition.related.subquery.table,
266
+ condition.related.correlation.childField
267
+ )
268
+ )})`;
269
+ case "NOT EXISTS":
270
+ return sql`NOT EXISTS (${this.select(
271
+ condition.related.subquery,
272
+ void 0,
273
+ void 0
274
+ )})`;
275
+ }
276
+ }
277
+ correlate(parentTable, parentTableAlias, parentColumns, childTable, childTableAlias, childColumns) {
278
+ return sql.join(
279
+ zip(parentColumns, childColumns).map(
280
+ ([parentColumn, childColumn]) => sql`${sql.ident(parentTableAlias)}.${this.#mapColumnNoAlias(
281
+ parentTable,
282
+ parentColumn
283
+ )} = ${sql.ident(childTableAlias)}.${this.#mapColumnNoAlias(
284
+ childTable,
285
+ childColumn
286
+ )}`
287
+ ),
288
+ " AND "
289
+ );
290
+ }
291
+ #mapColumn(table, column) {
292
+ const mapped = this.#nameMapper.columnName(table, column);
293
+ if (mapped === column) {
294
+ return sql.ident(column);
295
+ }
296
+ return sql`${sql.ident(mapped)} as ${sql.ident(column)}`;
297
+ }
298
+ #mapColumnNoAlias(table, column) {
299
+ const mapped = this.#nameMapper.columnName(table, column);
300
+ return sql.ident(mapped);
301
+ }
302
+ #mapTable(table) {
303
+ const mapped = this.#nameMapper.tableName(table);
304
+ if (mapped === table) {
305
+ return sql.ident(table);
306
+ }
307
+ return sql`${sql.ident(mapped)} as ${sql.ident(table)}`;
308
+ }
309
+ #mapTableNoAlias(table) {
310
+ const mapped = this.#nameMapper.tableName(table);
311
+ return sql.ident(mapped);
312
+ }
313
+ };
314
+ function zip(a1, a2) {
315
+ assert(a1.length === a2.length);
316
+ const result = [];
317
+ for (let i = 0; i < a1.length; i++) {
318
+ result.push([a1[i], a2[i]]);
319
+ }
320
+ return result;
321
+ }
322
+
323
+ // ../zql/src/query/ast-to-zql.ts
324
+ function astToZQL(ast) {
325
+ let code = "";
326
+ if (ast.where) {
327
+ code += transformCondition(ast.where, ".where", /* @__PURE__ */ new Set());
328
+ }
329
+ if (ast.related && ast.related.length > 0) {
330
+ for (const related of ast.related) {
331
+ if (!related.hidden) {
332
+ code += transformRelated(related);
333
+ }
334
+ }
335
+ }
336
+ if (ast.orderBy && ast.orderBy.length > 0) {
337
+ code += transformOrder(ast.orderBy);
338
+ }
339
+ if (ast.limit !== void 0) {
340
+ code += `.limit(${ast.limit})`;
341
+ }
342
+ if (ast.start) {
343
+ const { row, exclusive } = ast.start;
344
+ code += `.start(${JSON.stringify(row)}${exclusive ? "" : ", { inclusive: true }"})`;
345
+ }
346
+ return code;
347
+ }
348
+ function transformCondition(condition, prefix, args) {
349
+ switch (condition.type) {
350
+ case "simple":
351
+ return transformSimpleCondition(condition, prefix);
352
+ case "and":
353
+ case "or":
354
+ return transformLogicalCondition(condition, prefix, args);
355
+ case "correlatedSubquery":
356
+ return transformExistsCondition(condition, prefix, args);
357
+ default:
358
+ unreachable(condition);
359
+ }
360
+ }
361
+ function transformSimpleCondition(condition, prefix) {
362
+ const { left, op, right } = condition;
363
+ const leftCode = transformValuePosition(left);
364
+ const rightCode = transformValuePosition(right);
365
+ if (op === "=") {
366
+ return `${prefix}(${leftCode}, ${rightCode})`;
367
+ }
368
+ return `${prefix}(${leftCode}, '${op}', ${rightCode})`;
369
+ }
370
+ function transformLogicalCondition(condition, prefix, args) {
371
+ const { type, conditions } = condition;
372
+ if (conditions.length === 1) {
373
+ return transformCondition(conditions[0], prefix, args);
374
+ }
375
+ if (type === "and") {
376
+ const parts = conditions.map((c) => transformCondition(c, prefix, args));
377
+ if (prefix === ".where") {
378
+ return parts.join("");
379
+ }
380
+ args.add("and");
381
+ return "and(" + parts.join(", ") + ")";
382
+ }
383
+ args = /* @__PURE__ */ new Set();
384
+ const conditionsCode = conditions.map((c) => transformCondition(c, "cmp", args)).join(", ");
385
+ args.add("cmp");
386
+ args.add(type);
387
+ const argsCode = [...args].sort().join(", ");
388
+ return `.where(({${argsCode}}) => ${type}(${conditionsCode}))`;
389
+ }
390
+ function transformExistsCondition(condition, prefix, args) {
391
+ const { related, op } = condition;
392
+ const relationship = extractRelationshipName(related);
393
+ const hasSubQueryProps = related.subquery.where || related.subquery.related && related.subquery.related.length > 0 || related.subquery.orderBy || related.subquery.limit;
394
+ if (op === "EXISTS") {
395
+ if (!hasSubQueryProps) {
396
+ if (prefix === ".where") {
397
+ return `.whereExists('${relationship}')`;
398
+ }
399
+ args.add("exists");
400
+ return `exists('${relationship}')`;
401
+ }
402
+ if (prefix === ".where") {
403
+ return `.whereExists('${relationship}', q => q${astToZQL(
404
+ related.subquery
405
+ )}))`;
406
+ }
407
+ prefix;
408
+ args.add("exists");
409
+ return `exists('${relationship}', q => q${astToZQL(related.subquery)})`;
410
+ }
411
+ op;
412
+ if (hasSubQueryProps) {
413
+ if (prefix === ".where") {
414
+ return `.where(({exists, not}) => not(exists('${relationship}', q => q${astToZQL(
415
+ related.subquery
416
+ )})))`;
417
+ }
418
+ prefix;
419
+ args.add("not");
420
+ args.add("exists");
421
+ return `not(exists('${relationship}', q => q${astToZQL(
422
+ related.subquery
423
+ )}))`;
424
+ }
425
+ if (prefix === ".where") {
426
+ return `.where(({exists, not}) => not(exists('${relationship}')))`;
427
+ }
428
+ args.add("not");
429
+ args.add("exists");
430
+ return `not(exists('${relationship}')))`;
431
+ }
432
+ function extractRelationshipName(related) {
433
+ const alias = must(related.subquery.alias);
434
+ return alias.startsWith(SUBQ_PREFIX) ? alias.substring(SUBQ_PREFIX.length) : alias;
435
+ }
436
+ function transformRelated(related) {
437
+ const { alias } = related.subquery;
438
+ if (!alias) return "";
439
+ const relationship = alias;
440
+ let code = `.related('${relationship}'`;
441
+ if (related.subquery.where || related.subquery.related && related.subquery.related.length > 0 || related.subquery.orderBy || related.subquery.limit) {
442
+ code += ", q => q" + astToZQL(related.subquery);
443
+ }
444
+ code += ")";
445
+ return code;
446
+ }
447
+ function transformOrder(orderBy) {
448
+ let code = "";
449
+ for (const [field, direction] of orderBy) {
450
+ code += `.orderBy('${field}', '${direction}')`;
451
+ }
452
+ return code;
453
+ }
454
+ function transformValuePosition(value) {
455
+ switch (value.type) {
456
+ case "literal":
457
+ return transformLiteral(value);
458
+ case "column":
459
+ return `'${value.name}'`;
460
+ case "static":
461
+ return transformParameter(value);
462
+ default:
463
+ unreachable(value);
464
+ }
465
+ }
466
+ function transformLiteral(literal) {
467
+ if (literal.value === null) {
468
+ return "null";
469
+ }
470
+ if (Array.isArray(literal.value)) {
471
+ return JSON.stringify(literal.value);
472
+ }
473
+ if (typeof literal.value === "string") {
474
+ return `'${literal.value.replace(/'/g, "\\'")}'`;
475
+ }
476
+ return String(literal.value);
477
+ }
478
+ function transformParameter(param) {
479
+ const fieldStr = Array.isArray(param.field) ? `[${param.field.map((f) => `'${f}'`).join(", ")}]` : `'${param.field}'`;
480
+ return `authParam(${fieldStr})`;
481
+ }
482
+
483
+ // ../zero-client/src/client/inspector/inspector.ts
484
+ async function newInspector(rep, schema) {
485
+ const clientGroupID = await rep.clientGroupID;
486
+ return new Inspector(rep, schema, rep.clientID, clientGroupID);
487
+ }
488
+ var Inspector = class {
489
+ #rep;
490
+ client;
491
+ clientGroup;
492
+ #schema;
493
+ constructor(rep, schema, clientID, clientGroupID) {
494
+ this.#rep = rep;
495
+ this.#schema = schema;
496
+ this.client = new Client(rep, schema, clientID, clientGroupID);
497
+ this.clientGroup = this.client.clientGroup;
498
+ }
499
+ clients() {
500
+ return withDagRead(
501
+ this.#rep,
502
+ (dagRead) => clients(this.#rep, this.#schema, dagRead)
503
+ );
504
+ }
505
+ clientsWithQueries() {
506
+ return withDagRead(
507
+ this.#rep,
508
+ (dagRead) => clientsWithQueries(this.#rep, this.#schema, dagRead)
509
+ );
510
+ }
511
+ clientGroups() {
512
+ return withDagRead(this.#rep, async (dagRead) => {
513
+ const clientGroups = await getClientGroups(dagRead);
514
+ return [...clientGroups.keys()].map(
515
+ (clientGroupID) => new ClientGroup(this.#rep, this.#schema, clientGroupID)
516
+ );
517
+ });
518
+ }
519
+ };
520
+ var Client = class {
521
+ #rep;
522
+ id;
523
+ clientGroup;
524
+ #schema;
525
+ constructor(rep, schema, id, clientGroupID) {
526
+ this.#rep = rep;
527
+ this.#schema = schema;
528
+ this.id = id;
529
+ this.clientGroup = new ClientGroup(rep, schema, clientGroupID);
530
+ }
531
+ queries() {
532
+ return withDagRead(this.#rep, async (dagRead) => {
533
+ const prefix = desiredQueriesPrefixForClient(this.id);
534
+ const tree = await getBTree(dagRead, this.id);
535
+ const qs = [];
536
+ for await (const [key, value] of tree.scan(prefix)) {
537
+ if (!key.startsWith(prefix)) {
538
+ break;
539
+ }
540
+ const hash = key.substring(prefix.length);
541
+ const got = await tree.has(toGotQueriesKey(hash));
542
+ const q = new Query(
543
+ hash,
544
+ parse(value, astSchema),
545
+ got,
546
+ this.#schema
547
+ );
548
+ qs.push(q);
549
+ }
550
+ return qs;
551
+ });
552
+ }
553
+ map() {
554
+ return withDagRead(this.#rep, async (dagRead) => {
555
+ const tree = await getBTree(dagRead, this.id);
556
+ const map = /* @__PURE__ */ new Map();
557
+ for await (const [key, value] of tree.scan("")) {
558
+ map.set(key, value);
559
+ }
560
+ return map;
561
+ });
562
+ }
563
+ rows(tableName) {
564
+ return withDagRead(this.#rep, async (dagRead) => {
565
+ const prefix = ENTITIES_KEY_PREFIX + tableName;
566
+ const tree = await getBTree(dagRead, this.id);
567
+ const rows = [];
568
+ for await (const [key, value] of tree.scan(prefix)) {
569
+ if (!key.startsWith(prefix)) {
570
+ break;
571
+ }
572
+ rows.push(value);
573
+ }
574
+ return rows;
575
+ });
576
+ }
577
+ };
578
+ var ClientGroup = class {
579
+ #rep;
580
+ id;
581
+ #schema;
582
+ constructor(rep, schema, id) {
583
+ this.#rep = rep;
584
+ this.#schema = schema;
585
+ this.id = id;
586
+ }
587
+ clients() {
588
+ return withDagRead(
589
+ this.#rep,
590
+ (dagRead) => clients(
591
+ this.#rep,
592
+ this.#schema,
593
+ dagRead,
594
+ ([_, v]) => v.clientGroupID === this.id
595
+ )
596
+ );
597
+ }
598
+ clientsWithQueries() {
599
+ return withDagRead(
600
+ this.#rep,
601
+ (dagRead) => clientsWithQueries(
602
+ this.#rep,
603
+ this.#schema,
604
+ dagRead,
605
+ ([_, v]) => v.clientGroupID === this.id
606
+ )
607
+ );
608
+ }
609
+ queries() {
610
+ return withDagRead(this.#rep, async (dagRead) => {
611
+ const cs = await clients(
612
+ this.#rep,
613
+ this.#schema,
614
+ dagRead,
615
+ ([_, v]) => v.clientGroupID === this.id
616
+ );
617
+ const qs = /* @__PURE__ */ new Map();
618
+ await Promise.all(
619
+ cs.map(async (client) => {
620
+ const clientQueries = await client.queries();
621
+ clientQueries.forEach((q) => qs.set(q.id, q));
622
+ })
623
+ );
624
+ return [...qs.values()];
625
+ });
626
+ }
627
+ };
628
+ async function withDagRead(rep, f) {
629
+ await rep.refresh();
630
+ await rep.persist();
631
+ return withRead(rep.perdag, f);
632
+ }
633
+ async function getBTree(dagRead, clientID) {
634
+ const client = await getClient(clientID, dagRead);
635
+ assert(client, `Client not found: ${clientID}`);
636
+ const { clientGroupID } = client;
637
+ const clientGroup = await getClientGroup(clientGroupID, dagRead);
638
+ assert(clientGroup, `Client group not found: ${clientGroupID}`);
639
+ const dbRead = await readFromHash(
640
+ clientGroup.headHash,
641
+ dagRead,
642
+ Latest
643
+ );
644
+ return dbRead.map;
645
+ }
646
+ async function clients(rep, schema, dagRead, predicate = () => true) {
647
+ const clients2 = await getClients(dagRead);
648
+ return [...clients2.entries()].filter(predicate).map(
649
+ ([clientID, { clientGroupID }]) => new Client(rep, schema, clientID, clientGroupID)
650
+ );
651
+ }
652
+ async function clientsWithQueries(rep, schema, dagRead, predicate = () => true) {
653
+ const allClients = await clients(rep, schema, dagRead, predicate);
654
+ const clientsWithQueries2 = [];
655
+ await Promise.all(
656
+ allClients.map(async (client) => {
657
+ const queries = await client.queries();
658
+ if (queries.length > 0) {
659
+ clientsWithQueries2.push(client);
660
+ }
661
+ })
662
+ );
663
+ return clientsWithQueries2;
664
+ }
665
+ var Query = class {
666
+ id;
667
+ ast;
668
+ got;
669
+ #schema;
670
+ constructor(id, ast, got, schema) {
671
+ this.id = id;
672
+ this.ast = ast;
673
+ this.got = got;
674
+ this.#schema = schema;
675
+ }
676
+ get sql() {
677
+ const format = {
678
+ singular: false,
679
+ relationships: {}
680
+ };
681
+ const sqlQuery = formatPg(compile(this.ast, this.#schema.tables, format));
682
+ return sqlQuery.text;
683
+ }
684
+ get zql() {
685
+ return this.ast.table + astToZQL(this.ast);
686
+ }
687
+ };
688
+ export {
689
+ newInspector
690
+ };
691
+ //# sourceMappingURL=inspector-SVHRQAQN.js.map