@vibeorm/sql 5.0.0 → 5.1.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/ast.d.ts CHANGED
@@ -10,7 +10,7 @@ import type { RlsReadinessQuery } from "./dialects/postgres-rls-readiness.ts";
10
10
  * grows with the runtime in P2/P3. Every shape added here needs a catalog case
11
11
  * (tests/catalog/cases.ts) in the same change.
12
12
  */
13
- import type { AdvisoryLockMode, RlsContextIR, RowLockStrength, RowLockWaitPolicy, ScalarType, SchemaIR, SqlFragmentSpec } from "@vibeorm/schema";
13
+ import type { AdvisoryLockMode, DateTruncationStorage, DateTruncationUnit, RlsContextIR, RowLockStrength, RowLockWaitPolicy, ScalarType, SchemaIR, SqlFragmentSpec } from "@vibeorm/schema";
14
14
  /** A column reference, optionally table-qualified. */
15
15
  export type ColRef = {
16
16
  readonly table?: string;
@@ -53,6 +53,17 @@ export type ColRef = {
53
53
  * column. Never set on projections, so read-back keeps the stored text.
54
54
  */
55
55
  readonly castJsonb?: "jsonb" | "jsonb[]";
56
+ /**
57
+ * Calendar truncation of a timestamp column, for a `groupBy` date key. The
58
+ * reference renders as the start of its period in a named time zone
59
+ * (`date_trunc('day', "col", 'Europe/Oslo')`; see `renderDateTruncation`),
60
+ * in the column's own SQL type. Set on the projection, GROUP BY and ORDER BY
61
+ * refs of one key, which then repeat the identical expression text
62
+ * (PostgreSQL matches GROUP BY expressions by structure); a projection keeps
63
+ * `alias ?? name` as its output name. Never set on filter refs. Dialects
64
+ * without the `dateTruncation` capability refuse it when rendering.
65
+ */
66
+ readonly truncate?: DateTruncation;
56
67
  /**
57
68
  * Projection alias: renders `ref AS "alias"`. PROJECTION-ONLY — filter,
58
69
  * order and correlation refs never set it (the renderer ignores it outside
@@ -61,6 +72,13 @@ export type ColRef = {
61
72
  */
62
73
  readonly alias?: string;
63
74
  };
75
+ /** A {@link ColRef.truncate} request: the calendar unit, the zone that defines the calendar, and the column's storage form. */
76
+ export type DateTruncation = {
77
+ readonly unit: DateTruncationUnit;
78
+ /** A time-zone name (`"Europe/Oslo"`, `"UTC"`), rendered as a string literal after a strict character check. */
79
+ readonly timeZone: string;
80
+ readonly storage: DateTruncationStorage;
81
+ };
64
82
  export type TableRef = {
65
83
  readonly name: string;
66
84
  /** Explicit fixed PostgreSQL placement; absence preserves existing SQL. */
@@ -969,6 +987,16 @@ export type OnConflictUpdate = {
969
987
  */
970
988
  readonly conflictWhere?: string;
971
989
  readonly update: Readonly<Record<string, unknown>>;
990
+ /**
991
+ * Guard on the update arm, BOUND parameters: `ON CONFLICT (…) DO UPDATE SET
992
+ * … WHERE <guard>` — the conflicting row is updated (and returned) only when
993
+ * the guard admits it; otherwise the statement writes and returns nothing.
994
+ * Postgres has both the target row and `excluded` in scope there, so every
995
+ * column ref must carry `table` (the target table's name), exactly as the
996
+ * SET self-references do. Needs the `conflictUpdateWhere` capability:
997
+ * mysql's ODKU has no WHERE and refuses it (`VIBE_UNSUPPORTED_CAPABILITY`).
998
+ */
999
+ readonly updateWhere?: SqlExpr;
972
1000
  /**
973
1001
  * MySQL only (`upsertForm: "onDuplicateKey"`): append
974
1002
  * `` `col` = LAST_INSERT_ID(`col`) `` to the update list so the UPDATE arm
@@ -0,0 +1,27 @@
1
+ import type { DateTruncation } from "./ast.ts";
2
+ import type { SqlDialect } from "./dialect.ts";
3
+ /** Is this value a time-zone name the renderer will spell as a literal? (See {@link TIME_ZONE_NAME}.) */
4
+ export declare function isTimeZoneName(params: {
5
+ readonly value: unknown;
6
+ }): params is {
7
+ readonly value: string;
8
+ };
9
+ /**
10
+ * The start of the period that holds `reference`'s value, in the column's own
11
+ * SQL type, so the field's codec decodes it unchanged:
12
+ *
13
+ * - `timestamptz` → `date_trunc('day', "col", 'Europe/Oslo')` — the 3-argument
14
+ * form (PostgreSQL 12 or later) truncates in the named zone, never in the
15
+ * session's `TimeZone`;
16
+ * - `timestamp` (zone-less, UTC wall-clock by contract) →
17
+ * `(date_trunc('day', "col" AT TIME ZONE 'UTC', 'Europe/Oslo') AT TIME ZONE 'UTC')`.
18
+ *
19
+ * The unit and the zone are literals, so every site of one key (projection,
20
+ * GROUP BY, ORDER BY) repeats the identical text and binds no parameter.
21
+ */
22
+ export declare function renderDateTruncation(params: {
23
+ readonly dialect: SqlDialect;
24
+ readonly reference: string;
25
+ readonly truncation: DateTruncation;
26
+ }): string;
27
+ //# sourceMappingURL=date-truncation.d.ts.map
package/dist/index.d.ts CHANGED
@@ -40,7 +40,7 @@ export type { ResultDecoder, ResultReadDecoder, ResultParameterEncoder, ResultSc
40
40
  * lives in this package; see the root sql-catalog.md for the reviewable surface.
41
41
  */
42
42
  export { compileExclusion, renderExclusionSetup, renderExclusionDdl, renderExclusionCatalog, renderExclusionProbe } from "./exclusion.ts";
43
- export type { AdvisoryLockKey, AdvisoryLockStatement, AggFn, AggRef, AggSelect, ChainResult, ChainRowSource, ChainStatement, ChainStep, ChainStepStatement, ChainTally, ChainVeto, ColRef, ComparisonOp, CountSubquery, DeleteStatement, ExcludedRef, InnerJoin, KeyListMember, KeyPage, KeyRows, KeyTableJoin, KeyTableMember, InsertSelectStatement, InsertStatement, ReturningColumn, JsonFilterOp, LateralJoin, LeftJoin, ListSetOperation, OnConflictClause, OnConflictDoNothing, OnConflictUpdate, OrderByAggregate, OrderByColumn, OrderBySubquery, OrderTerm, RowLock, RowNumberProjection, DbNowMarker, JsonParamCell, SelectExpr, SelectStatement, SetOperation, SetOperator, SqlExpr, SqlStatement, StepColumnRef, TableRef, UpdateStatement, } from "./ast.ts";
43
+ export type { AdvisoryLockKey, AdvisoryLockStatement, AggFn, AggRef, AggSelect, ChainResult, ChainRowSource, ChainStatement, ChainStep, ChainStepStatement, ChainTally, ChainVeto, ColRef, ComparisonOp, DateTruncation, CountSubquery, DeleteStatement, ExcludedRef, InnerJoin, KeyListMember, KeyPage, KeyRows, KeyTableJoin, KeyTableMember, InsertSelectStatement, InsertStatement, ReturningColumn, JsonFilterOp, LateralJoin, LeftJoin, ListSetOperation, OnConflictClause, OnConflictDoNothing, OnConflictUpdate, OrderByAggregate, OrderByColumn, OrderBySubquery, OrderTerm, RowLock, RowNumberProjection, DbNowMarker, JsonParamCell, SelectExpr, SelectStatement, SetOperation, SetOperator, SqlExpr, SqlStatement, StepColumnRef, TableRef, UpdateStatement, } from "./ast.ts";
44
44
  export { ADVISORY_LOCK_RESULT_COLUMN, CHAIN_VETO_PREFIX, CHAIN_VETO_TOKEN_PATTERN, SQL_DEFAULT_CELL, SQL_KEEP_CELL, chainVetoToken, dbNowMarker, excludedRef, isDbNowMarker, isExcludedRef, isJsonParamCell, isListSetOperation, isSetOperation, isSqlDefaultCell, isSqlKeepCell, isStepColumnRef, jsonParamCell, returningColumnName, stepColumnRef, } from "./ast.ts";
45
45
  export type { AdvisoryLockFunctions, AdvisoryLockModeFunctions, RowLockSyntax, EmptyInsertSyntax, SqlDialect, } from "./dialect.ts";
46
46
  export { ADVISORY_KEY_BOUNDS, describeRowLock, renderAdvisoryLock, renderRowLockClause } from "./locks.ts";
@@ -53,6 +53,7 @@ export { tableDefinitionParts } from "./dialects/sqlite-table-definition.ts";
53
53
  export { keyListBindsUntypedRows, postgresBulkInsertArrayCastOf, postgresKeyListElementTypeOf } from "./key-list.ts";
54
54
  export { renderPredicate, renderSchemaExpression } from "./predicate.ts";
55
55
  export { renderLocalDateCheck } from "./local-date.ts";
56
+ export { isTimeZoneName, renderDateTruncation } from "./date-truncation.ts";
56
57
  export { renderGeneratedColumnClause } from "./generated-columns.ts";
57
58
  export type { GeneratedColumnDeclaration, ExtractedGeneratedColumn } from "./generated-columns.ts";
58
59
  export { renderPostgresGeneratedColumnProbe, renderPostgresDropGeneratedExpression } from "./dialects/postgres-generated-columns.ts";