@syncular/server 0.47.0 → 0.49.1

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/src/push.ts CHANGED
@@ -1110,6 +1110,34 @@ function commitValidationReader(
1110
1110
  serverVersion: stored.serverVersion,
1111
1111
  }));
1112
1112
  },
1113
+ scanRowsByIndex: async ({
1114
+ table: tableName,
1115
+ index,
1116
+ values,
1117
+ afterRowId = null,
1118
+ limit = 100,
1119
+ }) => {
1120
+ const table = tableFor(tableName);
1121
+ if (tx.scanRowsByIndex === undefined) {
1122
+ throw new Error(
1123
+ 'storage transaction does not support commit-validator index scans',
1124
+ );
1125
+ }
1126
+ const rows = await tx.scanRowsByIndex({
1127
+ table: tableName,
1128
+ index,
1129
+ values,
1130
+ afterRowId,
1131
+ limit,
1132
+ });
1133
+ return rows.map((stored) => ({
1134
+ row: toValidateRow(
1135
+ table.columns,
1136
+ decodeRow(table.columns, stored.payload),
1137
+ ),
1138
+ serverVersion: stored.serverVersion,
1139
+ }));
1140
+ },
1113
1141
  queryAuthoritative: (query) => transactionQuery(tx, query),
1114
1142
  };
1115
1143
  }
@@ -1268,6 +1296,32 @@ async function enforceReferences(
1268
1296
  });
1269
1297
  }
1270
1298
 
1299
+ // One candidate-state read per parent table, not one per reference: a
1300
+ // commit that points several rows at the same parent (a fight's
1301
+ // combatants, its chronicle) reads that parent once. Postgres binds the
1302
+ // ids as one array per table; SQLite and D1 answer with one lookup per id.
1303
+ const parentIds = new Map<string, Set<string>>();
1304
+ for (const { last: write } of finalByRow.values()) {
1305
+ if (write.op !== 'upsert' || write.values === undefined) continue;
1306
+ for (const reference of write.table.references) {
1307
+ const value = write.values[reference.columnIndex];
1308
+ if (value === null || value === undefined) continue;
1309
+ const parentRowId = renderScopeValue(value);
1310
+ if (parentRowId === undefined) continue;
1311
+ const ids = parentIds.get(reference.parentTable);
1312
+ if (ids === undefined)
1313
+ parentIds.set(reference.parentTable, new Set([parentRowId]));
1314
+ else ids.add(parentRowId);
1315
+ }
1316
+ }
1317
+ const parents = new Map<string, ReadonlyMap<string, StoredRow>>();
1318
+ for (const [parentTable, ids] of parentIds) {
1319
+ parents.set(
1320
+ parentTable,
1321
+ await readCandidateRows(tx, parentTable, [...ids]),
1322
+ );
1323
+ }
1324
+
1271
1325
  for (const { last: write } of finalByRow.values()) {
1272
1326
  if (write.op !== 'upsert' || write.values === undefined) continue;
1273
1327
  for (const reference of write.table.references) {
@@ -1275,7 +1329,7 @@ async function enforceReferences(
1275
1329
  if (value === null || value === undefined) continue;
1276
1330
  const parentRowId = renderScopeValue(value);
1277
1331
  if (parentRowId === undefined) continue;
1278
- if ((await tx.getRow(reference.parentTable, parentRowId)) !== undefined) {
1332
+ if (parents.get(reference.parentTable)?.has(parentRowId) === true) {
1279
1333
  continue;
1280
1334
  }
1281
1335
  return {
package/src/validate.ts CHANGED
@@ -97,6 +97,22 @@ export interface CommitValidationScanInput {
97
97
  readonly limit?: number;
98
98
  }
99
99
 
100
+ /**
101
+ * One exact declared-index lookup a push-transaction reader runs (§6.8): the
102
+ * whole-commit reader and a registered command share this shape.
103
+ */
104
+ export interface IndexScanInput {
105
+ readonly table: string;
106
+ /** `TableSchema.indexes[].name`; never exposed as a client subscription. */
107
+ readonly index: string;
108
+ /** Exact values in the index declaration's column order. */
109
+ readonly values: readonly RowValue[];
110
+ /** Resume after this rowId (exclusive); `null` = start of the match set. */
111
+ readonly afterRowId?: string | null;
112
+ /** Bounded per call by the server to 1..1,000; defaults to 100. */
113
+ readonly limit?: number;
114
+ }
115
+
100
116
  /**
101
117
  * Candidate-state reads bound to the same storage transaction as the commit.
102
118
  * Reads observe every staged sibling operation and no uncommitted competing
@@ -120,6 +136,16 @@ export interface CommitValidationReader {
120
136
  rowIds: readonly string[],
121
137
  ): Promise<ReadonlyMap<string, CommitValidationRow>>;
122
138
  scanRows(input: CommitValidationScanInput): Promise<CommitValidationRow[]>;
139
+ /**
140
+ * Candidate-state exact lookup through one declared relational index, with
141
+ * the same read-your-own-writes guarantee as `scanRows`: staged sibling
142
+ * upserts are present and staged deletes are absent. The values form the
143
+ * complete, order-sensitive index tuple and `afterRowId` is an exclusive
144
+ * keyset cursor, so a caller pages without a scope declaration. There is no
145
+ * scope filter and no actor scope authorization: the caller already holds
146
+ * partition authority, exactly as with `scanRows`.
147
+ */
148
+ scanRowsByIndex(input: IndexScanInput): Promise<CommitValidationRow[]>;
123
149
  /**
124
150
  * Run a generated registered query on the commit transaction (§6.8). The
125
151
  * request has the shape `ServerStorage.queryAuthoritative` takes; the