@expressive/sql 0.4.1 → 0.4.3

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
@@ -68,6 +68,39 @@ declare abstract class Table {
68
68
  static one<T extends Table>(this: Table.Type<T>, id: number): Promise<Table.Values<T>>;
69
69
  }
70
70
 
71
+ declare namespace Connection {
72
+ type Types = typeof Table[] | {
73
+ [key: string | number]: typeof Table;
74
+ };
75
+ type Using = {
76
+ [key: string | number]: typeof Table;
77
+ };
78
+ type Ready<T extends Connection = Connection> = Omit<T, "then">;
79
+ let None: Connection;
80
+ }
81
+ declare abstract class Connection {
82
+ readonly using: Readonly<Set<typeof Table>>;
83
+ readonly ready: boolean;
84
+ constructor(using: Connection.Types);
85
+ query<T extends {}, A extends unknown[]>(from: Query.Factory<T, A>): Query.TemplateSelects<T, A>;
86
+ query<A extends unknown[]>(from: Query.Factory<void, A>): Query.Template<A>;
87
+ query<T extends {}>(from: Query.Function<T>): Query.Selects<T>;
88
+ /**
89
+ * Creates a new query.
90
+ * Will return the number of rows that would be selected or modified.
91
+ */
92
+ query(from: Query.Function<void>): Query<number>;
93
+ then(onfulfilled?: (value: Connection.Ready<this>) => any, onrejected?: (reason: any) => any): Promise<any>;
94
+ abstract get schema(): string;
95
+ close(): Promise<void>;
96
+ abstract sync(fix?: boolean): Promise<this>;
97
+ abstract prepare<T = any>(builder: Builder): {
98
+ all: (args?: any[]) => Promise<T[]>;
99
+ get: (args?: any[]) => Promise<T | undefined>;
100
+ run: (args?: any[]) => Promise<number>;
101
+ };
102
+ }
103
+
71
104
  type ANumeric = Query.Match<string | number>;
72
105
  type Numeric = Query.Match<number>;
73
106
  interface Bitwise {
@@ -97,59 +130,6 @@ declare class Computed<T> extends Value {
97
130
  constructor(operator: string, value: Value | Field<T> | Computed<T>, right?: Value | Field<T> | Computed<T>, rank?: number);
98
131
  }
99
132
 
100
- declare class Generator {
101
- query: Builder;
102
- cte: Map<string, string>;
103
- constructor(query: Builder);
104
- toString(): string;
105
- escape(name: unknown): string;
106
- protected insert(): false | (string | false | (string | undefined)[] | undefined)[];
107
- protected with(): string | undefined;
108
- protected value(input: unknown): string;
109
- protected jsonTable(table: DataTable): void;
110
- computed(from: Computed<any>): string;
111
- protected joins(main: Builder.ITable): string | undefined;
112
- protected set(updates: Map<Query.ITable, Builder.Insert>): string;
113
- protected update(multiTableAllowed?: boolean): (string | undefined)[] | undefined;
114
- protected delete(): (string | false | undefined)[] | undefined;
115
- protected reference(from: unknown): string;
116
- protected select(): (string | (string | undefined)[] | undefined)[];
117
- protected from(): (string | undefined)[] | undefined;
118
- protected param(from: Parameter): string;
119
- protected filter(left: Field, op: string, right: unknown): string;
120
- protected where(): string[] | undefined;
121
- protected order(): string | undefined;
122
- protected limit(): (string | number)[] | undefined;
123
- }
124
-
125
- declare namespace Connection {
126
- type Types = typeof Table[] | {
127
- [key: string | number]: typeof Table;
128
- };
129
- type Using = {
130
- [key: string | number]: typeof Table;
131
- };
132
- type Ready<T extends Connection = Connection> = Omit<T, "then">;
133
- let None: Connection;
134
- }
135
- declare abstract class Connection {
136
- static generator: typeof Generator;
137
- readonly using: Readonly<Set<typeof Table>>;
138
- readonly ready: boolean;
139
- constructor(using: Connection.Types);
140
- generate(builder: Builder): string;
141
- then(onfulfilled?: (value: Connection.Ready<this>) => any, onrejected?: (reason: any) => any): Promise<any>;
142
- abstract get schema(): string;
143
- close(): Promise<void>;
144
- abstract sync(fix?: boolean): Promise<this>;
145
- abstract valid(type: Table.Type): Promise<boolean>;
146
- abstract prepare<T = any>(query: string): {
147
- all: (args?: any[]) => Promise<T[]>;
148
- get: (args?: any[]) => Promise<T | undefined>;
149
- run: (args?: any[]) => Promise<number>;
150
- };
151
- }
152
-
153
133
  declare namespace Query {
154
134
  type ITable = Builder.ITable;
155
135
  type From<T extends Table = Table> = {
@@ -406,6 +386,8 @@ declare namespace Field {
406
386
  } | null ? TT & Nullable : First extends {
407
387
  fallback?: any;
408
388
  increment?: true;
389
+ primary?: true;
390
+ default?: true;
409
391
  } ? TT & Optional : Mod<Rest, TT> : TT;
410
392
  type Infer<T extends any[], A, D extends Field = any> = Mod<T, Type<T, A, D>>;
411
393
  type Returns<T> = Field & {
@@ -665,6 +647,31 @@ declare namespace Time {
665
647
  var Type: typeof TimeColumn;
666
648
  }
667
649
 
650
+ declare class Generator {
651
+ query: Builder;
652
+ cte: Map<string, string>;
653
+ constructor(query: Builder);
654
+ toString(): string;
655
+ escape(name: unknown): string;
656
+ protected insert(): false | (string | false | (string | undefined)[] | undefined)[];
657
+ protected with(): string | undefined;
658
+ protected value(input: unknown): string;
659
+ protected jsonTable(table: DataTable): void;
660
+ computed(from: Computed<any>): string;
661
+ protected joins(main: Builder.ITable): string | undefined;
662
+ protected set(updates: Map<Query.ITable, Builder.Insert>): string;
663
+ protected update(multiTableAllowed?: boolean): (string | undefined)[] | undefined;
664
+ protected delete(): (string | false | undefined)[] | undefined;
665
+ protected reference(from: unknown): string;
666
+ protected select(): (string | (string | undefined)[] | undefined)[];
667
+ protected from(): (string | undefined)[] | undefined;
668
+ protected param(from: Parameter): string;
669
+ protected filter(left: Field, op: string, right: unknown): string;
670
+ protected where(): string[] | undefined;
671
+ protected order(): string | undefined;
672
+ protected limit(): (string | number)[] | undefined;
673
+ }
674
+
668
675
  declare class PrimaryColumn extends Field<number> {
669
676
  readonly type = "integer";
670
677
  readonly nullable = false;
package/dist/index.d.ts CHANGED
@@ -68,6 +68,39 @@ declare abstract class Table {
68
68
  static one<T extends Table>(this: Table.Type<T>, id: number): Promise<Table.Values<T>>;
69
69
  }
70
70
 
71
+ declare namespace Connection {
72
+ type Types = typeof Table[] | {
73
+ [key: string | number]: typeof Table;
74
+ };
75
+ type Using = {
76
+ [key: string | number]: typeof Table;
77
+ };
78
+ type Ready<T extends Connection = Connection> = Omit<T, "then">;
79
+ let None: Connection;
80
+ }
81
+ declare abstract class Connection {
82
+ readonly using: Readonly<Set<typeof Table>>;
83
+ readonly ready: boolean;
84
+ constructor(using: Connection.Types);
85
+ query<T extends {}, A extends unknown[]>(from: Query.Factory<T, A>): Query.TemplateSelects<T, A>;
86
+ query<A extends unknown[]>(from: Query.Factory<void, A>): Query.Template<A>;
87
+ query<T extends {}>(from: Query.Function<T>): Query.Selects<T>;
88
+ /**
89
+ * Creates a new query.
90
+ * Will return the number of rows that would be selected or modified.
91
+ */
92
+ query(from: Query.Function<void>): Query<number>;
93
+ then(onfulfilled?: (value: Connection.Ready<this>) => any, onrejected?: (reason: any) => any): Promise<any>;
94
+ abstract get schema(): string;
95
+ close(): Promise<void>;
96
+ abstract sync(fix?: boolean): Promise<this>;
97
+ abstract prepare<T = any>(builder: Builder): {
98
+ all: (args?: any[]) => Promise<T[]>;
99
+ get: (args?: any[]) => Promise<T | undefined>;
100
+ run: (args?: any[]) => Promise<number>;
101
+ };
102
+ }
103
+
71
104
  type ANumeric = Query.Match<string | number>;
72
105
  type Numeric = Query.Match<number>;
73
106
  interface Bitwise {
@@ -97,59 +130,6 @@ declare class Computed<T> extends Value {
97
130
  constructor(operator: string, value: Value | Field<T> | Computed<T>, right?: Value | Field<T> | Computed<T>, rank?: number);
98
131
  }
99
132
 
100
- declare class Generator {
101
- query: Builder;
102
- cte: Map<string, string>;
103
- constructor(query: Builder);
104
- toString(): string;
105
- escape(name: unknown): string;
106
- protected insert(): false | (string | false | (string | undefined)[] | undefined)[];
107
- protected with(): string | undefined;
108
- protected value(input: unknown): string;
109
- protected jsonTable(table: DataTable): void;
110
- computed(from: Computed<any>): string;
111
- protected joins(main: Builder.ITable): string | undefined;
112
- protected set(updates: Map<Query.ITable, Builder.Insert>): string;
113
- protected update(multiTableAllowed?: boolean): (string | undefined)[] | undefined;
114
- protected delete(): (string | false | undefined)[] | undefined;
115
- protected reference(from: unknown): string;
116
- protected select(): (string | (string | undefined)[] | undefined)[];
117
- protected from(): (string | undefined)[] | undefined;
118
- protected param(from: Parameter): string;
119
- protected filter(left: Field, op: string, right: unknown): string;
120
- protected where(): string[] | undefined;
121
- protected order(): string | undefined;
122
- protected limit(): (string | number)[] | undefined;
123
- }
124
-
125
- declare namespace Connection {
126
- type Types = typeof Table[] | {
127
- [key: string | number]: typeof Table;
128
- };
129
- type Using = {
130
- [key: string | number]: typeof Table;
131
- };
132
- type Ready<T extends Connection = Connection> = Omit<T, "then">;
133
- let None: Connection;
134
- }
135
- declare abstract class Connection {
136
- static generator: typeof Generator;
137
- readonly using: Readonly<Set<typeof Table>>;
138
- readonly ready: boolean;
139
- constructor(using: Connection.Types);
140
- generate(builder: Builder): string;
141
- then(onfulfilled?: (value: Connection.Ready<this>) => any, onrejected?: (reason: any) => any): Promise<any>;
142
- abstract get schema(): string;
143
- close(): Promise<void>;
144
- abstract sync(fix?: boolean): Promise<this>;
145
- abstract valid(type: Table.Type): Promise<boolean>;
146
- abstract prepare<T = any>(query: string): {
147
- all: (args?: any[]) => Promise<T[]>;
148
- get: (args?: any[]) => Promise<T | undefined>;
149
- run: (args?: any[]) => Promise<number>;
150
- };
151
- }
152
-
153
133
  declare namespace Query {
154
134
  type ITable = Builder.ITable;
155
135
  type From<T extends Table = Table> = {
@@ -406,6 +386,8 @@ declare namespace Field {
406
386
  } | null ? TT & Nullable : First extends {
407
387
  fallback?: any;
408
388
  increment?: true;
389
+ primary?: true;
390
+ default?: true;
409
391
  } ? TT & Optional : Mod<Rest, TT> : TT;
410
392
  type Infer<T extends any[], A, D extends Field = any> = Mod<T, Type<T, A, D>>;
411
393
  type Returns<T> = Field & {
@@ -665,6 +647,31 @@ declare namespace Time {
665
647
  var Type: typeof TimeColumn;
666
648
  }
667
649
 
650
+ declare class Generator {
651
+ query: Builder;
652
+ cte: Map<string, string>;
653
+ constructor(query: Builder);
654
+ toString(): string;
655
+ escape(name: unknown): string;
656
+ protected insert(): false | (string | false | (string | undefined)[] | undefined)[];
657
+ protected with(): string | undefined;
658
+ protected value(input: unknown): string;
659
+ protected jsonTable(table: DataTable): void;
660
+ computed(from: Computed<any>): string;
661
+ protected joins(main: Builder.ITable): string | undefined;
662
+ protected set(updates: Map<Query.ITable, Builder.Insert>): string;
663
+ protected update(multiTableAllowed?: boolean): (string | undefined)[] | undefined;
664
+ protected delete(): (string | false | undefined)[] | undefined;
665
+ protected reference(from: unknown): string;
666
+ protected select(): (string | (string | undefined)[] | undefined)[];
667
+ protected from(): (string | undefined)[] | undefined;
668
+ protected param(from: Parameter): string;
669
+ protected filter(left: Field, op: string, right: unknown): string;
670
+ protected where(): string[] | undefined;
671
+ protected order(): string | undefined;
672
+ protected limit(): (string | number)[] | undefined;
673
+ }
674
+
668
675
  declare class PrimaryColumn extends Field<number> {
669
676
  readonly type = "integer";
670
677
  readonly nullable = false;
package/dist/index.js CHANGED
@@ -32,7 +32,7 @@ __export(index_exports, {
32
32
  Primary: () => Primary,
33
33
  Query: () => Query,
34
34
  Str: () => Str,
35
- Table: () => Table,
35
+ Table: () => Table2,
36
36
  Time: () => Time
37
37
  });
38
38
  module.exports = __toCommonJS(index_exports);
@@ -118,7 +118,6 @@ var BooleanColumn = class extends Field {
118
118
  };
119
119
  function Bool(...opts) {
120
120
  return Bool.Type.new(...opts);
121
- ;
122
121
  }
123
122
  Bool.Type = BooleanColumn;
124
123
 
@@ -821,9 +820,12 @@ var Connection = class {
821
820
  return type;
822
821
  }));
823
822
  }
824
- generate(builder) {
825
- const { generator } = this.constructor;
826
- return new generator(builder).toString();
823
+ query(factory) {
824
+ const connection = this;
825
+ return Query(function(where, fn) {
826
+ where.connection = connection;
827
+ return factory.call(this, where, fn);
828
+ });
827
829
  }
828
830
  async then(onfulfilled, onrejected) {
829
831
  return (this.ready ? Promise.resolve() : this.sync()).then(() => {
@@ -837,7 +839,6 @@ var Connection = class {
837
839
  delete type.connection;
838
840
  }
839
841
  };
840
- Connection.generator = Generator;
841
842
  var NoConnection = class extends Connection {
842
843
  constructor() {
843
844
  super([]);
@@ -845,7 +846,8 @@ var NoConnection = class extends Connection {
845
846
  get schema() {
846
847
  throw new Error("No connection to generate schema.");
847
848
  }
848
- prepare(template) {
849
+ prepare(builder) {
850
+ const template = String(new Generator(builder));
849
851
  async function run() {
850
852
  throw new Error("No connection to run query.");
851
853
  }
@@ -861,9 +863,6 @@ var NoConnection = class extends Connection {
861
863
  async sync() {
862
864
  return this;
863
865
  }
864
- async valid() {
865
- return true;
866
- }
867
866
  async close() {
868
867
  return;
869
868
  }
@@ -990,13 +989,13 @@ var _Table = class _Table {
990
989
  }
991
990
  };
992
991
  _Table.schema = "";
993
- var Table = _Table;
992
+ var Table2 = _Table;
994
993
 
995
994
  // src/query/Query.ts
996
995
  function Query(factory) {
997
996
  let connection = void 0;
998
997
  function where(arg1) {
999
- if (Table.is(arg1)) {
998
+ if (Table2.is(arg1)) {
1000
999
  if (!connection)
1001
1000
  connection = arg1.connection;
1002
1001
  else if (connection !== arg1.connection)
@@ -1030,7 +1029,7 @@ function Query(factory) {
1030
1029
  }
1031
1030
  });
1032
1031
  const builder = new Builder();
1033
- const fn = factory.length > 1 ? assign((template2) => new QueryTemplate(template2, builder), Query.fn) : void 0;
1032
+ const fn = factory.length > 1 ? assign((template) => new QueryTemplate(template, builder), Query.fn) : void 0;
1034
1033
  let result = factory.call(builder, where, fn);
1035
1034
  let args;
1036
1035
  if (typeof result === "function") {
@@ -1040,8 +1039,7 @@ function Query(factory) {
1040
1039
  builder.commit(result);
1041
1040
  if (!connection)
1042
1041
  connection = Connection.None;
1043
- const template = connection.generate(builder);
1044
- const statement = connection.prepare(template);
1042
+ const statement = connection.prepare(builder);
1045
1043
  const runner = (...params) => {
1046
1044
  const get = () => statement.all(params).then((a) => a.map((x) => builder.parse(x)));
1047
1045
  const query = create(Query.prototype);
@@ -1057,7 +1055,7 @@ function Query(factory) {
1057
1055
  if (builder.returns)
1058
1056
  assign(query, {
1059
1057
  get,
1060
- one: async (orFail) => {
1058
+ async one(orFail) {
1061
1059
  const res = await statement.get();
1062
1060
  if (res)
1063
1061
  return builder.parse(res);
package/dist/index.mjs CHANGED
@@ -79,7 +79,6 @@ var BooleanColumn = class extends Field {
79
79
  };
80
80
  function Bool(...opts) {
81
81
  return Bool.Type.new(...opts);
82
- ;
83
82
  }
84
83
  Bool.Type = BooleanColumn;
85
84
 
@@ -782,9 +781,12 @@ var Connection = class {
782
781
  return type;
783
782
  }));
784
783
  }
785
- generate(builder) {
786
- const { generator } = this.constructor;
787
- return new generator(builder).toString();
784
+ query(factory) {
785
+ const connection = this;
786
+ return Query(function(where, fn) {
787
+ where.connection = connection;
788
+ return factory.call(this, where, fn);
789
+ });
788
790
  }
789
791
  async then(onfulfilled, onrejected) {
790
792
  return (this.ready ? Promise.resolve() : this.sync()).then(() => {
@@ -798,7 +800,6 @@ var Connection = class {
798
800
  delete type.connection;
799
801
  }
800
802
  };
801
- Connection.generator = Generator;
802
803
  var NoConnection = class extends Connection {
803
804
  constructor() {
804
805
  super([]);
@@ -806,7 +807,8 @@ var NoConnection = class extends Connection {
806
807
  get schema() {
807
808
  throw new Error("No connection to generate schema.");
808
809
  }
809
- prepare(template) {
810
+ prepare(builder) {
811
+ const template = String(new Generator(builder));
810
812
  async function run() {
811
813
  throw new Error("No connection to run query.");
812
814
  }
@@ -822,9 +824,6 @@ var NoConnection = class extends Connection {
822
824
  async sync() {
823
825
  return this;
824
826
  }
825
- async valid() {
826
- return true;
827
- }
828
827
  async close() {
829
828
  return;
830
829
  }
@@ -951,13 +950,13 @@ var _Table = class _Table {
951
950
  }
952
951
  };
953
952
  _Table.schema = "";
954
- var Table = _Table;
953
+ var Table2 = _Table;
955
954
 
956
955
  // src/query/Query.ts
957
956
  function Query(factory) {
958
957
  let connection = void 0;
959
958
  function where(arg1) {
960
- if (Table.is(arg1)) {
959
+ if (Table2.is(arg1)) {
961
960
  if (!connection)
962
961
  connection = arg1.connection;
963
962
  else if (connection !== arg1.connection)
@@ -991,7 +990,7 @@ function Query(factory) {
991
990
  }
992
991
  });
993
992
  const builder = new Builder();
994
- const fn = factory.length > 1 ? assign((template2) => new QueryTemplate(template2, builder), Query.fn) : void 0;
993
+ const fn = factory.length > 1 ? assign((template) => new QueryTemplate(template, builder), Query.fn) : void 0;
995
994
  let result = factory.call(builder, where, fn);
996
995
  let args;
997
996
  if (typeof result === "function") {
@@ -1001,8 +1000,7 @@ function Query(factory) {
1001
1000
  builder.commit(result);
1002
1001
  if (!connection)
1003
1002
  connection = Connection.None;
1004
- const template = connection.generate(builder);
1005
- const statement = connection.prepare(template);
1003
+ const statement = connection.prepare(builder);
1006
1004
  const runner = (...params) => {
1007
1005
  const get = () => statement.all(params).then((a) => a.map((x) => builder.parse(x)));
1008
1006
  const query = create(Query.prototype);
@@ -1018,7 +1016,7 @@ function Query(factory) {
1018
1016
  if (builder.returns)
1019
1017
  assign(query, {
1020
1018
  get,
1021
- one: async (orFail) => {
1019
+ async one(orFail) {
1022
1020
  const res = await statement.get();
1023
1021
  if (res)
1024
1022
  return builder.parse(res);
@@ -1049,6 +1047,6 @@ export {
1049
1047
  Primary,
1050
1048
  Query,
1051
1049
  Str,
1052
- Table,
1050
+ Table2 as Table,
1053
1051
  Time
1054
1052
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressive/sql",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Query builder and ORM",
5
5
  "license": "ISC",
6
6
  "publishConfig": {