sumak 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 productdevbook
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,200 @@
1
+ <p align="center">
2
+ <br>
3
+ <img src=".github/assets/cover.jpg" alt="pamuk — Type-safe SQL query builder" width="100%">
4
+ <br><br>
5
+ <b style="font-size: 2em;">pamuk</b>
6
+ <br><br>
7
+ Type-safe SQL query builder with powerful SQL printers.
8
+ <br>
9
+ Zero dependencies, AST-first, hookable, tree-shakeable. Pure TypeScript, works everywhere.
10
+ <br><br>
11
+ <a href="https://npmjs.com/package/pamuk"><img src="https://img.shields.io/npm/v/pamuk?style=flat&colorA=18181B&colorB=e11d48" alt="npm version"></a>
12
+ <a href="https://npmjs.com/package/pamuk"><img src="https://img.shields.io/npm/dm/pamuk?style=flat&colorA=18181B&colorB=e11d48" alt="npm downloads"></a>
13
+ <a href="https://bundlephobia.com/result?p=pamuk"><img src="https://img.shields.io/bundlephobia/minzip/pamuk?style=flat&colorA=18181B&colorB=e11d48" alt="bundle size"></a>
14
+ <a href="https://github.com/productdevbook/pamuk/blob/main/LICENSE"><img src="https://img.shields.io/github/license/productdevbook/pamuk?style=flat&colorA=18181B&colorB=e11d48" alt="license"></a>
15
+ </p>
16
+
17
+ ## Quick Start
18
+
19
+ ```sh
20
+ npm install pamuk
21
+ ```
22
+
23
+ ```ts
24
+ import { pamuk, pgDialect, serial, text, boolean, integer } from "pamuk";
25
+
26
+ const db = pamuk({
27
+ dialect: pgDialect(),
28
+ tables: {
29
+ users: {
30
+ id: serial().primaryKey(),
31
+ name: text().notNull(),
32
+ email: text().notNull(),
33
+ active: boolean().defaultTo(true),
34
+ },
35
+ posts: {
36
+ id: serial().primaryKey(),
37
+ title: text().notNull(),
38
+ userId: integer().references("users", "id"),
39
+ },
40
+ },
41
+ });
42
+ ```
43
+
44
+ ## Query Building
45
+
46
+ ```ts
47
+ // SELECT
48
+ db.selectFrom("users")
49
+ .select("id", "name")
50
+ .where(({ age, active }) => and(age.gte(18), active.eq(true)))
51
+ .orderBy("name")
52
+ .limit(10)
53
+ .compile(db.printer());
54
+ // → SELECT "id", "name" FROM "users" WHERE ("age" >= $1 AND "active" = $2) ORDER BY "name" ASC LIMIT 10
55
+
56
+ // INSERT
57
+ db.insertInto("users")
58
+ .values({
59
+ name: "Alice",
60
+ email: "alice@example.com",
61
+ })
62
+ .returningAll()
63
+ .compile(db.printer());
64
+
65
+ // UPDATE
66
+ db.update("users")
67
+ .set({ active: false })
68
+ .where(({ id }) => id.eq(1))
69
+ .compile(db.printer());
70
+
71
+ // DELETE
72
+ db.deleteFrom("users")
73
+ .where(({ id }) => id.eq(1))
74
+ .returning("id")
75
+ .compile(db.printer());
76
+ ```
77
+
78
+ ## Joins
79
+
80
+ ```ts
81
+ db.selectFrom("users")
82
+ .innerJoin("posts", ({ users, posts }) => users.id.eqCol(posts.userId))
83
+ .compile(db.printer());
84
+ // → SELECT * FROM "users" INNER JOIN "posts" ON ("users"."id" = "posts"."userId")
85
+ ```
86
+
87
+ ## Tree Shaking
88
+
89
+ Import only the dialect you need:
90
+
91
+ ```ts
92
+ import { pamuk } from "pamuk";
93
+ import { pgDialect } from "pamuk/pg";
94
+ import { mysqlDialect } from "pamuk/mysql";
95
+ import { sqliteDialect } from "pamuk/sqlite";
96
+ import { serial, text } from "pamuk/schema";
97
+ ```
98
+
99
+ ## Dialects
100
+
101
+ Same query, different SQL:
102
+
103
+ ```ts
104
+ // PostgreSQL → SELECT "id" FROM "users" WHERE ("id" = $1)
105
+ // MySQL → SELECT `id` FROM `users` WHERE (`id` = ?)
106
+ // SQLite → SELECT "id" FROM "users" WHERE ("id" = ?)
107
+ ```
108
+
109
+ ## Plugins
110
+
111
+ ```ts
112
+ import { WithSchemaPlugin, SoftDeletePlugin, CamelCasePlugin } from "pamuk";
113
+
114
+ const db = pamuk({
115
+ dialect: pgDialect(),
116
+ plugins: [
117
+ new WithSchemaPlugin("public"),
118
+ new SoftDeletePlugin({ tables: ["users"] }),
119
+ ],
120
+ tables: { ... },
121
+ });
122
+
123
+ // SELECT * FROM "public"."users" WHERE ("deleted_at" IS NULL)
124
+ ```
125
+
126
+ ## Hooks
127
+
128
+ ```ts
129
+ // Query logging
130
+ db.hook("query:after", (ctx) => {
131
+ console.log(`[SQL] ${ctx.query.sql}`);
132
+ });
133
+
134
+ // Add request tracing
135
+ db.hook("query:after", (ctx) => {
136
+ return {
137
+ ...ctx.query,
138
+ sql: `${ctx.query.sql} /* request_id=${requestId} */`,
139
+ };
140
+ });
141
+
142
+ // Modify AST before compilation
143
+ db.hook("select:before", (ctx) => {
144
+ // Add tenant isolation, audit filters, etc.
145
+ });
146
+
147
+ // Transform results
148
+ db.hook("result:transform", (rows) => {
149
+ return rows.map(toCamelCase);
150
+ });
151
+
152
+ // Unregister
153
+ const off = db.hook("query:before", handler);
154
+ off();
155
+ ```
156
+
157
+ ## Expression API
158
+
159
+ ```ts
160
+ .where(({ id }) => id.eq(42)) // "id" = $1
161
+ .where(({ name }) => name.like("%ali%")) // "name" LIKE '%ali%'
162
+ .where(({ age }) => age.between(18, 65)) // "age" BETWEEN $1 AND $2
163
+ .where(({ id }) => id.in([1, 2, 3])) // "id" IN ($1, $2, $3)
164
+ .where(({ bio }) => bio.isNull()) // "bio" IS NULL
165
+ .where(({ email }) => email.isNotNull()) // "email" IS NOT NULL
166
+ .where(({ a, b }) => // ("a" > $1 AND "b" != $2)
167
+ and(a.gt(0), b.neq("x")),
168
+ )
169
+ .where(({ a, b }) => // ("a" = $1 OR "b" = $2)
170
+ or(a.eq(1), b.eq(2)),
171
+ )
172
+ ```
173
+
174
+ ## Why lale?
175
+
176
+ | | lale | Drizzle | Kysely |
177
+ | ------------------ | ----------------- | --------------- | -------------- |
178
+ | **Architecture** | AST-first | Template | AST (98 nodes) |
179
+ | **Type inference** | Auto (no codegen) | Auto | Manual DB type |
180
+ | **Plugin system** | Hooks + plugins | None | Plugins only |
181
+ | **SQL printer** | Wadler algebra | Template concat | String append |
182
+ | **Dependencies** | 0 | 0 | 0 |
183
+ | **Node types** | ~35 (focused) | Config objects | 98 (complex) |
184
+ | **API style** | Callback proxy | Method chain | Method chain |
185
+
186
+ ## Architecture
187
+
188
+ ```
189
+ Schema → Builder → AST → Plugin/Hook → Printer → SQL
190
+ ```
191
+
192
+ - **Schema Layer** — `defineTable()`, `ColumnType<S,I,U>`, auto type inference
193
+ - **Builder Layer** — `Lale<DB>`, `TypedSelectBuilder<DB,TB,O>`, proxy-based expressions
194
+ - **AST Layer** — ~35 frozen node types, discriminated unions, visitor pattern
195
+ - **Plugin Layer** — `LalePlugin` interface, `Hookable` lifecycle hooks
196
+ - **Printer Layer** — `BasePrinter` with dialect subclasses, Wadler document algebra
197
+
198
+ ## License
199
+
200
+ [MIT](./LICENSE)
@@ -0,0 +1 @@
1
+ function e(e,t){switch(t){case`pg`:case`sqlite`:return`"${e.replaceAll(`"`,`""`)}"`;case`mysql`: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`?`}}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);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)}}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.alias&&(r+=` AS ${e(n.alias,this.dialect)}`),r}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`}}};export{t as i,n,e as r,r as t};
@@ -0,0 +1 @@
1
+ var e=class extends Error{constructor(e){super(e),this.name=`PamukError`}},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};
@@ -0,0 +1,119 @@
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
+ /** Extract the SELECT type from a column. */
18
+ type SelectType<C> = C extends ColumnType<infer S, any, any> ? S : C;
19
+ /** Extract the INSERT type from a column. */
20
+ type InsertType<C> = C extends ColumnType<any, infer I, any> ? I : C;
21
+ /** Extract the UPDATE type from a column. */
22
+ type UpdateType<C> = C extends ColumnType<any, any, infer U> ? U : C;
23
+ /** Make all properties nullable. */
24
+ type Nullable$1<T> = { [K in keyof T]: T[K] | null };
25
+ /**
26
+ * Infer a SELECT row type from a table column map.
27
+ * Every column present, nullable columns include null.
28
+ */
29
+ type Selectable<T> = { [K in keyof T]: SelectType<T[K]> };
30
+ /**
31
+ * Infer an INSERT row type from a table column map.
32
+ * Required columns: non-nullable without default.
33
+ * Optional columns: nullable, has default, or generated.
34
+ */
35
+ 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]> };
36
+ /**
37
+ * Infer an UPDATE row type. All columns optional.
38
+ */
39
+ type Updateable<T> = { [K in keyof T]?: UpdateType<T[K]> };
40
+ /**
41
+ * A column is required on INSERT if its InsertType does NOT include undefined or never.
42
+ */
43
+ type IsRequired<C> = InsertType<C> extends never ? false : undefined extends InsertType<C> ? false : true;
44
+ interface ColumnDef {
45
+ readonly dataType: string;
46
+ readonly isNotNull: boolean;
47
+ readonly hasDefault: boolean;
48
+ readonly isPrimaryKey: boolean;
49
+ readonly isGenerated: boolean;
50
+ readonly references?: {
51
+ table: string;
52
+ column: string;
53
+ };
54
+ }
55
+ declare class ColumnBuilder<S, I = S, U = I> {
56
+ /** @internal */
57
+ readonly _def: ColumnDef;
58
+ /** Phantom type — never exists at runtime */
59
+ readonly _type: ColumnType<S, I, U>;
60
+ constructor(dataType: string, def?: Partial<ColumnDef>);
61
+ notNull(): ColumnBuilder<Exclude<S, null>, Exclude<I, null>, Exclude<U, null>>;
62
+ nullable(): ColumnBuilder<S | null, I | null | undefined, U | null | undefined>;
63
+ defaultTo(_value: I): ColumnBuilder<S, I | undefined, U>;
64
+ primaryKey(): ColumnBuilder<S, I, U>;
65
+ references(table: string, column: string): ColumnBuilder<S, I, U>;
66
+ }
67
+ declare function integer(): ColumnBuilder<number, number, number>;
68
+ declare function bigint(): ColumnBuilder<bigint, bigint | number, bigint | number>;
69
+ declare function smallint(): ColumnBuilder<number, number, number>;
70
+ declare function serial(): ColumnBuilder<number, number | undefined, number>;
71
+ declare function bigserial(): ColumnBuilder<bigint, bigint | undefined, bigint>;
72
+ declare function text(): ColumnBuilder<string, string, string>;
73
+ declare function varchar(length?: number): ColumnBuilder<string, string, string>;
74
+ declare function char(length?: number): ColumnBuilder<string, string, string>;
75
+ declare function boolean(): ColumnBuilder<boolean, boolean, boolean>;
76
+ declare function timestamp(): ColumnBuilder<Date, Date | string, Date | string>;
77
+ declare function timestamptz(): ColumnBuilder<Date, Date | string, Date | string>;
78
+ declare function date(): ColumnBuilder<Date, Date | string, Date | string>;
79
+ declare function time(): ColumnBuilder<string, string, string>;
80
+ declare function uuid(): ColumnBuilder<string, string, string>;
81
+ declare function json<T = unknown>(): ColumnBuilder<T, T, T>;
82
+ declare function jsonb<T = unknown>(): ColumnBuilder<T, T, T>;
83
+ declare function numeric(precision?: number, scale?: number): ColumnBuilder<string, string | number, string | number>;
84
+ declare function real(): ColumnBuilder<number, number, number>;
85
+ declare function doublePrecision(): ColumnBuilder<number, number, number>;
86
+ declare function bytea(): ColumnBuilder<Buffer, Buffer | Uint8Array, Buffer | Uint8Array>;
87
+ declare function enumType<T extends string>(...values: [T, ...T[]]): ColumnBuilder<T, T, T>;
88
+ interface TableDefinition<TName extends string = string, TColumns extends Record<string, ColumnBuilder<any, any, any>> = Record<string, ColumnBuilder<any, any, any>>> {
89
+ readonly name: TName;
90
+ readonly columns: TColumns;
91
+ }
92
+ /**
93
+ * Define a table schema with typed columns.
94
+ *
95
+ * ```ts
96
+ * const users = defineTable("users", {
97
+ * id: serial().primaryKey(),
98
+ * name: text().notNull(),
99
+ * email: text().notNull(),
100
+ * active: boolean().defaultTo(true),
101
+ * });
102
+ * ```
103
+ */
104
+ declare function defineTable<TName extends string, TColumns extends Record<string, ColumnBuilder<any, any, any>>>(name: TName, columns: TColumns): TableDefinition<TName, TColumns>;
105
+ /**
106
+ * Extract the column type map from a table definition.
107
+ * Used to build the DB type.
108
+ *
109
+ * ```ts
110
+ * type DB = {
111
+ * users: InferTable<typeof usersTable>;
112
+ * posts: InferTable<typeof postsTable>;
113
+ * };
114
+ * ```
115
+ */
116
+ 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 };
@@ -0,0 +1,8 @@
1
+ import { S as InsertNode, j as SelectNode, 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
+ }
7
+ declare function mysqlDialect(): Dialect;
8
+ export { MysqlPrinter as n, mysqlDialect as t };
@@ -0,0 +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};
@@ -0,0 +1,7 @@
1
+ import { S as InsertNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
+ declare class PgPrinter extends BasePrinter {
3
+ constructor();
4
+ protected printInsert(node: InsertNode): string;
5
+ }
6
+ declare function pgDialect(): Dialect;
7
+ export { PgPrinter as n, pgDialect as t };
@@ -0,0 +1 @@
1
+ import{t as e}from"./base.mjs";var t=class extends e{constructor(){super(`pg`)}printInsert(e){return super.printInsert(e)}};function n(){return{name:`pg`,createPrinter(){return new t}}}export{t as n,n as t};
@@ -0,0 +1 @@
1
+ var e=class e{_def;constructor(e,t){this._def={dataType:e,isNotNull:!1,hasDefault:!1,isPrimaryKey:!1,isGenerated:!1,...t}}notNull(){return new e(this._def.dataType,{...this._def,isNotNull:!0})}nullable(){return new e(this._def.dataType,{...this._def,isNotNull:!1})}defaultTo(t){return new e(this._def.dataType,{...this._def,hasDefault:!0})}primaryKey(){return new e(this._def.dataType,{...this._def,isPrimaryKey:!0,isNotNull:!0})}references(t,n){return new e(this._def.dataType,{...this._def,references:{table:t,column:n}})}};function t(){return new e(`integer`)}function n(){return new e(`bigint`)}function r(){return new e(`smallint`)}function i(){return new e(`serial`,{hasDefault:!0,isNotNull:!0,isPrimaryKey:!0})}function a(){return new e(`bigserial`,{hasDefault:!0,isNotNull:!0})}function o(){return new e(`text`)}function s(t){return new e(t?`varchar(${t})`:`varchar`)}function c(t){return new e(t?`char(${t})`:`char`)}function l(){return new e(`boolean`)}function u(){return new e(`timestamp`)}function d(){return new e(`timestamptz`)}function f(){return new e(`date`)}function p(){return new e(`time`)}function m(){return new e(`uuid`)}function h(){return new e(`json`)}function g(){return new e(`jsonb`)}function _(t,n){return new e(t==null?`numeric`:n==null?`numeric(${t})`:`numeric(${t},${n})`)}function v(){return new e(`real`)}function y(){return new e(`double precision`)}function b(){return new e(`bytea`)}function x(...t){return new e(`enum(${t.map(e=>`'${e}'`).join(`,`)})`)}function S(e,t){return Object.freeze({name:e,columns:t})}export{s as C,m as S,r as _,l as a,u as b,f as c,t as d,h as f,i as g,v as h,a as i,y as l,_ as m,e as n,b as o,g as p,n as r,c as s,S as t,x as u,o as v,d as x,p as y};
@@ -0,0 +1,8 @@
1
+ import { S as InsertNode, j as SelectNode, n as BasePrinter, t as Dialect } from "./types.mjs";
2
+ declare class SqlitePrinter extends BasePrinter {
3
+ constructor();
4
+ protected printSelect(node: SelectNode): string;
5
+ protected printInsert(node: InsertNode): string;
6
+ }
7
+ declare function sqliteDialect(): Dialect;
8
+ export { SqlitePrinter as n, sqliteDialect as t };
@@ -0,0 +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};
@@ -0,0 +1,274 @@
1
+ type SQLDialect = "pg" | "mysql" | "sqlite";
2
+ interface CompiledQuery {
3
+ sql: string;
4
+ params: readonly unknown[];
5
+ }
6
+ type Primitive = string | number | boolean | null;
7
+ type OrderDirection = "ASC" | "DESC";
8
+ type JoinType = "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS";
9
+ type SetOperator = "UNION" | "UNION ALL" | "INTERSECT" | "EXCEPT";
10
+ interface DialectConfig {
11
+ name: SQLDialect;
12
+ quoteIdentifier(name: string): string;
13
+ formatParam(index: number): string;
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;
17
+ interface ColumnRefNode {
18
+ type: "column_ref";
19
+ table?: string;
20
+ column: string;
21
+ alias?: string;
22
+ }
23
+ interface LiteralNode {
24
+ type: "literal";
25
+ value: string | number | boolean | null;
26
+ }
27
+ interface BinaryOpNode {
28
+ type: "binary_op";
29
+ op: string;
30
+ left: ExpressionNode;
31
+ right: ExpressionNode;
32
+ }
33
+ interface UnaryOpNode {
34
+ type: "unary_op";
35
+ op: string;
36
+ operand: ExpressionNode;
37
+ position: "prefix" | "postfix";
38
+ }
39
+ interface FunctionCallNode {
40
+ type: "function_call";
41
+ name: string;
42
+ args: ExpressionNode[];
43
+ alias?: string;
44
+ }
45
+ interface ParamNode {
46
+ type: "param";
47
+ index: number;
48
+ value: unknown;
49
+ }
50
+ interface RawNode {
51
+ type: "raw";
52
+ sql: string;
53
+ params: unknown[];
54
+ }
55
+ interface SubqueryNode {
56
+ type: "subquery";
57
+ query: SelectNode;
58
+ alias?: string;
59
+ }
60
+ interface BetweenNode {
61
+ type: "between";
62
+ expr: ExpressionNode;
63
+ low: ExpressionNode;
64
+ high: ExpressionNode;
65
+ negated: boolean;
66
+ }
67
+ interface InNode {
68
+ type: "in";
69
+ expr: ExpressionNode;
70
+ values: ExpressionNode[] | SelectNode;
71
+ negated: boolean;
72
+ }
73
+ interface IsNullNode {
74
+ type: "is_null";
75
+ expr: ExpressionNode;
76
+ negated: boolean;
77
+ }
78
+ interface CaseNode {
79
+ type: "case";
80
+ operand?: ExpressionNode;
81
+ whens: {
82
+ condition: ExpressionNode;
83
+ result: ExpressionNode;
84
+ }[];
85
+ else_?: ExpressionNode;
86
+ }
87
+ interface CastNode {
88
+ type: "cast";
89
+ expr: ExpressionNode;
90
+ dataType: string;
91
+ }
92
+ interface ExistsNode {
93
+ type: "exists";
94
+ query: SelectNode;
95
+ negated: boolean;
96
+ }
97
+ interface StarNode {
98
+ type: "star";
99
+ table?: string;
100
+ }
101
+ interface TableRefNode {
102
+ type: "table_ref";
103
+ name: string;
104
+ alias?: string;
105
+ schema?: string;
106
+ }
107
+ interface JoinNode {
108
+ type: "join";
109
+ joinType: JoinType;
110
+ table: TableRefNode | SubqueryNode;
111
+ on?: ExpressionNode;
112
+ }
113
+ interface JsonAccessNode {
114
+ type: "json_access";
115
+ expr: ExpressionNode;
116
+ path: string;
117
+ operator: "->" | "->>" | "#>" | "#>>";
118
+ alias?: string;
119
+ }
120
+ interface ArrayExprNode {
121
+ type: "array_expr";
122
+ elements: ExpressionNode[];
123
+ }
124
+ type FrameKind = "ROWS" | "RANGE" | "GROUPS";
125
+ type FrameBound = {
126
+ type: "unbounded_preceding";
127
+ } | {
128
+ type: "preceding";
129
+ value: number;
130
+ } | {
131
+ type: "current_row";
132
+ } | {
133
+ type: "following";
134
+ value: number;
135
+ } | {
136
+ type: "unbounded_following";
137
+ };
138
+ interface FrameSpec {
139
+ kind: FrameKind;
140
+ start: FrameBound;
141
+ end?: FrameBound;
142
+ }
143
+ interface WindowFunctionNode {
144
+ type: "window_function";
145
+ fn: FunctionCallNode;
146
+ partitionBy: ExpressionNode[];
147
+ orderBy: OrderByNode[];
148
+ frame?: FrameSpec;
149
+ alias?: string;
150
+ }
151
+ interface OrderByNode {
152
+ expr: ExpressionNode;
153
+ direction: OrderDirection;
154
+ nulls?: "FIRST" | "LAST";
155
+ }
156
+ interface CTENode {
157
+ name: string;
158
+ query: SelectNode;
159
+ recursive: boolean;
160
+ }
161
+ interface SelectNode {
162
+ type: "select";
163
+ distinct: boolean;
164
+ columns: ExpressionNode[];
165
+ from?: TableRefNode | SubqueryNode;
166
+ joins: JoinNode[];
167
+ where?: ExpressionNode;
168
+ groupBy: ExpressionNode[];
169
+ having?: ExpressionNode;
170
+ orderBy: OrderByNode[];
171
+ limit?: ExpressionNode;
172
+ offset?: ExpressionNode;
173
+ ctes: CTENode[];
174
+ setOp?: {
175
+ op: SetOperator;
176
+ query: SelectNode;
177
+ };
178
+ forUpdate: boolean;
179
+ }
180
+ interface InsertNode {
181
+ type: "insert";
182
+ table: TableRefNode;
183
+ columns: string[];
184
+ values: ExpressionNode[][];
185
+ returning: ExpressionNode[];
186
+ onConflict?: OnConflictNode;
187
+ ctes: CTENode[];
188
+ }
189
+ interface OnConflictNode {
190
+ columns: string[];
191
+ action: "nothing" | {
192
+ set: {
193
+ column: string;
194
+ value: ExpressionNode;
195
+ }[];
196
+ };
197
+ where?: ExpressionNode;
198
+ }
199
+ interface UpdateNode {
200
+ type: "update";
201
+ table: TableRefNode;
202
+ set: {
203
+ column: string;
204
+ value: ExpressionNode;
205
+ }[];
206
+ where?: ExpressionNode;
207
+ returning: ExpressionNode[];
208
+ from?: TableRefNode;
209
+ ctes: CTENode[];
210
+ }
211
+ interface DeleteNode {
212
+ type: "delete";
213
+ table: TableRefNode;
214
+ where?: ExpressionNode;
215
+ returning: ExpressionNode[];
216
+ ctes: CTENode[];
217
+ }
218
+ declare function tableRef(name: string, alias?: string, schema?: string): TableRefNode;
219
+ declare function createSelectNode(): SelectNode;
220
+ declare function createInsertNode(table: TableRefNode): InsertNode;
221
+ declare function createUpdateNode(table: TableRefNode): UpdateNode;
222
+ declare function createDeleteNode(table: TableRefNode): DeleteNode;
223
+ type PrintMode = "compact" | "formatted" | "debug";
224
+ interface PrinterOptions {
225
+ dialect: SQLDialect;
226
+ mode?: PrintMode;
227
+ indent?: string;
228
+ width?: number;
229
+ }
230
+ interface Printer {
231
+ print(node: ASTNode): CompiledQuery;
232
+ }
233
+ declare class BasePrinter implements Printer {
234
+ protected params: unknown[];
235
+ protected dialect: SQLDialect;
236
+ constructor(dialect: SQLDialect);
237
+ print(node: ASTNode): CompiledQuery;
238
+ protected printNode(node: ASTNode): string;
239
+ protected printSelect(node: SelectNode): string;
240
+ protected printInsert(node: InsertNode): string;
241
+ protected printOnConflict(node: OnConflictNode): string;
242
+ protected printUpdate(node: UpdateNode): string;
243
+ protected printDelete(node: DeleteNode): string;
244
+ protected printExpression(node: ExpressionNode): string;
245
+ protected printColumnRef(node: ColumnRefNode): string;
246
+ protected printLiteral(node: LiteralNode): string;
247
+ protected printBinaryOp(node: BinaryOpNode): string;
248
+ protected printUnaryOp(node: UnaryOpNode): string;
249
+ protected printFunctionCall(node: FunctionCallNode): string;
250
+ protected printParam(node: ParamNode): string;
251
+ protected printRaw(node: RawNode): string;
252
+ protected printSubquery(node: SubqueryNode): string;
253
+ protected printBetween(node: BetweenNode): string;
254
+ protected printIn(node: InNode): string;
255
+ protected printIsNull(node: IsNullNode): string;
256
+ protected printCase(node: CaseNode): string;
257
+ protected printCast(node: CastNode): string;
258
+ protected printExists(node: ExistsNode): string;
259
+ protected printStar(node: StarNode): string;
260
+ protected printTableRef(ref: TableRefNode): string;
261
+ protected printJoin(node: JoinNode): string;
262
+ protected printOrderBy(node: OrderByNode): string;
263
+ protected printCTEs(ctes: CTENode[]): string;
264
+ protected printJsonAccess(node: JsonAccessNode): string;
265
+ protected printArrayExpr(node: ArrayExprNode): string;
266
+ protected printWindowFunction(node: WindowFunctionNode): string;
267
+ protected printFrameSpec(frame: FrameSpec): string;
268
+ protected printFrameBound(bound: FrameBound): string;
269
+ }
270
+ interface Dialect {
271
+ name: SQLDialect;
272
+ createPrinter(): Printer;
273
+ }
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 };