@atscript/db 0.1.155 → 0.1.156

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 (33) hide show
  1. package/dist/agg.d.cts +1 -1
  2. package/dist/agg.d.mts +1 -1
  3. package/dist/{buckets-goCoky8v.d.mts → buckets-8mG67Mtw.d.mts} +49 -7
  4. package/dist/{buckets-DcWm3SLt.d.cts → buckets-B4T_alUe.d.cts} +49 -7
  5. package/dist/{column-diff-C68V_eYM.mjs → column-diff-DR1glykD.mjs} +74 -11
  6. package/dist/{column-diff-DMkGYAGM.cjs → column-diff-DpeUbhEI.cjs} +74 -11
  7. package/dist/{fk-diff-BYLNWoF9.d.mts → fk-diff-5gX42hBt.d.mts} +1 -1
  8. package/dist/{fk-diff-DblOPBLS.d.cts → fk-diff-BWdQ7kRZ.d.cts} +1 -1
  9. package/dist/index.cjs +3 -3
  10. package/dist/index.d.cts +9 -8
  11. package/dist/index.d.mts +9 -8
  12. package/dist/index.mjs +3 -3
  13. package/dist/plugin.cjs +19 -2
  14. package/dist/plugin.mjs +19 -2
  15. package/dist/rel.d.cts +2 -2
  16. package/dist/rel.d.mts +2 -2
  17. package/dist/{relation-helpers-B9MVBrtx.d.mts → relation-helpers-17_2f5hX.d.mts} +1 -1
  18. package/dist/{relation-helpers-D9kMFfum.d.cts → relation-helpers-BLBrLQYi.d.cts} +1 -1
  19. package/dist/{search-fields-DBIZKwyb.mjs → search-fields-CE8cmj9X.mjs} +1 -0
  20. package/dist/{search-fields-DwrsJUC1.cjs → search-fields-CT_UZ9fg.cjs} +1 -0
  21. package/dist/shared.cjs +1 -1
  22. package/dist/shared.mjs +1 -1
  23. package/dist/sync.cjs +32 -8
  24. package/dist/sync.d.cts +2 -2
  25. package/dist/sync.d.mts +2 -2
  26. package/dist/sync.mjs +32 -8
  27. package/dist/{validation-utils-DxHIqa36.mjs → validation-utils-BGjqc2jU.mjs} +1 -1
  28. package/dist/{validation-utils-_PA7iSLm.cjs → validation-utils-CtLNlBtB.cjs} +1 -1
  29. package/dist/{validator-BC3nwFi4.mjs → validator-B3fPJjS1.mjs} +2 -2
  30. package/dist/{validator-DHwSyBej.cjs → validator-B4YDLUkj.cjs} +2 -2
  31. package/dist/validator.cjs +1 -1
  32. package/dist/validator.mjs +1 -1
  33. package/package.json +6 -6
package/dist/sync.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_db_error = require("./db-error-JcfJqqpk.cjs");
3
- const require_column_diff = require("./column-diff-DMkGYAGM.cjs");
4
- const require_validator = require("./validator-DHwSyBej.cjs");
3
+ const require_column_diff = require("./column-diff-DpeUbhEI.cjs");
4
+ const require_validator = require("./validator-B4YDLUkj.cjs");
5
5
  //#region src/schema/json-column-copy.ts
6
6
  const NONE = {
7
7
  copies: [],
@@ -946,8 +946,11 @@ async function executeSyncTable(facts, safe, deps, exec) {
946
946
  if (hasFkChanges && fkDiff && adapter.dropForeignKeys) {
947
947
  const keysToDrop = [...fkDiff.removed.map((fk) => require_column_diff.fkKey(fk.fields)), ...fkDiff.changed.map((fk) => require_column_diff.fkKey(require_column_diff.fkColumns(fk.desired).fields))];
948
948
  if (keysToDrop.length > 0) {
949
- await adapter.dropForeignKeys(keysToDrop);
950
- droppedFks.push(...keysToDrop);
949
+ const fresh = keysToDrop.filter((key) => !droppedFks.includes(key));
950
+ if (fresh.length > 0) {
951
+ await adapter.dropForeignKeys(fresh);
952
+ droppedFks.push(...fresh);
953
+ }
951
954
  init.status = "alter";
952
955
  }
953
956
  }
@@ -1000,9 +1003,10 @@ async function executeSyncTable(facts, safe, deps, exec) {
1000
1003
  }
1001
1004
  }
1002
1005
  }
1006
+ if (init.pkChange && facts.populated === true) init.pkChange.populated = true;
1003
1007
  if (init.status === "error") return new SyncEntry(init);
1004
1008
  phase = "Index/FK sync";
1005
- await adapter.syncIndexes();
1009
+ await adapter.syncIndexes(keptIndexes(init));
1006
1010
  if (adapter.syncForeignKeys && !exec.deferForeignKeysTo) await adapter.syncForeignKeys();
1007
1011
  if (adapter.afterSyncTable) await adapter.afterSyncTable();
1008
1012
  } catch (error) {
@@ -1114,9 +1118,29 @@ function afterJsonCopy(facts) {
1114
1118
  function markSkipped(init, kind) {
1115
1119
  init.skipped = [...init.skipped ?? [], kind];
1116
1120
  }
1117
- /** Whether nullable/default changes need DDL on this adapter (schema-less ones only update the snapshot). */
1121
+ /**
1122
+ * Whether nullable/default changes need DDL on this adapter: in-place modify,
1123
+ * or a recreate on a live-introspected one (SQLite). Snapshot-based adapters
1124
+ * (MongoDB) have no column constraint to change — they only update the
1125
+ * snapshot, even with a `recreateTable`.
1126
+ */
1118
1127
  function needsDdlForNullableDefaults(adapter) {
1119
- return !!adapter.supportsColumnModify && !!adapter.syncColumns || !!adapter.recreateTable;
1128
+ return !!adapter.supportsColumnModify && !!adapter.syncColumns || !!adapter.recreateTable && !!adapter.getExistingColumns;
1129
+ }
1130
+ /**
1131
+ * The indexes `syncIndexes` keeps as they are because safe mode skipped
1132
+ * the change they depend on — a skipped key rebuild, the columns of a
1133
+ * skipped type-change recreate (see `TSyncIndexesOptions`).
1134
+ */
1135
+ function keptIndexes(init) {
1136
+ const skipped = init.skipped ?? [];
1137
+ const keepPrimaryKey = skipped.includes("pk-rebuild");
1138
+ const keepColumns = skipped.includes("recreate") ? (init.typeChanges ?? []).map((tc) => tc.column) : [];
1139
+ if (!keepPrimaryKey && keepColumns.length === 0) return;
1140
+ return {
1141
+ keepPrimaryKey,
1142
+ keepColumns
1143
+ };
1120
1144
  }
1121
1145
  /**
1122
1146
  * The work `run({ safe: true })` skips on this table, in the order the
@@ -1238,7 +1262,7 @@ async function applyColumnDiff(adapter, readable, diff, init, safe, logger, ensu
1238
1262
  if (!safe && !init.recreated && init.status !== "error") if (adapter.supportsColumnModify && adapter.syncColumns) {
1239
1263
  needsSyncColumns = true;
1240
1264
  init.status = "alter";
1241
- } else if (adapter.recreateTable) await recreateKeepingRenames(adapter, diff, init);
1265
+ } else if (needsDdlForNullableDefaults(adapter)) await recreateKeepingRenames(adapter, diff, init);
1242
1266
  else init.status = "alter";
1243
1267
  else if (init.status !== "error") {
1244
1268
  if (safe && !init.recreated && needsDdlForNullableDefaults(adapter)) {
package/dist/sync.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { At as TPrimaryKeyChange, It as TTableOptionDiff, U as TDbFieldMeta, et as TDbObjectKind, gt as TEnsureTableOptions, jt as TReferencingForeignKey, mt as TDerivedChangeReason, qn as TGenericLogger, tn as DbSpace, vt as TExistingForeignKey, yt as TExistingTableOption } from "./buckets-DcWm3SLt.cjs";
2
- import { _ as computeTableSnapshot, a as hasForeignKeyChanges, b as snapshotToExistingTableOptions, c as TFieldSnapshot, d as TViewColumnSnapshot, f as TViewJoinSnapshot, g as computeTableHash, h as computeSchemaHash, i as fkKey, l as TForeignKeySnapshot, m as canonicalizeQueryNode, n as computeForeignKeyDiff, o as computeColumnDiff, p as TViewSnapshot, t as TForeignKeyDiff, u as TTableSnapshot, v as computeViewSnapshot, y as snapshotToExistingColumns } from "./fk-diff-DblOPBLS.cjs";
1
+ import { At as TPrimaryKeyChange, Jn as TGenericLogger, Lt as TTableOptionDiff, U as TDbFieldMeta, et as TDbObjectKind, gt as TEnsureTableOptions, jt as TReferencingForeignKey, mt as TDerivedChangeReason, nn as DbSpace, vt as TExistingForeignKey, yt as TExistingTableOption } from "./buckets-B4T_alUe.cjs";
2
+ import { _ as computeTableSnapshot, a as hasForeignKeyChanges, b as snapshotToExistingTableOptions, c as TFieldSnapshot, d as TViewColumnSnapshot, f as TViewJoinSnapshot, g as computeTableHash, h as computeSchemaHash, i as fkKey, l as TForeignKeySnapshot, m as canonicalizeQueryNode, n as computeForeignKeyDiff, o as computeColumnDiff, p as TViewSnapshot, t as TForeignKeyDiff, u as TTableSnapshot, v as computeViewSnapshot, y as snapshotToExistingColumns } from "./fk-diff-BWdQ7kRZ.cjs";
3
3
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
4
4
 
5
5
  //#region src/schema/sync-entry.d.ts
package/dist/sync.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { At as TPrimaryKeyChange, It as TTableOptionDiff, U as TDbFieldMeta, et as TDbObjectKind, gt as TEnsureTableOptions, jt as TReferencingForeignKey, mt as TDerivedChangeReason, qn as TGenericLogger, tn as DbSpace, vt as TExistingForeignKey, yt as TExistingTableOption } from "./buckets-goCoky8v.mjs";
2
- import { _ as computeTableSnapshot, a as hasForeignKeyChanges, b as snapshotToExistingTableOptions, c as TFieldSnapshot, d as TViewColumnSnapshot, f as TViewJoinSnapshot, g as computeTableHash, h as computeSchemaHash, i as fkKey, l as TForeignKeySnapshot, m as canonicalizeQueryNode, n as computeForeignKeyDiff, o as computeColumnDiff, p as TViewSnapshot, t as TForeignKeyDiff, u as TTableSnapshot, v as computeViewSnapshot, y as snapshotToExistingColumns } from "./fk-diff-BYLNWoF9.mjs";
1
+ import { At as TPrimaryKeyChange, Jn as TGenericLogger, Lt as TTableOptionDiff, U as TDbFieldMeta, et as TDbObjectKind, gt as TEnsureTableOptions, jt as TReferencingForeignKey, mt as TDerivedChangeReason, nn as DbSpace, vt as TExistingForeignKey, yt as TExistingTableOption } from "./buckets-8mG67Mtw.mjs";
2
+ import { _ as computeTableSnapshot, a as hasForeignKeyChanges, b as snapshotToExistingTableOptions, c as TFieldSnapshot, d as TViewColumnSnapshot, f as TViewJoinSnapshot, g as computeTableHash, h as computeSchemaHash, i as fkKey, l as TForeignKeySnapshot, m as canonicalizeQueryNode, n as computeForeignKeyDiff, o as computeColumnDiff, p as TViewSnapshot, t as TForeignKeyDiff, u as TTableSnapshot, v as computeViewSnapshot, y as snapshotToExistingColumns } from "./fk-diff-5gX42hBt.mjs";
3
3
  import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
4
4
 
5
5
  //#region src/schema/sync-entry.d.ts
package/dist/sync.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { r as DbError } from "./db-error-eSsQX7rt.mjs";
2
- import { a as computeTableHash, c as serializeDefaultValue, d as viewSnapshotSources, f as computeForeignKeyDiff, ft as relationalColumnName, h as hasForeignKeyChanges, i as computeSchemaHash, j as resolveDesignType, l as snapshotToExistingColumns, m as fkKey, o as computeTableSnapshot, p as fkColumns, pt as aliasTargetOf, r as canonicalizeQueryNode, s as computeViewSnapshot, t as computeColumnDiff, u as snapshotToExistingTableOptions, yt as NoopLogger } from "./column-diff-C68V_eYM.mjs";
3
- import { f as unionValueMembers } from "./validator-BC3nwFi4.mjs";
2
+ import { a as computeTableHash, c as serializeDefaultValue, d as viewSnapshotSources, f as computeForeignKeyDiff, ft as relationalColumnName, h as hasForeignKeyChanges, i as computeSchemaHash, j as resolveDesignType, l as snapshotToExistingColumns, m as fkKey, o as computeTableSnapshot, p as fkColumns, pt as aliasTargetOf, r as canonicalizeQueryNode, s as computeViewSnapshot, t as computeColumnDiff, u as snapshotToExistingTableOptions, yt as NoopLogger } from "./column-diff-DR1glykD.mjs";
3
+ import { f as unionValueMembers } from "./validator-B3fPJjS1.mjs";
4
4
  //#region src/schema/json-column-copy.ts
5
5
  const NONE = {
6
6
  copies: [],
@@ -945,8 +945,11 @@ async function executeSyncTable(facts, safe, deps, exec) {
945
945
  if (hasFkChanges && fkDiff && adapter.dropForeignKeys) {
946
946
  const keysToDrop = [...fkDiff.removed.map((fk) => fkKey(fk.fields)), ...fkDiff.changed.map((fk) => fkKey(fkColumns(fk.desired).fields))];
947
947
  if (keysToDrop.length > 0) {
948
- await adapter.dropForeignKeys(keysToDrop);
949
- droppedFks.push(...keysToDrop);
948
+ const fresh = keysToDrop.filter((key) => !droppedFks.includes(key));
949
+ if (fresh.length > 0) {
950
+ await adapter.dropForeignKeys(fresh);
951
+ droppedFks.push(...fresh);
952
+ }
950
953
  init.status = "alter";
951
954
  }
952
955
  }
@@ -999,9 +1002,10 @@ async function executeSyncTable(facts, safe, deps, exec) {
999
1002
  }
1000
1003
  }
1001
1004
  }
1005
+ if (init.pkChange && facts.populated === true) init.pkChange.populated = true;
1002
1006
  if (init.status === "error") return new SyncEntry(init);
1003
1007
  phase = "Index/FK sync";
1004
- await adapter.syncIndexes();
1008
+ await adapter.syncIndexes(keptIndexes(init));
1005
1009
  if (adapter.syncForeignKeys && !exec.deferForeignKeysTo) await adapter.syncForeignKeys();
1006
1010
  if (adapter.afterSyncTable) await adapter.afterSyncTable();
1007
1011
  } catch (error) {
@@ -1113,9 +1117,29 @@ function afterJsonCopy(facts) {
1113
1117
  function markSkipped(init, kind) {
1114
1118
  init.skipped = [...init.skipped ?? [], kind];
1115
1119
  }
1116
- /** Whether nullable/default changes need DDL on this adapter (schema-less ones only update the snapshot). */
1120
+ /**
1121
+ * Whether nullable/default changes need DDL on this adapter: in-place modify,
1122
+ * or a recreate on a live-introspected one (SQLite). Snapshot-based adapters
1123
+ * (MongoDB) have no column constraint to change — they only update the
1124
+ * snapshot, even with a `recreateTable`.
1125
+ */
1117
1126
  function needsDdlForNullableDefaults(adapter) {
1118
- return !!adapter.supportsColumnModify && !!adapter.syncColumns || !!adapter.recreateTable;
1127
+ return !!adapter.supportsColumnModify && !!adapter.syncColumns || !!adapter.recreateTable && !!adapter.getExistingColumns;
1128
+ }
1129
+ /**
1130
+ * The indexes `syncIndexes` keeps as they are because safe mode skipped
1131
+ * the change they depend on — a skipped key rebuild, the columns of a
1132
+ * skipped type-change recreate (see `TSyncIndexesOptions`).
1133
+ */
1134
+ function keptIndexes(init) {
1135
+ const skipped = init.skipped ?? [];
1136
+ const keepPrimaryKey = skipped.includes("pk-rebuild");
1137
+ const keepColumns = skipped.includes("recreate") ? (init.typeChanges ?? []).map((tc) => tc.column) : [];
1138
+ if (!keepPrimaryKey && keepColumns.length === 0) return;
1139
+ return {
1140
+ keepPrimaryKey,
1141
+ keepColumns
1142
+ };
1119
1143
  }
1120
1144
  /**
1121
1145
  * The work `run({ safe: true })` skips on this table, in the order the
@@ -1237,7 +1261,7 @@ async function applyColumnDiff(adapter, readable, diff, init, safe, logger, ensu
1237
1261
  if (!safe && !init.recreated && init.status !== "error") if (adapter.supportsColumnModify && adapter.syncColumns) {
1238
1262
  needsSyncColumns = true;
1239
1263
  init.status = "alter";
1240
- } else if (adapter.recreateTable) await recreateKeepingRenames(adapter, diff, init);
1264
+ } else if (needsDdlForNullableDefaults(adapter)) await recreateKeepingRenames(adapter, diff, init);
1241
1265
  else init.status = "alter";
1242
1266
  else if (init.status !== "error") {
1243
1267
  if (safe && !init.recreated && needsDdlForNullableDefaults(adapter)) {
@@ -1,4 +1,4 @@
1
- import { i as DB_ENTITY_ANNOTATIONS, t as searchKindOf } from "./search-fields-DBIZKwyb.mjs";
1
+ import { i as DB_ENTITY_ANNOTATIONS, t as searchKindOf } from "./search-fields-CE8cmj9X.mjs";
2
2
  import { AnnotationSpec, isArray, isInterface, isPrimitive, isQueryComparison, isQueryLogical, isRef, isStructure } from "@atscript/core";
3
3
  //#region src/shared/annotation-utils.ts
4
4
  /** Asserts the field carrying this annotation does not also carry any of `others`. */
@@ -1,4 +1,4 @@
1
- const require_search_fields = require("./search-fields-DwrsJUC1.cjs");
1
+ const require_search_fields = require("./search-fields-CT_UZ9fg.cjs");
2
2
  let _atscript_core = require("@atscript/core");
3
3
  //#region src/shared/annotation-utils.ts
4
4
  /** Asserts the field carrying this annotation does not also carry any of `others`. */
@@ -259,9 +259,9 @@ function createDbValidatorPlugin() {
259
259
  if (value === void 0 && (dbCtx.mode === "insert" || dbCtx.mode === "replace") && !complexItems.has(def)) {
260
260
  const meta = def.metadata;
261
261
  const baseMeta = columnUnionBase(def)?.metadata;
262
- const defaulted = !(belowAmbiguous(dbCtx, ctx.path) || pathBelowAmbiguous(ctx.root, ctx.path));
262
+ const defaulted = !(belowAmbiguous(dbCtx, ctx.path) || ctx.root !== void 0 && pathBelowAmbiguous(ctx.root, ctx.path));
263
263
  const has = (key) => defaulted && (meta.has(key) || baseMeta?.has(key) === true);
264
- const serverManaged = has("db.default") || has("db.default.increment") || has("db.default.uuid") || has("db.default.now") || meta.has("db.column.version") || meta.has("db.column.derived");
264
+ const serverManaged = has("db.default") || has("db.default.increment") || has("db.default.uuid") || has("db.default.now") || dbCtx.mode === "replace" && has("db.onUpdate.now") || meta.has("db.column.version") || meta.has("db.column.derived");
265
265
  const hasFK = meta.has("db.rel.FK");
266
266
  if (serverManaged || hasFK) {
267
267
  if (dbCtx.mode === "replace" && meta.has("meta.id") && !ctx.path.includes(".")) return;
@@ -259,9 +259,9 @@ function createDbValidatorPlugin() {
259
259
  if (value === void 0 && (dbCtx.mode === "insert" || dbCtx.mode === "replace") && !complexItems.has(def)) {
260
260
  const meta = def.metadata;
261
261
  const baseMeta = columnUnionBase(def)?.metadata;
262
- const defaulted = !(belowAmbiguous(dbCtx, ctx.path) || pathBelowAmbiguous(ctx.root, ctx.path));
262
+ const defaulted = !(belowAmbiguous(dbCtx, ctx.path) || ctx.root !== void 0 && pathBelowAmbiguous(ctx.root, ctx.path));
263
263
  const has = (key) => defaulted && (meta.has(key) || baseMeta?.has(key) === true);
264
- const serverManaged = has("db.default") || has("db.default.increment") || has("db.default.uuid") || has("db.default.now") || meta.has("db.column.version") || meta.has("db.column.derived");
264
+ const serverManaged = has("db.default") || has("db.default.increment") || has("db.default.uuid") || has("db.default.now") || dbCtx.mode === "replace" && has("db.onUpdate.now") || meta.has("db.column.version") || meta.has("db.column.derived");
265
265
  const hasFK = meta.has("db.rel.FK");
266
266
  if (serverManaged || hasFK) {
267
267
  if (dbCtx.mode === "replace" && meta.has("meta.id") && !ctx.path.includes(".")) return;
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_db_error = require("./db-error-JcfJqqpk.cjs");
3
- const require_validator = require("./validator-DHwSyBej.cjs");
3
+ const require_validator = require("./validator-B4YDLUkj.cjs");
4
4
  const require_object = require("./object-Djg28csK.cjs");
5
5
  const require_ops = require("./ops.cjs");
6
6
  exports.$cas = require_ops.$cas;
@@ -1,5 +1,5 @@
1
1
  import { r as DbError } from "./db-error-eSsQX7rt.mjs";
2
- import { a as forceNavNonOptional, c as getKeyProps, i as dbPlugin, n as buildPatchPartial, o as isNavRelation, r as buildValidationContext, s as createDbValidatorPlugin, t as buildDbValidator } from "./validator-BC3nwFi4.mjs";
2
+ import { a as forceNavNonOptional, c as getKeyProps, i as dbPlugin, n as buildPatchPartial, o as isNavRelation, r as buildValidationContext, s as createDbValidatorPlugin, t as buildDbValidator } from "./validator-B3fPJjS1.mjs";
3
3
  import { a as isPlainObject, i as isEmptyObject } from "./object-TkiJQ-Dp.mjs";
4
4
  import { $cas, $dec, $inc, $insert, $mul, $remove, $replace, $update, $upsert, isDbFieldOp, reconcileCas, separateCas } from "./ops.mjs";
5
5
  export { $cas, $dec, $inc, $insert, $mul, $remove, $replace, $update, $upsert, DbError, buildDbValidator, buildPatchPartial, buildValidationContext, createDbValidatorPlugin, dbPlugin, forceNavNonOptional, getKeyProps, isDbFieldOp, isEmptyObject, isNavRelation, isPlainObject, reconcileCas, separateCas };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db",
3
- "version": "0.1.155",
3
+ "version": "0.1.156",
4
4
  "description": "Database adapter utilities for atscript.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -71,14 +71,14 @@
71
71
  "access": "public"
72
72
  },
73
73
  "devDependencies": {
74
- "@atscript/core": "^0.1.105",
75
- "@atscript/typescript": "^0.1.105",
74
+ "@atscript/core": "^0.1.106",
75
+ "@atscript/typescript": "^0.1.106",
76
76
  "@uniqu/core": "^0.1.15",
77
- "unplugin-atscript": "^0.1.105"
77
+ "unplugin-atscript": "^0.1.106"
78
78
  },
79
79
  "peerDependencies": {
80
- "@atscript/core": "^0.1.105",
81
- "@atscript/typescript": "^0.1.105",
80
+ "@atscript/core": "^0.1.106",
81
+ "@atscript/typescript": "^0.1.106",
82
82
  "@uniqu/core": "^0.1.15"
83
83
  },
84
84
  "scripts": {