sumak 0.0.4 → 0.0.6

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 (121) hide show
  1. package/README.md +409 -5
  2. package/dist/ast/expression.d.mts +26 -0
  3. package/dist/ast/expression.mjs +140 -0
  4. package/dist/ast/nodes.d.mts +309 -0
  5. package/dist/ast/nodes.mjs +59 -0
  6. package/dist/ast/transformer.d.mts +10 -0
  7. package/dist/ast/transformer.mjs +140 -0
  8. package/dist/ast/typed-expression.d.mts +37 -0
  9. package/dist/ast/typed-expression.mjs +77 -0
  10. package/dist/ast/visitor.d.mts +13 -0
  11. package/dist/ast/visitor.mjs +11 -0
  12. package/dist/builder/delete.d.mts +20 -0
  13. package/dist/builder/delete.mjs +113 -0
  14. package/dist/builder/eb.d.mts +312 -0
  15. package/dist/builder/eb.mjs +641 -0
  16. package/dist/builder/expression.d.mts +5 -0
  17. package/dist/builder/expression.mjs +10 -0
  18. package/dist/builder/insert.d.mts +40 -0
  19. package/dist/builder/insert.mjs +146 -0
  20. package/dist/builder/merge.d.mts +20 -0
  21. package/dist/builder/merge.mjs +100 -0
  22. package/dist/builder/raw.d.mts +2 -0
  23. package/dist/builder/raw.mjs +4 -0
  24. package/dist/builder/select.d.mts +41 -0
  25. package/dist/builder/select.mjs +280 -0
  26. package/dist/builder/typed-delete.d.mts +49 -0
  27. package/dist/builder/typed-delete.mjs +89 -0
  28. package/dist/builder/typed-insert.d.mts +86 -0
  29. package/dist/builder/typed-insert.mjs +159 -0
  30. package/dist/builder/typed-merge.d.mts +31 -0
  31. package/dist/builder/typed-merge.mjs +93 -0
  32. package/dist/builder/typed-select.d.mts +164 -0
  33. package/dist/builder/typed-select.mjs +309 -0
  34. package/dist/builder/typed-update.d.mts +59 -0
  35. package/dist/builder/typed-update.mjs +110 -0
  36. package/dist/builder/update.d.mts +20 -0
  37. package/dist/builder/update.mjs +121 -0
  38. package/dist/dialect/mssql.d.mts +2 -0
  39. package/dist/dialect/mssql.mjs +9 -0
  40. package/dist/dialect/mysql.d.mts +2 -0
  41. package/dist/dialect/mysql.mjs +9 -0
  42. package/dist/dialect/pg.d.mts +2 -0
  43. package/dist/dialect/pg.mjs +9 -0
  44. package/dist/dialect/sqlite.d.mts +2 -0
  45. package/dist/dialect/sqlite.mjs +9 -0
  46. package/dist/dialect/types.d.mts +6 -0
  47. package/dist/dialect/types.mjs +1 -0
  48. package/dist/errors.d.mts +12 -0
  49. package/dist/errors.mjs +24 -0
  50. package/dist/index.d.mts +50 -806
  51. package/dist/index.mjs +48 -3
  52. package/dist/mssql.d.mts +2 -2
  53. package/dist/mssql.mjs +2 -1
  54. package/dist/mysql.d.mts +2 -2
  55. package/dist/mysql.mjs +2 -1
  56. package/dist/pg.d.mts +2 -2
  57. package/dist/pg.mjs +2 -1
  58. package/dist/plugin/camel-case.d.mts +11 -0
  59. package/dist/plugin/camel-case.mjs +16 -0
  60. package/dist/plugin/hooks.d.mts +72 -0
  61. package/dist/plugin/hooks.mjs +49 -0
  62. package/dist/plugin/plugin-manager.d.mts +17 -0
  63. package/dist/plugin/plugin-manager.mjs +37 -0
  64. package/dist/plugin/soft-delete.d.mts +27 -0
  65. package/dist/plugin/soft-delete.mjs +52 -0
  66. package/dist/plugin/types.d.mts +19 -0
  67. package/dist/plugin/types.mjs +1 -0
  68. package/dist/plugin/with-schema.d.mts +21 -0
  69. package/dist/plugin/with-schema.mjs +53 -0
  70. package/dist/printer/base.d.mts +49 -0
  71. package/dist/printer/base.mjs +472 -0
  72. package/dist/printer/document.d.mts +45 -0
  73. package/dist/printer/document.mjs +153 -0
  74. package/dist/printer/formatter.d.mts +5 -0
  75. package/dist/printer/formatter.mjs +134 -0
  76. package/dist/printer/mssql.d.mts +10 -0
  77. package/dist/printer/mssql.mjs +161 -0
  78. package/dist/printer/mysql.d.mts +8 -0
  79. package/dist/printer/mysql.mjs +41 -0
  80. package/dist/printer/pg.d.mts +6 -0
  81. package/dist/printer/pg.mjs +9 -0
  82. package/dist/printer/sqlite.d.mts +8 -0
  83. package/dist/printer/sqlite.mjs +29 -0
  84. package/dist/printer/types.d.mts +11 -0
  85. package/dist/printer/types.mjs +1 -0
  86. package/dist/schema/column.d.mts +52 -0
  87. package/dist/schema/column.mjs +120 -0
  88. package/dist/schema/index.d.mts +6 -0
  89. package/dist/schema/index.mjs +4 -0
  90. package/dist/schema/table.d.mts +37 -0
  91. package/dist/schema/table.mjs +7 -0
  92. package/dist/schema/type-utils.d.mts +46 -0
  93. package/dist/schema/type-utils.mjs +1 -0
  94. package/dist/schema/types.d.mts +64 -0
  95. package/dist/schema/types.mjs +1 -0
  96. package/dist/schema.d.mts +2 -2
  97. package/dist/schema.mjs +1 -1
  98. package/dist/sqlite.d.mts +2 -2
  99. package/dist/sqlite.mjs +2 -1
  100. package/dist/sumak.d.mts +110 -0
  101. package/dist/sumak.mjs +141 -0
  102. package/dist/types.d.mts +14 -0
  103. package/dist/types.mjs +1 -0
  104. package/dist/utils/identifier.d.mts +3 -0
  105. package/dist/utils/identifier.mjs +14 -0
  106. package/dist/utils/param.d.mts +2 -0
  107. package/dist/utils/param.mjs +8 -0
  108. package/package.json +1 -1
  109. package/dist/_chunks/base.mjs +0 -1
  110. package/dist/_chunks/errors.mjs +0 -1
  111. package/dist/_chunks/index.d.mts +0 -136
  112. package/dist/_chunks/mssql.d.mts +0 -11
  113. package/dist/_chunks/mssql.mjs +0 -1
  114. package/dist/_chunks/mysql.d.mts +0 -9
  115. package/dist/_chunks/mysql.mjs +0 -1
  116. package/dist/_chunks/pg.d.mts +0 -7
  117. package/dist/_chunks/pg.mjs +0 -1
  118. package/dist/_chunks/schema.mjs +0 -1
  119. package/dist/_chunks/sqlite.d.mts +0 -9
  120. package/dist/_chunks/sqlite.mjs +0 -1
  121. package/dist/_chunks/types.d.mts +0 -338
@@ -0,0 +1,46 @@
1
+ import type { ColumnType, SelectType } from "./types.mjs";
2
+ /** Make all properties of T nullable */
3
+ export type Nullable<T> = { [K in keyof T] : T[K] | null };
4
+ /** Extract table names from DB type */
5
+ export type TableName<DB> = keyof DB & string;
6
+ /** Extract column names for a table */
7
+ export type ColumnName<
8
+ DB,
9
+ TB extends keyof DB
10
+ > = keyof DB[TB] & string;
11
+ /** Qualified column reference: "table.column" */
12
+ export type QualifiedColumn<
13
+ DB,
14
+ TB extends keyof DB
15
+ > = TB extends string ? `${TB}.${keyof DB[TB] & string}` : never;
16
+ /** Any column reference (unqualified or qualified) in scope */
17
+ export type AnyColumn<
18
+ DB,
19
+ TB extends keyof DB
20
+ > = ColumnName<DB, TB> | QualifiedColumn<DB, TB>;
21
+ /**
22
+ * Resolve a column reference string to its SelectType.
23
+ * Handles both "column" and "table.column" formats.
24
+ */
25
+ export type ResolveColumnType<
26
+ DB,
27
+ TB extends keyof DB,
28
+ Ref extends string
29
+ > = Ref extends `${infer T}.${infer C}` ? T extends TB & string ? C extends keyof DB[T] ? SelectType<DB[T][C]> : never : never : TB extends keyof DB ? Ref extends keyof DB[TB] ? SelectType<DB[TB][Ref]> : never : never;
30
+ /**
31
+ * Resolve result type when selecting specific columns.
32
+ * Given DB, TB (tables in scope), and selected column strings,
33
+ * build the output row type.
34
+ */
35
+ export type SelectResult<
36
+ DB,
37
+ TB extends keyof DB,
38
+ Cols extends AnyColumn<DB, TB>
39
+ > = { [K in Cols as K extends `${string}.${infer C}` ? C : K] : ResolveColumnType<DB, TB, K> };
40
+ /**
41
+ * Full select model for a single table - all columns included.
42
+ */
43
+ export type FullSelectModel<
44
+ DB,
45
+ TB extends keyof DB
46
+ > = { [K in keyof DB[TB]] : DB[TB][K] extends ColumnType<infer S, any, any> ? S : DB[TB][K] };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Three-phase column type: Select, Insert, Update.
3
+ *
4
+ * S = type returned on SELECT
5
+ * I = type accepted on INSERT
6
+ * U = type accepted on UPDATE
7
+ */
8
+ export interface ColumnType<
9
+ S,
10
+ I = S,
11
+ U = I
12
+ > {
13
+ readonly __select: S;
14
+ readonly __insert: I;
15
+ readonly __update: U;
16
+ }
17
+ /** DB generates this value (autoincrement, default). Optional on INSERT/UPDATE. */
18
+ export type Generated<T> = ColumnType<T, T | undefined, T | undefined>;
19
+ /** DB always generates (identity always). Never provided by user. */
20
+ export type GeneratedAlways<T> = ColumnType<T, never, never>;
21
+ /**
22
+ * Extract the SELECT type from a column.
23
+ * Works with both ColumnType and ColumnBuilder (both declare __select).
24
+ */
25
+ export type SelectType<C> = C extends {
26
+ readonly __select: infer S;
27
+ } ? S : C;
28
+ /** Extract the INSERT type from a column. */
29
+ export type InsertType<C> = C extends {
30
+ readonly __insert: infer I;
31
+ } ? I : C;
32
+ /** Extract the UPDATE type from a column. */
33
+ export type UpdateType<C> = C extends {
34
+ readonly __update: infer U;
35
+ } ? U : C;
36
+ /** Make all properties nullable. */
37
+ export type Nullable<T> = { [K in keyof T] : T[K] | null };
38
+ /**
39
+ * Select row type for a table. Cached alias — tsgo instantiation cache
40
+ * deduplicates across selectFrom, join, returning.
41
+ */
42
+ export type SelectRow<
43
+ DB,
44
+ TB extends keyof DB
45
+ > = { [K in keyof DB[TB]] : SelectType<DB[TB][K]> };
46
+ /**
47
+ * Infer a SELECT row type from a column map.
48
+ */
49
+ export type Selectable<T> = { [K in keyof T] : SelectType<T[K]> };
50
+ /**
51
+ * Infer an INSERT row type from a column map.
52
+ * Required columns: non-nullable without default.
53
+ * Optional columns: nullable, has default, or generated.
54
+ */
55
+ export type Insertable<T> = { [K in keyof T as IsRequired<T[K]> extends true ? K : never] : InsertType<T[K]> } & { [K in keyof T as IsRequired<T[K]> extends true ? never : K]? : InsertType<T[K]> };
56
+ /**
57
+ * Infer an UPDATE row type. All columns optional.
58
+ */
59
+ export type Updateable<T> = { [K in keyof T]? : UpdateType<T[K]> };
60
+ /**
61
+ * A column is required on INSERT if its InsertType does NOT include undefined or never.
62
+ */
63
+ type IsRequired<C> = InsertType<C> extends never ? false : undefined extends InsertType<C> ? false : true;
64
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/dist/schema.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { A as InsertType, C as timestamp, D as ColumnType, E as varchar, F as Selectable, I as UpdateType, L as Updateable, M as Nullable, O as Generated, P as SelectType, S as time, T as uuid, _ as numeric, a as ColumnDef, b as smallint, c as boolean, d as date, f as doublePrecision, g as jsonb, h as json, i as ColumnBuilder, j as Insertable, k as GeneratedAlways, l as bytea, m as integer, n as TableDefinition, o as bigint, p as enumType, r as defineTable, s as bigserial, t as InferTable, u as char, v as real, w as timestamptz, x as text, y as serial } from "./_chunks/index.mjs";
2
- export { ColumnBuilder, type ColumnDef, type ColumnType, type Generated, type GeneratedAlways, type InferTable, type InsertType, type Insertable, type Nullable, type SelectType, type Selectable, type TableDefinition, type UpdateType, type Updateable, bigint, bigserial, boolean, bytea, char, date, defineTable, doublePrecision, enumType, integer, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar };
1
+ export { bigint, bigserial, boolean, bytea, char, ColumnBuilder, date, defineTable, doublePrecision, enumType, integer, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar } from "./schema/index.mjs";
2
+ export type { ColumnDef, ColumnType, Generated, GeneratedAlways, InferTable, Insertable, InsertType, Nullable, Selectable, SelectType, TableDefinition, Updateable, UpdateType } from "./schema/index.mjs";
package/dist/schema.mjs CHANGED
@@ -1 +1 @@
1
- import{C as e,S as t,_ as n,a as r,b as i,c as a,d as o,f as s,g as c,h as l,i as u,l as d,m as f,n as p,o as m,p as h,r as g,s as _,t as v,u as y,v as b,x,y as S}from"./_chunks/schema.mjs";export{p as ColumnBuilder,g as bigint,u as bigserial,r as boolean,m as bytea,_ as char,a as date,v as defineTable,d as doublePrecision,y as enumType,o as integer,s as json,h as jsonb,f as numeric,l as real,c as serial,n as smallint,b as text,S as time,i as timestamp,x as timestamptz,t as uuid,e as varchar};
1
+ export { bigint, bigserial, boolean, bytea, char, ColumnBuilder, date, defineTable, doublePrecision, enumType, integer, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar } from "./schema/index.mjs";
package/dist/sqlite.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { n as SqlitePrinter, t as sqliteDialect } from "./_chunks/sqlite.mjs";
2
- export { SqlitePrinter, sqliteDialect };
1
+ export { SqlitePrinter } from "./printer/sqlite.mjs";
2
+ export { sqliteDialect } from "./dialect/sqlite.mjs";
package/dist/sqlite.mjs CHANGED
@@ -1 +1,2 @@
1
- import{n as e,t}from"./_chunks/sqlite.mjs";export{e as SqlitePrinter,t as sqliteDialect};
1
+ export { SqlitePrinter } from "./printer/sqlite.mjs";
2
+ export { sqliteDialect } from "./dialect/sqlite.mjs";
@@ -0,0 +1,110 @@
1
+ import type { ASTNode } from "./ast/nodes.mjs";
2
+ import type { Expression } from "./ast/typed-expression.mjs";
3
+ import { Col } from "./builder/eb.mjs";
4
+ import { TypedDeleteBuilder } from "./builder/typed-delete.mjs";
5
+ import { TypedInsertBuilder } from "./builder/typed-insert.mjs";
6
+ import { TypedMergeBuilder } from "./builder/typed-merge.mjs";
7
+ import { TypedSelectBuilder } from "./builder/typed-select.mjs";
8
+ import { TypedUpdateBuilder } from "./builder/typed-update.mjs";
9
+ import type { Dialect } from "./dialect/types.mjs";
10
+ import type { HookName, SumakHooks } from "./plugin/hooks.mjs";
11
+ import type { SumakPlugin } from "./plugin/types.mjs";
12
+ import type { Printer } from "./printer/types.mjs";
13
+ import type { ColumnBuilder } from "./schema/column.mjs";
14
+ import type { SelectRow } from "./schema/types.mjs";
15
+ import type { CompiledQuery } from "./types.mjs";
16
+ /**
17
+ * Tables config constraint.
18
+ * Each table = Record of ColumnBuilder instances.
19
+ */
20
+ type TablesConfig = Record<string, Record<string, ColumnBuilder<any, any, any>>>;
21
+ export interface SumakConfig<T extends TablesConfig> {
22
+ dialect: Dialect;
23
+ tables: T;
24
+ plugins?: SumakPlugin[];
25
+ }
26
+ /**
27
+ * Create a fully typed sumak instance.
28
+ *
29
+ * DB type = typeof tables directly. No `InferDB` mapped type.
30
+ * ColumnBuilder carries __select/__insert/__update phantom fields,
31
+ * so SelectType/InsertType/UpdateType resolve via O(1) indexed access
32
+ * instead of conditional type evaluation.
33
+ *
34
+ * ```ts
35
+ * const db = sumak({
36
+ * dialect: pgDialect(),
37
+ * tables: {
38
+ * users: { id: serial(), name: text().notNull() },
39
+ * },
40
+ * });
41
+ *
42
+ * db.selectFrom("users").select("id", "name")...
43
+ * ```
44
+ */
45
+ export declare function sumak<T extends TablesConfig>(config: SumakConfig<T>): Sumak<T>;
46
+ /**
47
+ * Core sumak instance with hook system.
48
+ */
49
+ export declare class Sumak<DB> {
50
+ private _dialect;
51
+ private _plugins;
52
+ private _hooks;
53
+ constructor(dialect: Dialect, plugins?: SumakPlugin[]);
54
+ /**
55
+ * Register a hook handler. Returns an unregister function.
56
+ *
57
+ * ```ts
58
+ * const off = db.hook("query:before", (ctx) => { ... });
59
+ * off(); // unregister
60
+ * ```
61
+ */
62
+ hook<K extends HookName>(name: K, handler: SumakHooks[K]): () => void;
63
+ selectFrom<T extends keyof DB & string>(table: T, alias?: string): TypedSelectBuilder<DB, T, SelectRow<DB, T>>;
64
+ /**
65
+ * SELECT from a subquery (derived table).
66
+ *
67
+ * ```ts
68
+ * const sub = db.selectFrom("users").select("id", "name")
69
+ * db.selectFromSubquery(sub, "u").selectAll().compile(db.printer())
70
+ * // SELECT * FROM (SELECT "id", "name" FROM "users") AS "u"
71
+ * ```
72
+ */
73
+ selectFromSubquery<Alias extends string>(subquery: {
74
+ build(): import("./ast/nodes.ts").SelectNode;
75
+ }, alias: Alias): TypedSelectBuilder<DB, keyof DB & string, Record<string, unknown>>;
76
+ insertInto<T extends keyof DB & string>(table: T): TypedInsertBuilder<DB, T>;
77
+ update<T extends keyof DB & string>(table: T): TypedUpdateBuilder<DB, T>;
78
+ deleteFrom<T extends keyof DB & string>(table: T): TypedDeleteBuilder<DB, T>;
79
+ /**
80
+ * MERGE INTO target USING source ON condition.
81
+ *
82
+ * ```ts
83
+ * db.mergeInto("users", "staging", "s", ({ target, source }) =>
84
+ * target.id.eqCol(source.id),
85
+ * )
86
+ * .whenMatchedThenUpdate({ name: "updated" })
87
+ * .whenNotMatchedThenInsert({ name: "new", email: "e@x.com" })
88
+ * .compile(db.printer())
89
+ * ```
90
+ */
91
+ mergeInto<
92
+ T extends keyof DB & string,
93
+ S extends keyof DB & string
94
+ >(target: T, source: S, sourceAlias: string, on: (proxies: {
95
+ target: { [K in keyof DB[T] & string] : Col<any> };
96
+ source: { [K in keyof DB[S] & string] : Col<any> };
97
+ }) => Expression<boolean>): TypedMergeBuilder<DB, T, S>;
98
+ /**
99
+ * Compile an AST node through the full pipeline:
100
+ * plugins.transformNode → type-specific hooks → printer → plugins.transformQuery → query hooks
101
+ */
102
+ compile(node: ASTNode): CompiledQuery;
103
+ /**
104
+ * Transform result rows through plugins and hooks.
105
+ */
106
+ transformResult(rows: Record<string, unknown>[]): Record<string, unknown>[];
107
+ printer(): Printer;
108
+ private _extractTableName;
109
+ }
110
+ export {};
package/dist/sumak.mjs ADDED
@@ -0,0 +1,141 @@
1
+ import { Col } from "./builder/eb.mjs";
2
+ import { SelectBuilder } from "./builder/select.mjs";
3
+ import { TypedDeleteBuilder } from "./builder/typed-delete.mjs";
4
+ import { TypedInsertBuilder } from "./builder/typed-insert.mjs";
5
+ import { TypedMergeBuilder } from "./builder/typed-merge.mjs";
6
+ import { TypedSelectBuilder } from "./builder/typed-select.mjs";
7
+ import { TypedUpdateBuilder } from "./builder/typed-update.mjs";
8
+ import { Hookable } from "./plugin/hooks.mjs";
9
+ import { PluginManager } from "./plugin/plugin-manager.mjs";
10
+
11
+ export function sumak(config) {
12
+ return new Sumak(config.dialect, config.plugins ?? []);
13
+ }
14
+
15
+ export class Sumak {
16
+ _dialect;
17
+ _plugins;
18
+ _hooks;
19
+ constructor(dialect, plugins = []) {
20
+ this._dialect = dialect;
21
+ this._plugins = new PluginManager(plugins);
22
+ this._hooks = new Hookable();
23
+ }
24
+
25
+ hook(name, handler) {
26
+ return this._hooks.hook(name, handler);
27
+ }
28
+ selectFrom(table, alias) {
29
+ return new TypedSelectBuilder(new SelectBuilder().from(table, alias), table);
30
+ }
31
+
32
+ selectFromSubquery(subquery, alias) {
33
+ const sub = {
34
+ type: "subquery",
35
+ query: subquery.build(),
36
+ alias
37
+ };
38
+ return new TypedSelectBuilder(new SelectBuilder().from(sub), alias);
39
+ }
40
+ insertInto(table) {
41
+ return new TypedInsertBuilder(table);
42
+ }
43
+ update(table) {
44
+ return new TypedUpdateBuilder(table);
45
+ }
46
+ deleteFrom(table) {
47
+ return new TypedDeleteBuilder(table);
48
+ }
49
+
50
+ mergeInto(target, source, sourceAlias, on) {
51
+ const makeProxy = (prefix) => new Proxy({}, { get(_t, colName) {
52
+ return new Col(colName, prefix);
53
+ } });
54
+ const proxies = {
55
+ target: makeProxy(target),
56
+ source: makeProxy(sourceAlias)
57
+ };
58
+ const onExpr = on(proxies);
59
+ return new TypedMergeBuilder(target, source, sourceAlias, onExpr);
60
+ }
61
+
62
+ compile(node) {
63
+
64
+ let ast = this._plugins.transformNode(node);
65
+
66
+ const table = this._extractTableName(ast);
67
+ switch (ast.type) {
68
+ case "select": {
69
+ const result = this._hooks.callHook("select:before", {
70
+ node: ast,
71
+ table
72
+ });
73
+ if (result) ast = result;
74
+ break;
75
+ }
76
+ case "insert": {
77
+ const result = this._hooks.callHook("insert:before", {
78
+ node: ast,
79
+ table
80
+ });
81
+ if (result) ast = result;
82
+ break;
83
+ }
84
+ case "update": {
85
+ const result = this._hooks.callHook("update:before", {
86
+ node: ast,
87
+ table
88
+ });
89
+ if (result) ast = result;
90
+ break;
91
+ }
92
+ case "delete": {
93
+ const result = this._hooks.callHook("delete:before", {
94
+ node: ast,
95
+ table
96
+ });
97
+ if (result) ast = result;
98
+ break;
99
+ }
100
+ }
101
+
102
+ const beforeResult = this._hooks.callHook("query:before", {
103
+ node: ast,
104
+ table
105
+ });
106
+ if (beforeResult) ast = beforeResult;
107
+
108
+ const printer = this._dialect.createPrinter();
109
+ let query = printer.print(ast);
110
+
111
+ query = this._plugins.transformQuery(query);
112
+
113
+ const afterResult = this._hooks.callHook("query:after", {
114
+ node: ast,
115
+ table,
116
+ query
117
+ });
118
+ if (afterResult) query = afterResult;
119
+ return query;
120
+ }
121
+
122
+ transformResult(rows) {
123
+ let result = this._plugins.transformResult(rows);
124
+ const hookResult = this._hooks.callHook("result:transform", result);
125
+ if (hookResult) result = hookResult;
126
+ return result;
127
+ }
128
+ printer() {
129
+ return this._dialect.createPrinter();
130
+ }
131
+ _extractTableName(node) {
132
+ switch (node.type) {
133
+ case "select": return node.from?.type === "table_ref" ? node.from.name : undefined;
134
+ case "insert": return node.table.name;
135
+ case "update": return node.table.name;
136
+ case "delete": return node.table.name;
137
+ case "merge": return node.target.name;
138
+ default: return undefined;
139
+ }
140
+ }
141
+ }
@@ -0,0 +1,14 @@
1
+ export type SQLDialect = "pg" | "mysql" | "sqlite" | "mssql";
2
+ export interface CompiledQuery {
3
+ sql: string;
4
+ params: readonly unknown[];
5
+ }
6
+ export type Primitive = string | number | boolean | null;
7
+ export type OrderDirection = "ASC" | "DESC";
8
+ export type JoinType = "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS";
9
+ export type SetOperator = "UNION" | "UNION ALL" | "INTERSECT" | "INTERSECT ALL" | "EXCEPT" | "EXCEPT ALL";
10
+ export interface DialectConfig {
11
+ name: SQLDialect;
12
+ quoteIdentifier(name: string): string;
13
+ formatParam(index: number): string;
14
+ }
package/dist/types.mjs ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { SQLDialect } from "../types.mjs";
2
+ export declare function quoteIdentifier(name: string, dialect: SQLDialect): string;
3
+ export declare function quoteTableRef(name: string, dialect: SQLDialect, schema?: string): string;
@@ -0,0 +1,14 @@
1
+ export function quoteIdentifier(name, dialect) {
2
+ switch (dialect) {
3
+ case "pg":
4
+ case "sqlite": return `"${name.replaceAll("\"", "\"\"")}"`;
5
+ case "mysql": return `\`${name.replaceAll("`", "``")}\``;
6
+ case "mssql": return `[${name.replaceAll("]", "]]")}]`;
7
+ }
8
+ }
9
+ export function quoteTableRef(name, dialect, schema) {
10
+ if (schema) {
11
+ return `${quoteIdentifier(schema, dialect)}.${quoteIdentifier(name, dialect)}`;
12
+ }
13
+ return quoteIdentifier(name, dialect);
14
+ }
@@ -0,0 +1,2 @@
1
+ import type { SQLDialect } from "../types.mjs";
2
+ export declare function formatParam(index: number, dialect: SQLDialect): string;
@@ -0,0 +1,8 @@
1
+ export function formatParam(index, dialect) {
2
+ switch (dialect) {
3
+ case "pg": return `$${index + 1}`;
4
+ case "mysql":
5
+ case "sqlite": return "?";
6
+ case "mssql": return `@p${index}`;
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sumak",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Type-safe SQL query builder with powerful SQL printers. Zero dependencies, tree-shakeable. Pure TypeScript.",
5
5
  "keywords": [
6
6
  "database",
@@ -1 +0,0 @@
1
- function e(e,t){switch(t){case`pg`:case`sqlite`:return`"${e.replaceAll(`"`,`""`)}"`;case`mysql`:return`\`${e.replaceAll("`","``")}\``;case`mssql`:return`[${e.replaceAll(`]`,`]]`)}]`}}function t(t,n,r){return r?`${e(r,n)}.${e(t,n)}`:e(t,n)}function n(e,t){switch(t){case`pg`:return`$${e+1}`;case`mysql`:case`sqlite`:return`?`;case`mssql`:return`@p${e}`}}var r=class{params=[];dialect;constructor(e){this.dialect=e}print(e){return this.params=[],{sql:this.printNode(e),params:[...this.params]}}printNode(e){switch(e.type){case`select`:return this.printSelect(e);case`insert`:return this.printInsert(e);case`update`:return this.printUpdate(e);case`delete`:return this.printDelete(e);case`merge`:return this.printMerge(e);default:return this.printExpression(e)}}printSelect(e){let t=[];e.ctes.length>0&&t.push(this.printCTEs(e.ctes)),t.push(`SELECT`),e.distinct&&t.push(`DISTINCT`),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));return 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.limit&&t.push(`LIMIT`,this.printExpression(e.limit)),e.offset&&t.push(`OFFSET`,this.printExpression(e.offset)),e.setOp&&t.push(e.setOp.op,this.printSelect(e.setOp.query)),e.forUpdate&&t.push(`FOR UPDATE`),t.join(` `)}printInsert(t){let n=[];t.ctes.length>0&&n.push(this.printCTEs(t.ctes)),n.push(`INSERT INTO`,this.printTableRef(t.table)),t.columns.length>0&&n.push(`(${t.columns.map(t=>e(t,this.dialect)).join(`, `)})`),n.push(`VALUES`);let r=t.values.map(e=>`(${e.map(e=>this.printExpression(e)).join(`, `)})`);return n.push(r.join(`, `)),t.onConflict&&n.push(this.printOnConflict(t.onConflict)),t.returning.length>0&&n.push(`RETURNING`,t.returning.map(e=>this.printExpression(e)).join(`, `)),n.join(` `)}printOnConflict(t){let n=[`ON CONFLICT`];if(t.columns.length>0&&n.push(`(${t.columns.map(t=>e(t,this.dialect)).join(`, `)})`),t.action===`nothing`)n.push(`DO NOTHING`);else{n.push(`DO UPDATE SET`);let r=t.action.set.map(t=>`${e(t.column,this.dialect)} = ${this.printExpression(t.value)}`);n.push(r.join(`, `))}return t.where&&n.push(`WHERE`,this.printExpression(t.where)),n.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)}`);return n.push(r.join(`, `)),t.from&&n.push(`FROM`,this.printTableRef(t.from)),t.where&&n.push(`WHERE`,this.printExpression(t.where)),t.returning.length>0&&n.push(`RETURNING`,t.returning.map(e=>this.printExpression(e)).join(`, `)),n.join(` `)}printDelete(e){let t=[];return e.ctes.length>0&&t.push(this.printCTEs(e.ctes)),t.push(`DELETE FROM`,this.printTableRef(e.table)),e.where&&t.push(`WHERE`,this.printExpression(e.where)),e.returning.length>0&&t.push(`RETURNING`,e.returning.map(e=>this.printExpression(e)).join(`, `)),t.join(` `)}printExpression(e){switch(e.type){case`column_ref`:return this.printColumnRef(e);case`literal`:return this.printLiteral(e);case`binary_op`:return this.printBinaryOp(e);case`unary_op`:return this.printUnaryOp(e);case`function_call`:return this.printFunctionCall(e);case`param`:return this.printParam(e);case`raw`:return this.printRaw(e);case`subquery`:return this.printSubquery(e);case`between`:return this.printBetween(e);case`in`:return this.printIn(e);case`is_null`:return this.printIsNull(e);case`cast`:return this.printCast(e);case`exists`:return this.printExists(e);case`star`:return this.printStar(e);case`case`:return this.printCase(e);case`json_access`:return this.printJsonAccess(e);case`array_expr`:return this.printArrayExpr(e);case`window_function`:return this.printWindowFunction(e);case`aliased_expr`:return this.printAliasedExpr(e);case`full_text_search`:return this.printFullTextSearch(e)}}printColumnRef(t){let n=t.table?`${e(t.table,this.dialect)}.${e(t.column,this.dialect)}`:e(t.column,this.dialect);return t.alias&&(n+=` AS ${e(t.alias,this.dialect)}`),n}printLiteral(e){return e.value===null?`NULL`:typeof e.value==`boolean`?e.value?`TRUE`:`FALSE`:typeof e.value==`number`?String(e.value):`'${String(e.value).replaceAll(`'`,`''`)}'`}printBinaryOp(e){return`(${this.printExpression(e.left)} ${e.op} ${this.printExpression(e.right)})`}printUnaryOp(e){return e.position===`postfix`?`(${this.printExpression(e.operand)} ${e.op})`:`(${e.op} ${this.printExpression(e.operand)})`}printFunctionCall(t){let n=`${t.name}(${t.args.map(e=>this.printExpression(e)).join(`, `)})`;return t.alias&&(n+=` AS ${e(t.alias,this.dialect)}`),n}printParam(e){return this.params.push(e.value),n(this.params.length-1,this.dialect)}printRaw(e){return this.params.push(...e.params),e.sql}printSubquery(t){let n=`(${this.printSelect(t.query)})`;return t.alias&&(n+=` AS ${e(t.alias,this.dialect)}`),n}printBetween(e){let t=e.negated?`NOT `:``;return`(${this.printExpression(e.expr)} ${t}BETWEEN ${this.printExpression(e.low)} AND ${this.printExpression(e.high)})`}printIn(e){let t=e.negated?`NOT `:``;return Array.isArray(e.values)?`(${this.printExpression(e.expr)} ${t}IN (${e.values.map(e=>this.printExpression(e)).join(`, `)}))`:`(${this.printExpression(e.expr)} ${t}IN (${this.printSelect(e.values)}))`}printIsNull(e){let t=e.negated?` NOT`:``;return`(${this.printExpression(e.expr)} IS${t} NULL)`}printCase(e){let t=[`CASE`];e.operand&&t.push(this.printExpression(e.operand));for(let n of e.whens)t.push(`WHEN`,this.printExpression(n.condition),`THEN`,this.printExpression(n.result));return e.else_&&t.push(`ELSE`,this.printExpression(e.else_)),t.push(`END`),t.join(` `)}printCast(e){return`CAST(${this.printExpression(e.expr)} AS ${e.dataType})`}printExists(e){return`(${e.negated?`NOT `:``}EXISTS (${this.printSelect(e.query)}))`}printStar(t){return t.table?`${e(t.table,this.dialect)}.*`:`*`}printTableRef(n){let r=t(n.name,this.dialect,n.schema);return n.temporal&&(r+=` ${this.printTemporalClause(n.temporal)}`),n.alias&&(r+=` AS ${e(n.alias,this.dialect)}`),r}printTemporalClause(e){switch(e.kind){case`as_of`:return`FOR SYSTEM_TIME AS OF ${this.printExpression(e.timestamp)}`;case`from_to`:return`FOR SYSTEM_TIME FROM ${this.printExpression(e.start)} TO ${this.printExpression(e.end)}`;case`between`:return`FOR SYSTEM_TIME BETWEEN ${this.printExpression(e.start)} AND ${this.printExpression(e.end)}`;case`contained_in`:return`FOR SYSTEM_TIME CONTAINED IN (${this.printExpression(e.start)}, ${this.printExpression(e.end)})`;case`all`:return`FOR SYSTEM_TIME ALL`}}printJoin(e){let t=[];return t.push(`${e.joinType} JOIN`),e.table.type===`subquery`?t.push(this.printSubquery(e.table)):t.push(this.printTableRef(e.table)),e.on&&t.push(`ON`,this.printExpression(e.on)),t.join(` `)}printOrderBy(e){let t=`${this.printExpression(e.expr)} ${e.direction}`;return e.nulls&&(t+=` NULLS ${e.nulls}`),t}printCTEs(t){return`${t.some(e=>e.recursive)?`WITH RECURSIVE`:`WITH`} ${t.map(t=>`${e(t.name,this.dialect)} AS (${this.printSelect(t.query)})`).join(`, `)}`}printJsonAccess(t){let n=`${this.printExpression(t.expr)}${t.operator}${this.printLiteral({type:`literal`,value:t.path})}`;return t.alias&&(n+=` AS ${e(t.alias,this.dialect)}`),n}printArrayExpr(e){return`ARRAY[${e.elements.map(e=>this.printExpression(e)).join(`, `)}]`}printWindowFunction(t){let n=[];n.push(this.printFunctionCall(t.fn)),n.push(`OVER`);let r=[];return t.partitionBy.length>0&&r.push(`PARTITION BY ${t.partitionBy.map(e=>this.printExpression(e)).join(`, `)}`),t.orderBy.length>0&&r.push(`ORDER BY ${t.orderBy.map(e=>this.printOrderBy(e)).join(`, `)}`),t.frame&&r.push(this.printFrameSpec(t.frame)),n.push(`(${r.join(` `)})`),t.alias&&n.push(`AS`,e(t.alias,this.dialect)),n.join(` `)}printFrameSpec(e){let t=this.printFrameBound(e.start);return e.end?`${e.kind} BETWEEN ${t} AND ${this.printFrameBound(e.end)}`:`${e.kind} ${t}`}printFrameBound(e){switch(e.type){case`unbounded_preceding`:return`UNBOUNDED PRECEDING`;case`preceding`:return`${e.value} PRECEDING`;case`current_row`:return`CURRENT ROW`;case`following`:return`${e.value} FOLLOWING`;case`unbounded_following`:return`UNBOUNDED FOLLOWING`}}printMerge(t){let n=[];t.ctes.length>0&&n.push(this.printCTEs(t.ctes)),n.push(`MERGE INTO`,this.printTableRef(t.target)),n.push(`USING`),t.source.type===`subquery`?n.push(this.printSubquery(t.source)):n.push(this.printTableRef(t.source)),n.push(`AS`,e(t.sourceAlias,this.dialect)),n.push(`ON`,this.printExpression(t.on));for(let e of t.whens)e.type===`matched`?n.push(this.printMergeWhenMatched(e)):n.push(this.printMergeWhenNotMatched(e));return n.join(` `)}printMergeWhenMatched(t){let n=[`WHEN MATCHED`];if(t.condition&&n.push(`AND`,this.printExpression(t.condition)),t.action===`delete`)n.push(`THEN DELETE`);else{n.push(`THEN UPDATE SET`);let r=(t.set??[]).map(t=>`${e(t.column,this.dialect)} = ${this.printExpression(t.value)}`);n.push(r.join(`, `))}return n.join(` `)}printMergeWhenNotMatched(t){let n=[`WHEN NOT MATCHED`];return t.condition&&n.push(`AND`,this.printExpression(t.condition)),n.push(`THEN INSERT`),n.push(`(${t.columns.map(t=>e(t,this.dialect)).join(`, `)})`),n.push(`VALUES (${t.values.map(e=>this.printExpression(e)).join(`, `)})`),n.join(` `)}printAliasedExpr(t){return`${this.printExpression(t.expr)} AS ${e(t.alias,this.dialect)}`}printFullTextSearch(t){let n=t.columns.map(e=>this.printExpression(e)).join(` || ' ' || `),r=t.language?`'${t.language}', `:``,i=`(to_tsvector(${r}${n}) @@ to_tsquery(${r}${this.printExpression(t.query)}))`;return t.alias&&(i+=` AS ${e(t.alias,this.dialect)}`),i}};export{t as i,n,e as r,r as t};
@@ -1 +0,0 @@
1
- var e=class extends Error{constructor(e){super(e),this.name=`SumakError`}},t=class extends e{constructor(e){super(e),this.name=`InvalidExpressionError`}},n=class extends e{constructor(e,t){super(`${t} is not supported in ${e}`),this.name=`UnsupportedDialectFeatureError`}},r=class extends e{constructor(e){super(`Cannot build ${e}: missing required clauses`),this.name=`EmptyQueryError`}};export{n as i,t as n,e as r,r as t};
@@ -1,136 +0,0 @@
1
- /**
2
- * Three-phase column type: Select, Insert, Update.
3
- *
4
- * S = type returned on SELECT
5
- * I = type accepted on INSERT
6
- * U = type accepted on UPDATE
7
- */
8
- interface ColumnType<S, I = S, U = I> {
9
- readonly __select: S;
10
- readonly __insert: I;
11
- readonly __update: U;
12
- }
13
- /** DB generates this value (autoincrement, default). Optional on INSERT/UPDATE. */
14
- type Generated<T> = ColumnType<T, T | undefined, T | undefined>;
15
- /** DB always generates (identity always). Never provided by user. */
16
- type GeneratedAlways<T> = ColumnType<T, never, never>;
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;
24
- /** Extract the INSERT type from a column. */
25
- type InsertType<C> = C extends {
26
- readonly __insert: infer I;
27
- } ? I : C;
28
- /** Extract the UPDATE type from a column. */
29
- type UpdateType<C> = C extends {
30
- readonly __update: infer U;
31
- } ? U : C;
32
- /** Make all properties nullable. */
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]> };
39
- /**
40
- * Infer a SELECT row type from a column map.
41
- */
42
- type Selectable<T> = { [K in keyof T]: SelectType<T[K]> };
43
- /**
44
- * Infer an INSERT row type from a column map.
45
- * Required columns: non-nullable without default.
46
- * Optional columns: nullable, has default, or generated.
47
- */
48
- type Insertable<T> = { [K in keyof T as IsRequired<T[K]> extends true ? K : never]: InsertType<T[K]> } & { [K in keyof T as IsRequired<T[K]> extends true ? never : K]?: InsertType<T[K]> };
49
- /**
50
- * Infer an UPDATE row type. All columns optional.
51
- */
52
- type Updateable<T> = { [K in keyof T]?: UpdateType<T[K]> };
53
- /**
54
- * A column is required on INSERT if its InsertType does NOT include undefined or never.
55
- */
56
- type IsRequired<C> = InsertType<C> extends never ? false : undefined extends InsertType<C> ? false : true;
57
- interface ColumnDef {
58
- readonly dataType: string;
59
- readonly isNotNull: boolean;
60
- readonly hasDefault: boolean;
61
- readonly isPrimaryKey: boolean;
62
- readonly isGenerated: boolean;
63
- readonly references?: {
64
- table: string;
65
- column: string;
66
- };
67
- }
68
- declare class ColumnBuilder<S, I = S, U = I> {
69
- /** @internal */
70
- readonly _def: ColumnDef;
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;
79
- constructor(dataType: string, def?: Partial<ColumnDef>);
80
- notNull(): ColumnBuilder<Exclude<S, null>, Exclude<I, null>, Exclude<U, null>>;
81
- nullable(): ColumnBuilder<S | null, I | null | undefined, U | null | undefined>;
82
- defaultTo(_value: I): ColumnBuilder<S, I | undefined, U>;
83
- primaryKey(): ColumnBuilder<S, I, U>;
84
- references(table: string, column: string): ColumnBuilder<S, I, U>;
85
- }
86
- declare function integer(): ColumnBuilder<number, number, number>;
87
- declare function bigint(): ColumnBuilder<bigint, bigint | number, bigint | number>;
88
- declare function smallint(): ColumnBuilder<number, number, number>;
89
- declare function serial(): ColumnBuilder<number, number | undefined, number>;
90
- declare function bigserial(): ColumnBuilder<bigint, bigint | undefined, bigint>;
91
- declare function text(): ColumnBuilder<string, string, string>;
92
- declare function varchar(length?: number): ColumnBuilder<string, string, string>;
93
- declare function char(length?: number): ColumnBuilder<string, string, string>;
94
- declare function boolean(): ColumnBuilder<boolean, boolean, boolean>;
95
- declare function timestamp(): ColumnBuilder<Date, Date | string, Date | string>;
96
- declare function timestamptz(): ColumnBuilder<Date, Date | string, Date | string>;
97
- declare function date(): ColumnBuilder<Date, Date | string, Date | string>;
98
- declare function time(): ColumnBuilder<string, string, string>;
99
- declare function uuid(): ColumnBuilder<string, string, string>;
100
- declare function json<T = unknown>(): ColumnBuilder<T, T, T>;
101
- declare function jsonb<T = unknown>(): ColumnBuilder<T, T, T>;
102
- declare function numeric(precision?: number, scale?: number): ColumnBuilder<string, string | number, string | number>;
103
- declare function real(): ColumnBuilder<number, number, number>;
104
- declare function doublePrecision(): ColumnBuilder<number, number, number>;
105
- declare function bytea(): ColumnBuilder<Uint8Array, Uint8Array, Uint8Array>;
106
- declare function enumType<T extends string>(...values: [T, ...T[]]): ColumnBuilder<T, T, T>;
107
- interface TableDefinition<TName extends string = string, TColumns extends Record<string, ColumnBuilder<any, any, any>> = Record<string, ColumnBuilder<any, any, any>>> {
108
- readonly name: TName;
109
- readonly columns: TColumns;
110
- }
111
- /**
112
- * Define a table schema with typed columns.
113
- *
114
- * ```ts
115
- * const users = defineTable("users", {
116
- * id: serial().primaryKey(),
117
- * name: text().notNull(),
118
- * email: text().notNull(),
119
- * active: boolean().defaultTo(true),
120
- * });
121
- * ```
122
- */
123
- declare function defineTable<TName extends string, TColumns extends Record<string, ColumnBuilder<any, any, any>>>(name: TName, columns: TColumns): TableDefinition<TName, TColumns>;
124
- /**
125
- * Extract the column type map from a table definition.
126
- * Used to build the DB type.
127
- *
128
- * ```ts
129
- * type DB = {
130
- * users: InferTable<typeof usersTable>;
131
- * posts: InferTable<typeof postsTable>;
132
- * };
133
- * ```
134
- */
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;
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 };
@@ -1,11 +0,0 @@
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 };
@@ -1 +0,0 @@
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,9 +0,0 @@
1
- import { L as SelectNode, S as FullTextSearchNode, T as InsertNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
- declare class MysqlPrinter extends BasePrinter {
3
- constructor();
4
- protected printInsert(node: InsertNode): string;
5
- protected printSelect(node: SelectNode): string;
6
- protected printFullTextSearch(node: FullTextSearchNode): string;
7
- }
8
- declare function mysqlDialect(): Dialect;
9
- export { MysqlPrinter as n, mysqlDialect as t };
@@ -1 +0,0 @@
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};