rado 0.1.63 → 0.2.0

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.
@@ -4,6 +4,7 @@ import { Functions } from "./Functions.js";
4
4
  import { Query } from "./Query.js";
5
5
  import { Schema } from "./Schema.js";
6
6
  import { Selection } from "./Selection.js";
7
+ import { createTable, table } from "./Table.js";
7
8
  import { Target } from "./Target.js";
8
9
  var Cursor = class {
9
10
  [Selection.__cursorType]() {
@@ -21,11 +22,18 @@ var Cursor = class {
21
22
  query() {
22
23
  throw new Error("Not implemented");
23
24
  }
25
+ next(cursor) {
26
+ return new Cursor(
27
+ Query.Batch({
28
+ queries: [this.query(), cursor.query()]
29
+ })
30
+ );
31
+ }
24
32
  on(driver) {
25
33
  return driver.executeQuery(this.query());
26
34
  }
27
- compile(driver, options) {
28
- return driver.formatter.compile(this.query(), options);
35
+ toSql(driver, options = { forceInline: true }) {
36
+ return driver.formatter.compile(this.query(), options).sql;
29
37
  }
30
38
  toJSON() {
31
39
  return this.query();
@@ -77,7 +85,7 @@ function addWhere(query, where) {
77
85
  class InsertValuesReturning extends Cursor2 {
78
86
  }
79
87
  Cursor2.InsertValuesReturning = InsertValuesReturning;
80
- class InsertValues extends Cursor2 {
88
+ class Inserted extends Cursor2 {
81
89
  query() {
82
90
  return super.query();
83
91
  }
@@ -87,20 +95,25 @@ function addWhere(query, where) {
87
95
  );
88
96
  }
89
97
  }
90
- Cursor2.InsertValues = InsertValues;
98
+ Cursor2.Inserted = Inserted;
91
99
  class Insert {
92
100
  constructor(into) {
93
101
  this.into = into;
94
102
  }
103
+ selection(query) {
104
+ return new Inserted(
105
+ Query.Insert({ into: this.into, select: query.query() })
106
+ );
107
+ }
95
108
  values(...data) {
96
- return new InsertValues(Query.Insert({ into: this.into, data }));
109
+ return new Inserted(Query.Insert({ into: this.into, data }));
97
110
  }
98
111
  }
99
112
  Cursor2.Insert = Insert;
100
113
  class CreateTable extends Cursor2 {
101
- constructor(table) {
102
- super(Schema.create(table));
103
- this.table = table;
114
+ constructor(table2) {
115
+ super(Schema.create(table2));
116
+ this.table = table2;
104
117
  }
105
118
  }
106
119
  Cursor2.CreateTable = CreateTable;
@@ -109,8 +122,36 @@ function addWhere(query, where) {
109
122
  super(Query.Batch({ queries }));
110
123
  this.queries = queries;
111
124
  }
125
+ query() {
126
+ return super.query();
127
+ }
128
+ next(cursor) {
129
+ return new Cursor2(
130
+ Query.Batch({
131
+ queries: [...this.query().queries, cursor.query()]
132
+ })
133
+ );
134
+ }
112
135
  }
113
136
  Cursor2.Batch = Batch;
137
+ function joinTarget(joinType, query, to, on) {
138
+ const toQuery = to instanceof Cursor2 ? to.query() : void 0;
139
+ const target = toQuery ? toQuery.from : Target.Table(to[table.data]);
140
+ if (!query.from || !target)
141
+ throw new Error("No from clause");
142
+ const conditions = [...on];
143
+ if (toQuery) {
144
+ const where = toQuery.where;
145
+ if (where)
146
+ conditions.push(new Expr(where));
147
+ }
148
+ return Target.Join(
149
+ query.from,
150
+ target,
151
+ joinType,
152
+ Expr.and(...conditions).expr
153
+ );
154
+ }
114
155
  class SelectMultiple extends Cursor2 {
115
156
  query() {
116
157
  return super.query();
@@ -119,24 +160,14 @@ function addWhere(query, where) {
119
160
  const query = this.query();
120
161
  return new SelectMultiple({
121
162
  ...query,
122
- from: Target.Join(
123
- query.from,
124
- Target.Table(that.schema()),
125
- "left",
126
- Expr.and(...on).expr
127
- )
163
+ from: joinTarget("left", query, that, on)
128
164
  });
129
165
  }
130
166
  innerJoin(that, ...on) {
131
167
  const query = this.query();
132
168
  return new SelectMultiple({
133
169
  ...query,
134
- from: Target.Join(
135
- query.from,
136
- Target.Table(that.schema()),
137
- "inner",
138
- Expr.and(...on).expr
139
- )
170
+ from: joinTarget("inner", query, that, on)
140
171
  });
141
172
  }
142
173
  select(selection) {
@@ -185,11 +216,65 @@ function addWhere(query, where) {
185
216
  skip(offset) {
186
217
  return new SelectMultiple({ ...this.query(), offset });
187
218
  }
188
- toExpr() {
219
+ [Expr.toExpr]() {
189
220
  return new Expr(ExprData.Query(this.query()));
190
221
  }
191
222
  }
192
223
  Cursor2.SelectMultiple = SelectMultiple;
224
+ class TableSelect extends SelectMultiple {
225
+ constructor(table2, conditions = []) {
226
+ const target = Target.Table(table2);
227
+ super(
228
+ Query.Select({
229
+ from: target,
230
+ selection: ExprData.Row(target),
231
+ where: Expr.and(...conditions).expr
232
+ })
233
+ );
234
+ this.table = table2;
235
+ }
236
+ as(alias) {
237
+ return createTable({ ...this.table, alias });
238
+ }
239
+ create() {
240
+ return new Cursor2.CreateTable(this.table);
241
+ }
242
+ insertSelect(query) {
243
+ return new Cursor2.Inserted(
244
+ Query.Insert({ into: this.table, select: query.query() })
245
+ );
246
+ }
247
+ insertOne(record) {
248
+ return new Cursor2(
249
+ Query.Insert({
250
+ into: this.table,
251
+ data: [record],
252
+ selection: ExprData.Row(Target.Table(this.table)),
253
+ singleResult: true
254
+ })
255
+ );
256
+ }
257
+ insertAll(data) {
258
+ return new Cursor2.Insert(this.table).values(...data);
259
+ }
260
+ set(data) {
261
+ return new Cursor2.Update(
262
+ Query.Update({
263
+ table: this.table,
264
+ where: this.query().where
265
+ })
266
+ ).set(data);
267
+ }
268
+ delete() {
269
+ return new Cursor2.Delete(
270
+ Query.Delete({
271
+ table: this.table,
272
+ where: this.query().where
273
+ })
274
+ );
275
+ }
276
+ }
277
+ Cursor2.TableSelect = TableSelect;
193
278
  class SelectSingle extends Cursor2 {
194
279
  query() {
195
280
  return super.query();
@@ -198,24 +283,14 @@ function addWhere(query, where) {
198
283
  const query = this.query();
199
284
  return new SelectSingle({
200
285
  ...query,
201
- from: Target.Join(
202
- query.from,
203
- Target.Table(that.schema()),
204
- "left",
205
- Expr.and(...on).expr
206
- )
286
+ from: joinTarget("left", query, that, on)
207
287
  });
208
288
  }
209
289
  innerJoin(that, ...on) {
210
290
  const query = this.query();
211
291
  return new SelectSingle({
212
292
  ...query,
213
- from: Target.Join(
214
- query.from,
215
- Target.Table(that.schema()),
216
- "inner",
217
- Expr.and(...on).expr
218
- )
293
+ from: joinTarget("inner", query, that, on)
219
294
  });
220
295
  }
221
296
  select(selection) {
@@ -248,7 +323,7 @@ function addWhere(query, where) {
248
323
  all() {
249
324
  return new SelectMultiple({ ...this.query(), singleResult: false });
250
325
  }
251
- toExpr() {
326
+ [Expr.toExpr]() {
252
327
  return new Expr(ExprData.Query(this.query()));
253
328
  }
254
329
  }
@@ -128,7 +128,8 @@ export declare const ExprData: {
128
128
  export type EV<T> = Expr<T> | T;
129
129
  export declare class Expr<T> {
130
130
  expr: ExprData;
131
- static NULL: ExprData;
131
+ static NULL: Expr<null>;
132
+ static toExpr: symbol;
132
133
  static value<T>(value: T): Expr<T>;
133
134
  static create<T>(input: EV<T>): Expr<T>;
134
135
  static and(...conditions: Array<EV<boolean>>): Expr<boolean>;
@@ -139,27 +140,28 @@ export declare class Expr<T> {
139
140
  not(): Expr<boolean>;
140
141
  or(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
141
142
  and(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
143
+ is(that: EV<T> | Cursor.SelectSingle<T>): Expr<boolean>;
144
+ isNot(that: EV<T>): Expr<boolean>;
142
145
  isNull(): Expr<boolean>;
143
146
  isNotNull(): Expr<boolean>;
144
- isNot(that: EV<T>): Expr<boolean>;
145
- is(that: EV<T> | Cursor.SelectSingle<T>): Expr<boolean>;
146
- in(that: EV<Array<T>> | Cursor.SelectMultiple<T>): Expr<boolean>;
147
- notIn(that: EV<Array<T>> | Cursor.SelectMultiple<T>): Expr<boolean>;
147
+ isIn(that: EV<Array<T>> | Cursor.SelectMultiple<T>): Expr<boolean>;
148
+ isNotIn(that: EV<Array<T>> | Cursor.SelectMultiple<T>): Expr<boolean>;
149
+ isGreater(that: EV<any>): Expr<boolean>;
150
+ isGreaterOrEqual(that: EV<any>): Expr<boolean>;
151
+ isLess(that: EV<any>): Expr<boolean>;
152
+ isLessOrEqual(that: EV<any>): Expr<boolean>;
148
153
  add(this: Expr<number>, that: EV<number>): Expr<number>;
149
154
  substract(this: Expr<number>, that: EV<number>): Expr<number>;
150
155
  multiply(this: Expr<number>, that: EV<number>): Expr<number>;
151
156
  remainder(this: Expr<number>, that: EV<number>): Expr<number>;
152
157
  divide(this: Expr<number>, that: EV<number>): Expr<number>;
153
- greater(that: EV<any>): Expr<boolean>;
154
- greaterOrEqual(that: EV<any>): Expr<boolean>;
155
- less(that: EV<any>): Expr<boolean>;
156
- lessOrEqual(that: EV<any>): Expr<boolean>;
157
158
  concat(this: Expr<string>, that: EV<string>): Expr<string>;
158
159
  like(this: Expr<string>, that: EV<string>): Expr<boolean>;
159
160
  glob(this: Expr<string>, that: EV<string>): Expr<boolean>;
160
161
  match(this: Expr<string>, that: EV<string>): Expr<boolean>;
161
162
  with<X extends Selection>(that: X): Selection.With<T, X>;
162
163
  at<T>(this: Expr<Array<T>>, index: number): Expr<T | null>;
164
+ includes<T>(this: Expr<Array<T>>, value: EV<T>): Expr<boolean>;
163
165
  filter<T>(this: Expr<Array<T>>, fn: (cursor: Fields<T>) => Expr<boolean>): Expr<Array<T>>;
164
166
  map<T, X extends Selection>(this: Expr<Array<T>>, fn: (cursor: Fields<T>) => X): Expr<Array<Selection.Infer<X>>>;
165
167
  sure(): Expr<NonNullable<T>>;
@@ -91,8 +91,8 @@ var ExprData = {
91
91
  create(input) {
92
92
  if (input === null || input === void 0)
93
93
  return ExprData.Param(ParamData.Value(null));
94
- if (input && typeof input === "object" && typeof input.toExpr === "function")
95
- input = input.toExpr();
94
+ if (input && typeof input[Expr.toExpr] === "function")
95
+ input = input[Expr.toExpr]();
96
96
  if (input instanceof Expr)
97
97
  return input.expr;
98
98
  if (input && typeof input === "object" && !Array.isArray(input))
@@ -151,7 +151,7 @@ var _Expr = class {
151
151
  return { expr: this.expr, order: OrderDirection.Desc };
152
152
  }
153
153
  not() {
154
- return unop(this, "Not" /* Not */);
154
+ return new _Expr(ExprData.UnOp("Not" /* Not */, this.expr));
155
155
  }
156
156
  or(that) {
157
157
  const a = this.expr;
@@ -171,66 +171,72 @@ var _Expr = class {
171
171
  return new _Expr(b);
172
172
  return new _Expr(ExprData.BinOp("And" /* And */, a, b));
173
173
  }
174
+ is(that) {
175
+ if (that === null || that instanceof _Expr && isConstant(that.expr, null))
176
+ return this.isNull();
177
+ return new _Expr(ExprData.BinOp("Equals" /* Equals */, this.expr, toExpr(that)));
178
+ }
179
+ isNot(that) {
180
+ if (that === null || that instanceof _Expr && isConstant(that.expr, null))
181
+ return this.isNotNull();
182
+ return new _Expr(
183
+ ExprData.BinOp("NotEquals" /* NotEquals */, this.expr, toExpr(that))
184
+ );
185
+ }
174
186
  isNull() {
175
- return unop(this, "IsNull" /* IsNull */);
187
+ return new _Expr(ExprData.UnOp("IsNull" /* IsNull */, this.expr));
176
188
  }
177
189
  isNotNull() {
178
190
  return this.isNull().not();
179
191
  }
180
- isNot(that) {
181
- if (that === null || that instanceof _Expr && isConstant(that.expr, null))
182
- return this.isNotNull();
183
- return binop(this, "NotEquals" /* NotEquals */, that);
192
+ isIn(that) {
193
+ return new _Expr(ExprData.BinOp("In" /* In */, this.expr, toExpr(that)));
184
194
  }
185
- is(that) {
186
- if (that === null || that instanceof _Expr && isConstant(that.expr, null))
187
- return this.isNull();
188
- return binop(this, "Equals" /* Equals */, that);
195
+ isNotIn(that) {
196
+ return new _Expr(ExprData.BinOp("NotIn" /* NotIn */, this.expr, toExpr(that)));
197
+ }
198
+ isGreater(that) {
199
+ return new _Expr(ExprData.BinOp("Greater" /* Greater */, this.expr, toExpr(that)));
189
200
  }
190
- in(that) {
191
- return binop(this, "In" /* In */, that);
201
+ isGreaterOrEqual(that) {
202
+ return new _Expr(
203
+ ExprData.BinOp("GreaterOrEqual" /* GreaterOrEqual */, this.expr, toExpr(that))
204
+ );
192
205
  }
193
- notIn(that) {
194
- return binop(this, "NotIn" /* NotIn */, that);
206
+ isLess(that) {
207
+ return new _Expr(ExprData.BinOp("Less" /* Less */, this.expr, toExpr(that)));
208
+ }
209
+ isLessOrEqual(that) {
210
+ return new _Expr(
211
+ ExprData.BinOp("LessOrEqual" /* LessOrEqual */, this.expr, toExpr(that))
212
+ );
195
213
  }
196
214
  add(that) {
197
- return binop(this, "Add" /* Add */, that);
215
+ return new _Expr(ExprData.BinOp("Add" /* Add */, this.expr, toExpr(that)));
198
216
  }
199
217
  substract(that) {
200
- return binop(this, "Subt" /* Subt */, that);
218
+ return new _Expr(ExprData.BinOp("Subt" /* Subt */, this.expr, toExpr(that)));
201
219
  }
202
220
  multiply(that) {
203
- return binop(this, "Mult" /* Mult */, that);
221
+ return new _Expr(ExprData.BinOp("Mult" /* Mult */, this.expr, toExpr(that)));
204
222
  }
205
223
  remainder(that) {
206
- return binop(this, "Mod" /* Mod */, that);
224
+ return new _Expr(ExprData.BinOp("Mod" /* Mod */, this.expr, toExpr(that)));
207
225
  }
208
226
  divide(that) {
209
- return binop(this, "Div" /* Div */, that);
210
- }
211
- greater(that) {
212
- return binop(this, "Greater" /* Greater */, that);
213
- }
214
- greaterOrEqual(that) {
215
- return binop(this, "GreaterOrEqual" /* GreaterOrEqual */, that);
216
- }
217
- less(that) {
218
- return binop(this, "Less" /* Less */, that);
219
- }
220
- lessOrEqual(that) {
221
- return binop(this, "LessOrEqual" /* LessOrEqual */, that);
227
+ return new _Expr(ExprData.BinOp("Div" /* Div */, this.expr, toExpr(that)));
222
228
  }
223
229
  concat(that) {
224
- return binop(this, "Concat" /* Concat */, that);
230
+ return new _Expr(ExprData.BinOp("Concat" /* Concat */, this.expr, toExpr(that)));
225
231
  }
226
232
  like(that) {
227
- return binop(this, "Like" /* Like */, that);
233
+ return new _Expr(ExprData.BinOp("Like" /* Like */, this.expr, toExpr(that)));
228
234
  }
229
235
  glob(that) {
230
- return binop(this, "Glob" /* Glob */, that);
236
+ return new _Expr(ExprData.BinOp("Glob" /* Glob */, this.expr, toExpr(that)));
231
237
  }
232
238
  match(that) {
233
- return binop(this, "Match" /* Match */, that);
239
+ return new _Expr(ExprData.BinOp("Match" /* Match */, this.expr, toExpr(that)));
234
240
  }
235
241
  with(that) {
236
242
  return new _Expr(
@@ -240,6 +246,9 @@ var _Expr = class {
240
246
  at(index) {
241
247
  return this.get(`[${Number(index)}]`);
242
248
  }
249
+ includes(value) {
250
+ return _Expr.create(value).isIn(this);
251
+ }
243
252
  filter(fn) {
244
253
  const alias = `__${Math.random().toString(36).slice(2, 9)}`;
245
254
  const target = Target.Expr(this.expr, alias);
@@ -268,13 +277,8 @@ var _Expr = class {
268
277
  }
269
278
  };
270
279
  var Expr = _Expr;
271
- __publicField(Expr, "NULL", toExpr(null));
272
- function unop(self, type) {
273
- return new Expr(ExprData.UnOp(type, self.expr));
274
- }
275
- function binop(self, type, that) {
276
- return new Expr(ExprData.BinOp(type, self.expr, toExpr(that)));
277
- }
280
+ __publicField(Expr, "NULL", _Expr.create(null));
281
+ __publicField(Expr, "toExpr", Symbol("toExpr"));
278
282
  export {
279
283
  BinOpType,
280
284
  Expr,
@@ -1,10 +1,10 @@
1
- import { Column, PrimaryKey } from './Column';
1
+ import { OptionalColumn, PrimaryColumn, PrimaryKey } from './Column';
2
2
  import { Expr } from './Expr';
3
3
  type ObjectUnion<T> = {
4
4
  [K in T extends infer P ? keyof P : never]: T extends infer P ? K extends keyof P ? P[K] : never : never;
5
5
  };
6
6
  type RecordField<T> = Expr<T> & FieldsOf<ObjectUnion<T>>;
7
- type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [Column.IsPrimary<infer V, infer K>] ? Expr<PrimaryKey<V, K>> : [T] extends [Column.IsOptional<infer V>] ? Field<V> : [T] extends [number | string | boolean] ? Expr<T> : [T] extends [Record<string, any> | null] ? RecordField<T> : Expr<T>;
7
+ type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [PrimaryColumn<infer V, infer K>] ? Expr<PrimaryKey<V, K>> : [T] extends [OptionalColumn<infer V>] ? Field<V> : [T] extends [number | string | boolean] ? Expr<T> : [T] extends [Record<string, any> | null] ? RecordField<T> : Expr<T>;
8
8
  type FieldsOf<Row> = Row extends Record<string, any> ? {
9
9
  [K in keyof Row]-?: Field<Row[K]>;
10
10
  } : never;
@@ -1,5 +1,7 @@
1
1
  import { Cursor } from './Cursor';
2
+ import { Selection } from './Selection';
2
3
  import { Table } from './Table';
4
+ export declare function select<X extends Selection>(selection: X): Cursor.SelectMultiple<Selection.Infer<X>>;
3
5
  export declare function from<Row>(source: Table<Row> | Cursor.SelectMultiple<Row>): Cursor.SelectMultiple<Row>;
4
6
  export declare function update<Row>(table: Table<Row>): Cursor.Update<Row>;
5
7
  export declare function insertInto<Row>(table: Table<Row>): Cursor.Insert<Row>;
@@ -3,9 +3,17 @@ import { Cursor } from "./Cursor.js";
3
3
  import { ExprData } from "./Expr.js";
4
4
  import { Query } from "./Query.js";
5
5
  import { Schema } from "./Schema.js";
6
+ import { table as createTable } from "./Table.js";
6
7
  import { Target } from "./Target.js";
8
+ function select(selection) {
9
+ return new Cursor.SelectMultiple(
10
+ Query.Select({
11
+ selection: ExprData.create(selection)
12
+ })
13
+ );
14
+ }
7
15
  function from(source) {
8
- const target = "schema" in source ? Target.Table(source.schema()) : Target.Query(source.query());
16
+ const target = source instanceof Cursor ? Target.Query(source.query()) : Target.Table(source[createTable.data]);
9
17
  return new Cursor.SelectMultiple(
10
18
  Query.Select({
11
19
  from: target,
@@ -14,17 +22,17 @@ function from(source) {
14
22
  );
15
23
  }
16
24
  function update(table) {
17
- return new Cursor.Update(Query.Update({ table: table.schema() }));
25
+ return new Cursor.Update(Query.Update({ table: table[createTable.data] }));
18
26
  }
19
27
  function insertInto(table) {
20
- return new Cursor.Insert(table.schema());
28
+ return new Cursor.Insert(table[createTable.data]);
21
29
  }
22
30
  function deleteFrom(table) {
23
- return new Cursor.Delete(Query.Delete({ table: table.schema() }));
31
+ return new Cursor.Delete(Query.Delete({ table: table[createTable.data] }));
24
32
  }
25
33
  function create(...tables) {
26
34
  return new Cursor.Batch(
27
- tables.flatMap((table) => Schema.create(table.schema()).queries)
35
+ tables.flatMap((table) => Schema.create(table[createTable.data]).queries)
28
36
  );
29
37
  }
30
38
  export {
@@ -32,5 +40,6 @@ export {
32
40
  deleteFrom,
33
41
  from,
34
42
  insertInto,
43
+ select,
35
44
  update
36
45
  };
@@ -2,7 +2,7 @@ import { ColumnData } from './Column';
2
2
  import { ExprData } from './Expr';
3
3
  import { IndexData } from './Index';
4
4
  import { OrderBy } from './OrderBy';
5
- import { Schema } from './Schema';
5
+ import { TableData } from './Table';
6
6
  import { Target } from './Target';
7
7
  export declare enum QueryType {
8
8
  Insert = "Insert",
@@ -10,14 +10,15 @@ export declare enum QueryType {
10
10
  Update = "Update",
11
11
  Delete = "Delete",
12
12
  CreateTable = "CreateTable",
13
+ AlterTable = "AlterTable",
14
+ DropTable = "DropTable",
13
15
  CreateIndex = "CreateIndex",
14
16
  DropIndex = "DropIndex",
15
- AlterTable = "AlterTable",
16
17
  Batch = "Batch",
17
18
  Transaction = "Transaction",
18
19
  Raw = "Raw"
19
20
  }
20
- export type Query<T = any> = Query.Insert | Query.Select | Query.Update | Query.Delete | Query.CreateTable | Query.CreateIndex | Query.DropIndex | Query.AlterTable | Query.Batch | Query.Transaction | Query.Raw;
21
+ export type Query<T = any> = Query.Insert | Query.Select | Query.Update | Query.Delete | Query.CreateTable | Query.AlterTable | Query.DropTable | Query.CreateIndex | Query.DropIndex | Query.Batch | Query.Transaction | Query.Raw;
21
22
  export declare namespace Query {
22
23
  interface QueryBase {
23
24
  limit?: number;
@@ -33,43 +34,65 @@ export declare namespace Query {
33
34
  }
34
35
  interface Insert extends QueryBase {
35
36
  type: QueryType.Insert;
36
- into: Schema;
37
- data: Array<any>;
37
+ into: TableData;
38
+ data?: Array<any>;
39
+ select?: Query.Select;
38
40
  }
39
41
  function Insert(insert: Omit<Insert, 'type'>): Query.Insert;
40
42
  interface Select extends QueryBase {
41
43
  type: QueryType.Select;
42
44
  selection: ExprData;
43
- from: Target;
45
+ from?: Target;
44
46
  }
45
47
  function Select(select: Omit<Select, 'type'>): Query.Select;
46
48
  interface Update extends QueryBase {
47
49
  type: QueryType.Update;
48
- table: Schema;
50
+ table: TableData;
49
51
  set?: Record<string, any>;
50
52
  }
51
53
  function Update(update: Omit<Update, 'type'>): Query.Update;
52
54
  interface Delete extends QueryBase {
53
55
  type: QueryType.Delete;
54
- table: Schema;
56
+ table: TableData;
55
57
  }
56
58
  function Delete(del: Omit<Delete, 'type'>): Query.Delete;
57
59
  interface CreateTable extends QueryBase {
58
60
  type: QueryType.CreateTable;
59
- table: Schema;
61
+ table: TableData;
60
62
  ifNotExists?: boolean;
61
63
  }
62
64
  function CreateTable(create: Omit<CreateTable, 'type'>): Query.CreateTable;
65
+ interface AlterTable extends QueryBase {
66
+ type: QueryType.AlterTable;
67
+ table: TableData;
68
+ alterColumn?: ColumnData;
69
+ renameColumn?: {
70
+ from: string;
71
+ to: string;
72
+ };
73
+ addColumn?: ColumnData;
74
+ dropColumn?: string;
75
+ renameTable?: {
76
+ from: string;
77
+ };
78
+ }
79
+ function AlterTable(alter: Omit<AlterTable, 'type'>): Query.AlterTable;
80
+ interface DropTable extends QueryBase {
81
+ type: QueryType.DropTable;
82
+ table: TableData;
83
+ ifExists?: boolean;
84
+ }
85
+ function DropTable(drop: Omit<DropTable, 'type'>): Query.DropTable;
63
86
  interface CreateIndex extends QueryBase {
64
87
  type: QueryType.CreateIndex;
65
- table: Schema;
88
+ table: TableData;
66
89
  index: IndexData;
67
90
  ifNotExists?: boolean;
68
91
  }
69
92
  function CreateIndex(create: Omit<CreateIndex, 'type'>): Query.CreateIndex;
70
93
  interface DropIndex extends QueryBase {
71
94
  type: QueryType.DropIndex;
72
- table: Schema;
95
+ table: TableData;
73
96
  name: string;
74
97
  ifExists?: boolean;
75
98
  }
@@ -98,12 +121,4 @@ export declare namespace Query {
98
121
  params: Array<any>;
99
122
  }
100
123
  function Raw(raw: Omit<Raw, 'type'>): Query.Raw;
101
- interface AlterTable extends QueryBase {
102
- type: QueryType.AlterTable;
103
- table: Schema;
104
- alterColumn?: ColumnData;
105
- addColumn?: ColumnData;
106
- dropColumn?: string;
107
- }
108
- function AlterTable(alter: Omit<AlterTable, 'type'>): Query.AlterTable;
109
124
  }
@@ -5,9 +5,10 @@ var QueryType = /* @__PURE__ */ ((QueryType2) => {
5
5
  QueryType2["Update"] = "Update";
6
6
  QueryType2["Delete"] = "Delete";
7
7
  QueryType2["CreateTable"] = "CreateTable";
8
+ QueryType2["AlterTable"] = "AlterTable";
9
+ QueryType2["DropTable"] = "DropTable";
8
10
  QueryType2["CreateIndex"] = "CreateIndex";
9
11
  QueryType2["DropIndex"] = "DropIndex";
10
- QueryType2["AlterTable"] = "AlterTable";
11
12
  QueryType2["Batch"] = "Batch";
12
13
  QueryType2["Transaction"] = "Transaction";
13
14
  QueryType2["Raw"] = "Raw";
@@ -35,6 +36,14 @@ var Query;
35
36
  return { type: "CreateTable" /* CreateTable */, ...create };
36
37
  }
37
38
  Query2.CreateTable = CreateTable;
39
+ function AlterTable(alter) {
40
+ return { type: "AlterTable" /* AlterTable */, ...alter };
41
+ }
42
+ Query2.AlterTable = AlterTable;
43
+ function DropTable(drop) {
44
+ return { type: "DropTable" /* DropTable */, ...drop };
45
+ }
46
+ Query2.DropTable = DropTable;
38
47
  function CreateIndex(create) {
39
48
  return { type: "CreateIndex" /* CreateIndex */, ...create };
40
49
  }
@@ -63,10 +72,6 @@ var Query;
63
72
  return { type: "Raw" /* Raw */, ...raw };
64
73
  }
65
74
  Query2.Raw = Raw;
66
- function AlterTable(alter) {
67
- return { type: "AlterTable" /* AlterTable */, ...alter };
68
- }
69
- Query2.AlterTable = AlterTable;
70
75
  })(Query || (Query = {}));
71
76
  export {
72
77
  Query,
@@ -1,18 +1,11 @@
1
1
  import { Formatter } from '../lib/Formatter';
2
- import { ColumnData } from './Column';
3
- import { IndexData } from './Index';
4
2
  import { Query } from './Query';
5
- export interface Schema {
6
- name: string;
7
- alias?: string;
8
- columns: Record<string, ColumnData>;
9
- indexes: Record<string, IndexData>;
10
- }
3
+ import { TableData } from './Table';
11
4
  export interface SchemaInstructions {
12
5
  columns: Record<string, string>;
13
6
  indexes: Record<string, string>;
14
7
  }
15
8
  export declare namespace Schema {
16
- function create(schema: Schema): Query.Batch;
17
- function upgrade(formatter: Formatter, local: SchemaInstructions, schema: Schema): Array<Query>;
9
+ function create(schema: TableData): Query.Batch;
10
+ function upgrade(formatter: Formatter, local: SchemaInstructions, schema: TableData): Array<Query>;
18
11
  }