tempest-db-js 0.3.0 → 0.4.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.
package/dist/index.cjs CHANGED
@@ -1751,7 +1751,8 @@ function createPostgresDriver(url, pool) {
1751
1751
  var DEFAULT_FLAGS = {
1752
1752
  primaryKey: false,
1753
1753
  notNull: false,
1754
- hasDefault: false
1754
+ hasDefault: false,
1755
+ unique: false
1755
1756
  };
1756
1757
  var sql = {
1757
1758
  /** Current timestamp at insert (`CURRENT_TIMESTAMP` / `now()`). */
@@ -1771,23 +1772,38 @@ var sql = {
1771
1772
  function isDefaultValue(value) {
1772
1773
  return typeof value === "object" && value !== null && "kind" in value && (value.kind === "literal" || value.kind === "expression");
1773
1774
  }
1775
+ function parseReference(ref, options) {
1776
+ const dot = ref.lastIndexOf(".");
1777
+ if (dot <= 0 || dot === ref.length - 1) {
1778
+ throw new Error(`Invalid foreign key reference "${ref}"; expected "table.column".`);
1779
+ }
1780
+ return {
1781
+ table: ref.slice(0, dot),
1782
+ column: ref.slice(dot + 1),
1783
+ onDelete: options?.onDelete,
1784
+ onUpdate: options?.onUpdate
1785
+ };
1786
+ }
1774
1787
  var Column = class _Column {
1775
- constructor(type, flags, defaultValue = null, onUpdateValue = null) {
1788
+ constructor(type, flags, defaultValue = null, onUpdateValue = null, reference = null) {
1776
1789
  this.type = type;
1777
1790
  this.flags = flags;
1778
1791
  this.defaultValue = defaultValue;
1779
1792
  this.onUpdateValue = onUpdateValue;
1793
+ this.reference = reference;
1780
1794
  }
1781
1795
  type;
1782
1796
  flags;
1783
1797
  defaultValue;
1784
1798
  onUpdateValue;
1799
+ reference;
1785
1800
  primaryKey() {
1786
1801
  return new _Column(
1787
1802
  this.type,
1788
1803
  { ...this.flags, primaryKey: true, hasDefault: true },
1789
1804
  this.defaultValue,
1790
- this.onUpdateValue
1805
+ this.onUpdateValue,
1806
+ this.reference
1791
1807
  );
1792
1808
  }
1793
1809
  notNull() {
@@ -1795,7 +1811,40 @@ var Column = class _Column {
1795
1811
  this.type,
1796
1812
  { ...this.flags, notNull: true },
1797
1813
  this.defaultValue,
1798
- this.onUpdateValue
1814
+ this.onUpdateValue,
1815
+ this.reference
1816
+ );
1817
+ }
1818
+ /**
1819
+ * Add a `UNIQUE` constraint to the column (mirrors SQLAlchemy's
1820
+ * `mapped_column(unique=True)`). DDL-only — does not change the inferred type.
1821
+ */
1822
+ unique() {
1823
+ return new _Column(
1824
+ this.type,
1825
+ { ...this.flags, unique: true },
1826
+ this.defaultValue,
1827
+ this.onUpdateValue,
1828
+ this.reference
1829
+ );
1830
+ }
1831
+ /**
1832
+ * Declare a foreign-key reference to another table's column, à la SQLAlchemy's
1833
+ * `mapped_column(ForeignKey("table.column", ondelete=...))`. DDL-only — does
1834
+ * not change the inferred type.
1835
+ *
1836
+ * @param ref The target as `"table.column"` (e.g. `"users.id"`).
1837
+ * @param options Optional `onDelete` / `onUpdate` referential actions.
1838
+ * @returns A new column carrying the reference.
1839
+ * @throws Error When `ref` is not a valid `"table.column"` string.
1840
+ */
1841
+ references(ref, options) {
1842
+ return new _Column(
1843
+ this.type,
1844
+ this.flags,
1845
+ this.defaultValue,
1846
+ this.onUpdateValue,
1847
+ parseReference(ref, options)
1799
1848
  );
1800
1849
  }
1801
1850
  /**
@@ -1808,7 +1857,8 @@ var Column = class _Column {
1808
1857
  this.type,
1809
1858
  { ...this.flags, hasDefault: true },
1810
1859
  resolved,
1811
- this.onUpdateValue
1860
+ this.onUpdateValue,
1861
+ this.reference
1812
1862
  );
1813
1863
  }
1814
1864
  /**
@@ -1817,7 +1867,7 @@ var Column = class _Column {
1817
1867
  */
1818
1868
  onUpdate(value) {
1819
1869
  const resolved = isDefaultValue(value) ? value : { kind: "literal", value };
1820
- return new _Column(this.type, this.flags, this.defaultValue, resolved);
1870
+ return new _Column(this.type, this.flags, this.defaultValue, resolved, this.reference);
1821
1871
  }
1822
1872
  };
1823
1873
  function makeColumn(kind, meta = {}) {
@@ -1871,8 +1921,36 @@ var column = {
1871
1921
  /** `ENUM(...values)` → a string-literal union of the given values. */
1872
1922
  enum: (...values) => makeColumn("enum", { values })
1873
1923
  };
1924
+ function unique(...columns) {
1925
+ if (columns.length === 0) {
1926
+ throw new Error("unique() requires at least one column.");
1927
+ }
1928
+ return { kind: "unique", columns };
1929
+ }
1930
+ function foreignKey(columns, refTable, refColumns, options) {
1931
+ if (columns.length === 0 || columns.length !== refColumns.length) {
1932
+ throw new Error(
1933
+ "foreignKey() requires matching, non-empty local and referenced column lists."
1934
+ );
1935
+ }
1936
+ return {
1937
+ kind: "foreignKey",
1938
+ name: options?.name,
1939
+ columns,
1940
+ refTable,
1941
+ refColumns,
1942
+ onDelete: options?.onDelete,
1943
+ onUpdate: options?.onUpdate
1944
+ };
1945
+ }
1874
1946
  var Model = class {
1875
1947
  static tablename;
1948
+ /**
1949
+ * Optional table-level constraints (composite unique / foreign keys), returned
1950
+ * by a thunk so forward references resolve lazily. Mirrors SQLAlchemy's
1951
+ * `__table_args__`.
1952
+ */
1953
+ static tableArgs;
1876
1954
  };
1877
1955
  var columnsCache = /* @__PURE__ */ new WeakMap();
1878
1956
  function columnsOf(model) {
@@ -1927,6 +2005,7 @@ exports.createEngine = createEngine;
1927
2005
  exports.createSyncEngine = createSyncEngine;
1928
2006
  exports.del = del;
1929
2007
  exports.detectDialect = detectDialect;
2008
+ exports.foreignKey = foreignKey;
1930
2009
  exports.fromDict = fromDict;
1931
2010
  exports.getDialect = getDialect;
1932
2011
  exports.hasMany = hasMany;
@@ -1947,6 +2026,7 @@ exports.sum = sum;
1947
2026
  exports.toCondNode = toCondNode;
1948
2027
  exports.toDict = toDict;
1949
2028
  exports.toJSON = toJSON;
2029
+ exports.unique = unique;
1950
2030
  exports.update = update;
1951
2031
  //# sourceMappingURL=index.cjs.map
1952
2032
  //# sourceMappingURL=index.cjs.map