sumak 0.0.2 → 0.0.4

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.
@@ -14,21 +14,34 @@ interface ColumnType<S, I = S, U = I> {
14
14
  type Generated<T> = ColumnType<T, T | undefined, T | undefined>;
15
15
  /** DB always generates (identity always). Never provided by user. */
16
16
  type GeneratedAlways<T> = ColumnType<T, never, never>;
17
- /** Extract the SELECT type from a column. */
18
- type SelectType<C> = C extends ColumnType<infer S, any, any> ? S : C;
17
+ /**
18
+ * Extract the SELECT type from a column.
19
+ * Works with both ColumnType and ColumnBuilder (both declare __select).
20
+ */
21
+ type SelectType<C> = C extends {
22
+ readonly __select: infer S;
23
+ } ? S : C;
19
24
  /** Extract the INSERT type from a column. */
20
- type InsertType<C> = C extends ColumnType<any, infer I, any> ? I : C;
25
+ type InsertType<C> = C extends {
26
+ readonly __insert: infer I;
27
+ } ? I : C;
21
28
  /** Extract the UPDATE type from a column. */
22
- type UpdateType<C> = C extends ColumnType<any, any, infer U> ? U : C;
29
+ type UpdateType<C> = C extends {
30
+ readonly __update: infer U;
31
+ } ? U : C;
23
32
  /** Make all properties nullable. */
24
- type Nullable$1<T> = { [K in keyof T]: T[K] | null };
33
+ type Nullable<T> = { [K in keyof T]: T[K] | null };
34
+ /**
35
+ * Select row type for a table. Cached alias — tsgo instantiation cache
36
+ * deduplicates across selectFrom, join, returning.
37
+ */
38
+ type SelectRow<DB, TB extends keyof DB> = { [K in keyof DB[TB]]: SelectType<DB[TB][K]> };
25
39
  /**
26
- * Infer a SELECT row type from a table column map.
27
- * Every column present, nullable columns include null.
40
+ * Infer a SELECT row type from a column map.
28
41
  */
29
42
  type Selectable<T> = { [K in keyof T]: SelectType<T[K]> };
30
43
  /**
31
- * Infer an INSERT row type from a table column map.
44
+ * Infer an INSERT row type from a column map.
32
45
  * Required columns: non-nullable without default.
33
46
  * Optional columns: nullable, has default, or generated.
34
47
  */
@@ -55,8 +68,14 @@ interface ColumnDef {
55
68
  declare class ColumnBuilder<S, I = S, U = I> {
56
69
  /** @internal */
57
70
  readonly _def: ColumnDef;
58
- /** Phantom type — never exists at runtime */
59
- readonly _type: ColumnType<S, I, U>;
71
+ /**
72
+ * Phantom branded fields — carry type info for indexed access.
73
+ * tsgo resolves `C["__select"]` via O(1) symbol table lookup,
74
+ * avoiding conditional type evaluation entirely.
75
+ */
76
+ readonly __select: S;
77
+ readonly __insert: I;
78
+ readonly __update: U;
60
79
  constructor(dataType: string, def?: Partial<ColumnDef>);
61
80
  notNull(): ColumnBuilder<Exclude<S, null>, Exclude<I, null>, Exclude<U, null>>;
62
81
  nullable(): ColumnBuilder<S | null, I | null | undefined, U | null | undefined>;
@@ -83,7 +102,7 @@ declare function jsonb<T = unknown>(): ColumnBuilder<T, T, T>;
83
102
  declare function numeric(precision?: number, scale?: number): ColumnBuilder<string, string | number, string | number>;
84
103
  declare function real(): ColumnBuilder<number, number, number>;
85
104
  declare function doublePrecision(): ColumnBuilder<number, number, number>;
86
- declare function bytea(): ColumnBuilder<Buffer, Buffer | Uint8Array, Buffer | Uint8Array>;
105
+ declare function bytea(): ColumnBuilder<Uint8Array, Uint8Array, Uint8Array>;
87
106
  declare function enumType<T extends string>(...values: [T, ...T[]]): ColumnBuilder<T, T, T>;
88
107
  interface TableDefinition<TName extends string = string, TColumns extends Record<string, ColumnBuilder<any, any, any>> = Record<string, ColumnBuilder<any, any, any>>> {
89
108
  readonly name: TName;
@@ -114,6 +133,4 @@ declare function defineTable<TName extends string, TColumns extends Record<strin
114
133
  * ```
115
134
  */
116
135
  type InferTable<T extends TableDefinition> = T extends TableDefinition<any, infer Cols> ? { [K in keyof Cols]: Cols[K] extends ColumnBuilder<infer S, infer I, infer U> ? ColumnType<S, I, U> : never } : never;
117
- /** Make all properties of T nullable */
118
- type Nullable<T> = { [K in keyof T]: T[K] | null };
119
- export { GeneratedAlways as A, time as C, varchar as D, uuid as E, Selectable as F, UpdateType as I, Updateable as L, Insertable as M, Nullable$1 as N, ColumnType as O, SelectType as P, text as S, timestamptz as T, jsonb as _, ColumnBuilder as a, serial as b, bigserial as c, char as d, date as f, json as g, integer as h, defineTable as i, InsertType as j, Generated as k, boolean as l, enumType as m, InferTable as n, ColumnDef as o, doublePrecision as p, TableDefinition as r, bigint as s, Nullable as t, bytea as u, numeric as v, timestamp as w, smallint as x, real as y };
136
+ export { InsertType as A, timestamp as C, ColumnType as D, varchar as E, Selectable as F, UpdateType as I, Updateable as L, Nullable as M, SelectRow as N, Generated as O, SelectType as P, time as S, uuid as T, numeric as _, ColumnDef as a, smallint as b, boolean as c, date as d, doublePrecision as f, jsonb as g, json as h, ColumnBuilder as i, Insertable as j, GeneratedAlways as k, bytea as l, integer as m, TableDefinition as n, bigint as o, enumType as p, defineTable as r, bigserial as s, InferTable as t, char as u, real as v, timestamptz as w, text as x, serial as y };
@@ -0,0 +1,11 @@
1
+ import { L as SelectNode, S as FullTextSearchNode, T as InsertNode, U as UpdateNode, h as DeleteNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
+ declare class MssqlPrinter extends BasePrinter {
3
+ constructor();
4
+ protected printSelect(node: SelectNode): string;
5
+ protected printInsert(node: InsertNode): string;
6
+ protected printUpdate(node: UpdateNode): string;
7
+ protected printDelete(node: DeleteNode): string;
8
+ protected printFullTextSearch(node: FullTextSearchNode): string;
9
+ }
10
+ declare function mssqlDialect(): Dialect;
11
+ export { MssqlPrinter as n, mssqlDialect as t };
@@ -0,0 +1 @@
1
+ import{r as e,t}from"./base.mjs";import{i as n}from"./errors.mjs";var r=class extends t{constructor(){super(`mssql`)}printSelect(e){let t=[];e.ctes.length>0&&t.push(this.printCTEs(e.ctes)),t.push(`SELECT`),e.distinct&&t.push(`DISTINCT`),e.limit&&!e.offset&&t.push(`TOP ${this.printExpression(e.limit)}`),e.columns.length===0?t.push(`*`):t.push(e.columns.map(e=>this.printExpression(e)).join(`, `)),e.from&&(t.push(`FROM`),e.from.type===`subquery`?t.push(this.printSubquery(e.from)):t.push(this.printTableRef(e.from)));for(let n of e.joins)t.push(this.printJoin(n));if(e.where&&t.push(`WHERE`,this.printExpression(e.where)),e.groupBy.length>0&&t.push(`GROUP BY`,e.groupBy.map(e=>this.printExpression(e)).join(`, `)),e.having&&t.push(`HAVING`,this.printExpression(e.having)),e.orderBy.length>0&&t.push(`ORDER BY`,e.orderBy.map(e=>this.printOrderBy(e)).join(`, `)),e.offset&&(t.push(`OFFSET ${this.printExpression(e.offset)} ROWS`),e.limit&&t.push(`FETCH NEXT ${this.printExpression(e.limit)} ROWS ONLY`)),e.setOp&&t.push(e.setOp.op,this.printSelect(e.setOp.query)),e.forUpdate)throw new n(`mssql`,`FOR UPDATE (use WITH (UPDLOCK) instead)`);return t.join(` `)}printInsert(t){if(t.onConflict)throw new n(`mssql`,`ON CONFLICT (use MERGE for upsert in MSSQL)`);let r=[];if(t.ctes.length>0&&r.push(this.printCTEs(t.ctes)),r.push(`INSERT INTO`,this.printTableRef(t.table)),t.columns.length>0&&r.push(`(${t.columns.map(t=>e(t,this.dialect)).join(`, `)})`),t.returning.length>0){let e=t.returning.map(e=>{let t=this.printExpression(e);return t===`*`?`INSERTED.*`:`INSERTED.${t}`});r.push(`OUTPUT`,e.join(`, `))}r.push(`VALUES`);let i=t.values.map(e=>`(${e.map(e=>this.printExpression(e)).join(`, `)})`);return r.push(i.join(`, `)),r.join(` `)}printUpdate(t){let n=[];t.ctes.length>0&&n.push(this.printCTEs(t.ctes)),n.push(`UPDATE`,this.printTableRef(t.table),`SET`);let r=t.set.map(t=>`${e(t.column,this.dialect)} = ${this.printExpression(t.value)}`);if(n.push(r.join(`, `)),t.returning.length>0){let e=t.returning.map(e=>{let t=this.printExpression(e);return t===`*`?`INSERTED.*`:`INSERTED.${t}`});n.push(`OUTPUT`,e.join(`, `))}return t.from&&n.push(`FROM`,this.printTableRef(t.from)),t.where&&n.push(`WHERE`,this.printExpression(t.where)),n.join(` `)}printDelete(e){let t=[];if(e.ctes.length>0&&t.push(this.printCTEs(e.ctes)),t.push(`DELETE FROM`,this.printTableRef(e.table)),e.returning.length>0){let n=e.returning.map(e=>{let t=this.printExpression(e);return t===`*`?`DELETED.*`:`DELETED.${t}`});t.push(`OUTPUT`,n.join(`, `))}return e.where&&t.push(`WHERE`,this.printExpression(e.where)),t.join(` `)}printFullTextSearch(t){let n=t.columns.map(e=>this.printExpression(e)).join(`, `),r=this.printExpression(t.query),i=`${t.mode===`natural`?`FREETEXT`:`CONTAINS`}((${n}), ${r})`;return t.alias&&(i+=` AS ${e(t.alias,this.dialect)}`),i}};function i(){return{name:`mssql`,createPrinter(){return new r}}}export{r as n,i as t};
@@ -1,8 +1,9 @@
1
- import { S as InsertNode, j as SelectNode, n as BasePrinter, t as Dialect } from "./types.mjs";
1
+ import { L as SelectNode, S as FullTextSearchNode, T as InsertNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
2
  declare class MysqlPrinter extends BasePrinter {
3
3
  constructor();
4
4
  protected printInsert(node: InsertNode): string;
5
5
  protected printSelect(node: SelectNode): string;
6
+ protected printFullTextSearch(node: FullTextSearchNode): string;
6
7
  }
7
8
  declare function mysqlDialect(): Dialect;
8
9
  export { MysqlPrinter as n, mysqlDialect as t };
@@ -1 +1 @@
1
- import{t as e}from"./base.mjs";import{i as t}from"./errors.mjs";var n=class extends e{constructor(){super(`mysql`)}printInsert(e){if(e.returning.length>0)throw new t(`mysql`,`RETURNING`);return super.printInsert(e)}printSelect(e){return e.forUpdate,super.printSelect(e)}};function r(){return{name:`mysql`,createPrinter(){return new n}}}export{n,r as t};
1
+ import{r as e,t}from"./base.mjs";import{i as n}from"./errors.mjs";var r=class extends t{constructor(){super(`mysql`)}printInsert(e){if(e.returning.length>0)throw new n(`mysql`,`RETURNING`);return super.printInsert(e)}printSelect(e){return e.forUpdate,super.printSelect(e)}printFullTextSearch(t){let n=t.columns.map(e=>this.printExpression(e)).join(`, `),r=this.printExpression(t.query),i=``;t.mode===`boolean`?i=` IN BOOLEAN MODE`:t.mode===`expansion`?i=` WITH QUERY EXPANSION`:t.mode===`natural`&&(i=` IN NATURAL LANGUAGE MODE`);let a=`MATCH(${n}) AGAINST(${r}${i})`;return t.alias&&(a+=` AS ${e(t.alias,this.dialect)}`),a}};function i(){return{name:`mysql`,createPrinter(){return new r}}}export{r as n,i as t};
@@ -1,4 +1,4 @@
1
- import { S as InsertNode, n as BasePrinter, t as Dialect } from "./types.mjs";
1
+ import { T as InsertNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
2
  declare class PgPrinter extends BasePrinter {
3
3
  constructor();
4
4
  protected printInsert(node: InsertNode): string;
@@ -1,8 +1,9 @@
1
- import { S as InsertNode, j as SelectNode, n as BasePrinter, t as Dialect } from "./types.mjs";
1
+ import { L as SelectNode, S as FullTextSearchNode, T as InsertNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
2
  declare class SqlitePrinter extends BasePrinter {
3
3
  constructor();
4
4
  protected printSelect(node: SelectNode): string;
5
5
  protected printInsert(node: InsertNode): string;
6
+ protected printFullTextSearch(node: FullTextSearchNode): string;
6
7
  }
7
8
  declare function sqliteDialect(): Dialect;
8
9
  export { SqlitePrinter as n, sqliteDialect as t };
@@ -1 +1 @@
1
- import{t as e}from"./base.mjs";import{i as t}from"./errors.mjs";var n=class extends e{constructor(){super(`sqlite`)}printSelect(e){if(e.forUpdate)throw new t(`sqlite`,`FOR UPDATE`);return super.printSelect(e)}printInsert(e){return super.printInsert(e)}};function r(){return{name:`sqlite`,createPrinter(){return new n}}}export{n,r as t};
1
+ import{r as e,t}from"./base.mjs";import{i as n}from"./errors.mjs";var r=class extends t{constructor(){super(`sqlite`)}printSelect(e){if(e.forUpdate)throw new n(`sqlite`,`FOR UPDATE`);return super.printSelect(e)}printInsert(e){return super.printInsert(e)}printFullTextSearch(t){let n=`(${t.columns.length>0?this.printExpression(t.columns[0]):`'*'`} MATCH ${this.printExpression(t.query)})`;return t.alias&&(n+=` AS ${e(t.alias,this.dialect)}`),n}};function i(){return{name:`sqlite`,createPrinter(){return new r}}}export{r as n,i as t};
@@ -1,4 +1,4 @@
1
- type SQLDialect = "pg" | "mysql" | "sqlite";
1
+ type SQLDialect = "pg" | "mysql" | "sqlite" | "mssql";
2
2
  interface CompiledQuery {
3
3
  sql: string;
4
4
  params: readonly unknown[];
@@ -12,8 +12,8 @@ interface DialectConfig {
12
12
  quoteIdentifier(name: string): string;
13
13
  formatParam(index: number): string;
14
14
  }
15
- type ASTNode = SelectNode | InsertNode | UpdateNode | DeleteNode | ExpressionNode;
16
- type ExpressionNode = ColumnRefNode | LiteralNode | BinaryOpNode | UnaryOpNode | FunctionCallNode | ParamNode | RawNode | SubqueryNode | BetweenNode | InNode | IsNullNode | CaseNode | CastNode | ExistsNode | StarNode | JsonAccessNode | ArrayExprNode | WindowFunctionNode;
15
+ type ASTNode = SelectNode | InsertNode | UpdateNode | DeleteNode | MergeNode | ExpressionNode;
16
+ type ExpressionNode = ColumnRefNode | LiteralNode | BinaryOpNode | UnaryOpNode | FunctionCallNode | ParamNode | RawNode | SubqueryNode | BetweenNode | InNode | IsNullNode | CaseNode | CastNode | ExistsNode | StarNode | JsonAccessNode | ArrayExprNode | WindowFunctionNode | AliasedExprNode | FullTextSearchNode;
17
17
  interface ColumnRefNode {
18
18
  type: "column_ref";
19
19
  table?: string;
@@ -98,11 +98,44 @@ interface StarNode {
98
98
  type: "star";
99
99
  table?: string;
100
100
  }
101
+ type FullTextSearchMode = "natural" | "boolean" | "expansion";
102
+ interface FullTextSearchNode {
103
+ type: "full_text_search";
104
+ columns: ExpressionNode[];
105
+ query: ExpressionNode;
106
+ mode?: FullTextSearchMode;
107
+ language?: string;
108
+ alias?: string;
109
+ }
110
+ interface AliasedExprNode {
111
+ type: "aliased_expr";
112
+ expr: ExpressionNode;
113
+ alias: string;
114
+ }
115
+ type TemporalClause = {
116
+ kind: "as_of";
117
+ timestamp: ExpressionNode;
118
+ } | {
119
+ kind: "from_to";
120
+ start: ExpressionNode;
121
+ end: ExpressionNode;
122
+ } | {
123
+ kind: "between";
124
+ start: ExpressionNode;
125
+ end: ExpressionNode;
126
+ } | {
127
+ kind: "contained_in";
128
+ start: ExpressionNode;
129
+ end: ExpressionNode;
130
+ } | {
131
+ kind: "all";
132
+ };
101
133
  interface TableRefNode {
102
134
  type: "table_ref";
103
135
  name: string;
104
136
  alias?: string;
105
137
  schema?: string;
138
+ temporal?: TemporalClause;
106
139
  }
107
140
  interface JoinNode {
108
141
  type: "join";
@@ -215,6 +248,31 @@ interface DeleteNode {
215
248
  returning: ExpressionNode[];
216
249
  ctes: CTENode[];
217
250
  }
251
+ interface MergeWhenMatched {
252
+ type: "matched";
253
+ condition?: ExpressionNode;
254
+ action: "update" | "delete";
255
+ set?: {
256
+ column: string;
257
+ value: ExpressionNode;
258
+ }[];
259
+ }
260
+ interface MergeWhenNotMatched {
261
+ type: "not_matched";
262
+ condition?: ExpressionNode;
263
+ columns: string[];
264
+ values: ExpressionNode[];
265
+ }
266
+ interface MergeNode {
267
+ type: "merge";
268
+ target: TableRefNode;
269
+ source: TableRefNode | SubqueryNode;
270
+ sourceAlias: string;
271
+ on: ExpressionNode;
272
+ whens: (MergeWhenMatched | MergeWhenNotMatched)[];
273
+ ctes: CTENode[];
274
+ }
275
+ declare function createMergeNode(target: TableRefNode, source: TableRefNode | SubqueryNode, sourceAlias: string, on: ExpressionNode): MergeNode;
218
276
  declare function tableRef(name: string, alias?: string, schema?: string): TableRefNode;
219
277
  declare function createSelectNode(): SelectNode;
220
278
  declare function createInsertNode(table: TableRefNode): InsertNode;
@@ -258,6 +316,7 @@ declare class BasePrinter implements Printer {
258
316
  protected printExists(node: ExistsNode): string;
259
317
  protected printStar(node: StarNode): string;
260
318
  protected printTableRef(ref: TableRefNode): string;
319
+ protected printTemporalClause(clause: TemporalClause): string;
261
320
  protected printJoin(node: JoinNode): string;
262
321
  protected printOrderBy(node: OrderByNode): string;
263
322
  protected printCTEs(ctes: CTENode[]): string;
@@ -266,9 +325,14 @@ declare class BasePrinter implements Printer {
266
325
  protected printWindowFunction(node: WindowFunctionNode): string;
267
326
  protected printFrameSpec(frame: FrameSpec): string;
268
327
  protected printFrameBound(bound: FrameBound): string;
328
+ protected printMerge(node: MergeNode): string;
329
+ protected printMergeWhenMatched(when: MergeWhenMatched): string;
330
+ protected printMergeWhenNotMatched(when: MergeWhenNotMatched): string;
331
+ protected printAliasedExpr(node: AliasedExprNode): string;
332
+ protected printFullTextSearch(node: FullTextSearchNode): string;
269
333
  }
270
334
  interface Dialect {
271
335
  name: SQLDialect;
272
336
  createPrinter(): Printer;
273
337
  }
274
- export { RawNode as A, createSelectNode as B, IsNullNode as C, OnConflictNode as D, LiteralNode as E, UnaryOpNode as F, JoinType as G, tableRef as H, UpdateNode as I, SQLDialect as J, OrderDirection as K, WindowFunctionNode as L, StarNode as M, SubqueryNode as N, OrderByNode as O, TableRefNode as P, createDeleteNode as R, InsertNode as S, JsonAccessNode as T, CompiledQuery as U, createUpdateNode as V, DialectConfig as W, SetOperator as Y, FrameBound as _, PrinterOptions as a, FunctionCallNode as b, BetweenNode as c, CaseNode as d, CastNode as f, ExpressionNode as g, ExistsNode as h, Printer as i, SelectNode as j, ParamNode as k, BinaryOpNode as l, DeleteNode as m, BasePrinter as n, ASTNode as o, ColumnRefNode as p, Primitive as q, PrintMode as r, ArrayExprNode as s, Dialect as t, CTENode as u, FrameKind as v, JoinNode as w, InNode as x, FrameSpec as y, createInsertNode as z };
338
+ export { JoinType as $, MergeNode as A, TableRefNode as B, FunctionCallNode as C, JoinNode as D, IsNullNode as E, ParamNode as F, createDeleteNode as G, UnaryOpNode as H, RawNode as I, createSelectNode as J, createInsertNode as K, SelectNode as L, MergeWhenNotMatched as M, OnConflictNode as N, JsonAccessNode as O, OrderByNode as P, DialectConfig as Q, StarNode as R, FullTextSearchNode as S, InsertNode as T, UpdateNode as U, TemporalClause as V, WindowFunctionNode as W, tableRef as X, createUpdateNode as Y, CompiledQuery as Z, ExpressionNode as _, PrinterOptions as a, FrameSpec as b, ArrayExprNode as c, CTENode as d, OrderDirection as et, CaseNode as f, ExistsNode as g, DeleteNode as h, Printer as i, MergeWhenMatched as j, LiteralNode as k, BetweenNode as l, ColumnRefNode as m, BasePrinter as n, SQLDialect as nt, ASTNode as o, CastNode as p, createMergeNode as q, PrintMode as r, SetOperator as rt, AliasedExprNode as s, Dialect as t, Primitive as tt, BinaryOpNode as u, FrameBound as v, InNode as w, FullTextSearchMode as x, FrameKind as y, SubqueryNode as z };