metal-orm 1.0.94 → 1.0.96

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metal-orm",
3
- "version": "1.0.94",
3
+ "version": "1.0.96",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "engines": {
package/src/index.ts CHANGED
@@ -33,9 +33,10 @@ export * from './core/functions/array.js';
33
33
  export * from './orm/als.js';
34
34
  export * from './orm/hydration.js';
35
35
  export * from './codegen/typescript.js';
36
- export * from './orm/orm-session.js';
37
- export * from './orm/orm.js';
38
- export * from './orm/entity.js';
36
+ export * from './orm/orm-session.js';
37
+ export * from './orm/orm.js';
38
+ export * from './orm/column-introspection.js';
39
+ export * from './orm/entity.js';
39
40
  export * from './orm/lazy-batch.js';
40
41
  export * from './orm/relations/has-many.js';
41
42
  export * from './orm/relations/belongs-to.js';
@@ -0,0 +1,43 @@
1
+ import { normalizeColumnType } from '../schema/column-types.js';
2
+ import type { ColumnType } from '../schema/column-types.js';
3
+ import type { TableDef } from '../schema/table.js';
4
+ import type { ColumnDefLike, EntityConstructor } from './entity-metadata.js';
5
+ import { getEntityMetadata } from './entity-metadata.js';
6
+
7
+ type ColumnTarget = TableDef | EntityConstructor;
8
+
9
+ const getColumnDefFromTarget = (
10
+ target: ColumnTarget,
11
+ column: string
12
+ ): ColumnDefLike | undefined => {
13
+ if (typeof target === 'function') {
14
+ const meta = getEntityMetadata(target);
15
+ if (!meta?.columns) return undefined;
16
+ return meta.columns[column];
17
+ }
18
+
19
+ const table = target as TableDef;
20
+ return table.columns[column];
21
+ };
22
+
23
+ export const getColumnType = (
24
+ target: ColumnTarget,
25
+ column: string
26
+ ): ColumnType | undefined => {
27
+ const col = getColumnDefFromTarget(target, column);
28
+ if (!col?.type) return undefined;
29
+ return normalizeColumnType(col.type);
30
+ };
31
+
32
+ export const getDateKind = (
33
+ target: ColumnTarget,
34
+ column: string
35
+ ): 'date' | 'date-time' | undefined => {
36
+ const type = getColumnType(target, column);
37
+ if (!type) return undefined;
38
+ if (type === 'date') return 'date';
39
+ if (type === 'datetime' || type === 'timestamp' || type === 'timestamptz') {
40
+ return 'date-time';
41
+ }
42
+ return undefined;
43
+ };
@@ -14,10 +14,16 @@ import {
14
14
  import { JoinNode } from '../core/ast/join.js';
15
15
  import { createTableNode } from '../core/ast/builders.js';
16
16
 
17
+ /**
18
+ * Allowed literal type names - single source of truth for both type and error messages
19
+ */
20
+ const LITERAL_VALUE_TYPES = ['string', 'number', 'boolean', 'Date', 'null'] as const;
21
+
17
22
  /**
18
23
  * Literal values that can be used in UPDATE statements
24
+ * Derived from LITERAL_VALUE_TYPES to maintain single source of truth
19
25
  */
20
- type LiteralValue = string | number | boolean | null;
26
+ type LiteralValue = typeof LITERAL_VALUE_TYPES[number] | null;
21
27
 
22
28
  /**
23
29
  * Values allowed in UPDATE SET clauses
@@ -31,6 +37,7 @@ type UpdateValue = OperandNode | LiteralValue;
31
37
  */
32
38
  const isUpdateValue = (value: unknown): value is UpdateValue => {
33
39
  if (value === null) return true;
40
+ if (value instanceof Date) return true;
34
41
  switch (typeof value) {
35
42
  case 'string':
36
43
  case 'number':
@@ -80,8 +87,9 @@ export class UpdateQueryState {
80
87
  withSet(values: Record<string, unknown>): UpdateQueryState {
81
88
  const assignments: UpdateAssignmentNode[] = Object.entries(values).map(([column, rawValue]) => {
82
89
  if (!isUpdateValue(rawValue)) {
90
+ const allowedTypes = [...LITERAL_VALUE_TYPES, 'OperandNode'];
83
91
  throw new Error(
84
- `Invalid update value for column "${column}": only primitives, null, or OperandNodes are allowed`
92
+ `Invalid update value for column "${column}": only ${allowedTypes.join(', ')} are allowed`
85
93
  );
86
94
  }
87
95