sumak 0.0.1 → 0.0.2
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/README.md +16 -16
- package/dist/_chunks/errors.mjs +1 -1
- package/dist/index.d.mts +24 -24
- package/dist/index.mjs +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<br>
|
|
3
|
-
<img src=".github/assets/cover.jpg" alt="
|
|
3
|
+
<img src=".github/assets/cover.jpg" alt="sumak — Type-safe SQL query builder" width="100%">
|
|
4
4
|
<br><br>
|
|
5
|
-
<b style="font-size: 2em;">
|
|
5
|
+
<b style="font-size: 2em;">sumak</b>
|
|
6
6
|
<br><br>
|
|
7
7
|
Type-safe SQL query builder with powerful SQL printers.
|
|
8
8
|
<br>
|
|
9
9
|
Zero dependencies, AST-first, hookable, tree-shakeable. Pure TypeScript, works everywhere.
|
|
10
10
|
<br><br>
|
|
11
|
-
<a href="https://npmjs.com/package/
|
|
12
|
-
<a href="https://npmjs.com/package/
|
|
13
|
-
<a href="https://bundlephobia.com/result?p=
|
|
14
|
-
<a href="https://github.com/productdevbook/
|
|
11
|
+
<a href="https://npmjs.com/package/sumak"><img src="https://img.shields.io/npm/v/sumak?style=flat&colorA=18181B&colorB=e11d48" alt="npm version"></a>
|
|
12
|
+
<a href="https://npmjs.com/package/sumak"><img src="https://img.shields.io/npm/dm/sumak?style=flat&colorA=18181B&colorB=e11d48" alt="npm downloads"></a>
|
|
13
|
+
<a href="https://bundlephobia.com/result?p=sumak"><img src="https://img.shields.io/bundlephobia/minzip/sumak?style=flat&colorA=18181B&colorB=e11d48" alt="bundle size"></a>
|
|
14
|
+
<a href="https://github.com/productdevbook/sumak/blob/main/LICENSE"><img src="https://img.shields.io/github/license/productdevbook/sumak?style=flat&colorA=18181B&colorB=e11d48" alt="license"></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
17
|
## Quick Start
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
|
-
npm install
|
|
20
|
+
npm install sumak
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
|
-
import {
|
|
24
|
+
import { sumak, pgDialect, serial, text, boolean, integer } from "sumak";
|
|
25
25
|
|
|
26
|
-
const db =
|
|
26
|
+
const db = sumak({
|
|
27
27
|
dialect: pgDialect(),
|
|
28
28
|
tables: {
|
|
29
29
|
users: {
|
|
@@ -89,11 +89,11 @@ db.selectFrom("users")
|
|
|
89
89
|
Import only the dialect you need:
|
|
90
90
|
|
|
91
91
|
```ts
|
|
92
|
-
import {
|
|
93
|
-
import { pgDialect } from "
|
|
94
|
-
import { mysqlDialect } from "
|
|
95
|
-
import { sqliteDialect } from "
|
|
96
|
-
import { serial, text } from "
|
|
92
|
+
import { sumak } from "sumak";
|
|
93
|
+
import { pgDialect } from "sumak/pg";
|
|
94
|
+
import { mysqlDialect } from "sumak/mysql";
|
|
95
|
+
import { sqliteDialect } from "sumak/sqlite";
|
|
96
|
+
import { serial, text } from "sumak/schema";
|
|
97
97
|
```
|
|
98
98
|
|
|
99
99
|
## Dialects
|
|
@@ -109,9 +109,9 @@ Same query, different SQL:
|
|
|
109
109
|
## Plugins
|
|
110
110
|
|
|
111
111
|
```ts
|
|
112
|
-
import { WithSchemaPlugin, SoftDeletePlugin, CamelCasePlugin } from "
|
|
112
|
+
import { WithSchemaPlugin, SoftDeletePlugin, CamelCasePlugin } from "sumak";
|
|
113
113
|
|
|
114
|
-
const db =
|
|
114
|
+
const db = sumak({
|
|
115
115
|
dialect: pgDialect(),
|
|
116
116
|
plugins: [
|
|
117
117
|
new WithSchemaPlugin("public"),
|
package/dist/_chunks/errors.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=class extends Error{constructor(e){super(e),this.name=`
|
|
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};
|
package/dist/index.d.mts
CHANGED
|
@@ -428,14 +428,14 @@ declare class TypedDeleteReturningBuilder<DB, _TB extends keyof DB, _R> {
|
|
|
428
428
|
compile(printer: Printer): CompiledQuery;
|
|
429
429
|
}
|
|
430
430
|
/**
|
|
431
|
-
* Plugin interface for
|
|
431
|
+
* Plugin interface for sumak.
|
|
432
432
|
*
|
|
433
433
|
* Plugins can intercept at three points:
|
|
434
434
|
* 1. transformNode — modify the AST before compilation
|
|
435
435
|
* 2. transformQuery — modify the compiled SQL after generation
|
|
436
436
|
* 3. transformResult — modify result rows after execution
|
|
437
437
|
*/
|
|
438
|
-
interface
|
|
438
|
+
interface SumakPlugin {
|
|
439
439
|
readonly name: string;
|
|
440
440
|
/** Transform AST before compilation. Return a new node (never mutate). */
|
|
441
441
|
transformNode?(node: ASTNode): ASTNode;
|
|
@@ -458,7 +458,7 @@ interface HookContext<T extends ASTNode = ASTNode> {
|
|
|
458
458
|
/**
|
|
459
459
|
* All available hook points in the query lifecycle.
|
|
460
460
|
*/
|
|
461
|
-
interface
|
|
461
|
+
interface SumakHooks {
|
|
462
462
|
/** Fires before any query is compiled. Can modify the AST. */
|
|
463
463
|
"query:before": (ctx: HookContext) => ASTNode | void;
|
|
464
464
|
/** Fires after a query is compiled to SQL. Can modify the compiled query. */
|
|
@@ -476,7 +476,7 @@ interface PamukHooks {
|
|
|
476
476
|
/** Transform result rows. */
|
|
477
477
|
"result:transform": (rows: Record<string, unknown>[]) => Record<string, unknown>[];
|
|
478
478
|
}
|
|
479
|
-
type HookName = keyof
|
|
479
|
+
type HookName = keyof SumakHooks;
|
|
480
480
|
/**
|
|
481
481
|
* Hookable system — register and execute hooks.
|
|
482
482
|
*
|
|
@@ -494,13 +494,13 @@ declare class Hookable {
|
|
|
494
494
|
* Register a hook handler.
|
|
495
495
|
* Returns an unregister function.
|
|
496
496
|
*/
|
|
497
|
-
hook<K extends HookName>(name: K, handler:
|
|
497
|
+
hook<K extends HookName>(name: K, handler: SumakHooks[K]): () => void;
|
|
498
498
|
/**
|
|
499
499
|
* Execute all handlers for a hook.
|
|
500
500
|
* For AST hooks: each handler can return a modified node, which feeds into the next.
|
|
501
501
|
* For result hooks: each handler transforms the rows.
|
|
502
502
|
*/
|
|
503
|
-
callHook<K extends HookName>(name: K, ...args: Parameters<
|
|
503
|
+
callHook<K extends HookName>(name: K, ...args: Parameters<SumakHooks[K]>): ReturnType<SumakHooks[K]> | undefined;
|
|
504
504
|
/**
|
|
505
505
|
* Check if any handlers are registered for a hook.
|
|
506
506
|
*/
|
|
@@ -518,16 +518,16 @@ declare class Hookable {
|
|
|
518
518
|
* Extract the DB type from a tables config object.
|
|
519
519
|
*/
|
|
520
520
|
type InferDB<T extends Record<string, Record<string, ColumnBuilder<any, any, any>>>> = { [Table in keyof T]: { [Col in keyof T[Table]]: T[Table][Col] extends ColumnBuilder<infer S, infer I, infer U> ? ColumnType<S, I, U> : never } };
|
|
521
|
-
interface
|
|
521
|
+
interface SumakConfig<T extends Record<string, Record<string, ColumnBuilder<any, any, any>>>> {
|
|
522
522
|
dialect: Dialect;
|
|
523
523
|
tables: T;
|
|
524
|
-
plugins?:
|
|
524
|
+
plugins?: SumakPlugin[];
|
|
525
525
|
}
|
|
526
526
|
/**
|
|
527
|
-
* Create a fully typed
|
|
527
|
+
* Create a fully typed sumak instance. DB type is inferred automatically.
|
|
528
528
|
*
|
|
529
529
|
* ```ts
|
|
530
|
-
* const db =
|
|
530
|
+
* const db = sumak({
|
|
531
531
|
* dialect: pgDialect(),
|
|
532
532
|
* tables: {
|
|
533
533
|
* users: { id: serial(), name: text().notNull() },
|
|
@@ -542,15 +542,15 @@ interface PamukConfig<T extends Record<string, Record<string, ColumnBuilder<any,
|
|
|
542
542
|
* db.selectFrom("users").select("id", "name")...
|
|
543
543
|
* ```
|
|
544
544
|
*/
|
|
545
|
-
declare function
|
|
545
|
+
declare function sumak<T extends Record<string, Record<string, ColumnBuilder<any, any, any>>>>(config: SumakConfig<T>): Lale<InferDB<T>>;
|
|
546
546
|
/**
|
|
547
|
-
* Core
|
|
547
|
+
* Core sumak instance with hook system.
|
|
548
548
|
*/
|
|
549
|
-
declare class
|
|
549
|
+
declare class Sumak<DB> {
|
|
550
550
|
private _dialect;
|
|
551
551
|
private _plugins;
|
|
552
552
|
private _hooks;
|
|
553
|
-
constructor(dialect: Dialect, plugins?:
|
|
553
|
+
constructor(dialect: Dialect, plugins?: SumakPlugin[]);
|
|
554
554
|
/**
|
|
555
555
|
* Register a hook handler. Returns an unregister function.
|
|
556
556
|
*
|
|
@@ -559,7 +559,7 @@ declare class Pamuk<DB> {
|
|
|
559
559
|
* off(); // unregister
|
|
560
560
|
* ```
|
|
561
561
|
*/
|
|
562
|
-
hook<K extends HookName>(name: K, handler:
|
|
562
|
+
hook<K extends HookName>(name: K, handler: SumakHooks[K]): () => void;
|
|
563
563
|
selectFrom<T extends keyof DB & string>(table: T, alias?: string): TypedSelectBuilder<DB, T, { [K in keyof DB[T]]: SelectType<DB[T][K]> }>;
|
|
564
564
|
insertInto<T extends keyof DB & string>(table: T): TypedInsertBuilder<DB, T>;
|
|
565
565
|
update<T extends keyof DB & string>(table: T): TypedUpdateBuilder<DB, T>;
|
|
@@ -582,7 +582,7 @@ declare class Pamuk<DB> {
|
|
|
582
582
|
*/
|
|
583
583
|
declare class PluginManager {
|
|
584
584
|
private readonly plugins;
|
|
585
|
-
constructor(plugins:
|
|
585
|
+
constructor(plugins: SumakPlugin[]);
|
|
586
586
|
/** Apply all transformNode phases in order. */
|
|
587
587
|
transformNode(node: ASTNode): ASTNode;
|
|
588
588
|
/** Apply all transformQuery phases in order. */
|
|
@@ -598,7 +598,7 @@ declare class PluginManager {
|
|
|
598
598
|
* // SELECT * FROM "users" → SELECT * FROM "public"."users"
|
|
599
599
|
* ```
|
|
600
600
|
*/
|
|
601
|
-
declare class WithSchemaPlugin implements
|
|
601
|
+
declare class WithSchemaPlugin implements SumakPlugin {
|
|
602
602
|
readonly name = "with-schema";
|
|
603
603
|
private schema;
|
|
604
604
|
constructor(schema: string);
|
|
@@ -618,7 +618,7 @@ declare class WithSchemaPlugin implements PamukPlugin {
|
|
|
618
618
|
* // SELECT * FROM "users" → SELECT * FROM "users" WHERE "deleted_at" IS NULL
|
|
619
619
|
* ```
|
|
620
620
|
*/
|
|
621
|
-
declare class SoftDeletePlugin implements
|
|
621
|
+
declare class SoftDeletePlugin implements SumakPlugin {
|
|
622
622
|
readonly name = "soft-delete";
|
|
623
623
|
private tables;
|
|
624
624
|
private column;
|
|
@@ -640,20 +640,20 @@ declare class SoftDeletePlugin implements PamukPlugin {
|
|
|
640
640
|
* This plugin operates on results only — it does NOT transform the AST.
|
|
641
641
|
* Use it when your database uses snake_case but your TypeScript code uses camelCase.
|
|
642
642
|
*/
|
|
643
|
-
declare class CamelCasePlugin implements
|
|
643
|
+
declare class CamelCasePlugin implements SumakPlugin {
|
|
644
644
|
readonly name = "camel-case";
|
|
645
645
|
transformResult(rows: Record<string, unknown>[]): Record<string, unknown>[];
|
|
646
646
|
}
|
|
647
|
-
declare class
|
|
647
|
+
declare class SumakError extends Error {
|
|
648
648
|
constructor(message: string);
|
|
649
649
|
}
|
|
650
|
-
declare class InvalidExpressionError extends
|
|
650
|
+
declare class InvalidExpressionError extends SumakError {
|
|
651
651
|
constructor(message: string);
|
|
652
652
|
}
|
|
653
|
-
declare class UnsupportedDialectFeatureError extends
|
|
653
|
+
declare class UnsupportedDialectFeatureError extends SumakError {
|
|
654
654
|
constructor(dialect: string, feature: string);
|
|
655
655
|
}
|
|
656
|
-
declare class EmptyQueryError extends
|
|
656
|
+
declare class EmptyQueryError extends SumakError {
|
|
657
657
|
constructor(queryType: string);
|
|
658
658
|
}
|
|
659
|
-
export { type ASTNode, ASTTransformer, type ASTVisitor, type ArrayExprNode, BasePrinter, type BetweenNode, type BinaryOpNode, type CTENode, CamelCasePlugin, type CaseNode, type CastNode, Col$1 as Col, ColumnBuilder, type ColumnDef, type ColumnProxies, type ColumnRefNode, type ColumnType, type CompiledQuery, DeleteBuilder, type DeleteNode, type Dialect, type DialectConfig, type Doc, EmptyQueryError, type ExistsNode, type Expression, type ExpressionNode, type FormatOptions, type FrameBound, type FrameKind, type FrameSpec, type FunctionCallNode, type Generated, type GeneratedAlways, type HookContext, type HookName, Hookable, type InNode, type InferTable, InsertBuilder, type InsertNode, type InsertType, type Insertable, InvalidExpressionError, type IsNullNode, type JoinNode, type JoinType, type JsonAccessNode, type LiteralNode, MysqlPrinter, type Nullable, type OnConflictNode, type OrderByNode, type OrderDirection,
|
|
659
|
+
export { type ASTNode, ASTTransformer, type ASTVisitor, type ArrayExprNode, BasePrinter, type BetweenNode, type BinaryOpNode, type CTENode, CamelCasePlugin, type CaseNode, type CastNode, Col$1 as Col, ColumnBuilder, type ColumnDef, type ColumnProxies, type ColumnRefNode, type ColumnType, type CompiledQuery, DeleteBuilder, type DeleteNode, type Dialect, type DialectConfig, type Doc, EmptyQueryError, type ExistsNode, type Expression, type ExpressionNode, type FormatOptions, type FrameBound, type FrameKind, type FrameSpec, type FunctionCallNode, type Generated, type GeneratedAlways, type HookContext, type HookName, Hookable, type InNode, type InferTable, InsertBuilder, type InsertNode, type InsertType, type Insertable, InvalidExpressionError, type IsNullNode, type JoinNode, type JoinType, type JsonAccessNode, type LiteralNode, MysqlPrinter, type Nullable, type OnConflictNode, type OrderByNode, type OrderDirection, type ParamNode, PgPrinter, PluginManager, type Primitive, type PrintMode, type Printer, type PrinterOptions, type RawNode, type SQLDialect, SelectBuilder, type SelectNode, type SelectType, type Selectable, type SetOperator, SoftDeletePlugin, SqlitePrinter, type StarNode, type SubqueryNode, Sumak, type SumakConfig, SumakError, type SumakHooks, type SumakPlugin, type TableDefinition, type TableRefNode, TypedDeleteBuilder, TypedDeleteReturningBuilder, TypedInsertBuilder, TypedInsertReturningBuilder, TypedSelectBuilder, TypedUpdateBuilder, TypedUpdateReturningBuilder, type UnaryOpNode, UnsupportedDialectFeatureError, UpdateBuilder, type UpdateNode, type UpdateType, type Updateable, type WhereCallback, type WindowFunctionNode, WithSchemaPlugin, and, between, bigint, bigserial, binOp, boolean, bytea, cast, char, col, colAs, count, createDeleteNode, createInsertNode, createSelectNode, createUpdateNode, date, defineTable, deleteFrom, concat as docConcat, empty as docEmpty, group as docGroup, join as docJoin, line as docLine, nest as docNest, render as docRender, text as docText, textLine as docTextLine, doublePrecision, enumType, eq, exists, fn, formatParam, formatSQL, gt, gte, inList, insert, integer, isNull, json, jsonb, like, lit, lt, lte, mysqlDialect, neq, not, numeric, or, param, pgDialect, quoteIdentifier, quoteTableRef, raw, real, resetParams, select, serial, smallint, sqlFn, sqliteDialect, star, subquery, sumak, tableRef, text$1 as text, time, timestamp, timestamptz, unaryOp, update, uuid, val, varchar, visitNode };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import{i as e,n as t,r as n,t as r}from"./_chunks/base.mjs";import{n as i,t as a}from"./_chunks/pg.mjs";import{i as o,n as s,r as ee,t as te}from"./_chunks/errors.mjs";import{n as ne,t as re}from"./_chunks/mysql.mjs";import{n as ie,t as ae}from"./_chunks/sqlite.mjs";import{C as oe,S as se,_ as ce,a as le,b as ue,c as de,d as fe,f as c,g as pe,h as me,i as he,l as ge,m as _e,n as ve,o as ye,p as be,r as xe,s as Se,t as Ce,u as we,v as Te,x as Ee,y as De}from"./_chunks/schema.mjs";function l(e,t,n){return Object.freeze({type:`table_ref`,name:e,alias:t,schema:n})}function u(){return Object.freeze({type:`select`,distinct:!1,columns:Object.freeze([]),joins:Object.freeze([]),groupBy:Object.freeze([]),orderBy:Object.freeze([]),ctes:Object.freeze([]),forUpdate:!1})}function d(e){return Object.freeze({type:`insert`,table:e,columns:Object.freeze([]),values:Object.freeze([]),returning:Object.freeze([]),ctes:Object.freeze([])})}function f(e){return Object.freeze({type:`update`,table:e,set:Object.freeze([]),returning:Object.freeze([]),ctes:Object.freeze([])})}function p(e){return Object.freeze({type:`delete`,table:e,returning:Object.freeze([]),ctes:Object.freeze([])})}function m(e,t){return{type:`column_ref`,column:e,table:t}}function Oe(e,t,n){return{type:`column_ref`,column:e,table:n,alias:t}}function h(e){return{type:`literal`,value:e}}function g(e){return{type:`star`,table:e}}function _(e,t){return{type:`param`,index:e,value:t}}function ke(e,t=[]){return{type:`raw`,sql:e,params:t}}function Ae(e,t){return{type:`subquery`,query:e,alias:t}}function v(e,t,n){return{type:`function_call`,name:e,args:t,alias:n}}function y(e,t,n){return{type:`binary_op`,op:e,left:t,right:n}}function b(e,t,n=`prefix`){return{type:`unary_op`,op:e,operand:t,position:n}}function x(e,t){return y(`AND`,e,t)}function S(e,t){return y(`OR`,e,t)}function C(e,t){return y(`=`,e,t)}function je(e,t){return y(`!=`,e,t)}function Me(e,t){return y(`>`,e,t)}function Ne(e,t){return y(`>=`,e,t)}function Pe(e,t){return y(`<`,e,t)}function Fe(e,t){return y(`<=`,e,t)}function Ie(e,t){return y(`LIKE`,e,t)}function Le(e,t,n,r=!1){return{type:`between`,expr:e,low:t,high:n,negated:r}}function Re(e,t,n=!1){return{type:`in`,expr:e,values:t,negated:n}}function w(e,t=!1){return{type:`is_null`,expr:e,negated:t}}function ze(e,t){return{type:`cast`,expr:e,dataType:t}}function Be(e,t=!1){return{type:`exists`,query:e,negated:t}}function Ve(e){return b(`NOT`,e)}var He=class{transform(e){switch(e.type){case`select`:return this.transformSelect(e);case`insert`:return this.transformInsert(e);case`update`:return this.transformUpdate(e);case`delete`:return this.transformDelete(e);default:return this.transformExpression(e)}}transformSelect(e){return{...e,columns:e.columns.map(e=>this.transformExpression(e)),where:e.where?this.transformExpression(e.where):void 0,having:e.having?this.transformExpression(e.having):void 0,joins:e.joins.map(e=>({...e,on:e.on?this.transformExpression(e.on):void 0})),orderBy:e.orderBy.map(e=>({...e,expr:this.transformExpression(e.expr)}))}}transformInsert(e){return{...e,values:e.values.map(e=>e.map(e=>this.transformExpression(e)))}}transformUpdate(e){return{...e,set:e.set.map(e=>({...e,value:this.transformExpression(e.value)})),where:e.where?this.transformExpression(e.where):void 0}}transformDelete(e){return{...e,where:e.where?this.transformExpression(e.where):void 0}}transformExpression(e){switch(e.type){case`binary_op`:return{...e,left:this.transformExpression(e.left),right:this.transformExpression(e.right)};case`unary_op`:return{...e,operand:this.transformExpression(e.operand)};case`function_call`:return{...e,args:e.args.map(e=>this.transformExpression(e))};case`between`:return{...e,expr:this.transformExpression(e.expr),low:this.transformExpression(e.low),high:this.transformExpression(e.high)};case`in`:return{...e,expr:this.transformExpression(e.expr),values:Array.isArray(e.values)?e.values.map(e=>this.transformExpression(e)):e.values};case`is_null`:return{...e,expr:this.transformExpression(e.expr)};case`cast`:return{...e,expr:this.transformExpression(e.expr)};case`json_access`:return{...e,expr:this.transformExpression(e.expr)};case`array_expr`:return{...e,elements:e.elements.map(e=>this.transformExpression(e))};case`window_function`:return{...e,fn:this.transformExpression(e.fn),partitionBy:e.partitionBy.map(e=>this.transformExpression(e)),orderBy:e.orderBy.map(e=>({...e,expr:this.transformExpression(e.expr)}))};default:return e}}};function Ue(e,t){switch(e.type){case`select`:return t.visitSelect(e);case`insert`:return t.visitInsert(e);case`update`:return t.visitUpdate(e);case`delete`:return t.visitDelete(e);default:return t.visitExpression(e)}}var T=class e{node;constructor(e){this.node=e??u()}columns(...t){let n=t.map(e=>typeof e==`string`?m(e):e);return new e({...this.node,columns:[...this.node.columns,...n]})}allColumns(){return new e({...this.node,columns:[...this.node.columns,g()]})}distinct(){return new e({...this.node,distinct:!0})}from(t,n){if(typeof t==`string`){let r={type:`table_ref`,name:t,alias:n};return new e({...this.node,from:r})}return n&&t.type!==`subquery`?new e({...this.node,from:{...t,alias:n}}):new e({...this.node,from:t})}where(t){return new e({...this.node,where:t})}join(t,n,r,i){let a={type:`join`,joinType:t,table:typeof n==`string`?{type:`table_ref`,name:n,alias:i}:n,on:r};return new e({...this.node,joins:[...this.node.joins,a]})}innerJoin(e,t,n){return this.join(`INNER`,e,t,n)}leftJoin(e,t,n){return this.join(`LEFT`,e,t,n)}rightJoin(e,t,n){return this.join(`RIGHT`,e,t,n)}groupBy(...t){let n=t.map(e=>typeof e==`string`?m(e):e);return new e({...this.node,groupBy:[...this.node.groupBy,...n]})}having(t){return new e({...this.node,having:t})}orderBy(t,n=`ASC`,r){let i={expr:typeof t==`string`?m(t):t,direction:n,nulls:r};return new e({...this.node,orderBy:[...this.node.orderBy,i]})}limit(t){return new e({...this.node,limit:t})}offset(t){return new e({...this.node,offset:t})}forUpdate(){return new e({...this.node,forUpdate:!0})}with(t,n,r=!1){let i={name:t,query:n,recursive:r};return new e({...this.node,ctes:[...this.node.ctes,i]})}union(e){return this.setOp(`UNION`,e)}unionAll(e){return this.setOp(`UNION ALL`,e)}intersect(e){return this.setOp(`INTERSECT`,e)}except(e){return this.setOp(`EXCEPT`,e)}setOp(t,n){return new e({...this.node,setOp:{op:t,query:n}})}build(){return{...this.node}}};function We(...e){let t=new T;return e.length>0?t.columns(...e):t.allColumns()}var E=class e{node;paramIndex;constructor(e,t=0){this.node=e??d({name:``}),this.paramIndex=t}into(t){let n=typeof t==`string`?{type:`table_ref`,name:t}:t;return new e({...this.node,table:n},this.paramIndex)}columns(...t){return new e({...this.node,columns:[...this.node.columns,...t]},this.paramIndex)}values(...t){let n=this.paramIndex,r=t.map(e=>{let t=_(n,e);return n++,t});return new e({...this.node,values:[...this.node.values,r]},n)}returning(...t){return new e({...this.node,returning:[...this.node.returning,...t]},this.paramIndex)}onConflictDoNothing(...t){let n={columns:t,action:`nothing`};return new e({...this.node,onConflict:n},this.paramIndex)}onConflictDoUpdate(t,n,r){let i={columns:t,action:{set:n},where:r};return new e({...this.node,onConflict:i},this.paramIndex)}with(t,n,r=!1){let i={name:t,query:n,recursive:r};return new e({...this.node,ctes:[...this.node.ctes,i]},this.paramIndex)}build(){return{...this.node}}};function Ge(e){return new E().into(e)}var D=class e{node;constructor(e){this.node=e??f({name:``})}table(t){let n=typeof t==`string`?{type:`table_ref`,name:t}:t;return new e({...this.node,table:n})}set(t,n){return new e({...this.node,set:[...this.node.set,{column:t,value:n}]})}where(t){return new e({...this.node,where:t})}from(t){let n=typeof t==`string`?{type:`table_ref`,name:t}:t;return new e({...this.node,from:n})}returning(...t){return new e({...this.node,returning:[...this.node.returning,...t]})}with(t,n,r=!1){let i={name:t,query:n,recursive:r};return new e({...this.node,ctes:[...this.node.ctes,i]})}build(){return{...this.node}}};function Ke(e){return new D().table(e)}var O=class e{node;constructor(e){this.node=e??p({name:``})}from(t){let n=typeof t==`string`?{type:`table_ref`,name:t}:t;return new e({...this.node,table:n})}where(t){return new e({...this.node,where:t})}returning(...t){return new e({...this.node,returning:[...this.node.returning,...t]})}with(t,n,r=!1){let i={name:t,query:n,recursive:r};return new e({...this.node,ctes:[...this.node.ctes,i]})}build(){return{...this.node}}};function qe(e){return new O().from(e)}const k=new Set(`SELECT.FROM.WHERE.AND.OR.JOIN.INNER JOIN.LEFT JOIN.RIGHT JOIN.FULL JOIN.CROSS JOIN.ON.GROUP BY.HAVING.ORDER BY.LIMIT.OFFSET.INSERT INTO.VALUES.UPDATE.SET.DELETE FROM.RETURNING.WITH.WITH RECURSIVE.UNION.UNION ALL.INTERSECT.EXCEPT.FOR UPDATE.ON CONFLICT.DO NOTHING.DO UPDATE SET`.split(`.`));function Je(e,t={}){let n=t.indent??` `,r=[],i=0,a=Ye(e),o=0;for(;o<a.length;){let e=a[o];if(e===`(`){i++,A(r,e),o++;continue}if(e===`)`){i=Math.max(0,i-1),A(r,e),o++;continue}let t=o+1<a.length?`${e} ${a[o+1]}`:``,s=o+2<a.length?`${e} ${a[o+1]} ${a[o+2]}`:``;if(k.has(s.toUpperCase())){r.push(`${n.repeat(i)}${s}`),o+=3;continue}if(k.has(t.toUpperCase())){r.push(`${n.repeat(i)}${t}`),o+=2;continue}if(k.has(e.toUpperCase())){r.push(`${n.repeat(i)}${e}`),o++;continue}A(r,e),o++}return r.join(`
|
|
2
2
|
`)}function A(e,t){e.length===0?e.push(t):e[e.length-1]+=` ${t}`}function Ye(e){let t=[],n=``;for(let r=0;r<e.length;r++){let i=e[r];if(i===`'`||i===`"`){for(n+=i,r++;r<e.length&&e[r]!==i;){if(e[r]===i&&r+1<e.length&&e[r+1]===i){n+=i+i,r+=2;continue}n+=e[r],r++}r<e.length&&(n+=e[r]);continue}if(i===`(`||i===`)`){n.trim()&&(t.push(n.trim()),n=``),t.push(i);continue}if(i===`,`||i===`;`){n.trim()&&(t.push(n.trim()),n=``),t.push(i);continue}if(/\s/.test(i)){n.trim()&&(t.push(n.trim()),n=``);continue}n+=i}return n.trim()&&t.push(n.trim()),t}function j(){return{tag:`empty`}}function M(e){return{tag:`text`,text:e}}function N(){return{tag:`line`}}function Xe(e,t){return{tag:`nest`,indent:e,doc:t}}function Ze(e){return{tag:`group`,doc:e}}function P(...e){let t=e.filter(e=>e.tag!==`empty`);return t.length===0?j():t.length===1?t[0]:{tag:`concat`,docs:t}}function Qe(e,t){if(t.length===0)return j();let n=[];for(let r=0;r<t.length;r++)r>0&&n.push(e),n.push(t[r]);return P(...n)}function $e(e){return P(M(e),N())}function et(e,t=80){let n=``,r=0,i=[[0,`break`,e]];for(;i.length>0;){let[e,a,o]=i.pop();switch(o.tag){case`empty`:break;case`text`:n+=o.text,r+=o.text.length;break;case`line`:a===`flat`?(n+=` `,r+=1):(n+=`
|
|
3
|
-
`+` `.repeat(e),r=e);break;case`nest`:i.push([e+o.indent,a,o.doc]);break;case`group`:if(a===`flat`)i.push([e,`flat`,o.doc]);else{let n=tt(o.doc);n!==null&&r+n<=t?i.push([e,`flat`,o.doc]):i.push([e,`break`,o.doc])}break;case`concat`:for(let t=o.docs.length-1;t>=0;t--)i.push([e,a,o.docs[t]]);break}}return n}function tt(e){let t=0,n=[e];for(;n.length>0;){let e=n.pop();switch(e.tag){case`empty`:break;case`text`:t+=e.text.length;break;case`line`:t+=1;break;case`nest`:n.push(e.doc);break;case`group`:n.push(e.doc);break;case`concat`:for(let t=e.docs.length-1;t>=0;t--)n.push(e.docs[t]);break}}return t}var F=class{_node;constructor(e,t){this._node=m(e,t)}eq(e){return B(z(`=`,this._node,R(e)))}neq(e){return B(z(`!=`,this._node,R(e)))}gt(e){return B(z(`>`,this._node,R(e)))}gte(e){return B(z(`>=`,this._node,R(e)))}lt(e){return B(z(`<`,this._node,R(e)))}lte(e){return B(z(`<=`,this._node,R(e)))}like(e){return B(z(`LIKE`,this._node,h(e)))}in(e){return B({type:`in`,expr:this._node,values:e.map(e=>R(e)),negated:!1})}notIn(e){return B({type:`in`,expr:this._node,values:e.map(e=>R(e)),negated:!0})}isNull(){return B({type:`is_null`,expr:this._node,negated:!1})}isNotNull(){return B({type:`is_null`,expr:this._node,negated:!0})}between(e,t){return B({type:`between`,expr:this._node,low:R(e),high:R(t),negated:!1})}eqCol(e){return B(z(`=`,this._node,e._node))}toExpr(){return B(this._node)}};let I=0;function L(){I=0}function R(e){return _(I++,e)}function z(e,t,n){return{type:`binary_op`,op:e,left:t,right:n}}function B(e){return{node:e}}function V(e){return new Proxy({},{get(e,t){return new F(t,void 0)}})}function nt(e,t){return B(x(e.node,t.node))}function rt(e,t){return B(S(e.node,t.node))}function it(e){return B(h(e))}function at(e,...t){return B(v(e,t.map(e=>e.node)))}function ot(){return B(v(`COUNT`,[g()]))}function H(e){return e.node}var U=class e{_builder;_table;constructor(e,t){this._builder=e,this._table=t??``}select(...t){return new e(this._builder.columns(...t),this._table)}selectAll(){return new e(this._builder.allColumns(),this._table)}selectExpr(t,n){let r=H(t),i=r.type===`column_ref`||r.type===`function_call`?{...r,alias:n}:{type:`raw`,sql:``,params:[]};return new e(this._builder.columns(i),this._table)}distinct(){return new e(this._builder.distinct(),this._table)}where(t){if(typeof t==`function`){L();let n=t(V(this._table));return new e(this._builder.where(H(n)),this._table)}return new e(this._builder.where(H(t)),this._table)}innerJoin(t,n){let r=W(n,this._table,t);return new e(this._builder.innerJoin(t,H(r)),this._table)}leftJoin(t,n){let r=W(n,this._table,t);return new e(this._builder.leftJoin(t,H(r)),this._table)}rightJoin(t,n){let r=W(n,this._table,t);return new e(this._builder.rightJoin(t,H(r)),this._table)}groupBy(...t){return new e(this._builder.groupBy(...t),this._table)}having(t){if(typeof t==`function`){L();let n=t(V(this._table));return new e(this._builder.having(H(n)),this._table)}return new e(this._builder.having(H(t)),this._table)}orderBy(t,n=`ASC`,r){return new e(this._builder.orderBy(t,n,r),this._table)}limit(t){return new e(this._builder.limit({type:`literal`,value:t}),this._table)}offset(t){return new e(this._builder.offset({type:`literal`,value:t}),this._table)}forUpdate(){return new e(this._builder.forUpdate(),this._table)}with(t,n,r=!1){return new e(this._builder.with(t,n,r),this._table)}union(t){return new e(this._builder.union(t.build()),this._table)}unionAll(t){return new e(this._builder.unionAll(t.build()),this._table)}build(){return this._builder.build()}compile(e){return e.print(this.build())}};function st(e,t){return new Proxy({},{get(e,t){return new Proxy({},{get(e,n){return new F(n,t)}})}})}function W(e,t,n){return typeof e==`function`?e(st(t,n)):e}var G=class e{_builder;_paramIdx;constructor(e,t=0){this._builder=new E().into(e),this._paramIdx=t}_withBuilder(t,n){let r=new e(``);return r._builder=t,r._paramIdx=n,r}values(e){let t=Object.entries(e),n=t.map(([e])=>e),r=t.map(([e,t])=>{let n=_(this._paramIdx,t);return this._paramIdx++,n}),i=this._builder;return i.build().columns.length===0&&(i=i.columns(...n)),i=new E({...i.build(),values:[...i.build().values,r]},this._paramIdx),this._withBuilder(i,this._paramIdx)}returning(...e){let t=e.map(e=>({type:`column_ref`,column:e}));return new K(new E({...this._builder.build(),returning:t},this._paramIdx))}returningAll(){return new K(new E({...this._builder.build(),returning:[g()]},this._paramIdx))}onConflictDoNothing(...e){return this._withBuilder(this._builder.onConflictDoNothing(...e),this._paramIdx)}onConflictDoUpdate(e,t){return this._withBuilder(this._builder.onConflictDoUpdate(e,t.map(e=>({column:e.column,value:H(e.value)}))),this._paramIdx)}build(){return this._builder.build()}compile(e){return e.print(this.build())}},K=class{_builder;constructor(e){this._builder=e}build(){return this._builder.build()}compile(e){return e.print(this.build())}},q=class e{_builder;_paramIdx;constructor(e,t=0){this._builder=new D().table(e),this._paramIdx=t}_with(t,n){let r=new e(``);return r._builder=t,r._paramIdx=n,r}set(e){let t=this._builder,n=this._paramIdx;for(let[r,i]of Object.entries(e))i!==void 0&&(t=t.set(r,_(n,i)),n++);return this._with(t,n)}setExpr(e,t){return this._with(this._builder.set(e,H(t)),this._paramIdx)}where(e){if(typeof e==`function`){L();let t=e(V(this._table));return this._with(this._builder.where(H(t)),this._paramIdx)}return this._with(this._builder.where(H(e)),this._paramIdx)}get _table(){return this._builder.build().table.name}returning(...e){let t=e.map(e=>({type:`column_ref`,column:e}));return new J(new D({...this._builder.build(),returning:t}))}returningAll(){return new J(new D({...this._builder.build(),returning:[g()]}))}build(){return this._builder.build()}compile(e){return e.print(this.build())}},J=class{_builder;constructor(e){this._builder=e}build(){return this._builder.build()}compile(e){return e.print(this.build())}},Y=class e{_builder;constructor(e){this._builder=new O().from(e)}_with(t){let n=new e(``);return n._builder=t,n}where(e){if(typeof e==`function`){L();let t=this._builder.build().table.name,n=e(V(t));return this._with(this._builder.where(H(n)))}return this._with(this._builder.where(H(e)))}returning(...e){let t=e.map(e=>({type:`column_ref`,column:e}));return new X(new O({...this._builder.build(),returning:t}))}returningAll(){return new X(new O({...this._builder.build(),returning:[g()]}))}build(){return this._builder.build()}compile(e){return e.print(this.build())}},X=class{_builder;constructor(e){this._builder=e}build(){return this._builder.build()}compile(e){return e.print(this.build())}},Z=class{plugins;constructor(e){this.plugins=Object.freeze([...e])}transformNode(e){let t=e;for(let e of this.plugins)e.transformNode&&(t=e.transformNode(t));return t}transformQuery(e){let t=e;for(let e of this.plugins)e.transformQuery&&(t=e.transformQuery(t));return t}transformResult(e){let t=e;for(let e of this.plugins)e.transformResult&&(t=e.transformResult(t));return t}},Q=class{_hooks=new Map;hook(e,t){return this._hooks.has(e)||this._hooks.set(e,[]),this._hooks.get(e).push(t),()=>{let n=this._hooks.get(e);if(n){let e=n.indexOf(t);e!==-1&&n.splice(e,1)}}}callHook(e,...t){let n=this._hooks.get(e);if(!n||n.length===0)return;let r;for(let i of n){let n=i(...t);n!==void 0&&(r=n,e!==`result:transform`&&t[0]&&typeof t[0]==`object`&&`node`in t[0]&&(t[0].node=n))}return r}hasHook(e){let t=this._hooks.get(e);return t!==void 0&&t.length>0}removeHook(e){this._hooks.delete(e)}removeAllHooks(){this._hooks.clear()}};function ct(e){return new $(e.dialect,e.plugins??[])}var $=class{_dialect;_plugins;_hooks;constructor(e,t=[]){this._dialect=e,this._plugins=new Z(t),this._hooks=new Q}hook(e,t){return this._hooks.hook(e,t)}selectFrom(e,t){return new U(new T().from(e,t),e)}insertInto(e){return new G(e)}update(e){return new q(e)}deleteFrom(e){return new Y(e)}compile(e){let t=this._plugins.transformNode(e),n=this._extractTableName(t);switch(t.type){case`select`:{let e=this._hooks.callHook(`select:before`,{node:t,table:n});e&&(t=e);break}case`insert`:{let e=this._hooks.callHook(`insert:before`,{node:t,table:n});e&&(t=e);break}case`update`:{let e=this._hooks.callHook(`update:before`,{node:t,table:n});e&&(t=e);break}case`delete`:{let e=this._hooks.callHook(`delete:before`,{node:t,table:n});e&&(t=e);break}}let r=this._hooks.callHook(`query:before`,{node:t,table:n});r&&(t=r);let i=this._dialect.createPrinter().print(t);i=this._plugins.transformQuery(i);let a=this._hooks.callHook(`query:after`,{node:t,table:n,query:i});return a&&(i=a),i}transformResult(e){let t=this._plugins.transformResult(e),n=this._hooks.callHook(`result:transform`,t);return n&&(t=n),t}printer(){return this._dialect.createPrinter()}_extractTableName(e){switch(e.type){case`select`:return e.from?.type===`table_ref`?e.from.name:void 0;case`insert`:return e.table.name;case`update`:return e.table.name;case`delete`:return e.table.name;default:return}}},lt=class{name=`with-schema`;schema;constructor(e){this.schema=e}transformNode(e){switch(e.type){case`select`:return this.transformSelect(e);case`insert`:return this.transformInsert(e);case`update`:return this.transformUpdate(e);case`delete`:return this.transformDelete(e);default:return e}}addSchema(e){return e.schema?e:{...e,schema:this.schema}}transformSelect(e){return{...e,from:e.from?e.from.type===`table_ref`?this.addSchema(e.from):e.from:void 0,joins:e.joins.map(e=>({...e,table:e.table.type===`table_ref`?this.addSchema(e.table):e.table}))}}transformInsert(e){return{...e,table:this.addSchema(e.table)}}transformUpdate(e){return{...e,table:this.addSchema(e.table),from:e.from?this.addSchema(e.from):void 0}}transformDelete(e){return{...e,table:this.addSchema(e.table)}}},ut=class{name=`soft-delete`;tables;column;constructor(e){this.tables=new Set(e.tables),this.column=e.column??`deleted_at`}transformNode(e){switch(e.type){case`select`:return this.transformSelect(e);case`update`:return this.transformUpdate(e);case`delete`:return this.transformDelete(e);default:return e}}isTargetTable(e){return this.tables.has(e)}softDeleteCondition(){return w(m(this.column))}addCondition(e){let t=this.softDeleteCondition();return e?x(e,t):t}transformSelect(e){return!e.from||e.from.type!==`table_ref`||!this.isTargetTable(e.from.name)?e:{...e,where:this.addCondition(e.where)}}transformUpdate(e){return this.isTargetTable(e.table.name)?{...e,where:this.addCondition(e.where)}:e}transformDelete(e){return this.isTargetTable(e.table.name)?{...e,where:this.addCondition(e.where)}:e}},dt=class{name=`camel-case`;transformResult(e){return e.map(e=>{let t={};for(let n of Object.keys(e))t[ft(n)]=e[n];return t})}};function ft(e){return e.replace(/_([a-z])/g,(e,t)=>t.toUpperCase())}export{He as ASTTransformer,r as BasePrinter,dt as CamelCasePlugin,F as Col,ve as ColumnBuilder,O as DeleteBuilder,te as EmptyQueryError,Q as Hookable,E as InsertBuilder,s as InvalidExpressionError,ne as MysqlPrinter,$ as Pamuk,ee as PamukError,i as PgPrinter,Z as PluginManager,T as SelectBuilder,ut as SoftDeletePlugin,ie as SqlitePrinter,Y as TypedDeleteBuilder,X as TypedDeleteReturningBuilder,G as TypedInsertBuilder,K as TypedInsertReturningBuilder,U as TypedSelectBuilder,q as TypedUpdateBuilder,J as TypedUpdateReturningBuilder,o as UnsupportedDialectFeatureError,D as UpdateBuilder,lt as WithSchemaPlugin,nt as and,Le as between,xe as bigint,he as bigserial,y as binOp,le as boolean,ye as bytea,ze as cast,Se as char,m as col,Oe as colAs,ot as count,p as createDeleteNode,d as createInsertNode,u as createSelectNode,f as createUpdateNode,de as date,Ce as defineTable,qe as deleteFrom,P as docConcat,j as docEmpty,Ze as docGroup,Qe as docJoin,N as docLine,Xe as docNest,et as docRender,M as docText,$e as docTextLine,ge as doublePrecision,we as enumType,C as eq,Be as exists,v as fn,t as formatParam,Je as formatSQL,Me as gt,Ne as gte,Re as inList,Ge as insert,fe as integer,w as isNull,c as json,be as jsonb,Ie as like,h as lit,Pe as lt,Fe as lte,re as mysqlDialect,je as neq,Ve as not,_e as numeric,rt as or,ct as pamuk,_ as param,a as pgDialect,n as quoteIdentifier,e as quoteTableRef,ke as raw,me as real,L as resetParams,We as select,pe as serial,ce as smallint,at as sqlFn,ae as sqliteDialect,g as star,Ae as subquery,l as tableRef,Te as text,De as time,ue as timestamp,Ee as timestamptz,b as unaryOp,Ke as update,se as uuid,it as val,oe as varchar,Ue as visitNode};
|
|
3
|
+
`+` `.repeat(e),r=e);break;case`nest`:i.push([e+o.indent,a,o.doc]);break;case`group`:if(a===`flat`)i.push([e,`flat`,o.doc]);else{let n=tt(o.doc);n!==null&&r+n<=t?i.push([e,`flat`,o.doc]):i.push([e,`break`,o.doc])}break;case`concat`:for(let t=o.docs.length-1;t>=0;t--)i.push([e,a,o.docs[t]]);break}}return n}function tt(e){let t=0,n=[e];for(;n.length>0;){let e=n.pop();switch(e.tag){case`empty`:break;case`text`:t+=e.text.length;break;case`line`:t+=1;break;case`nest`:n.push(e.doc);break;case`group`:n.push(e.doc);break;case`concat`:for(let t=e.docs.length-1;t>=0;t--)n.push(e.docs[t]);break}}return t}var F=class{_node;constructor(e,t){this._node=m(e,t)}eq(e){return B(z(`=`,this._node,R(e)))}neq(e){return B(z(`!=`,this._node,R(e)))}gt(e){return B(z(`>`,this._node,R(e)))}gte(e){return B(z(`>=`,this._node,R(e)))}lt(e){return B(z(`<`,this._node,R(e)))}lte(e){return B(z(`<=`,this._node,R(e)))}like(e){return B(z(`LIKE`,this._node,h(e)))}in(e){return B({type:`in`,expr:this._node,values:e.map(e=>R(e)),negated:!1})}notIn(e){return B({type:`in`,expr:this._node,values:e.map(e=>R(e)),negated:!0})}isNull(){return B({type:`is_null`,expr:this._node,negated:!1})}isNotNull(){return B({type:`is_null`,expr:this._node,negated:!0})}between(e,t){return B({type:`between`,expr:this._node,low:R(e),high:R(t),negated:!1})}eqCol(e){return B(z(`=`,this._node,e._node))}toExpr(){return B(this._node)}};let I=0;function L(){I=0}function R(e){return _(I++,e)}function z(e,t,n){return{type:`binary_op`,op:e,left:t,right:n}}function B(e){return{node:e}}function V(e){return new Proxy({},{get(e,t){return new F(t,void 0)}})}function nt(e,t){return B(x(e.node,t.node))}function rt(e,t){return B(S(e.node,t.node))}function it(e){return B(h(e))}function at(e,...t){return B(v(e,t.map(e=>e.node)))}function ot(){return B(v(`COUNT`,[g()]))}function H(e){return e.node}var U=class e{_builder;_table;constructor(e,t){this._builder=e,this._table=t??``}select(...t){return new e(this._builder.columns(...t),this._table)}selectAll(){return new e(this._builder.allColumns(),this._table)}selectExpr(t,n){let r=H(t),i=r.type===`column_ref`||r.type===`function_call`?{...r,alias:n}:{type:`raw`,sql:``,params:[]};return new e(this._builder.columns(i),this._table)}distinct(){return new e(this._builder.distinct(),this._table)}where(t){if(typeof t==`function`){L();let n=t(V(this._table));return new e(this._builder.where(H(n)),this._table)}return new e(this._builder.where(H(t)),this._table)}innerJoin(t,n){let r=W(n,this._table,t);return new e(this._builder.innerJoin(t,H(r)),this._table)}leftJoin(t,n){let r=W(n,this._table,t);return new e(this._builder.leftJoin(t,H(r)),this._table)}rightJoin(t,n){let r=W(n,this._table,t);return new e(this._builder.rightJoin(t,H(r)),this._table)}groupBy(...t){return new e(this._builder.groupBy(...t),this._table)}having(t){if(typeof t==`function`){L();let n=t(V(this._table));return new e(this._builder.having(H(n)),this._table)}return new e(this._builder.having(H(t)),this._table)}orderBy(t,n=`ASC`,r){return new e(this._builder.orderBy(t,n,r),this._table)}limit(t){return new e(this._builder.limit({type:`literal`,value:t}),this._table)}offset(t){return new e(this._builder.offset({type:`literal`,value:t}),this._table)}forUpdate(){return new e(this._builder.forUpdate(),this._table)}with(t,n,r=!1){return new e(this._builder.with(t,n,r),this._table)}union(t){return new e(this._builder.union(t.build()),this._table)}unionAll(t){return new e(this._builder.unionAll(t.build()),this._table)}build(){return this._builder.build()}compile(e){return e.print(this.build())}};function st(e,t){return new Proxy({},{get(e,t){return new Proxy({},{get(e,n){return new F(n,t)}})}})}function W(e,t,n){return typeof e==`function`?e(st(t,n)):e}var G=class e{_builder;_paramIdx;constructor(e,t=0){this._builder=new E().into(e),this._paramIdx=t}_withBuilder(t,n){let r=new e(``);return r._builder=t,r._paramIdx=n,r}values(e){let t=Object.entries(e),n=t.map(([e])=>e),r=t.map(([e,t])=>{let n=_(this._paramIdx,t);return this._paramIdx++,n}),i=this._builder;return i.build().columns.length===0&&(i=i.columns(...n)),i=new E({...i.build(),values:[...i.build().values,r]},this._paramIdx),this._withBuilder(i,this._paramIdx)}returning(...e){let t=e.map(e=>({type:`column_ref`,column:e}));return new K(new E({...this._builder.build(),returning:t},this._paramIdx))}returningAll(){return new K(new E({...this._builder.build(),returning:[g()]},this._paramIdx))}onConflictDoNothing(...e){return this._withBuilder(this._builder.onConflictDoNothing(...e),this._paramIdx)}onConflictDoUpdate(e,t){return this._withBuilder(this._builder.onConflictDoUpdate(e,t.map(e=>({column:e.column,value:H(e.value)}))),this._paramIdx)}build(){return this._builder.build()}compile(e){return e.print(this.build())}},K=class{_builder;constructor(e){this._builder=e}build(){return this._builder.build()}compile(e){return e.print(this.build())}},q=class e{_builder;_paramIdx;constructor(e,t=0){this._builder=new D().table(e),this._paramIdx=t}_with(t,n){let r=new e(``);return r._builder=t,r._paramIdx=n,r}set(e){let t=this._builder,n=this._paramIdx;for(let[r,i]of Object.entries(e))i!==void 0&&(t=t.set(r,_(n,i)),n++);return this._with(t,n)}setExpr(e,t){return this._with(this._builder.set(e,H(t)),this._paramIdx)}where(e){if(typeof e==`function`){L();let t=e(V(this._table));return this._with(this._builder.where(H(t)),this._paramIdx)}return this._with(this._builder.where(H(e)),this._paramIdx)}get _table(){return this._builder.build().table.name}returning(...e){let t=e.map(e=>({type:`column_ref`,column:e}));return new J(new D({...this._builder.build(),returning:t}))}returningAll(){return new J(new D({...this._builder.build(),returning:[g()]}))}build(){return this._builder.build()}compile(e){return e.print(this.build())}},J=class{_builder;constructor(e){this._builder=e}build(){return this._builder.build()}compile(e){return e.print(this.build())}},Y=class e{_builder;constructor(e){this._builder=new O().from(e)}_with(t){let n=new e(``);return n._builder=t,n}where(e){if(typeof e==`function`){L();let t=this._builder.build().table.name,n=e(V(t));return this._with(this._builder.where(H(n)))}return this._with(this._builder.where(H(e)))}returning(...e){let t=e.map(e=>({type:`column_ref`,column:e}));return new X(new O({...this._builder.build(),returning:t}))}returningAll(){return new X(new O({...this._builder.build(),returning:[g()]}))}build(){return this._builder.build()}compile(e){return e.print(this.build())}},X=class{_builder;constructor(e){this._builder=e}build(){return this._builder.build()}compile(e){return e.print(this.build())}},Z=class{plugins;constructor(e){this.plugins=Object.freeze([...e])}transformNode(e){let t=e;for(let e of this.plugins)e.transformNode&&(t=e.transformNode(t));return t}transformQuery(e){let t=e;for(let e of this.plugins)e.transformQuery&&(t=e.transformQuery(t));return t}transformResult(e){let t=e;for(let e of this.plugins)e.transformResult&&(t=e.transformResult(t));return t}},Q=class{_hooks=new Map;hook(e,t){return this._hooks.has(e)||this._hooks.set(e,[]),this._hooks.get(e).push(t),()=>{let n=this._hooks.get(e);if(n){let e=n.indexOf(t);e!==-1&&n.splice(e,1)}}}callHook(e,...t){let n=this._hooks.get(e);if(!n||n.length===0)return;let r;for(let i of n){let n=i(...t);n!==void 0&&(r=n,e!==`result:transform`&&t[0]&&typeof t[0]==`object`&&`node`in t[0]&&(t[0].node=n))}return r}hasHook(e){let t=this._hooks.get(e);return t!==void 0&&t.length>0}removeHook(e){this._hooks.delete(e)}removeAllHooks(){this._hooks.clear()}};function ct(e){return new $(e.dialect,e.plugins??[])}var $=class{_dialect;_plugins;_hooks;constructor(e,t=[]){this._dialect=e,this._plugins=new Z(t),this._hooks=new Q}hook(e,t){return this._hooks.hook(e,t)}selectFrom(e,t){return new U(new T().from(e,t),e)}insertInto(e){return new G(e)}update(e){return new q(e)}deleteFrom(e){return new Y(e)}compile(e){let t=this._plugins.transformNode(e),n=this._extractTableName(t);switch(t.type){case`select`:{let e=this._hooks.callHook(`select:before`,{node:t,table:n});e&&(t=e);break}case`insert`:{let e=this._hooks.callHook(`insert:before`,{node:t,table:n});e&&(t=e);break}case`update`:{let e=this._hooks.callHook(`update:before`,{node:t,table:n});e&&(t=e);break}case`delete`:{let e=this._hooks.callHook(`delete:before`,{node:t,table:n});e&&(t=e);break}}let r=this._hooks.callHook(`query:before`,{node:t,table:n});r&&(t=r);let i=this._dialect.createPrinter().print(t);i=this._plugins.transformQuery(i);let a=this._hooks.callHook(`query:after`,{node:t,table:n,query:i});return a&&(i=a),i}transformResult(e){let t=this._plugins.transformResult(e),n=this._hooks.callHook(`result:transform`,t);return n&&(t=n),t}printer(){return this._dialect.createPrinter()}_extractTableName(e){switch(e.type){case`select`:return e.from?.type===`table_ref`?e.from.name:void 0;case`insert`:return e.table.name;case`update`:return e.table.name;case`delete`:return e.table.name;default:return}}},lt=class{name=`with-schema`;schema;constructor(e){this.schema=e}transformNode(e){switch(e.type){case`select`:return this.transformSelect(e);case`insert`:return this.transformInsert(e);case`update`:return this.transformUpdate(e);case`delete`:return this.transformDelete(e);default:return e}}addSchema(e){return e.schema?e:{...e,schema:this.schema}}transformSelect(e){return{...e,from:e.from?e.from.type===`table_ref`?this.addSchema(e.from):e.from:void 0,joins:e.joins.map(e=>({...e,table:e.table.type===`table_ref`?this.addSchema(e.table):e.table}))}}transformInsert(e){return{...e,table:this.addSchema(e.table)}}transformUpdate(e){return{...e,table:this.addSchema(e.table),from:e.from?this.addSchema(e.from):void 0}}transformDelete(e){return{...e,table:this.addSchema(e.table)}}},ut=class{name=`soft-delete`;tables;column;constructor(e){this.tables=new Set(e.tables),this.column=e.column??`deleted_at`}transformNode(e){switch(e.type){case`select`:return this.transformSelect(e);case`update`:return this.transformUpdate(e);case`delete`:return this.transformDelete(e);default:return e}}isTargetTable(e){return this.tables.has(e)}softDeleteCondition(){return w(m(this.column))}addCondition(e){let t=this.softDeleteCondition();return e?x(e,t):t}transformSelect(e){return!e.from||e.from.type!==`table_ref`||!this.isTargetTable(e.from.name)?e:{...e,where:this.addCondition(e.where)}}transformUpdate(e){return this.isTargetTable(e.table.name)?{...e,where:this.addCondition(e.where)}:e}transformDelete(e){return this.isTargetTable(e.table.name)?{...e,where:this.addCondition(e.where)}:e}},dt=class{name=`camel-case`;transformResult(e){return e.map(e=>{let t={};for(let n of Object.keys(e))t[ft(n)]=e[n];return t})}};function ft(e){return e.replace(/_([a-z])/g,(e,t)=>t.toUpperCase())}export{He as ASTTransformer,r as BasePrinter,dt as CamelCasePlugin,F as Col,ve as ColumnBuilder,O as DeleteBuilder,te as EmptyQueryError,Q as Hookable,E as InsertBuilder,s as InvalidExpressionError,ne as MysqlPrinter,i as PgPrinter,Z as PluginManager,T as SelectBuilder,ut as SoftDeletePlugin,ie as SqlitePrinter,$ as Sumak,ee as SumakError,Y as TypedDeleteBuilder,X as TypedDeleteReturningBuilder,G as TypedInsertBuilder,K as TypedInsertReturningBuilder,U as TypedSelectBuilder,q as TypedUpdateBuilder,J as TypedUpdateReturningBuilder,o as UnsupportedDialectFeatureError,D as UpdateBuilder,lt as WithSchemaPlugin,nt as and,Le as between,xe as bigint,he as bigserial,y as binOp,le as boolean,ye as bytea,ze as cast,Se as char,m as col,Oe as colAs,ot as count,p as createDeleteNode,d as createInsertNode,u as createSelectNode,f as createUpdateNode,de as date,Ce as defineTable,qe as deleteFrom,P as docConcat,j as docEmpty,Ze as docGroup,Qe as docJoin,N as docLine,Xe as docNest,et as docRender,M as docText,$e as docTextLine,ge as doublePrecision,we as enumType,C as eq,Be as exists,v as fn,t as formatParam,Je as formatSQL,Me as gt,Ne as gte,Re as inList,Ge as insert,fe as integer,w as isNull,c as json,be as jsonb,Ie as like,h as lit,Pe as lt,Fe as lte,re as mysqlDialect,je as neq,Ve as not,_e as numeric,rt as or,_ as param,a as pgDialect,n as quoteIdentifier,e as quoteTableRef,ke as raw,me as real,L as resetParams,We as select,pe as serial,ce as smallint,at as sqlFn,ae as sqliteDialect,g as star,Ae as subquery,ct as sumak,l as tableRef,Te as text,De as time,ue as timestamp,Ee as timestamptz,b as unaryOp,Ke as update,se as uuid,it as val,oe as varchar,Ue as visitNode};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sumak",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Type-safe SQL query builder with powerful SQL printers. Zero dependencies, tree-shakeable. Pure TypeScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"database",
|
|
@@ -16,15 +16,15 @@
|
|
|
16
16
|
"typescript",
|
|
17
17
|
"zero-dependency"
|
|
18
18
|
],
|
|
19
|
-
"homepage": "https://github.com/productdevbook/
|
|
19
|
+
"homepage": "https://github.com/productdevbook/sumak",
|
|
20
20
|
"bugs": {
|
|
21
|
-
"url": "https://github.com/productdevbook/
|
|
21
|
+
"url": "https://github.com/productdevbook/sumak/issues"
|
|
22
22
|
},
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"author": "productdevbook",
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
|
-
"url": "git+https://github.com/productdevbook/
|
|
27
|
+
"url": "git+https://github.com/productdevbook/sumak.git"
|
|
28
28
|
},
|
|
29
29
|
"funding": "https://github.com/sponsors/productdevbook",
|
|
30
30
|
"files": [
|