tempest-db-js 0.1.0 → 0.2.0

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.
@@ -1,28 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var module$1 = require('module');
4
-
5
- var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
6
- // src/query.ts
7
- var OPERATORS = [
8
- "eq",
9
- "ne",
10
- "gt",
11
- "gte",
12
- "lt",
13
- "lte",
14
- "like",
15
- "ilike",
16
- "in",
17
- "notIn",
18
- "between",
19
- "isNull"
20
- ];
21
-
22
- // src/dialect.ts
23
- new Set(OPERATORS);
24
- module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
25
-
26
3
  // src/index.ts
27
4
  function isDefaultValue(value) {
28
5
  return typeof value === "object" && value !== null && "kind" in value && (value.kind === "literal" || value.kind === "expression");
@@ -76,7 +53,10 @@ var Column = class _Column {
76
53
  return new _Column(this.type, this.flags, this.defaultValue, resolved);
77
54
  }
78
55
  };
56
+ var columnsCache = /* @__PURE__ */ new WeakMap();
79
57
  function columnsOf(model) {
58
+ const cached = columnsCache.get(model);
59
+ if (cached) return cached;
80
60
  const instance = new model();
81
61
  const out = {};
82
62
  for (const [key, value] of Object.entries(instance)) {
@@ -84,6 +64,7 @@ function columnsOf(model) {
84
64
  out[key] = value;
85
65
  }
86
66
  }
67
+ columnsCache.set(model, out);
87
68
  return out;
88
69
  }
89
70
 
@@ -257,6 +238,14 @@ function renderColumnDef(col, dialect) {
257
238
  function enumTypeName(table, column) {
258
239
  return `${table}_${column}`;
259
240
  }
241
+ function isAutoIncrementPk(table, col) {
242
+ return table.primaryKey.length === 1 && table.primaryKey[0] === col.name && col.default === null && (col.type.kind === "smallint" || col.type.kind === "integer" || col.type.kind === "bigint");
243
+ }
244
+ function postgresSerialType(kind) {
245
+ if (kind === "bigint") return "BIGSERIAL";
246
+ if (kind === "smallint") return "SMALLSERIAL";
247
+ return "SERIAL";
248
+ }
260
249
  function renderCreateTable(table, dialect) {
261
250
  const typeStmts = [];
262
251
  const cols = Object.values(table.columns).map((c) => {
@@ -269,6 +258,9 @@ function renderCreateTable(table, dialect) {
269
258
  if (c.default !== null) def += ` DEFAULT ${renderDefault(c.default, dialect)}`;
270
259
  return def;
271
260
  }
261
+ if (dialect === "postgresql" && isAutoIncrementPk(table, c)) {
262
+ return `${quoteId(c.name)} ${postgresSerialType(c.type.kind)}`;
263
+ }
272
264
  return renderColumnDef(c, dialect);
273
265
  });
274
266
  if (table.primaryKey.length > 0) {
@@ -917,13 +909,133 @@ function replaySchema(migrations) {
917
909
  return schema;
918
910
  }
919
911
 
912
+ // src/migrations/renames.ts
913
+ function columnShape(col) {
914
+ return JSON.stringify({
915
+ type: col.type,
916
+ notNull: col.notNull,
917
+ primaryKey: col.primaryKey,
918
+ default: col.default
919
+ });
920
+ }
921
+ function tableShape(columns) {
922
+ return Object.entries(columns).map(([name, col]) => `${name}:${columnShape(col)}`).sort().join("|");
923
+ }
924
+ function detectRenames(ops) {
925
+ const candidates = [];
926
+ const creates = ops.filter((o) => o.kind === "create_table");
927
+ const tableDrops = ops.filter((o) => o.kind === "drop_table");
928
+ const takenCreate = /* @__PURE__ */ new Set();
929
+ const takenDrop = /* @__PURE__ */ new Set();
930
+ for (const create of creates) {
931
+ const createShape = tableShape(create.table.columns);
932
+ const matches = tableDrops.filter(
933
+ (d) => !takenDrop.has(d.table.name) && tableShape(d.table.columns) === createShape
934
+ );
935
+ const uniqueCreate = creates.filter(
936
+ (c) => !takenCreate.has(c.table.name) && tableShape(c.table.columns) === createShape
937
+ ).length === 1;
938
+ if (matches.length === 1 && uniqueCreate) {
939
+ const drop = matches[0];
940
+ if (drop.table.name !== create.table.name) {
941
+ candidates.push({ kind: "table", from: drop.table.name, to: create.table.name });
942
+ takenCreate.add(create.table.name);
943
+ takenDrop.add(drop.table.name);
944
+ }
945
+ }
946
+ }
947
+ const adds = ops.filter((o) => o.kind === "add_column");
948
+ const colDrops = ops.filter((o) => o.kind === "drop_column");
949
+ const tables = /* @__PURE__ */ new Set([...adds.map((o) => o.table), ...colDrops.map((o) => o.table)]);
950
+ for (const table of tables) {
951
+ const tableAdds = adds.filter((o) => o.table === table);
952
+ const tableColDrops = colDrops.filter((o) => o.table === table);
953
+ const takenAdd = /* @__PURE__ */ new Set();
954
+ const takenColDrop = /* @__PURE__ */ new Set();
955
+ for (const add of tableAdds) {
956
+ const shape = columnShape(add.column);
957
+ const dropMatches = tableColDrops.filter(
958
+ (d) => !takenColDrop.has(d.column.name) && columnShape(d.column) === shape
959
+ );
960
+ const addMatches = tableAdds.filter(
961
+ (a) => !takenAdd.has(a.column.name) && columnShape(a.column) === shape
962
+ );
963
+ if (dropMatches.length === 1 && addMatches.length === 1) {
964
+ const drop = dropMatches[0];
965
+ candidates.push({
966
+ kind: "column",
967
+ table,
968
+ from: drop.column.name,
969
+ to: add.column.name
970
+ });
971
+ takenAdd.add(add.column.name);
972
+ takenColDrop.add(drop.column.name);
973
+ }
974
+ }
975
+ }
976
+ return candidates;
977
+ }
978
+ function isTableRename(op, r) {
979
+ return op.kind === "create_table" && op.table.name === r.to || op.kind === "drop_table" && op.table.name === r.from;
980
+ }
981
+ function isColumnRename(op, r) {
982
+ return op.kind === "add_column" && op.table === r.table && op.column.name === r.to || op.kind === "drop_column" && op.table === r.table && op.column.name === r.from;
983
+ }
984
+ function applyRenames(ops, confirmed) {
985
+ const out = [];
986
+ const emitted = /* @__PURE__ */ new Set();
987
+ for (const op of ops) {
988
+ const match = confirmed.find(
989
+ (r) => r.kind === "table" ? isTableRename(op, r) : isColumnRename(op, r)
990
+ );
991
+ if (!match) {
992
+ out.push(op);
993
+ continue;
994
+ }
995
+ if (!emitted.has(match)) {
996
+ emitted.add(match);
997
+ out.push(
998
+ match.kind === "table" ? { kind: "rename_table", from: match.from, to: match.to } : { kind: "rename_column", table: match.table, from: match.from, to: match.to }
999
+ );
1000
+ }
1001
+ }
1002
+ return out;
1003
+ }
1004
+
920
1005
  // src/migrations/cli.ts
1006
+ function defineMigrationConfig(config) {
1007
+ return config;
1008
+ }
921
1009
  function ok(lines) {
922
1010
  return { code: 0, lines };
923
1011
  }
924
1012
  function fail(lines) {
925
1013
  return { code: 1, lines };
926
1014
  }
1015
+ function parseRenameFlags(rest) {
1016
+ const out = [];
1017
+ for (let i = 0; i < rest.length; i += 1) {
1018
+ const arg = rest[i];
1019
+ if (arg === "--rename-table") {
1020
+ const [from, to] = (rest[i + 1] ?? "").split(":");
1021
+ i += 1;
1022
+ if (from && to) out.push({ kind: "table", from, to });
1023
+ } else if (arg === "--rename-column") {
1024
+ const [left, to] = (rest[i + 1] ?? "").split(":");
1025
+ i += 1;
1026
+ const dot = left?.lastIndexOf(".") ?? -1;
1027
+ if (left && to && dot > 0) {
1028
+ out.push({
1029
+ kind: "column",
1030
+ table: left.slice(0, dot),
1031
+ from: left.slice(dot + 1),
1032
+ to
1033
+ });
1034
+ }
1035
+ }
1036
+ }
1037
+ return out;
1038
+ }
927
1039
  function pending(config, runner) {
928
1040
  const done = runner.applied();
929
1041
  return topoOrder(config.migrations).filter((m) => !done.has(m.revision));
@@ -990,7 +1102,11 @@ function runMigrationCli(argv, config) {
990
1102
  const msgIndex = rest.indexOf("-m");
991
1103
  const label = msgIndex >= 0 ? rest[msgIndex + 1] ?? "revision" : "revision";
992
1104
  const parents = heads(config.migrations);
993
- const ops = rest.includes("--autogenerate") ? diffSchema(replaySchema(config.migrations), reflectSchema(config.models)) : [];
1105
+ let ops = rest.includes("--autogenerate") ? diffSchema(replaySchema(config.migrations), reflectSchema(config.models)) : [];
1106
+ if (rest.includes("--autogenerate")) {
1107
+ const confirmed = rest.includes("--autorename") ? detectRenames(ops) : parseRenameFlags(rest);
1108
+ if (confirmed.length > 0) ops = applyRenames(ops, confirmed);
1109
+ }
994
1110
  const source = generateMigration({
995
1111
  revision: makeRevisionId(label, parents),
996
1112
  downRevision: parents,
@@ -1013,8 +1129,11 @@ exports.MigrationRunner = MigrationRunner;
1013
1129
  exports.Op = Op;
1014
1130
  exports.UnknownRevision = UnknownRevision;
1015
1131
  exports.applyOperation = applyOperation;
1132
+ exports.applyRenames = applyRenames;
1016
1133
  exports.checkDrift = checkDrift;
1017
1134
  exports.checkDriftPostgres = checkDriftPostgres;
1135
+ exports.defineMigrationConfig = defineMigrationConfig;
1136
+ exports.detectRenames = detectRenames;
1018
1137
  exports.diffSchema = diffSchema;
1019
1138
  exports.emptySchema = emptySchema;
1020
1139
  exports.generateMigration = generateMigration;