@drzl/analyzer 1.10.0 → 1.12.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
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  SchemaAnalyzer: () => SchemaAnalyzer,
34
34
  default: () => index_default,
35
35
  describeV1Column: () => describeV1Column,
36
+ isReadOnlyRelation: () => isReadOnlyRelation,
36
37
  isRelationsV2: () => isRelationsV2,
37
38
  readRelationsV2: () => readRelationsV2
38
39
  });
@@ -228,6 +229,12 @@ function getSymbolOf(target, key) {
228
229
  }
229
230
  return target[Symbol.for(key)];
230
231
  }
232
+ function isReadOnlyRelation(val) {
233
+ if (!val || typeof val !== "object") return false;
234
+ return Object.getOwnPropertySymbols(val).some(
235
+ (sym) => String(sym.description).includes("MaterializedViewConfig")
236
+ );
237
+ }
231
238
  function isRelationsV2(val) {
232
239
  if (!val || typeof val !== "object" || Array.isArray(val)) return false;
233
240
  const entries = Object.values(val);
@@ -651,6 +658,8 @@ var _SchemaAnalyzer = class _SchemaAnalyzer {
651
658
  }
652
659
  const ev = col?.enumValues;
653
660
  const nullable = !col?.notNull && !col?.config?.notNull;
661
+ const rawDefault = col?.default;
662
+ const defaultValue = rawDefault !== void 0 && !(rawDefault && typeof rawDefault === "object" && "queryChunks" in rawDefault) ? rawDefault : void 0;
654
663
  const generatedIdentity = col?.generatedIdentity;
655
664
  const isGenerated = !!(col?.generated || generatedIdentity?.type === "always" || col?.isGenerated);
656
665
  const hasDefault = col?.hasDefault === true || col?.default !== void 0 || col?.config?.default !== void 0 || col?.defaultFn !== void 0 || isGenerated;
@@ -678,6 +687,7 @@ var _SchemaAnalyzer = class _SchemaAnalyzer {
678
687
  defaultExpression: void 0,
679
688
  references,
680
689
  enumValues: Array.isArray(ev) ? ev : void 0,
690
+ ...defaultValue !== void 0 ? { defaultValue } : {},
681
691
  ...constraints,
682
692
  ...v1 ?? {}
683
693
  });
@@ -754,6 +764,9 @@ var _SchemaAnalyzer = class _SchemaAnalyzer {
754
764
  indexes: [...pkCols.length ? [{ columns: pkCols }] : [], ...indexes],
755
765
  checks,
756
766
  foreignKeys,
767
+ // A materialized view refuses every write, so the generators skip its insert and update
768
+ // schemas rather than describe an operation the database will always reject.
769
+ ...isReadOnlyRelation(tbl) ? { readOnly: true } : {},
757
770
  meta: {}
758
771
  };
759
772
  }
@@ -954,6 +967,7 @@ var index_default = SchemaAnalyzer;
954
967
  0 && (module.exports = {
955
968
  SchemaAnalyzer,
956
969
  describeV1Column,
970
+ isReadOnlyRelation,
957
971
  isRelationsV2,
958
972
  readRelationsV2
959
973
  });
package/dist/index.d.cts CHANGED
@@ -72,6 +72,15 @@ interface Column {
72
72
  * checking at all. See `COLUMN_FORMATS` in `@drzl/validation-core`.
73
73
  */
74
74
  format?: 'uuid' | 'numeric';
75
+ /**
76
+ * The column's default, when it is a literal a schema can reproduce.
77
+ *
78
+ * `.default('GB')` stores a plain JS value. `defaultNow()`, `defaultRandom()` and any
79
+ * `sql` default store an object the database evaluates, and `$defaultFn` stores a function
80
+ * Drizzle calls at insert time. A schema guessing at either of those would produce a different
81
+ * value than the one actually stored, so only the literal case is carried.
82
+ */
83
+ defaultValue?: unknown;
75
84
  /**
76
85
  * Array depth for a column declared with `.array()`, absent when the column is a scalar.
77
86
  *
@@ -173,6 +182,12 @@ interface Table {
173
182
  indexes: Index[];
174
183
  checks?: Check[];
175
184
  foreignKeys?: ForeignKey[];
185
+ /**
186
+ * Set when the relation refuses writes, which today means a materialized view. Insert and
187
+ * update schemas are not emitted for one, because the database will always refuse the
188
+ * operation they describe.
189
+ */
190
+ readOnly?: boolean;
176
191
  meta?: Record<string, unknown>;
177
192
  }
178
193
  interface Enum {
@@ -214,6 +229,18 @@ declare function describeV1Column(column: any): Partial<Column> | null;
214
229
  * `relations` record whose entries carry a `relationType`. That is specific enough not to
215
230
  * collide with a table or an enum, both of which are checked before this.
216
231
  */
232
+ /**
233
+ * Whether a relation refuses writes outright.
234
+ *
235
+ * A materialized view does: `INSERT INTO mv ...` fails with `cannot change materialized view`,
236
+ * verified against Postgres. An insert or update schema for one describes an operation the
237
+ * database will always refuse.
238
+ *
239
+ * An ordinary view is deliberately not included. Postgres accepts an INSERT into a simple
240
+ * auto-updatable view, and whether a given view qualifies depends on its query rather than on
241
+ * anything the schema file states, so refusing them all would take away something that works.
242
+ */
243
+ declare function isReadOnlyRelation(val: any): boolean;
217
244
  declare function isRelationsV2(val: any): boolean;
218
245
  /**
219
246
  * Read the relations declared by `defineRelations`.
@@ -329,4 +356,4 @@ declare class SchemaAnalyzer {
329
356
  analyze(opts?: AnalyzeOptions): Promise<Analysis>;
330
357
  }
331
358
 
332
- export { type Analysis, type AnalyzeOptions, type Check, type Column, type ColumnRef, type ColumnShape, type Dialect, type Enum, type ForeignKey, type Index, type Issue, type Key, type Relation, SchemaAnalyzer, type Table, SchemaAnalyzer as default, describeV1Column, isRelationsV2, readRelationsV2 };
359
+ export { type Analysis, type AnalyzeOptions, type Check, type Column, type ColumnRef, type ColumnShape, type Dialect, type Enum, type ForeignKey, type Index, type Issue, type Key, type Relation, SchemaAnalyzer, type Table, SchemaAnalyzer as default, describeV1Column, isReadOnlyRelation, isRelationsV2, readRelationsV2 };
package/dist/index.d.ts CHANGED
@@ -72,6 +72,15 @@ interface Column {
72
72
  * checking at all. See `COLUMN_FORMATS` in `@drzl/validation-core`.
73
73
  */
74
74
  format?: 'uuid' | 'numeric';
75
+ /**
76
+ * The column's default, when it is a literal a schema can reproduce.
77
+ *
78
+ * `.default('GB')` stores a plain JS value. `defaultNow()`, `defaultRandom()` and any
79
+ * `sql` default store an object the database evaluates, and `$defaultFn` stores a function
80
+ * Drizzle calls at insert time. A schema guessing at either of those would produce a different
81
+ * value than the one actually stored, so only the literal case is carried.
82
+ */
83
+ defaultValue?: unknown;
75
84
  /**
76
85
  * Array depth for a column declared with `.array()`, absent when the column is a scalar.
77
86
  *
@@ -173,6 +182,12 @@ interface Table {
173
182
  indexes: Index[];
174
183
  checks?: Check[];
175
184
  foreignKeys?: ForeignKey[];
185
+ /**
186
+ * Set when the relation refuses writes, which today means a materialized view. Insert and
187
+ * update schemas are not emitted for one, because the database will always refuse the
188
+ * operation they describe.
189
+ */
190
+ readOnly?: boolean;
176
191
  meta?: Record<string, unknown>;
177
192
  }
178
193
  interface Enum {
@@ -214,6 +229,18 @@ declare function describeV1Column(column: any): Partial<Column> | null;
214
229
  * `relations` record whose entries carry a `relationType`. That is specific enough not to
215
230
  * collide with a table or an enum, both of which are checked before this.
216
231
  */
232
+ /**
233
+ * Whether a relation refuses writes outright.
234
+ *
235
+ * A materialized view does: `INSERT INTO mv ...` fails with `cannot change materialized view`,
236
+ * verified against Postgres. An insert or update schema for one describes an operation the
237
+ * database will always refuse.
238
+ *
239
+ * An ordinary view is deliberately not included. Postgres accepts an INSERT into a simple
240
+ * auto-updatable view, and whether a given view qualifies depends on its query rather than on
241
+ * anything the schema file states, so refusing them all would take away something that works.
242
+ */
243
+ declare function isReadOnlyRelation(val: any): boolean;
217
244
  declare function isRelationsV2(val: any): boolean;
218
245
  /**
219
246
  * Read the relations declared by `defineRelations`.
@@ -329,4 +356,4 @@ declare class SchemaAnalyzer {
329
356
  analyze(opts?: AnalyzeOptions): Promise<Analysis>;
330
357
  }
331
358
 
332
- export { type Analysis, type AnalyzeOptions, type Check, type Column, type ColumnRef, type ColumnShape, type Dialect, type Enum, type ForeignKey, type Index, type Issue, type Key, type Relation, SchemaAnalyzer, type Table, SchemaAnalyzer as default, describeV1Column, isRelationsV2, readRelationsV2 };
359
+ export { type Analysis, type AnalyzeOptions, type Check, type Column, type ColumnRef, type ColumnShape, type Dialect, type Enum, type ForeignKey, type Index, type Issue, type Key, type Relation, SchemaAnalyzer, type Table, SchemaAnalyzer as default, describeV1Column, isReadOnlyRelation, isRelationsV2, readRelationsV2 };
package/dist/index.js CHANGED
@@ -189,6 +189,12 @@ function getSymbolOf(target, key) {
189
189
  }
190
190
  return target[Symbol.for(key)];
191
191
  }
192
+ function isReadOnlyRelation(val) {
193
+ if (!val || typeof val !== "object") return false;
194
+ return Object.getOwnPropertySymbols(val).some(
195
+ (sym) => String(sym.description).includes("MaterializedViewConfig")
196
+ );
197
+ }
192
198
  function isRelationsV2(val) {
193
199
  if (!val || typeof val !== "object" || Array.isArray(val)) return false;
194
200
  const entries = Object.values(val);
@@ -612,6 +618,8 @@ var _SchemaAnalyzer = class _SchemaAnalyzer {
612
618
  }
613
619
  const ev = col?.enumValues;
614
620
  const nullable = !col?.notNull && !col?.config?.notNull;
621
+ const rawDefault = col?.default;
622
+ const defaultValue = rawDefault !== void 0 && !(rawDefault && typeof rawDefault === "object" && "queryChunks" in rawDefault) ? rawDefault : void 0;
615
623
  const generatedIdentity = col?.generatedIdentity;
616
624
  const isGenerated = !!(col?.generated || generatedIdentity?.type === "always" || col?.isGenerated);
617
625
  const hasDefault = col?.hasDefault === true || col?.default !== void 0 || col?.config?.default !== void 0 || col?.defaultFn !== void 0 || isGenerated;
@@ -639,6 +647,7 @@ var _SchemaAnalyzer = class _SchemaAnalyzer {
639
647
  defaultExpression: void 0,
640
648
  references,
641
649
  enumValues: Array.isArray(ev) ? ev : void 0,
650
+ ...defaultValue !== void 0 ? { defaultValue } : {},
642
651
  ...constraints,
643
652
  ...v1 ?? {}
644
653
  });
@@ -715,6 +724,9 @@ var _SchemaAnalyzer = class _SchemaAnalyzer {
715
724
  indexes: [...pkCols.length ? [{ columns: pkCols }] : [], ...indexes],
716
725
  checks,
717
726
  foreignKeys,
727
+ // A materialized view refuses every write, so the generators skip its insert and update
728
+ // schemas rather than describe an operation the database will always reject.
729
+ ...isReadOnlyRelation(tbl) ? { readOnly: true } : {},
718
730
  meta: {}
719
731
  };
720
732
  }
@@ -915,6 +927,7 @@ export {
915
927
  SchemaAnalyzer,
916
928
  index_default as default,
917
929
  describeV1Column,
930
+ isReadOnlyRelation,
918
931
  isRelationsV2,
919
932
  readRelationsV2
920
933
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drzl/analyzer",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "private": false,
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",