rado 0.2.44 → 0.3.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.
@@ -6,12 +6,10 @@ import { Index, IndexData } from './Index.js';
6
6
  import type { Selection } from './Selection.js';
7
7
  import { SelectFirst } from './query/Select.js';
8
8
  import { TableSelect } from './query/TableSelect.js';
9
- interface TableDefinition {
10
- }
11
9
  export declare class TableData {
12
10
  name: string;
13
11
  alias?: string;
14
- definition: TableDefinition;
12
+ definition: {};
15
13
  columns: Record<string, ColumnData>;
16
14
  meta: () => {
17
15
  indexes: Record<string, IndexData>;
@@ -29,6 +27,10 @@ export declare class TableInstance<Definition> {
29
27
  get [Table.Data](): TableData;
30
28
  }
31
29
  export type Table<Definition> = Definition & TableInstance<Definition>;
30
+ declare class HasIndexes<Indexes extends string | number | symbol> {
31
+ get [Table.Indexes](): Record<Indexes, IndexData>;
32
+ }
33
+ export type IndexedTable<Definition, Indexes extends string | number | symbol> = Table<Definition> & HasIndexes<Indexes>;
32
34
  type TableOf<Row> = Table<{
33
35
  [K in keyof Row as K extends string ? K : never]: Column<Row[K]> & Fields<Row[K]>;
34
36
  }>;
@@ -40,7 +42,7 @@ type UpdateOf<Definition> = Partial<{
40
42
  }>;
41
43
  export declare namespace Table {
42
44
  export const Data: unique symbol;
43
- export const Meta: unique symbol;
45
+ export const Indexes: unique symbol;
44
46
  export type Of<Row> = TableOf<Row>;
45
47
  export type Select<Definition> = RowOf<Definition>;
46
48
  export type Update<Definition> = UpdateOf<Definition>;
@@ -60,8 +62,11 @@ interface Define<T> {
60
62
  }
61
63
  export type table<T> = Table.Select<T extends Table<infer D> ? D : T>;
62
64
  export declare function createTable<Definition>(data: TableData): Table<Definition>;
63
- export declare function table<T extends {}>(define: Record<string, T | Define<T>>): Table<T>;
65
+ export declare function table<T extends {}, Indexes extends Record<string, Index>>(define: {
66
+ [key: string]: T | Define<T>;
67
+ [Table.Indexes]?: (this: T) => Indexes;
68
+ }): {} extends Indexes ? Table<T> : IndexedTable<T, keyof Indexes>;
64
69
  export declare namespace table {
65
- const meta: typeof Table.Meta;
70
+ const indexes: typeof Table.Indexes;
66
71
  }
67
72
  export {};
@@ -23,7 +23,7 @@ class TableData {
23
23
  var Table;
24
24
  ((Table2) => {
25
25
  Table2.Data = Symbol("Table.Data");
26
- Table2.Meta = Symbol("Table.Meta");
26
+ Table2.Indexes = Symbol("Table.Indexes");
27
27
  })(Table || (Table = {}));
28
28
  function createTable(data) {
29
29
  const target = new Target.Table(data);
@@ -61,6 +61,15 @@ function createTable(data) {
61
61
  for (const [key, value] of entries(expressions))
62
62
  defineProperty(call, key, { value, enumerable: true, configurable: true });
63
63
  defineProperty(call, Table.Data, { value: data, enumerable: false });
64
+ let indexes;
65
+ defineProperty(call, Table.Indexes, {
66
+ get() {
67
+ if (indexes)
68
+ return indexes;
69
+ return indexes = data.meta().indexes;
70
+ },
71
+ enumerable: false
72
+ });
64
73
  defineProperty(call, Expr.ToExpr, { value: toExpr, enumerable: false });
65
74
  setPrototypeOf(call, getPrototypeOf(data.definition));
66
75
  return call;
@@ -98,13 +107,13 @@ function table(define) {
98
107
  })
99
108
  ),
100
109
  meta() {
101
- const createMeta = res[table.meta];
102
- const meta = createMeta ? createMeta.apply(res) : {};
110
+ const createIndexes = define[Table.Indexes];
111
+ const indexes = createIndexes ? createIndexes.apply(res) : {};
103
112
  return {
104
113
  indexes: fromEntries(
105
- entries(meta?.indexes || {}).map(([key, index]) => {
114
+ entries(indexes || {}).map(([key, index]) => {
106
115
  const indexName = `${name}.${key}`;
107
- return [indexName, { name: indexName, ...index.data }];
116
+ return [key, { name: indexName, ...index.data }];
108
117
  })
109
118
  )
110
119
  };
@@ -114,7 +123,7 @@ function table(define) {
114
123
  return res;
115
124
  }
116
125
  ((table2) => {
117
- table2.meta = Table.Meta;
126
+ table2.indexes = Table.Indexes;
118
127
  })(table || (table = {}));
119
128
  export {
120
129
  Table,
@@ -1,4 +1,5 @@
1
1
  import type { ExprData } from './Expr.js';
2
+ import { IndexData } from './Index.js';
2
3
  import type { QueryData } from './Query.js';
3
4
  import type { TableData } from './Table.js';
4
5
  export declare enum TargetType {
@@ -30,8 +31,9 @@ export declare namespace Target {
30
31
  }
31
32
  class Table {
32
33
  table: TableData;
34
+ indexedBy?: IndexData | undefined;
33
35
  type: TargetType.Table;
34
- constructor(table: TableData);
36
+ constructor(table: TableData, indexedBy?: IndexData | undefined);
35
37
  }
36
38
  class Join {
37
39
  left: Target;
@@ -33,8 +33,9 @@ var Target;
33
33
  }
34
34
  Target2.Query = Query;
35
35
  class Table {
36
- constructor(table) {
36
+ constructor(table, indexedBy) {
37
37
  this.table = table;
38
+ this.indexedBy = indexedBy;
38
39
  }
39
40
  type = "Target.Table" /* Table */;
40
41
  }
@@ -1,4 +1,5 @@
1
1
  import { EV, Expr } from '../Expr.js';
2
+ import { IndexData } from '../Index.js';
2
3
  import { OrderBy } from '../OrderBy.js';
3
4
  import { Query, QueryData } from '../Query.js';
4
5
  import { Selection } from '../Selection.js';
@@ -9,6 +10,7 @@ export declare class Select<Row> extends Union<Row> {
9
10
  [Query.Data]: QueryData.Select;
10
11
  constructor(query: QueryData.Select);
11
12
  from(table: Table<any> | VirtualTable<any>): Select<Row>;
13
+ indexedBy(index: IndexData): Select<unknown>;
12
14
  leftJoin<C>(that: Table<C> | Select<C>, ...on: Array<EV<boolean>>): Select<Row>;
13
15
  innerJoin<C>(that: Table<C> | Select<C>, ...on: Array<EV<boolean>>): Select<Row>;
14
16
  select<X extends Selection>(selection: X): Select<Selection.Infer<X>>;
@@ -2,7 +2,7 @@ import { Expr, ExprData } from "../Expr.js";
2
2
  import { Functions } from "../Functions.js";
3
3
  import { Query } from "../Query.js";
4
4
  import { Table } from "../Table.js";
5
- import { Target } from "../Target.js";
5
+ import { Target, TargetType } from "../Target.js";
6
6
  import { VirtualTable } from "../VirtualTable.js";
7
7
  import { Union } from "./Union.js";
8
8
  function joinTarget(joinType, query, to, on) {
@@ -33,6 +33,19 @@ class Select extends Union {
33
33
  this[Query.Data].with({ from: virtual?.target || new Target.Table(table) })
34
34
  );
35
35
  }
36
+ indexedBy(index) {
37
+ const from = this[Query.Data].from;
38
+ switch (from?.type) {
39
+ case TargetType.Table:
40
+ return new Select(
41
+ this[Query.Data].with({
42
+ from: new Target.Table(from.table, index)
43
+ })
44
+ );
45
+ default:
46
+ throw new Error("Cannot index by without table target");
47
+ }
48
+ }
36
49
  leftJoin(that, ...on) {
37
50
  const query = this[Query.Data];
38
51
  return new Select(
@@ -398,6 +398,9 @@ class Formatter {
398
398
  stmt.identifier(target.table.name);
399
399
  if (target.table.alias)
400
400
  stmt.add("AS").addIdentifier(target.table.alias);
401
+ if (target.indexedBy) {
402
+ stmt.add("INDEXED BY").addIdentifier(target.indexedBy.name);
403
+ }
401
404
  return stmt;
402
405
  case TargetType.Join:
403
406
  const { left, right, joinType } = target;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.2.44",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",