rado 0.3.3 → 0.3.5

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.
@@ -1,6 +1,6 @@
1
1
  import { Callable } from '../util/Callable.js';
2
2
  import { Column, ColumnData, OptionalColumn, PrimaryColumn } from './Column.js';
3
- import { EV, Expr } from './Expr.js';
3
+ import { EV, Expr, ExprData } from './Expr.js';
4
4
  import type { Fields } from './Fields.js';
5
5
  import { Index, IndexData } from './Index.js';
6
6
  import { SelectFirst, TableSelect } from './Query.js';
@@ -11,6 +11,7 @@ export declare class TableData {
11
11
  definition: {};
12
12
  columns: Record<string, ColumnData>;
13
13
  meta: () => {
14
+ primaryKey?: Array<ExprData>;
14
15
  indexes: Record<string, IndexData>;
15
16
  };
16
17
  constructor(data: TableData);
@@ -42,6 +43,7 @@ type UpdateOf<Definition> = Partial<{
42
43
  export declare namespace Table {
43
44
  export const Data: unique symbol;
44
45
  export const Indexes: unique symbol;
46
+ export const PrimaryKey: unique symbol;
45
47
  export type Of<Row> = TableOf<Row>;
46
48
  export type Select<Definition> = RowOf<Definition>;
47
49
  export type Update<Definition> = UpdateOf<Definition>;
@@ -64,8 +66,10 @@ export declare function createTable<Definition>(data: TableData): Table<Definiti
64
66
  export declare function table<T extends {}, Indexes extends Record<string, Index>>(define: {
65
67
  [key: string]: T | Define<T>;
66
68
  [Table.Indexes]?: (this: T) => Indexes;
69
+ [Table.PrimaryKey]?: (this: T) => Array<Column<any>>;
67
70
  }): {} extends Indexes ? Table<T> : IndexedTable<T, keyof Indexes>;
68
71
  export declare namespace table {
69
72
  const indexes: typeof Table.Indexes;
73
+ const primaryKey: typeof Table.PrimaryKey;
70
74
  }
71
75
  export {};
@@ -24,6 +24,7 @@ var Table;
24
24
  ((Table2) => {
25
25
  Table2.Data = Symbol("Table.Data");
26
26
  Table2.Indexes = Symbol("Table.Indexes");
27
+ Table2.PrimaryKey = Symbol("Table.PrimaryKey");
27
28
  })(Table || (Table = {}));
28
29
  function createTable(data) {
29
30
  const target = new Target.Table(data);
@@ -109,7 +110,10 @@ function table(define) {
109
110
  meta() {
110
111
  const createIndexes = define[Table.Indexes];
111
112
  const indexes = createIndexes ? createIndexes.apply(res) : {};
113
+ const createPrimaryKey = define[Table.PrimaryKey];
114
+ const primaryKey = createPrimaryKey ? createPrimaryKey.apply(res).map(ExprData.create) : void 0;
112
115
  return {
116
+ primaryKey,
113
117
  indexes: fromEntries(
114
118
  entries(indexes || {}).map(([key, index]) => {
115
119
  const indexName = `${name}.${key}`;
@@ -124,6 +128,7 @@ function table(define) {
124
128
  }
125
129
  ((table2) => {
126
130
  table2.indexes = Table.Indexes;
131
+ table2.primaryKey = Table.PrimaryKey;
127
132
  })(table || (table = {}));
128
133
  export {
129
134
  Table,
@@ -201,10 +201,24 @@ class Formatter {
201
201
  stmt.add("CREATE TABLE");
202
202
  if (query.ifNotExists)
203
203
  stmt.add("IF NOT EXISTS");
204
- stmt.addIdentifier(query.table.name);
205
- for (const column of stmt.call(Object.values(query.table.columns))) {
204
+ stmt.addIdentifier(query.table.name).space();
205
+ stmt.openParenthesis();
206
+ for (const column of stmt.separate(Object.values(query.table.columns))) {
206
207
  this.formatColumn(ctx, column);
207
208
  }
209
+ const meta = query.table.meta();
210
+ if (meta.primaryKey) {
211
+ stmt.raw(",").newline().raw("PRIMARY KEY").space();
212
+ for (const expr of stmt.call(meta.primaryKey))
213
+ switch (expr.type) {
214
+ case ExprType.Field:
215
+ stmt.identifier(expr.field);
216
+ continue;
217
+ default:
218
+ throw new Error(`Cannot format index of type ${expr.type}`);
219
+ }
220
+ }
221
+ stmt.closeParenthesis();
208
222
  return stmt;
209
223
  }
210
224
  formatCreateIndex(ctx, query) {
@@ -475,11 +489,11 @@ class Formatter {
475
489
  return stmt;
476
490
  }
477
491
  formatLimit(ctx, { limit, offset, singleResult }) {
478
- const { stmt, forceInline } = ctx;
492
+ const { stmt } = ctx;
479
493
  if (!limit && !offset && !singleResult)
480
494
  return stmt;
481
495
  stmt.addLine("LIMIT").space();
482
- this.formatValue(ctx, singleResult ? 1 : limit);
496
+ this.formatValue(ctx, (singleResult ? 1 : limit) || -1);
483
497
  if (offset && offset > 0) {
484
498
  stmt.add("OFFSET").space();
485
499
  this.formatValue(ctx, offset);
@@ -593,7 +607,7 @@ class Formatter {
593
607
  case UnOpType.IsNull:
594
608
  return this.formatExprValue(ctx, expr.expr).add("IS NULL");
595
609
  case UnOpType.Not:
596
- stmt.raw("NOT").openParenthesis();
610
+ stmt.raw("NOT").space().openParenthesis();
597
611
  this.formatExprValue(ctx, expr.expr);
598
612
  return stmt.closeParenthesis();
599
613
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",