@pylonts/dsl 1.1.2 → 1.1.3

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 (3) hide show
  1. package/dist/db.js +20 -0
  2. package/package.json +2 -2
  3. package/src/db.ts +26 -0
package/dist/db.js CHANGED
@@ -68,9 +68,24 @@ export function defineTable(name, schema) {
68
68
  table.columns[key].name = key;
69
69
  table.columns[key].schema = table;
70
70
  }
71
+ if (table.primaryKey) {
72
+ const pkFields = Array.isArray(table.primaryKey) ? table.primaryKey : [table.primaryKey];
73
+ const colRefs = Object.values(table.columns);
74
+ for (const pk of pkFields) {
75
+ if (!colRefs.includes(pk)) {
76
+ throw new Error(`table '${name}': primaryKey field '${pk.name}' must be the exact same object as the corresponding column (extract it to a const and reuse)`);
77
+ }
78
+ }
79
+ }
71
80
  for (const [fkName, fk] of Object.entries(table.foreignKeys ?? {})) {
72
81
  const refs = Array.isArray(fk.references) ? fk.references : [fk.references];
73
82
  const fields = Array.isArray(fk.columns) ? fk.columns : [fk.columns];
83
+ const colRefs = Object.values(table.columns);
84
+ for (const fkField of fields) {
85
+ if (!colRefs.includes(fkField)) {
86
+ throw new Error(`foreign key ${fkName}: column '${fkField.name}' must be the exact same object as the corresponding column in the table (extract it to a const and reuse)`);
87
+ }
88
+ }
74
89
  for (let i = 0; i < refs.length; i++) {
75
90
  const ref = refs[i];
76
91
  if (!ref.schema)
@@ -89,11 +104,16 @@ export function defineTable(name, schema) {
89
104
  }
90
105
  for (const [idxKey, index] of Object.entries(table.indexes ?? {})) {
91
106
  const cols = Array.isArray(index.columns) ? index.columns : [index.columns];
107
+ const colRefs = Object.values(table.columns);
92
108
  for (const col of cols) {
93
109
  if (typeof col !== 'object' || col === null || typeof col.type !== 'string') {
94
110
  const idxName = index.name ?? idxKey;
95
111
  throw new Error(`index ${idxName}: columns must be Field instances (stringField()/intField()/...), got ${JSON.stringify(col)}`);
96
112
  }
113
+ if (!colRefs.includes(col)) {
114
+ const idxName = index.name ?? idxKey;
115
+ throw new Error(`index ${idxName}: column '${col.name}' must be the exact same object as the corresponding column in the table (extract it to a const and reuse)`);
116
+ }
97
117
  }
98
118
  }
99
119
  return table;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pylonts/dsl",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Schema definition DSL with drivers: MySQL DDL, TS enum, TypeBox schema codegen.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -28,4 +28,4 @@
28
28
  "typescript": "^7.0.2",
29
29
  "vitest": "^4.1.10"
30
30
  }
31
- }
31
+ }
package/src/db.ts CHANGED
@@ -120,9 +120,28 @@ export function defineTable<
120
120
  table.columns[key].name = key;
121
121
  table.columns[key].schema = table;
122
122
  }
123
+ if (table.primaryKey) {
124
+ const pkFields = Array.isArray(table.primaryKey) ? table.primaryKey : [table.primaryKey];
125
+ const colRefs = Object.values(table.columns);
126
+ for (const pk of pkFields) {
127
+ if (!colRefs.includes(pk)) {
128
+ throw new Error(
129
+ `table '${name}': primaryKey field '${pk.name}' must be the exact same object as the corresponding column (extract it to a const and reuse)`,
130
+ );
131
+ }
132
+ }
133
+ }
123
134
  for (const [fkName, fk] of Object.entries(table.foreignKeys ?? {})) {
124
135
  const refs = Array.isArray(fk.references) ? fk.references : [fk.references];
125
136
  const fields = Array.isArray(fk.columns) ? fk.columns : [fk.columns];
137
+ const colRefs = Object.values(table.columns);
138
+ for (const fkField of fields) {
139
+ if (!colRefs.includes(fkField)) {
140
+ throw new Error(
141
+ `foreign key ${fkName}: column '${fkField.name}' must be the exact same object as the corresponding column in the table (extract it to a const and reuse)`,
142
+ );
143
+ }
144
+ }
126
145
  for (let i = 0; i < refs.length; i++) {
127
146
  const ref = refs[i];
128
147
  if (!ref.schema) throw new Error(`foreign key ${fkName}: references field has no schema`);
@@ -140,6 +159,7 @@ export function defineTable<
140
159
  }
141
160
  for (const [idxKey, index] of Object.entries(table.indexes ?? {})) {
142
161
  const cols = Array.isArray(index.columns) ? index.columns : [index.columns];
162
+ const colRefs = Object.values(table.columns);
143
163
  for (const col of cols) {
144
164
  if (typeof col !== 'object' || col === null || typeof (col as Field).type !== 'string') {
145
165
  const idxName = index.name ?? idxKey;
@@ -147,6 +167,12 @@ export function defineTable<
147
167
  `index ${idxName}: columns must be Field instances (stringField()/intField()/...), got ${JSON.stringify(col)}`,
148
168
  );
149
169
  }
170
+ if (!colRefs.includes(col)) {
171
+ const idxName = index.name ?? idxKey;
172
+ throw new Error(
173
+ `index ${idxName}: column '${col.name}' must be the exact same object as the corresponding column in the table (extract it to a const and reuse)`,
174
+ );
175
+ }
150
176
  }
151
177
  }
152
178
  return table;