rado 0.2.43 → 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.
- package/dist/define/Table.d.ts +11 -6
- package/dist/define/Table.js +15 -6
- package/dist/define/Target.d.ts +3 -1
- package/dist/define/Target.js +2 -1
- package/dist/define/query/Delete.d.ts +2 -0
- package/dist/define/query/Delete.js +3 -0
- package/dist/define/query/Select.d.ts +2 -0
- package/dist/define/query/Select.js +14 -1
- package/dist/lib/Formatter.js +5 -2
- package/dist/lib/Statement.d.ts +1 -1
- package/dist/lib/Statement.js +8 -5
- package/package.json +1 -1
package/dist/define/Table.d.ts
CHANGED
|
@@ -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:
|
|
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
|
|
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 {}
|
|
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
|
|
70
|
+
const indexes: typeof Table.Indexes;
|
|
66
71
|
}
|
|
67
72
|
export {};
|
package/dist/define/Table.js
CHANGED
|
@@ -23,7 +23,7 @@ class TableData {
|
|
|
23
23
|
var Table;
|
|
24
24
|
((Table2) => {
|
|
25
25
|
Table2.Data = Symbol("Table.Data");
|
|
26
|
-
Table2.
|
|
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
|
|
102
|
-
const
|
|
110
|
+
const createIndexes = define[Table.Indexes];
|
|
111
|
+
const indexes = createIndexes ? createIndexes.apply(res) : {};
|
|
103
112
|
return {
|
|
104
113
|
indexes: fromEntries(
|
|
105
|
-
entries(
|
|
114
|
+
entries(indexes || {}).map(([key, index]) => {
|
|
106
115
|
const indexName = `${name}.${key}`;
|
|
107
|
-
return [
|
|
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.
|
|
126
|
+
table2.indexes = Table.Indexes;
|
|
118
127
|
})(table || (table = {}));
|
|
119
128
|
export {
|
|
120
129
|
Table,
|
package/dist/define/Target.d.ts
CHANGED
|
@@ -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;
|
package/dist/define/Target.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EV } from '../Expr.js';
|
|
2
|
+
import { OrderBy } from '../OrderBy.js';
|
|
2
3
|
import { Query, QueryData } from '../Query.js';
|
|
3
4
|
export declare class Delete extends Query<{
|
|
4
5
|
rowsAffected: number;
|
|
@@ -6,6 +7,7 @@ export declare class Delete extends Query<{
|
|
|
6
7
|
[Query.Data]: QueryData.Delete;
|
|
7
8
|
constructor(query: QueryData.Delete);
|
|
8
9
|
where(...where: Array<EV<boolean>>): Delete;
|
|
10
|
+
orderBy(...orderBy: Array<OrderBy>): Delete;
|
|
9
11
|
take(limit: number | undefined): Delete;
|
|
10
12
|
skip(offset: number | undefined): Delete;
|
|
11
13
|
}
|
|
@@ -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(
|
package/dist/lib/Formatter.js
CHANGED
|
@@ -177,15 +177,15 @@ class Formatter {
|
|
|
177
177
|
this.formatValue({ ...ctx, formatAsInsert: true }, input);
|
|
178
178
|
}
|
|
179
179
|
this.formatWhere(ctx, query.where);
|
|
180
|
+
this.formatOrderBy(ctx, query.orderBy);
|
|
180
181
|
this.formatLimit(ctx, query);
|
|
181
182
|
return stmt;
|
|
182
183
|
}
|
|
183
184
|
formatDelete(ctx, query) {
|
|
184
185
|
const { stmt } = ctx;
|
|
185
186
|
stmt.add("DELETE FROM").addIdentifier(query.table.name);
|
|
186
|
-
stmt.space();
|
|
187
187
|
this.formatWhere(ctx, query.where);
|
|
188
|
-
|
|
188
|
+
this.formatOrderBy(ctx, query.orderBy);
|
|
189
189
|
this.formatLimit(ctx, query);
|
|
190
190
|
return stmt;
|
|
191
191
|
}
|
|
@@ -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/dist/lib/Statement.d.ts
CHANGED
package/dist/lib/Statement.js
CHANGED
|
@@ -66,9 +66,12 @@ class Statement {
|
|
|
66
66
|
/**
|
|
67
67
|
* Adds a newline
|
|
68
68
|
*/
|
|
69
|
-
newline() {
|
|
69
|
+
newline(spaceRequired = true) {
|
|
70
70
|
if (this.options.skipNewlines)
|
|
71
|
-
|
|
71
|
+
if (spaceRequired)
|
|
72
|
+
return this.space();
|
|
73
|
+
else
|
|
74
|
+
return this;
|
|
72
75
|
return this.raw(NEWLINE + this.currentIndent);
|
|
73
76
|
}
|
|
74
77
|
/**
|
|
@@ -113,13 +116,13 @@ class Statement {
|
|
|
113
116
|
* Opens a parenthesis and indents the next line
|
|
114
117
|
*/
|
|
115
118
|
openParenthesis() {
|
|
116
|
-
return this.raw("(").indent().newline();
|
|
119
|
+
return this.raw("(").indent().newline(false);
|
|
117
120
|
}
|
|
118
121
|
/**
|
|
119
122
|
* Closes a parenthesis and dedents the next line
|
|
120
123
|
*/
|
|
121
124
|
closeParenthesis() {
|
|
122
|
-
return this.dedent().newline().raw(")");
|
|
125
|
+
return this.dedent().newline(false).raw(")");
|
|
123
126
|
}
|
|
124
127
|
/**
|
|
125
128
|
* Adds parenthesis on start and end, and a seperator to the output on
|
|
@@ -138,7 +141,7 @@ class Statement {
|
|
|
138
141
|
*separate(parts, separator = SEPARATE) {
|
|
139
142
|
for (let i = 0; i < parts.length; i++) {
|
|
140
143
|
if (i > 0)
|
|
141
|
-
this.raw(separator).newline();
|
|
144
|
+
this.raw(separator).newline(false);
|
|
142
145
|
yield parts[i];
|
|
143
146
|
}
|
|
144
147
|
}
|