@expressive/sql 0.1.1 → 0.2.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/dist/index.d.mts CHANGED
@@ -40,7 +40,7 @@ declare abstract class Type {
40
40
  static get<T extends Type>(this: Type.EntityType<T>, limit?: number): Query.Selects<T>;
41
41
  static get<T extends Type>(this: Type.EntityType<T>, where?: Type.Query<T, void>): Query.Selects<T>;
42
42
  static get<T extends Type, R>(this: Type.EntityType<T>, limit: number, where: Type.Query<T, R>): Query.Selects<R>;
43
- static one<T extends Type, R extends {}>(this: Type.EntityType<T>, query: Type.Query<T, R>): Promise<Query.Extract<R>>;
43
+ static one<T extends Type, R extends {}>(this: Type.EntityType<T>, query: Type.Query<T, R>): Promise<Query.Returns<R>>;
44
44
  static one<T extends Type>(this: Type.EntityType<T>, query?: Type.Query<T, void>): Promise<Type.Values<T>>;
45
45
  static one<T extends Type>(this: Type.EntityType<T>, id: number): Promise<Type.Values<T>>;
46
46
  }
@@ -73,8 +73,7 @@ interface MathOps {
73
73
  neg(value: Numeric): Numeric;
74
74
  pos(value: Numeric): Numeric;
75
75
  }
76
- declare const math: MathOps;
77
- declare const bit: Bitwise;
76
+ declare const MathOps: MathOps;
78
77
  declare class Computed<T> extends Value {
79
78
  readonly left?: Query.Value | Field<T> | Computed<T>;
80
79
  readonly right: Query.Value | Field<T> | Computed<T>;
@@ -98,8 +97,8 @@ declare class Generator {
98
97
  protected set(updates: Map<Query.Table, Builder.Insert>): string;
99
98
  protected update(multiTableAllowed?: boolean): (string | undefined)[] | undefined;
100
99
  protected delete(): (string | false | undefined)[] | undefined;
101
- protected reference(from: Field | Value): Field<unknown> | Value;
102
- protected select(): (Field<unknown> | Value | undefined)[];
100
+ protected reference(from: unknown): string;
101
+ protected select(): (string | (string | undefined)[] | undefined)[];
103
102
  protected from(): (string | undefined)[] | undefined;
104
103
  protected param(from: Parameter): string;
105
104
  protected filter(left: Field, op: string, right: unknown): string;
@@ -127,7 +126,7 @@ declare abstract class Connection {
127
126
  then(onfulfilled?: (value: Connection.Ready<this>) => any, onrejected?: (reason: any) => any): Promise<any>;
128
127
  abstract get schema(): string;
129
128
  close(): Promise<void>;
130
- abstract sync(fix?: boolean): Promise<void>;
129
+ abstract sync(fix?: boolean): Promise<this>;
131
130
  abstract valid(type: Type.EntityType): Promise<boolean>;
132
131
  abstract prepare<T = any>(query: string): {
133
132
  all: (args?: any[]) => Promise<T[]>;
@@ -156,77 +155,80 @@ declare namespace Query {
156
155
  } & {
157
156
  [K in Type.Fields<T>]?: Field.Assigns<T[K]> | Field;
158
157
  };
158
+ interface Functions extends MathOps {
159
+ (template: string): QueryTemplate;
160
+ bit: Bitwise;
161
+ }
159
162
  /** Main callback for adding instructions to a Query. */
160
163
  type Where = {
164
+ /** Specify the limit of results returned. */
165
+ (limit: number): void;
166
+ /** Evaluate an expression */
167
+ (template: string): Compare<Field<unknown>>;
168
+ /**
169
+ * Accepts other where() assertions for nesting in parenthesis.
170
+ *
171
+ * Will alternate between AND-OR depending on depth, starting with OR.
172
+ */
173
+ (...orWhere: (Cond | Group)[]): Group;
161
174
  /**
162
175
  * Declare inserts to be made into a given table.
163
176
  */
164
- <T extends Type>(entity: Type.EntityType<T>, ...inserts: Insert<T>[]): Query.From<T>;
177
+ <T extends Type>(entity: Type.EntityType<T>, ...inserts: Insert<T>[]): From<T>;
165
178
  /**
166
179
  * Create a reference to the primary table, returned
167
180
  * object can be used to query against that table.
168
181
  */
169
- <T extends Type>(entity: Type.EntityType<T>): Query.From<T>;
182
+ <T extends Type>(entity: Type.EntityType<T>): From<T>;
170
183
  /**
171
184
  * Registers a type as a left join, returned object has optional
172
185
  * properties which may be undefined where the join is not present.
173
186
  */
174
- <T extends Type>(entity: Type.EntityType<T>, optional: true): Query.Join<T>;
187
+ <T extends Type>(entity: Type.EntityType<T>, optional: true): Join<T>;
175
188
  /**
176
189
  * Select a table for comparison or write operations.
177
190
  */
178
- <T extends Type>(field: Query.From<T> | Query.Join<T>): Query.Verbs<T>;
191
+ <T extends Type>(field: From<T> | Join<T>): Verbs<T>;
179
192
  /**
180
193
  * Prepare comparison against a particilar field,
181
194
  * returns operations for the given type.
182
195
  */
183
- <T extends Field>(field: T | undefined): Query.Asserts<T>;
196
+ <T extends Field>(field: T | undefined): Asserts<T>;
184
197
  <T extends {}>(data: Iterable<T>): {
185
198
  [K in keyof T]: Field<T[K]>;
186
199
  };
187
- /**
188
- * Accepts other where() assertions for nesting in parenthesis.
189
- *
190
- * Will alternate between AND-OR depending on depth, starting with OR.
191
- */
192
- (...orWhere: (Cond | Group)[]): Group;
193
- /** Specify the limit of results returned. */
194
- (limit: number): void;
195
200
  connection: Connection.Ready;
196
201
  };
202
+ type Compare<T extends Field> = ReturnType<T["where"]>;
203
+ type Asserts<T extends Field> = Compare<T> & {
204
+ asc(): void;
205
+ desc(): void;
206
+ };
197
207
  type Updates<T> = Field.Updates<T> | T | Field;
198
- type Function<R> = (this: Builder, where: Where) => R;
199
- type Factory<R, A extends any[]> = (this: Builder, where: Where) => (...args: A) => R;
200
- interface Template<A extends any[]> {
201
- (...args: A): Query;
202
- /** Prepared query for this template. */
203
- toString(): string;
204
- }
208
+ type Function<R> = (this: Builder, where: Where, fn: Query.Functions) => R;
209
+ type Factory<R, A extends any[]> = Function<(...args: A) => R>;
210
+ type Template<A extends any[]> = (...args: A) => Query;
205
211
  type TemplateSelects<T, A extends any[]> = (...args: A) => Selects<T>;
206
- type Extract<T> = T extends Field.Returns<infer R> ? R : T extends From<infer U> ? {
207
- [J in Type.Fields<U>]: Extract<U[J]>;
212
+ type Returns<T> = T extends Field.Returns<infer R> ? R : T extends From<infer U> ? {
213
+ [J in Type.Fields<U>]: Returns<U[J]>;
208
214
  } : T extends {} ? {
209
- [K in keyof T]: Extract<T[K]>;
215
+ [K in keyof T]: Returns<T[K]>;
210
216
  } : T;
211
- type Asserts<T extends Field> = ReturnType<T["where"]> & {
212
- asc(): void;
213
- desc(): void;
214
- };
215
217
  interface Verbs<T extends Type> {
216
218
  delete(): void;
217
- update(values: Query.Update<T>): void;
219
+ update(values: Update<T>): void;
218
220
  }
219
- interface Selects<T> extends Query<Extract<T>[]> {
221
+ interface Selects<T> extends Query<Returns<T>[]> {
220
222
  /**
221
223
  * Returns rows which match creteria.
222
224
  */
223
- get(): Promise<Extract<T>[]>;
225
+ get(): Promise<Returns<T>[]>;
224
226
  /**
225
227
  * Returns the first row that matches creteria.
226
228
  *
227
229
  * @param orFail If true, will throw an error if no match is found.
228
230
  */
229
- one(orFail?: boolean): Promise<Extract<T>>;
231
+ one(orFail?: boolean): Promise<Returns<T>>;
230
232
  }
231
233
  }
232
234
  interface Query<T = any> extends PromiseLike<T> {
@@ -248,6 +250,7 @@ declare function Query<T extends {}>(from: Query.Function<T>): Query.Selects<T>;
248
250
  declare function Query(from: Query.Function<void>): Query<number>;
249
251
  declare namespace Query {
250
252
  var Builder: typeof Builder;
253
+ var fn: Functions;
251
254
  }
252
255
 
253
256
  declare namespace Builder {
@@ -277,6 +280,7 @@ declare class Builder {
277
280
  deletes: Set<Query.Table>;
278
281
  updates: Map<Query.Table, Builder.Insert>;
279
282
  inserts: Map<Query.Table, Builder.Insert>;
283
+ register: Map<string, Field<unknown>>;
280
284
  returns?: Map<string, Field | Value> | Field | Value;
281
285
  limit?: number;
282
286
  commit(returns: unknown): void;
@@ -347,6 +351,11 @@ declare class Group {
347
351
  delete(child: Cond | Group): void;
348
352
  get size(): number;
349
353
  }
354
+ declare class QueryTemplate extends Value {
355
+ from: string;
356
+ parts: unknown[];
357
+ constructor(from: string, parent: Builder);
358
+ }
350
359
 
351
360
  type Nullable = {
352
361
  nullable: true;
@@ -604,4 +613,4 @@ declare namespace Table {
604
613
  declare function Table(name: string, opts?: Table.Options): Type.EntityType;
605
614
  declare function Table(opts: Table.Options): Type.EntityType;
606
615
 
607
- export { Bool, Builder, Connection, DataTable, Field, Generator, type Nullable, Num, One, type Optional, Parameter, Query, Str, Table, Time, Type, bit, math };
616
+ export { Bool, Builder, Connection, DataTable, Field, Generator, type Nullable, Num, One, type Optional, Parameter, Query, Str, Table, Time, Type };
package/dist/index.d.ts CHANGED
@@ -40,7 +40,7 @@ declare abstract class Type {
40
40
  static get<T extends Type>(this: Type.EntityType<T>, limit?: number): Query.Selects<T>;
41
41
  static get<T extends Type>(this: Type.EntityType<T>, where?: Type.Query<T, void>): Query.Selects<T>;
42
42
  static get<T extends Type, R>(this: Type.EntityType<T>, limit: number, where: Type.Query<T, R>): Query.Selects<R>;
43
- static one<T extends Type, R extends {}>(this: Type.EntityType<T>, query: Type.Query<T, R>): Promise<Query.Extract<R>>;
43
+ static one<T extends Type, R extends {}>(this: Type.EntityType<T>, query: Type.Query<T, R>): Promise<Query.Returns<R>>;
44
44
  static one<T extends Type>(this: Type.EntityType<T>, query?: Type.Query<T, void>): Promise<Type.Values<T>>;
45
45
  static one<T extends Type>(this: Type.EntityType<T>, id: number): Promise<Type.Values<T>>;
46
46
  }
@@ -73,8 +73,7 @@ interface MathOps {
73
73
  neg(value: Numeric): Numeric;
74
74
  pos(value: Numeric): Numeric;
75
75
  }
76
- declare const math: MathOps;
77
- declare const bit: Bitwise;
76
+ declare const MathOps: MathOps;
78
77
  declare class Computed<T> extends Value {
79
78
  readonly left?: Query.Value | Field<T> | Computed<T>;
80
79
  readonly right: Query.Value | Field<T> | Computed<T>;
@@ -98,8 +97,8 @@ declare class Generator {
98
97
  protected set(updates: Map<Query.Table, Builder.Insert>): string;
99
98
  protected update(multiTableAllowed?: boolean): (string | undefined)[] | undefined;
100
99
  protected delete(): (string | false | undefined)[] | undefined;
101
- protected reference(from: Field | Value): Field<unknown> | Value;
102
- protected select(): (Field<unknown> | Value | undefined)[];
100
+ protected reference(from: unknown): string;
101
+ protected select(): (string | (string | undefined)[] | undefined)[];
103
102
  protected from(): (string | undefined)[] | undefined;
104
103
  protected param(from: Parameter): string;
105
104
  protected filter(left: Field, op: string, right: unknown): string;
@@ -127,7 +126,7 @@ declare abstract class Connection {
127
126
  then(onfulfilled?: (value: Connection.Ready<this>) => any, onrejected?: (reason: any) => any): Promise<any>;
128
127
  abstract get schema(): string;
129
128
  close(): Promise<void>;
130
- abstract sync(fix?: boolean): Promise<void>;
129
+ abstract sync(fix?: boolean): Promise<this>;
131
130
  abstract valid(type: Type.EntityType): Promise<boolean>;
132
131
  abstract prepare<T = any>(query: string): {
133
132
  all: (args?: any[]) => Promise<T[]>;
@@ -156,77 +155,80 @@ declare namespace Query {
156
155
  } & {
157
156
  [K in Type.Fields<T>]?: Field.Assigns<T[K]> | Field;
158
157
  };
158
+ interface Functions extends MathOps {
159
+ (template: string): QueryTemplate;
160
+ bit: Bitwise;
161
+ }
159
162
  /** Main callback for adding instructions to a Query. */
160
163
  type Where = {
164
+ /** Specify the limit of results returned. */
165
+ (limit: number): void;
166
+ /** Evaluate an expression */
167
+ (template: string): Compare<Field<unknown>>;
168
+ /**
169
+ * Accepts other where() assertions for nesting in parenthesis.
170
+ *
171
+ * Will alternate between AND-OR depending on depth, starting with OR.
172
+ */
173
+ (...orWhere: (Cond | Group)[]): Group;
161
174
  /**
162
175
  * Declare inserts to be made into a given table.
163
176
  */
164
- <T extends Type>(entity: Type.EntityType<T>, ...inserts: Insert<T>[]): Query.From<T>;
177
+ <T extends Type>(entity: Type.EntityType<T>, ...inserts: Insert<T>[]): From<T>;
165
178
  /**
166
179
  * Create a reference to the primary table, returned
167
180
  * object can be used to query against that table.
168
181
  */
169
- <T extends Type>(entity: Type.EntityType<T>): Query.From<T>;
182
+ <T extends Type>(entity: Type.EntityType<T>): From<T>;
170
183
  /**
171
184
  * Registers a type as a left join, returned object has optional
172
185
  * properties which may be undefined where the join is not present.
173
186
  */
174
- <T extends Type>(entity: Type.EntityType<T>, optional: true): Query.Join<T>;
187
+ <T extends Type>(entity: Type.EntityType<T>, optional: true): Join<T>;
175
188
  /**
176
189
  * Select a table for comparison or write operations.
177
190
  */
178
- <T extends Type>(field: Query.From<T> | Query.Join<T>): Query.Verbs<T>;
191
+ <T extends Type>(field: From<T> | Join<T>): Verbs<T>;
179
192
  /**
180
193
  * Prepare comparison against a particilar field,
181
194
  * returns operations for the given type.
182
195
  */
183
- <T extends Field>(field: T | undefined): Query.Asserts<T>;
196
+ <T extends Field>(field: T | undefined): Asserts<T>;
184
197
  <T extends {}>(data: Iterable<T>): {
185
198
  [K in keyof T]: Field<T[K]>;
186
199
  };
187
- /**
188
- * Accepts other where() assertions for nesting in parenthesis.
189
- *
190
- * Will alternate between AND-OR depending on depth, starting with OR.
191
- */
192
- (...orWhere: (Cond | Group)[]): Group;
193
- /** Specify the limit of results returned. */
194
- (limit: number): void;
195
200
  connection: Connection.Ready;
196
201
  };
202
+ type Compare<T extends Field> = ReturnType<T["where"]>;
203
+ type Asserts<T extends Field> = Compare<T> & {
204
+ asc(): void;
205
+ desc(): void;
206
+ };
197
207
  type Updates<T> = Field.Updates<T> | T | Field;
198
- type Function<R> = (this: Builder, where: Where) => R;
199
- type Factory<R, A extends any[]> = (this: Builder, where: Where) => (...args: A) => R;
200
- interface Template<A extends any[]> {
201
- (...args: A): Query;
202
- /** Prepared query for this template. */
203
- toString(): string;
204
- }
208
+ type Function<R> = (this: Builder, where: Where, fn: Query.Functions) => R;
209
+ type Factory<R, A extends any[]> = Function<(...args: A) => R>;
210
+ type Template<A extends any[]> = (...args: A) => Query;
205
211
  type TemplateSelects<T, A extends any[]> = (...args: A) => Selects<T>;
206
- type Extract<T> = T extends Field.Returns<infer R> ? R : T extends From<infer U> ? {
207
- [J in Type.Fields<U>]: Extract<U[J]>;
212
+ type Returns<T> = T extends Field.Returns<infer R> ? R : T extends From<infer U> ? {
213
+ [J in Type.Fields<U>]: Returns<U[J]>;
208
214
  } : T extends {} ? {
209
- [K in keyof T]: Extract<T[K]>;
215
+ [K in keyof T]: Returns<T[K]>;
210
216
  } : T;
211
- type Asserts<T extends Field> = ReturnType<T["where"]> & {
212
- asc(): void;
213
- desc(): void;
214
- };
215
217
  interface Verbs<T extends Type> {
216
218
  delete(): void;
217
- update(values: Query.Update<T>): void;
219
+ update(values: Update<T>): void;
218
220
  }
219
- interface Selects<T> extends Query<Extract<T>[]> {
221
+ interface Selects<T> extends Query<Returns<T>[]> {
220
222
  /**
221
223
  * Returns rows which match creteria.
222
224
  */
223
- get(): Promise<Extract<T>[]>;
225
+ get(): Promise<Returns<T>[]>;
224
226
  /**
225
227
  * Returns the first row that matches creteria.
226
228
  *
227
229
  * @param orFail If true, will throw an error if no match is found.
228
230
  */
229
- one(orFail?: boolean): Promise<Extract<T>>;
231
+ one(orFail?: boolean): Promise<Returns<T>>;
230
232
  }
231
233
  }
232
234
  interface Query<T = any> extends PromiseLike<T> {
@@ -248,6 +250,7 @@ declare function Query<T extends {}>(from: Query.Function<T>): Query.Selects<T>;
248
250
  declare function Query(from: Query.Function<void>): Query<number>;
249
251
  declare namespace Query {
250
252
  var Builder: typeof Builder;
253
+ var fn: Functions;
251
254
  }
252
255
 
253
256
  declare namespace Builder {
@@ -277,6 +280,7 @@ declare class Builder {
277
280
  deletes: Set<Query.Table>;
278
281
  updates: Map<Query.Table, Builder.Insert>;
279
282
  inserts: Map<Query.Table, Builder.Insert>;
283
+ register: Map<string, Field<unknown>>;
280
284
  returns?: Map<string, Field | Value> | Field | Value;
281
285
  limit?: number;
282
286
  commit(returns: unknown): void;
@@ -347,6 +351,11 @@ declare class Group {
347
351
  delete(child: Cond | Group): void;
348
352
  get size(): number;
349
353
  }
354
+ declare class QueryTemplate extends Value {
355
+ from: string;
356
+ parts: unknown[];
357
+ constructor(from: string, parent: Builder);
358
+ }
350
359
 
351
360
  type Nullable = {
352
361
  nullable: true;
@@ -604,4 +613,4 @@ declare namespace Table {
604
613
  declare function Table(name: string, opts?: Table.Options): Type.EntityType;
605
614
  declare function Table(opts: Table.Options): Type.EntityType;
606
615
 
607
- export { Bool, Builder, Connection, DataTable, Field, Generator, type Nullable, Num, One, type Optional, Parameter, Query, Str, Table, Time, Type, bit, math };
616
+ export { Bool, Builder, Connection, DataTable, Field, Generator, type Nullable, Num, One, type Optional, Parameter, Query, Str, Table, Time, Type };
package/dist/index.js CHANGED
@@ -33,9 +33,7 @@ __export(index_exports, {
33
33
  Str: () => Str,
34
34
  Table: () => Table,
35
35
  Time: () => Time,
36
- Type: () => Type,
37
- bit: () => bit,
38
- math: () => math
36
+ Type: () => Type
39
37
  });
40
38
  module.exports = __toCommonJS(index_exports);
41
39
 
@@ -310,6 +308,7 @@ var Builder = class {
310
308
  this.deletes = /* @__PURE__ */ new Set();
311
309
  this.updates = /* @__PURE__ */ new Map();
312
310
  this.inserts = /* @__PURE__ */ new Map();
311
+ this.register = /* @__PURE__ */ new Map();
313
312
  }
314
313
  commit(returns) {
315
314
  if (this.returns)
@@ -420,6 +419,7 @@ var Builder = class {
420
419
  field.table = table;
421
420
  field.toString = () => `${table.alias || table.name}.${field.column}`;
422
421
  table.fields.set(key, field);
422
+ this.register.set(uid(), field);
423
423
  let value;
424
424
  defineProperty(proxy, key, {
425
425
  get: () => value || (value = field.use ? field.use(this) : field)
@@ -618,6 +618,16 @@ var Group = class {
618
618
  return this.children.size;
619
619
  }
620
620
  };
621
+ var QueryTemplate = class extends Value {
622
+ constructor(from, parent) {
623
+ super();
624
+ this.from = from;
625
+ this.parts = from.split(/(\0[a-zA-Z0-9]+)/).filter(Boolean).map((part) => parent.register.get(part) || part);
626
+ }
627
+ };
628
+ function uid() {
629
+ return "\0" + Math.random().toString(32).slice(2, 7);
630
+ }
621
631
 
622
632
  // src/query/Computed.ts
623
633
  function op(operator, rank, unary) {
@@ -625,7 +635,7 @@ function op(operator, rank, unary) {
625
635
  return new Computed(operator, left, unary ? void 0 : right, rank);
626
636
  };
627
637
  }
628
- var math = {
638
+ var MathOps = {
629
639
  add: op("+", 4),
630
640
  sub: op("-", 4),
631
641
  mul: op("*", 5),
@@ -634,7 +644,7 @@ var math = {
634
644
  neg: op("-", 7, true),
635
645
  pos: op("+", 7, true)
636
646
  };
637
- var bit = {
647
+ var BitWise = {
638
648
  not: op("~", 6, true),
639
649
  left: op("<<", 3),
640
650
  right: op(">>", 3),
@@ -689,8 +699,7 @@ var Generator = class {
689
699
  return [
690
700
  "INSERT INTO",
691
701
  this.escape(table.name),
692
- `(${keys})`,
693
- `SELECT ${values2}`,
702
+ `(${keys}) SELECT ${values2}`,
694
703
  tables.size > 1 && this.from()
695
704
  ];
696
705
  }
@@ -797,6 +806,8 @@ var Generator = class {
797
806
  reference(from) {
798
807
  if (from instanceof DataField)
799
808
  return this.escape(from.table.name + "." + from.column);
809
+ if (from instanceof QueryTemplate)
810
+ return from.parts.map((x) => String(this.reference(x))).join("");
800
811
  if (from instanceof Computed)
801
812
  return this.computed(from);
802
813
  if (from instanceof Parameter)
@@ -807,7 +818,7 @@ var Generator = class {
807
818
  if (table)
808
819
  return this.escape(table.alias || table.name) + "." + column;
809
820
  }
810
- return from;
821
+ return String(from);
811
822
  }
812
823
  select() {
813
824
  const { returns, tables } = this.query;
@@ -891,7 +902,7 @@ var Connection = class {
891
902
  return new generator(builder).toString();
892
903
  }
893
904
  async then(onfulfilled, onrejected) {
894
- return (this.ready ? Promise.resolve() : this.sync(true)).then(() => {
905
+ return (this.ready ? Promise.resolve() : this.sync()).then(() => {
895
906
  Object.defineProperty(this, "then", { value: void 0 });
896
907
  if (onfulfilled)
897
908
  onfulfilled(this);
@@ -924,7 +935,7 @@ var NoConnection = class extends Connection {
924
935
  };
925
936
  }
926
937
  async sync() {
927
- return;
938
+ return this;
928
939
  }
929
940
  async valid() {
930
941
  return true;
@@ -1041,6 +1052,8 @@ function Query(factory) {
1041
1052
  builder.limit = arg1;
1042
1053
  return;
1043
1054
  }
1055
+ if (typeof arg1 == "string")
1056
+ return new QueryTemplate(arg1, builder);
1044
1057
  if (Symbol.iterator in arg1)
1045
1058
  return builder.with(arg1);
1046
1059
  throw new Error(`Argument ${arg1} is not a query argument.`);
@@ -1054,7 +1067,8 @@ function Query(factory) {
1054
1067
  }
1055
1068
  });
1056
1069
  const builder = new Builder();
1057
- let result = factory.call(builder, where);
1070
+ const fn = factory.length > 1 ? assign((template2) => new QueryTemplate(template2, builder), Query.fn) : void 0;
1071
+ let result = factory.call(builder, where, fn);
1058
1072
  let args;
1059
1073
  if (typeof result === "function") {
1060
1074
  const params = builder.inject(args = result.length);
@@ -1097,6 +1111,7 @@ function Query(factory) {
1097
1111
  return runner();
1098
1112
  }
1099
1113
  Query.Builder = Builder;
1114
+ Query.fn = { ...MathOps, bit: BitWise };
1100
1115
 
1101
1116
  // src/type/Table.ts
1102
1117
  function Table(arg1, arg2) {
@@ -1126,7 +1141,5 @@ function Table(arg1, arg2) {
1126
1141
  Str,
1127
1142
  Table,
1128
1143
  Time,
1129
- Type,
1130
- bit,
1131
- math
1144
+ Type
1132
1145
  });
package/dist/index.mjs CHANGED
@@ -269,6 +269,7 @@ var Builder = class {
269
269
  this.deletes = /* @__PURE__ */ new Set();
270
270
  this.updates = /* @__PURE__ */ new Map();
271
271
  this.inserts = /* @__PURE__ */ new Map();
272
+ this.register = /* @__PURE__ */ new Map();
272
273
  }
273
274
  commit(returns) {
274
275
  if (this.returns)
@@ -379,6 +380,7 @@ var Builder = class {
379
380
  field.table = table;
380
381
  field.toString = () => `${table.alias || table.name}.${field.column}`;
381
382
  table.fields.set(key, field);
383
+ this.register.set(uid(), field);
382
384
  let value;
383
385
  defineProperty(proxy, key, {
384
386
  get: () => value || (value = field.use ? field.use(this) : field)
@@ -577,6 +579,16 @@ var Group = class {
577
579
  return this.children.size;
578
580
  }
579
581
  };
582
+ var QueryTemplate = class extends Value {
583
+ constructor(from, parent) {
584
+ super();
585
+ this.from = from;
586
+ this.parts = from.split(/(\0[a-zA-Z0-9]+)/).filter(Boolean).map((part) => parent.register.get(part) || part);
587
+ }
588
+ };
589
+ function uid() {
590
+ return "\0" + Math.random().toString(32).slice(2, 7);
591
+ }
580
592
 
581
593
  // src/query/Computed.ts
582
594
  function op(operator, rank, unary) {
@@ -584,7 +596,7 @@ function op(operator, rank, unary) {
584
596
  return new Computed(operator, left, unary ? void 0 : right, rank);
585
597
  };
586
598
  }
587
- var math = {
599
+ var MathOps = {
588
600
  add: op("+", 4),
589
601
  sub: op("-", 4),
590
602
  mul: op("*", 5),
@@ -593,7 +605,7 @@ var math = {
593
605
  neg: op("-", 7, true),
594
606
  pos: op("+", 7, true)
595
607
  };
596
- var bit = {
608
+ var BitWise = {
597
609
  not: op("~", 6, true),
598
610
  left: op("<<", 3),
599
611
  right: op(">>", 3),
@@ -648,8 +660,7 @@ var Generator = class {
648
660
  return [
649
661
  "INSERT INTO",
650
662
  this.escape(table.name),
651
- `(${keys})`,
652
- `SELECT ${values2}`,
663
+ `(${keys}) SELECT ${values2}`,
653
664
  tables.size > 1 && this.from()
654
665
  ];
655
666
  }
@@ -756,6 +767,8 @@ var Generator = class {
756
767
  reference(from) {
757
768
  if (from instanceof DataField)
758
769
  return this.escape(from.table.name + "." + from.column);
770
+ if (from instanceof QueryTemplate)
771
+ return from.parts.map((x) => String(this.reference(x))).join("");
759
772
  if (from instanceof Computed)
760
773
  return this.computed(from);
761
774
  if (from instanceof Parameter)
@@ -766,7 +779,7 @@ var Generator = class {
766
779
  if (table)
767
780
  return this.escape(table.alias || table.name) + "." + column;
768
781
  }
769
- return from;
782
+ return String(from);
770
783
  }
771
784
  select() {
772
785
  const { returns, tables } = this.query;
@@ -850,7 +863,7 @@ var Connection = class {
850
863
  return new generator(builder).toString();
851
864
  }
852
865
  async then(onfulfilled, onrejected) {
853
- return (this.ready ? Promise.resolve() : this.sync(true)).then(() => {
866
+ return (this.ready ? Promise.resolve() : this.sync()).then(() => {
854
867
  Object.defineProperty(this, "then", { value: void 0 });
855
868
  if (onfulfilled)
856
869
  onfulfilled(this);
@@ -883,7 +896,7 @@ var NoConnection = class extends Connection {
883
896
  };
884
897
  }
885
898
  async sync() {
886
- return;
899
+ return this;
887
900
  }
888
901
  async valid() {
889
902
  return true;
@@ -1000,6 +1013,8 @@ function Query(factory) {
1000
1013
  builder.limit = arg1;
1001
1014
  return;
1002
1015
  }
1016
+ if (typeof arg1 == "string")
1017
+ return new QueryTemplate(arg1, builder);
1003
1018
  if (Symbol.iterator in arg1)
1004
1019
  return builder.with(arg1);
1005
1020
  throw new Error(`Argument ${arg1} is not a query argument.`);
@@ -1013,7 +1028,8 @@ function Query(factory) {
1013
1028
  }
1014
1029
  });
1015
1030
  const builder = new Builder();
1016
- let result = factory.call(builder, where);
1031
+ const fn = factory.length > 1 ? assign((template2) => new QueryTemplate(template2, builder), Query.fn) : void 0;
1032
+ let result = factory.call(builder, where, fn);
1017
1033
  let args;
1018
1034
  if (typeof result === "function") {
1019
1035
  const params = builder.inject(args = result.length);
@@ -1056,6 +1072,7 @@ function Query(factory) {
1056
1072
  return runner();
1057
1073
  }
1058
1074
  Query.Builder = Builder;
1075
+ Query.fn = { ...MathOps, bit: BitWise };
1059
1076
 
1060
1077
  // src/type/Table.ts
1061
1078
  function Table(arg1, arg2) {
@@ -1084,7 +1101,5 @@ export {
1084
1101
  Str,
1085
1102
  Table,
1086
1103
  Time,
1087
- Type,
1088
- bit,
1089
- math
1104
+ Type
1090
1105
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressive/sql",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Query builder and ORM",
5
5
  "license": "ISC",
6
6
  "publishConfig": {
@@ -24,6 +24,6 @@
24
24
  },
25
25
  "scripts": {
26
26
  "build": "tsup src/index.ts --dts --format esm,cjs",
27
- "test": "vitest"
27
+ "test": "vitest run"
28
28
  }
29
29
  }