primitive-admin 1.2.0-alpha.3 → 1.2.0-alpha.5

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 (143) hide show
  1. package/assets/skill/skills/primitive-platform/SKILL.md +17 -0
  2. package/dist/src/commands/agent-sessions.js +4 -0
  3. package/dist/src/commands/agent-sessions.js.map +1 -1
  4. package/dist/src/commands/auth.js +7 -4
  5. package/dist/src/commands/auth.js.map +1 -1
  6. package/dist/src/commands/connections.js +11 -12
  7. package/dist/src/commands/connections.js.map +1 -1
  8. package/dist/src/commands/database-type-configs.js +2 -76
  9. package/dist/src/commands/database-type-configs.js.map +1 -1
  10. package/dist/src/commands/database-types.js +313 -13
  11. package/dist/src/commands/database-types.js.map +1 -1
  12. package/dist/src/commands/databases.d.ts +9 -0
  13. package/dist/src/commands/databases.js +78 -403
  14. package/dist/src/commands/databases.js.map +1 -1
  15. package/dist/src/commands/documents.d.ts +32 -2
  16. package/dist/src/commands/documents.js +344 -711
  17. package/dist/src/commands/documents.js.map +1 -1
  18. package/dist/src/commands/functions.js +2 -2
  19. package/dist/src/commands/functions.js.map +1 -1
  20. package/dist/src/commands/llm.js +6 -9
  21. package/dist/src/commands/llm.js.map +1 -1
  22. package/dist/src/commands/prompts.d.ts +7 -0
  23. package/dist/src/commands/prompts.js +44 -0
  24. package/dist/src/commands/prompts.js.map +1 -1
  25. package/dist/src/commands/settings.d.ts +9 -12
  26. package/dist/src/commands/settings.js +79 -31
  27. package/dist/src/commands/settings.js.map +1 -1
  28. package/dist/src/commands/sync.d.ts +43 -110
  29. package/dist/src/commands/sync.js +356 -1251
  30. package/dist/src/commands/sync.js.map +1 -1
  31. package/dist/src/lib/aggregate-output.d.ts +25 -0
  32. package/dist/src/lib/aggregate-output.js +53 -0
  33. package/dist/src/lib/aggregate-output.js.map +1 -0
  34. package/dist/src/lib/api-client.d.ts +48 -204
  35. package/dist/src/lib/api-client.js +57 -321
  36. package/dist/src/lib/api-client.js.map +1 -1
  37. package/dist/src/lib/codegen-shared/generatedFiles.d.ts +6 -7
  38. package/dist/src/lib/codegen-shared/generatedFiles.js +2 -2
  39. package/dist/src/lib/codegen-shared/generatedFiles.js.map +1 -1
  40. package/dist/src/lib/codegen-shared/prettierStable.d.ts +1 -1
  41. package/dist/src/lib/codegen-shared/prettierStable.js +1 -1
  42. package/dist/src/lib/codegen-shared/resolveCodegenSourceDir.d.ts +2 -2
  43. package/dist/src/lib/codegen-shared/resolveCodegenSourceDir.js +2 -2
  44. package/dist/src/lib/config-object-descriptor.js +15 -10
  45. package/dist/src/lib/config-object-descriptor.js.map +1 -1
  46. package/dist/src/lib/config-surface.d.ts +2 -1
  47. package/dist/src/lib/config-surface.js +4 -1
  48. package/dist/src/lib/config-surface.js.map +1 -1
  49. package/dist/src/lib/connections-list.d.ts +10 -0
  50. package/dist/src/lib/connections-list.js +18 -0
  51. package/dist/src/lib/connections-list.js.map +1 -0
  52. package/dist/src/lib/csv.d.ts +8 -9
  53. package/dist/src/lib/csv.js +3 -4
  54. package/dist/src/lib/csv.js.map +1 -1
  55. package/dist/src/lib/db-codegen/dbGenerator.js +5 -16
  56. package/dist/src/lib/db-codegen/dbGenerator.js.map +1 -1
  57. package/dist/src/lib/db-codegen/dbNaming.d.ts +5 -13
  58. package/dist/src/lib/db-codegen/dbNaming.js +5 -17
  59. package/dist/src/lib/db-codegen/dbNaming.js.map +1 -1
  60. package/dist/src/lib/db-codegen/dbTemplates.d.ts +7 -56
  61. package/dist/src/lib/db-codegen/dbTemplates.js +25 -125
  62. package/dist/src/lib/db-codegen/dbTemplates.js.map +1 -1
  63. package/dist/src/lib/db-codegen/dbTsTypes.d.ts +2 -2
  64. package/dist/src/lib/db-codegen/dbTsTypes.js +2 -2
  65. package/dist/src/lib/db-codegen/dbTypeIR.d.ts +60 -2
  66. package/dist/src/lib/db-codegen/dbTypeIR.js +12 -1
  67. package/dist/src/lib/db-codegen/dbTypeIR.js.map +1 -1
  68. package/dist/src/lib/document-export-record-log.d.ts +57 -0
  69. package/dist/src/lib/document-export-record-log.js +215 -0
  70. package/dist/src/lib/document-export-record-log.js.map +1 -0
  71. package/dist/src/lib/document-ingest-artifact.d.ts +13 -2
  72. package/dist/src/lib/document-ingest-artifact.js +7 -3
  73. package/dist/src/lib/document-ingest-artifact.js.map +1 -1
  74. package/dist/src/lib/document-ingest-rows.d.ts +16 -0
  75. package/dist/src/lib/document-ingest-rows.js +17 -0
  76. package/dist/src/lib/document-ingest-rows.js.map +1 -1
  77. package/dist/src/lib/document-ingest.d.ts +22 -0
  78. package/dist/src/lib/document-ingest.js +33 -0
  79. package/dist/src/lib/document-ingest.js.map +1 -1
  80. package/dist/src/lib/function-sync.d.ts +3 -16
  81. package/dist/src/lib/function-sync.js +14 -18
  82. package/dist/src/lib/function-sync.js.map +1 -1
  83. package/dist/src/lib/function-triggers.d.ts +3 -15
  84. package/dist/src/lib/function-triggers.js +10 -72
  85. package/dist/src/lib/function-triggers.js.map +1 -1
  86. package/dist/src/lib/generated-allowlist.js +0 -20
  87. package/dist/src/lib/generated-allowlist.js.map +1 -1
  88. package/dist/src/lib/generated-config-surfaces.d.ts +212 -49
  89. package/dist/src/lib/generated-config-surfaces.js +759 -184
  90. package/dist/src/lib/generated-config-surfaces.js.map +1 -1
  91. package/dist/src/lib/generated-sdk-types.d.ts +1 -1
  92. package/dist/src/lib/generated-sdk-types.js +1 -1
  93. package/dist/src/lib/generated-sdk-types.js.map +1 -1
  94. package/dist/src/lib/login-scope-lines.d.ts +15 -0
  95. package/dist/src/lib/login-scope-lines.js +22 -0
  96. package/dist/src/lib/login-scope-lines.js.map +1 -0
  97. package/dist/src/lib/paginate.d.ts +0 -5
  98. package/dist/src/lib/paginate.js +0 -5
  99. package/dist/src/lib/paginate.js.map +1 -1
  100. package/dist/src/lib/prompt-schema-codegen.js +5 -2
  101. package/dist/src/lib/prompt-schema-codegen.js.map +1 -1
  102. package/dist/src/lib/refresh-admin-credentials.d.ts +7 -0
  103. package/dist/src/lib/refresh-admin-credentials.js +16 -4
  104. package/dist/src/lib/refresh-admin-credentials.js.map +1 -1
  105. package/dist/src/lib/root-import-decision.d.ts +15 -14
  106. package/dist/src/lib/root-import-decision.js +44 -51
  107. package/dist/src/lib/root-import-decision.js.map +1 -1
  108. package/dist/src/lib/snapshot-build-rows.d.ts +21 -0
  109. package/dist/src/lib/snapshot-build-rows.js +22 -0
  110. package/dist/src/lib/snapshot-build-rows.js.map +1 -1
  111. package/dist/src/lib/snapshot-build.js +8 -3
  112. package/dist/src/lib/snapshot-build.js.map +1 -1
  113. package/dist/src/lib/swift-codegen/banners.d.ts +34 -7
  114. package/dist/src/lib/swift-codegen/banners.js +37 -7
  115. package/dist/src/lib/swift-codegen/banners.js.map +1 -1
  116. package/dist/src/lib/swift-codegen/dbGenerator.d.ts +4 -3
  117. package/dist/src/lib/swift-codegen/dbGenerator.js +14 -117
  118. package/dist/src/lib/swift-codegen/dbGenerator.js.map +1 -1
  119. package/dist/src/lib/swift-codegen/functionGenerator.d.ts +3 -3
  120. package/dist/src/lib/swift-codegen/functionGenerator.js +22 -17
  121. package/dist/src/lib/swift-codegen/functionGenerator.js.map +1 -1
  122. package/dist/src/lib/swift-codegen/generator.d.ts +5 -5
  123. package/dist/src/lib/swift-codegen/generator.js +35 -40
  124. package/dist/src/lib/swift-codegen/generator.js.map +1 -1
  125. package/dist/src/lib/toml-database-config.d.ts +14 -75
  126. package/dist/src/lib/toml-database-config.js +14 -305
  127. package/dist/src/lib/toml-database-config.js.map +1 -1
  128. package/dist/src/lib/workflow-codegen/generator.d.ts +7 -7
  129. package/dist/src/lib/workflow-codegen/generator.js +16 -23
  130. package/dist/src/lib/workflow-codegen/generator.js.map +1 -1
  131. package/dist/src/lib/workflow-codegen/invokerIR.d.ts +6 -12
  132. package/dist/src/lib/workflow-codegen/invokerIR.js +4 -5
  133. package/dist/src/lib/workflow-codegen/invokerIR.js.map +1 -1
  134. package/dist/src/lib/workflow-toml-validator.d.ts +2 -0
  135. package/dist/src/lib/workflow-toml-validator.js +28 -1
  136. package/dist/src/lib/workflow-toml-validator.js.map +1 -1
  137. package/package.json +3 -5
  138. package/dist/src/lib/generated-workflow-model-fields.d.ts +0 -20
  139. package/dist/src/lib/generated-workflow-model-fields.js +0 -53
  140. package/dist/src/lib/generated-workflow-model-fields.js.map +0 -1
  141. package/dist/src/lib/workflow-field-descriptor.d.ts +0 -91
  142. package/dist/src/lib/workflow-field-descriptor.js +0 -153
  143. package/dist/src/lib/workflow-field-descriptor.js.map +0 -1
@@ -2,13 +2,11 @@ import { readFileSync, writeFileSync, appendFileSync, mkdirSync, existsSync, rea
2
2
  import * as path from "path";
3
3
  import { ApiClient } from "../lib/api-client.js";
4
4
  import { getCurrentAppId, resolveAppId } from "../lib/config.js";
5
- import { resolveCodegenSourceDir } from "../lib/codegen-shared/resolveCodegenSourceDir.js";
6
5
  import { resolveSyncDir } from "../lib/sync-paths.js";
7
6
  import { parseCsv, applyColumnMap, buildRows } from "../lib/csv.js";
8
7
  import { chunk, parseBatchSize } from "../lib/batch.js";
9
- import { generateDbTypes, } from "../lib/db-codegen/dbGenerator.js";
10
- import { generateDbSwiftTypes } from "../lib/swift-codegen/dbGenerator.js";
11
8
  import { success, error, info, warn, keyValue, result as printResult, formatTable, formatId, formatDate, json, } from "../lib/output.js";
9
+ import { renderAggregateResult } from "../lib/aggregate-output.js";
12
10
  import { confirmPrompt } from "../lib/confirm-prompt.js";
13
11
  import { parseFilterOptions } from "../lib/record-filter.js";
14
12
  import { parseDataOption } from "../lib/data-input.js";
@@ -52,49 +50,6 @@ function assertDataIdMatches(data, recordId, writtenId, commandPath) {
52
50
  : `Pass the record id as the argument, not inside --data: ${commandPath} ${dataId} --data ...`);
53
51
  process.exit(1);
54
52
  }
55
- /**
56
- * Validate that the named operation is an import-compatible, save-like
57
- * mutation before importing (issue #145, Codex finding (a)).
58
- *
59
- * The importer always builds `params: { modelName, id, data }`, and the batch
60
- * endpoint rejects undeclared params per item. So we require the op to be a
61
- * `mutation` whose declared `params` cover `modelName`, `id`, and `data`. This
62
- * surfaces a clear up-front error instead of letting every row fail
63
- * server-side. Exits non-zero on any mismatch.
64
- */
65
- async function validateImportOperation(client, appId, databaseId, operationName) {
66
- let ops;
67
- try {
68
- ops = await client.listDatabaseOperations(appId, databaseId);
69
- }
70
- catch (err) {
71
- error(`Could not list operations for this database: ${err?.message || err}`);
72
- process.exit(1);
73
- return;
74
- }
75
- const op = (ops || []).find((o) => o?.name === operationName);
76
- if (!op) {
77
- error(`Operation "${operationName}" not found — create a mutation op (e.g. ` +
78
- `\`seed_save\`) on this database type first.`);
79
- process.exit(1);
80
- return;
81
- }
82
- if (op.type !== "mutation") {
83
- error(`Operation "${operationName}" is a "${op.type}" op, not an import-compatible ` +
84
- `mutation. CSV import needs a save-like mutation taking {modelName, id, data}.`);
85
- process.exit(1);
86
- return;
87
- }
88
- const params = op.params || {};
89
- const missing = ["modelName", "id", "data"].filter((p) => !(p in params));
90
- if (missing.length > 0) {
91
- error(`Operation "${operationName}" is not import-compatible: its params are missing ` +
92
- `${missing.join(", ")}. CSV import builds {modelName, id, data} per item, so the ` +
93
- `op must declare those params (like \`seed_save\`).`);
94
- process.exit(1);
95
- return;
96
- }
97
- }
98
53
  /**
99
54
  * The full `databases records bulk` precondition value space, audited in one
100
55
  * pass (issue #2437, Codex cycle 3). A precondition is field-equality only, so
@@ -760,43 +715,18 @@ database-type-config' and the 'database-type-configs' group for inspection:
760
715
  json(result);
761
716
  return;
762
717
  }
763
- const aggregated = result?.result;
764
- // Label the value column after the operation, including the field it
765
- // ran over: `SUM` alone does not say what was summed. The field name is
766
- // reproduced verbatim in both labels — it is a user-supplied
767
- // identifier, and case-folding it produced `avg(lastvalue)` for
768
- // `--field lastValue` (issue #2357).
769
- const valueLabel = options.op === "count"
770
- ? "COUNT"
771
- : `${options.op.toUpperCase()}(${options.field})`;
772
- const scalarLabel = options.op === "count"
773
- ? "count"
774
- : `${options.op.toLowerCase()}(${options.field})`;
775
- if (groupBy.length === 0) {
776
- // Ungrouped: one scalar per operation, keyed by the endpoint's
777
- // operation name (`count`, `sum_<field>`, …). Render it as a
778
- // key/value line — there is no group column to table.
779
- const value = (aggregated ?? {})[options.op === "count" ? "count" : `${options.op}_${options.field}`];
780
- keyValue(scalarLabel, value === null || value === undefined ? "—" : String(value));
781
- return;
782
- }
783
- if (aggregated &&
784
- typeof aggregated === "object" &&
785
- !Array.isArray(aggregated) &&
786
- Object.values(aggregated).every((v) => typeof v !== "object" || v === null)) {
787
- // Flat { group -> value } shape (single operation, single group-by): render a table.
788
- const rows = Object.entries(aggregated).map(([group, value]) => ({
789
- group,
790
- value: value === null || value === undefined ? "—" : String(value),
791
- }));
792
- console.log(formatTable(rows, [
793
- { header: "GROUP", key: "group" },
794
- { header: valueLabel, key: "value" },
795
- ]));
718
+ // The route answers rows (#4265): one key/value line when
719
+ // ungrouped, a table with one column per group key when grouped.
720
+ const rendered = renderAggregateResult(result?.result, {
721
+ groupBy,
722
+ op: options.op,
723
+ field: options.field,
724
+ });
725
+ if (rendered.kind === "scalar") {
726
+ keyValue(rendered.label, rendered.value);
796
727
  }
797
728
  else {
798
- // Grouped/nested or multi-operation shape: print the structured result.
799
- console.log(JSON.stringify(aggregated ?? {}, null, 2));
729
+ console.log(rendered.text);
800
730
  }
801
731
  }
802
732
  catch (err) {
@@ -1182,145 +1112,6 @@ database-type-config' and the 'database-type-configs' group for inspection:
1182
1112
  process.exit(1);
1183
1113
  }
1184
1114
  });
1185
- // ---- Operations subcommand group ----
1186
- const operations = databases
1187
- .command("operations")
1188
- .description("List and execute registered operations on a database");
1189
- // List operations
1190
- operations
1191
- .command("list")
1192
- .description("List registered operations available on a database")
1193
- .argument("<database-id>", "Database ID")
1194
- .option("--app <app-id>", "App ID")
1195
- .option("--json", "Output as JSON")
1196
- .action(async (databaseId, options) => {
1197
- const resolvedAppId = resolveAppId(undefined, options);
1198
- const client = new ApiClient();
1199
- try {
1200
- const result = await client.listDatabaseOperations(resolvedAppId, databaseId);
1201
- const list = Array.isArray(result) ? result : [];
1202
- if (options.json) {
1203
- json(wholeListEnvelope(list));
1204
- return;
1205
- }
1206
- if (list.length === 0) {
1207
- info("No operations found.");
1208
- return;
1209
- }
1210
- console.log(formatTable(list, [
1211
- { header: "NAME", key: "name" },
1212
- { header: "TYPE", key: "type" },
1213
- { header: "MODEL", key: "modelName" },
1214
- { header: "ACCESS", key: "access" },
1215
- ]));
1216
- }
1217
- catch (err) {
1218
- error(err.message);
1219
- process.exit(1);
1220
- }
1221
- });
1222
- // Execute operation
1223
- operations
1224
- .command("execute")
1225
- .description("Execute a registered operation on a database")
1226
- .argument("<database-id>", "Database ID")
1227
- .argument("<operation-name>", "Operation name")
1228
- .option("--params <json>", "Operation parameters as JSON")
1229
- .option("--limit <n>", "Max records to return (for queries)")
1230
- .option("--cursor <cursor>", "Pagination cursor")
1231
- .option("--token <jwt>", "Execute as a specific user (dev/test — JWT visible in process args)")
1232
- .option("--app <app-id>", "App ID")
1233
- .option("--json", "Output as JSON")
1234
- .option("--timing", "Show server-side timing breakdown")
1235
- .action(async (databaseId, operationName, options) => {
1236
- const resolvedAppId = resolveAppId(undefined, options);
1237
- const client = new ApiClient();
1238
- let params;
1239
- if (options.params) {
1240
- try {
1241
- params = JSON.parse(options.params);
1242
- }
1243
- catch {
1244
- error("Invalid --params JSON.");
1245
- process.exit(1);
1246
- return;
1247
- }
1248
- }
1249
- const body = {};
1250
- if (params)
1251
- body.params = params;
1252
- if (options.limit)
1253
- body.limit = parseInt(options.limit, 10);
1254
- if (options.cursor)
1255
- body.cursor = options.cursor;
1256
- try {
1257
- const result = await client.executeDatabaseOperation(resolvedAppId, databaseId, operationName, body, options.token, { timing: !!options.timing });
1258
- if (options.json) {
1259
- json(result);
1260
- return;
1261
- }
1262
- // Smart display based on result shape. `executeDatabaseOperation()`
1263
- // has already normalized a query page to `{ items, hasMore,
1264
- // nextCursor? }` (#2440), so `items` is exactly "this result was a
1265
- // list" — the two branches cannot disagree about it.
1266
- if (Array.isArray(result.items)) {
1267
- if (result.items.length === 0) {
1268
- info("No records found.");
1269
- }
1270
- else {
1271
- const cols = Object.keys(result.items[0]).filter((k) => k !== "type");
1272
- console.log(formatTable(result.items, cols.map((c) => ({
1273
- header: c.toUpperCase(),
1274
- key: c,
1275
- format: (v) => {
1276
- if (v === null || v === undefined)
1277
- return "—";
1278
- if (typeof v === "object") {
1279
- const s = JSON.stringify(v);
1280
- return s.length > 50 ? s.slice(0, 47) + "..." : s;
1281
- }
1282
- const s = String(v);
1283
- return s.length > 50 ? s.slice(0, 47) + "..." : s;
1284
- },
1285
- }))));
1286
- }
1287
- // Gate on the cursor itself, the way `records query` does: `hasMore`
1288
- // without a cursor used to print the literal `undefined` (#2440).
1289
- if (result.nextCursor) {
1290
- info(`More records available. Use --cursor ${result.nextCursor} to continue.`);
1291
- }
1292
- }
1293
- else if (result.count !== undefined) {
1294
- console.log(`Count: ${result.count}`);
1295
- }
1296
- else if (result.result !== undefined) {
1297
- // Aggregate result
1298
- json(result);
1299
- }
1300
- else if (result.results && Array.isArray(result.results)) {
1301
- // Batch/mutation results
1302
- success(`Operation executed (${result.results.length} result${result.results.length === 1 ? "" : "s"}).`);
1303
- }
1304
- else if (result.success !== undefined) {
1305
- success("Operation executed.");
1306
- }
1307
- else {
1308
- json(result);
1309
- }
1310
- // Display timing if present
1311
- if (result._timing) {
1312
- console.log("");
1313
- console.log("Timing:");
1314
- for (const [key, value] of Object.entries(result._timing)) {
1315
- console.log(` ${key}: ${value}ms`);
1316
- }
1317
- }
1318
- }
1319
- catch (err) {
1320
- error(err.message);
1321
- process.exit(1);
1322
- }
1323
- });
1324
1115
  // ---- Indexes subcommand group ----
1325
1116
  const indexes = databases
1326
1117
  .command("indexes")
@@ -1641,11 +1432,10 @@ database-type-config' and the 'database-type-configs' group for inspection:
1641
1432
  // ============================================
1642
1433
  databases
1643
1434
  .command("import-csv")
1644
- .description("Import rows from a CSV file into a database via a registered batch (bulk) save operation")
1435
+ .description("Import rows from a CSV file into a database: one save per row through records/batch, best-effort (a row the database refuses is counted as failed and the rest are written)")
1645
1436
  .argument("<database-id>", "Database ID")
1646
1437
  .argument("<file>", "Path to the CSV file")
1647
- .requiredOption("--model <name>", "Model name passed in each item's params")
1648
- .option("--operation <op>", "Registered mutation operation to use (must be a {modelName,id,data} save-like op)", "seed_save")
1438
+ .requiredOption("--model <name>", "Model every row is saved into")
1649
1439
  .option("--column-map <json>", 'Map CSV headers to field names, e.g. \'{"CSV Header":"fieldName"}\'')
1650
1440
  .option("--types <json>", 'Coerce fields by type, e.g. \'{"price":"number","active":"boolean"}\'')
1651
1441
  .option("--id-column <col>", "Use this CSV column for record ids (else a ULID is generated)")
@@ -1681,11 +1471,11 @@ database-type-config' and the 'database-type-configs' group for inspection:
1681
1471
  return;
1682
1472
  }
1683
1473
  }
1684
- // The registered-batch (`executeBatch`) path rejects requests over
1685
- // 100000 items server-side; keep chunks under it.
1474
+ // `records/batch` has no fixed server item cap — bound it with the same
1475
+ // conservative client-side ceiling `databases import` uses.
1686
1476
  const parsedBatchSize = parseBatchSize(options.batchSize, {
1687
- max: 100000,
1688
- overMaxMessage: "--batch-size cannot exceed 100000 (the server's per-request cap).",
1477
+ max: IMPORT_BATCH_SIZE_CEILING,
1478
+ overMaxMessage: `--batch-size cannot exceed ${IMPORT_BATCH_SIZE_CEILING} (a conservative client-side limit for batch write requests).`,
1689
1479
  });
1690
1480
  if ("error" in parsedBatchSize) {
1691
1481
  error(parsedBatchSize.error);
@@ -1704,21 +1494,14 @@ database-type-config' and the 'database-type-configs' group for inspection:
1704
1494
  let rows = parseCsv(raw, options.delimiter);
1705
1495
  // 2. Apply column map.
1706
1496
  rows = applyColumnMap(rows, columnMap);
1707
- // 3. Coerce types, assign ids, shape into batch items.
1497
+ // 3. Coerce types, assign ids, shape into records/batch saves.
1708
1498
  const { batch, rowCount } = buildRows(rows, {
1709
1499
  modelName: options.model,
1710
1500
  types,
1711
1501
  idColumn: options.idColumn,
1712
1502
  });
1713
1503
  const chunks = chunk(batch, batchSize);
1714
- // 4. Validate the chosen op is an import-compatible save-like mutation
1715
- // BEFORE writing (op-not-found / incompatible → clear up-front error
1716
- // rather than per-row server-side failure). Skip the round-trip only
1717
- // when there is genuinely nothing to import in a dry run.
1718
- if (!options.dryRun || rowCount > 0) {
1719
- await validateImportOperation(client, resolvedAppId, databaseId, options.operation);
1720
- }
1721
- // 5. Empty / headers-only CSV: no request, report 0/0.
1504
+ // 4. Empty / headers-only CSV: no request, report 0/0.
1722
1505
  if (rowCount === 0) {
1723
1506
  if (options.json) {
1724
1507
  json({ imported: 0, failed: 0, rows: 0, batches: 0, errors: [], dryRun: !!options.dryRun });
@@ -1729,7 +1512,7 @@ database-type-config' and the 'database-type-configs' group for inspection:
1729
1512
  }
1730
1513
  return;
1731
1514
  }
1732
- // 6. Dry run: report counts, no writes.
1515
+ // 5. Dry run: report counts, no writes.
1733
1516
  if (options.dryRun) {
1734
1517
  if (options.json) {
1735
1518
  json({ dryRun: true, rows: rowCount, batches: chunks.length, batchSize });
@@ -1742,32 +1525,60 @@ database-type-config' and the 'database-type-configs' group for inspection:
1742
1525
  }
1743
1526
  return;
1744
1527
  }
1745
- // 7. Import chunk by chunk; accumulate imported/failed + per-chunk errors.
1528
+ // 6. Import chunk by chunk through records/batch, non-atomic (the
1529
+ // route's default): each row's outcome is its own, so a refused row
1530
+ // is counted failed and its siblings commit.
1746
1531
  let imported = 0;
1747
1532
  let failed = 0;
1748
1533
  const errors = [];
1749
1534
  let aborted = false;
1750
1535
  for (let i = 0; i < chunks.length; i++) {
1751
1536
  const piece = chunks[i];
1537
+ let chunkFailed = false;
1752
1538
  try {
1753
- const res = await client.executeBatch(resolvedAppId, databaseId, options.operation, piece);
1754
- imported += res.imported ?? 0;
1755
- failed += res.failed ?? 0;
1539
+ const res = await client.batchDatabaseRecords(resolvedAppId, databaseId, piece);
1540
+ const results = res?.results ?? [];
1541
+ let chunkImported = 0;
1542
+ let chunkRefused = 0;
1543
+ const firstErrors = [];
1544
+ for (let j = 0; j < piece.length; j++) {
1545
+ const r = results[j];
1546
+ if (r && r.success) {
1547
+ chunkImported++;
1548
+ }
1549
+ else {
1550
+ chunkRefused++;
1551
+ if (r?.error && firstErrors.length < 3)
1552
+ firstErrors.push(r.error);
1553
+ }
1554
+ }
1555
+ imported += chunkImported;
1556
+ failed += chunkRefused;
1557
+ if (chunkRefused > 0) {
1558
+ chunkFailed = true;
1559
+ errors.push({
1560
+ batch: i,
1561
+ error: `${chunkRefused} row(s) failed${firstErrors.length ? `: ${firstErrors.join("; ")}` : ""}`,
1562
+ });
1563
+ }
1756
1564
  if (!options.json) {
1757
- info(`Batch ${i + 1}/${chunks.length}: imported ${res.imported ?? 0}, failed ${res.failed ?? 0}`);
1565
+ info(`Batch ${i + 1}/${chunks.length}: imported ${chunkImported}, failed ${chunkRefused}`);
1758
1566
  }
1759
1567
  }
1760
1568
  catch (err) {
1569
+ // A chunk-level failure (a 400 for a malformed op): the whole
1570
+ // chunk was refused, so every row in it counts as failed.
1761
1571
  const msg = err?.message || String(err);
1572
+ chunkFailed = true;
1762
1573
  errors.push({ batch: i, error: msg });
1763
1574
  failed += piece.length;
1764
1575
  if (!options.json) {
1765
1576
  warn(`Batch ${i + 1}/${chunks.length} failed: ${msg}`);
1766
1577
  }
1767
- if (options.stopOnError) {
1768
- aborted = true;
1769
- break;
1770
- }
1578
+ }
1579
+ if (chunkFailed && options.stopOnError) {
1580
+ aborted = true;
1581
+ break;
1771
1582
  }
1772
1583
  }
1773
1584
  if (options.json) {
@@ -1785,7 +1596,7 @@ database-type-config' and the 'database-type-configs' group for inspection:
1785
1596
  }
1786
1597
  }
1787
1598
  }
1788
- // Non-zero exit if any chunk errored.
1599
+ // Non-zero exit if any row or chunk failed.
1789
1600
  if (errors.length > 0) {
1790
1601
  process.exit(1);
1791
1602
  }
@@ -1803,7 +1614,7 @@ database-type-config' and the 'database-type-configs' group for inspection:
1803
1614
  .description("Manage the TOML schema attached to a database type");
1804
1615
  schema
1805
1616
  .command("generate")
1806
- .description("Scaffold a starting TOML schema for <database-type> from existing operations + DO introspection, and insert it into the local database-type-configs/<type>.toml file.")
1617
+ .description("Scaffold a starting TOML schema for <database-type> from the models and fields its databases hold, and append it to the local database-type-configs/<type>.toml file.")
1807
1618
  .argument("<database-type>", "Database type name")
1808
1619
  .option("-f, --force", "Overwrite an existing [models.*] block in the local file")
1809
1620
  .option("--json", "Output the generated TOML as JSON")
@@ -1842,10 +1653,10 @@ database-type-config' and the 'database-type-configs' group for inspection:
1842
1653
  console.log(scaffoldedToml);
1843
1654
  return;
1844
1655
  }
1845
- // 3. For each candidate, splice the scaffolded TOML at a stable
1846
- // anchor (before the first `[[operations]]` block, or at EOF). Per
1847
- // the plan-of-record, textual insertion is deliberate — smol-toml
1848
- // parse/stringify drops comments, so we splice as a raw string.
1656
+ // 3. For each candidate, append the scaffolded TOML at the end of the
1657
+ // file. Per the plan-of-record, textual insertion is deliberate —
1658
+ // smol-toml parse/stringify drops comments, so we splice as a raw
1659
+ // string.
1849
1660
  for (const targetPath of candidateFiles) {
1850
1661
  const original = readFileSync(targetPath, "utf-8");
1851
1662
  // Detect existing [models.*] block. If present, refuse unless --force.
@@ -1854,26 +1665,9 @@ database-type-config' and the 'database-type-configs' group for inspection:
1854
1665
  error(`${targetPath} already has [models.*] blocks. Re-run with --force to overwrite.`);
1855
1666
  process.exit(1);
1856
1667
  }
1857
- // Strip existing [models.*] blocks if --force.
1858
- let cleaned = original;
1859
- if (hasModelsBlock && options.force) {
1860
- cleaned = stripModelsBlocks(original);
1861
- }
1862
- // Splice: insert before the first [[operations]] block, otherwise
1863
- // at the end of file.
1864
- let merged;
1865
- const opsAnchor = cleaned.indexOf("\n[[operations]]");
1866
- if (opsAnchor >= 0) {
1867
- merged =
1868
- cleaned.slice(0, opsAnchor + 1) +
1869
- scaffoldedToml +
1870
- "\n" +
1871
- cleaned.slice(opsAnchor + 1);
1872
- }
1873
- else {
1874
- const sep = cleaned.endsWith("\n") ? "" : "\n";
1875
- merged = cleaned + sep + scaffoldedToml + "\n";
1876
- }
1668
+ const merged = spliceScaffoldedSchema(original, scaffoldedToml, {
1669
+ force: !!options.force,
1670
+ });
1877
1671
  writeFileSync(targetPath, merged);
1878
1672
  success(`Inserted schema into ${targetPath}`);
1879
1673
  }
@@ -1884,137 +1678,18 @@ database-type-config' and the 'database-type-configs' group for inspection:
1884
1678
  process.exit(1);
1885
1679
  }
1886
1680
  });
1887
- // ============================================
1888
- // Codegen command (issue #814)
1889
- // ============================================
1890
- databases
1891
- .command("codegen")
1892
- .description("Generate record + operation types from the local database-type-configs/*.toml schema. TypeScript (default): a <type>.generated.ts per database type (record interfaces, per-op input-params interfaces, per-op result aliases, and a typed <type>Ops factory). Swift (--lang swift): a <type>.generated.swift per database type (record structs, per-op params structs, per-op result typealiases, and a typed <Type>Ops factory).\n\nFormatting: the emitted TypeScript is already formatted the way Prettier's DEFAULT config formats it, so a `codegen` -> `prettier --write` -> `codegen --check` pipeline stays green. If your project uses a NON-default Prettier config (a different printWidth, trailingComma, or quote style), Prettier will reformat the generated files and `--check` will then fail: add the generated directory to `.prettierignore` and commit the raw emission instead.")
1893
- .argument("[database-type]", "Generate for a single database type (defaults to every database-type-configs/*.toml found)")
1894
- .option("-o, --output <dir>", "Output directory for the generated files (defaults to <config-dir>/database-type-configs/generated/)")
1895
- .option("--lang <lang>", "Target language for the generated types: 'ts' (default) or 'swift'.", "ts")
1896
- .option("--check", "Exit non-zero if generated output is out of date (CI guard); does not write. Compares byte-for-byte, so the file on disk must match the emission exactly.")
1897
- .option("--json", "Output the result summary as JSON")
1898
- .action(async (databaseType, options) => {
1899
- try {
1900
- // 0. Validate --lang up front so an unknown value fails clearly rather
1901
- // than silently defaulting to the TS emitter.
1902
- const lang = String(options.lang ?? "ts").toLowerCase();
1903
- if (lang !== "ts" && lang !== "swift") {
1904
- error(`Unknown --lang "${options.lang}". Use "ts" or "swift".`);
1905
- process.exit(1);
1906
- }
1907
- // 1. The single source database-type-configs/ directory: the
1908
- // environment's, and only ever the environment's (#1510, #3154).
1909
- const dbTypesSourceDir = resolveCodegenSourceDir({
1910
- subdir: "database-type-configs",
1911
- });
1912
- // 2. Collect the source .toml files (one per database type), filtered
1913
- // to a single type when an argument is given.
1914
- const inputs = [];
1915
- for (const fileName of readdirSync(dbTypesSourceDir)) {
1916
- if (!fileName.endsWith(".toml"))
1917
- continue;
1918
- const typeName = fileName.slice(0, -".toml".length);
1919
- if (databaseType && typeName !== databaseType)
1920
- continue;
1921
- const tomlPath = path.join(dbTypesSourceDir, fileName);
1922
- inputs.push({
1923
- databaseType: typeName,
1924
- tomlPath,
1925
- tomlContent: readFileSync(tomlPath, "utf-8"),
1926
- });
1927
- }
1928
- if (inputs.length === 0) {
1929
- error(databaseType
1930
- ? `No database-type-configs/${databaseType}.toml found under primitive/<env>/.`
1931
- : "No database-type-configs/*.toml files found to generate from.");
1932
- process.exit(1);
1933
- }
1934
- // 3. Resolve the output directory. Default: a `generated/` subdir of
1935
- // the first source dir (keeps generated output beside the schema).
1936
- const outputDir = options.output
1937
- ? path.resolve(options.output)
1938
- : path.join(path.dirname(inputs[0].tomlPath), "generated");
1939
- // 4. Run codegen (or --check) with the selected emitter. Both the TS
1940
- // and Swift generators return the same WriteOrCheckResult shape, so
1941
- // the reporting below is emitter-agnostic; only the emitter (and the
1942
- // regenerate hint) differs. When a single database-type filter
1943
- // argument is given, the input set is partial — flag it so stale
1944
- // cleanup is scoped to that type and sibling types' generated files
1945
- // are left intact.
1946
- const codegenResult = lang === "swift"
1947
- ? await generateDbSwiftTypes({
1948
- inputs,
1949
- outputDir,
1950
- check: !!options.check,
1951
- singleType: !!databaseType,
1952
- })
1953
- : await generateDbTypes({
1954
- inputs,
1955
- outputDir,
1956
- check: !!options.check,
1957
- singleType: !!databaseType,
1958
- });
1959
- if (options.check) {
1960
- if (codegenResult.mismatches.length > 0) {
1961
- if (options.json) {
1962
- json({ ok: false, mismatches: codegenResult.mismatches });
1963
- }
1964
- else {
1965
- error(`Check failed: ${codegenResult.mismatches.length} file(s) out of date.`);
1966
- for (const m of codegenResult.mismatches) {
1967
- error(` ${m.reason}: ${path.relative(process.cwd(), m.filePath)}`);
1968
- }
1969
- // Echo back the options that decide WHERE and WHAT was
1970
- // checked, the way `workflows codegen` does (#2895). Suggesting
1971
- // a bare `databases codegen` after a `--check -o <dir>` sent
1972
- // people to regenerate into the default
1973
- // <config-dir>/database-type-configs/generated/ instead of the
1974
- // directory that is actually out of date, so the files stayed
1975
- // stale and the next check failed the same way (#2911).
1976
- const regenerate = ["primitive databases codegen"];
1977
- if (databaseType)
1978
- regenerate.push(databaseType);
1979
- if (lang === "swift")
1980
- regenerate.push("--lang swift");
1981
- if (options.output)
1982
- regenerate.push(`-o ${options.output}`);
1983
- info(`Run \`${regenerate.join(" ")}\` to regenerate.`);
1984
- }
1985
- process.exit(1);
1986
- }
1987
- if (options.json) {
1988
- json({ ok: true, checked: codegenResult.writtenFiles.length });
1989
- }
1990
- else {
1991
- success(`Check passed: ${codegenResult.writtenFiles.length} file(s) up to date.`);
1992
- }
1993
- return;
1994
- }
1995
- if (options.json) {
1996
- json({
1997
- written: codegenResult.writtenFiles,
1998
- deleted: codegenResult.deletedFiles,
1999
- });
2000
- return;
2001
- }
2002
- success(`Generated ${codegenResult.writtenFiles.length} file(s)` +
2003
- (codegenResult.deletedFiles.length > 0
2004
- ? `, deleted ${codegenResult.deletedFiles.length} stale file(s).`
2005
- : "."));
2006
- for (const f of codegenResult.writtenFiles) {
2007
- keyValue(" wrote", path.relative(process.cwd(), f));
2008
- }
2009
- for (const f of codegenResult.deletedFiles) {
2010
- keyValue(" deleted", path.relative(process.cwd(), f));
2011
- }
2012
- }
2013
- catch (err) {
2014
- error(err.message);
2015
- process.exit(1);
2016
- }
2017
- });
1681
+ }
1682
+ /**
1683
+ * Append a scaffolded `[models.*]` schema to a database type file's text,
1684
+ * first stripping the file's existing `[models.*]` blocks when `force` is set.
1685
+ * Appended at the end of the file: #3189 retired the `[[operations]]` blocks
1686
+ * the scaffold used to be inserted ahead of.
1687
+ */
1688
+ export function spliceScaffoldedSchema(original, scaffoldedToml, options = {}) {
1689
+ const hasModelsBlock = /\n\[models\./.test("\n" + original);
1690
+ const cleaned = hasModelsBlock && options.force ? stripModelsBlocks(original) : original;
1691
+ const sep = cleaned.endsWith("\n") ? "" : "\n";
1692
+ return cleaned + sep + scaffoldedToml + "\n";
2018
1693
  }
2019
1694
  /** Remove every existing `[models.*]` block from a TOML file body. */
2020
1695
  function stripModelsBlocks(text) {