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.
- package/README.md +269 -0
- package/dist/define/Column.d.ts +18 -18
- package/dist/define/Column.js +17 -5
- package/dist/define/Cursor.d.ts +48 -37
- package/dist/define/Cursor.js +109 -34
- package/dist/define/Expr.d.ts +11 -9
- package/dist/define/Expr.js +48 -44
- package/dist/define/Fields.d.ts +2 -2
- package/dist/define/Ops.d.ts +2 -0
- package/dist/define/Ops.js +14 -5
- package/dist/define/Query.d.ts +34 -19
- package/dist/define/Query.js +10 -5
- package/dist/define/Schema.d.ts +3 -10
- package/dist/define/Schema.js +51 -6
- package/dist/define/Selection.d.ts +3 -3
- package/dist/define/Table.d.ts +56 -51
- package/dist/define/Table.js +146 -103
- package/dist/define/Target.d.ts +4 -4
- package/dist/lib/Callable.d.ts +3 -0
- package/dist/lib/Callable.js +14 -0
- package/dist/lib/Driver.js +5 -4
- package/dist/lib/Formatter.d.ts +3 -0
- package/dist/lib/Formatter.js +71 -27
- package/package.json +1 -1
package/dist/define/Schema.js
CHANGED
|
@@ -1,25 +1,63 @@
|
|
|
1
1
|
// src/define/Schema.ts
|
|
2
2
|
import { Statement } from "../lib/Statement.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
+
import { Query, QueryType } from "./Query.js";
|
|
5
|
+
import { Target } from "./Target.js";
|
|
4
6
|
var Schema;
|
|
5
7
|
((Schema2) => {
|
|
6
8
|
function create(schema) {
|
|
7
9
|
const queries = [];
|
|
8
10
|
queries.push(Query.CreateTable({ table: schema, ifNotExists: true }));
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
const meta = schema.meta();
|
|
12
|
+
if (meta.indexes)
|
|
13
|
+
for (const index of Object.values(meta.indexes))
|
|
14
|
+
queries.push(
|
|
15
|
+
Query.CreateIndex({ table: schema, index, ifNotExists: true })
|
|
16
|
+
);
|
|
11
17
|
return Query.Batch({ queries });
|
|
12
18
|
}
|
|
13
19
|
Schema2.create = create;
|
|
14
20
|
function removeLeadingWhitespace(str) {
|
|
15
21
|
return str.replace(/\n\s+/g, "\n");
|
|
16
22
|
}
|
|
23
|
+
function recreateTable(table, addedColumns) {
|
|
24
|
+
const queries = [];
|
|
25
|
+
const tempTable = { ...table, name: `$$new_${table.name}` };
|
|
26
|
+
queries.push(Query.CreateTable({ table: tempTable }));
|
|
27
|
+
queries.push(
|
|
28
|
+
Query.Insert({
|
|
29
|
+
into: tempTable,
|
|
30
|
+
select: Query.Select({
|
|
31
|
+
from: Target.Table(table),
|
|
32
|
+
selection: ExprData.Record(
|
|
33
|
+
Object.fromEntries(
|
|
34
|
+
Object.entries(table.columns).map(([key, column]) => [
|
|
35
|
+
key,
|
|
36
|
+
addedColumns.has(key) ? (typeof column.defaultValue === "function" ? column.defaultValue() : column.defaultValue) || Expr.NULL.expr : ExprData.Field(ExprData.Row(Target.Table(table)), key)
|
|
37
|
+
])
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
queries.push(Query.DropTable({ table, ifExists: true }));
|
|
44
|
+
queries.push(
|
|
45
|
+
Query.AlterTable({
|
|
46
|
+
table,
|
|
47
|
+
renameTable: { from: tempTable.name }
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
for (const index of Object.values(table.meta().indexes))
|
|
51
|
+
queries.push(Query.CreateIndex({ table, index }));
|
|
52
|
+
return queries;
|
|
53
|
+
}
|
|
17
54
|
function upgrade(formatter, local, schema) {
|
|
18
55
|
const columnNames = /* @__PURE__ */ new Set([
|
|
19
56
|
...Object.keys(local.columns),
|
|
20
57
|
...Object.keys(schema.columns)
|
|
21
58
|
]);
|
|
22
59
|
const res = [];
|
|
60
|
+
let recreate = false;
|
|
23
61
|
for (const columnName of columnNames) {
|
|
24
62
|
const localInstruction = local.columns[columnName];
|
|
25
63
|
const schemaCol = schema.columns[columnName];
|
|
@@ -33,17 +71,24 @@ var Schema;
|
|
|
33
71
|
{ ...schemaCol, references: void 0 }
|
|
34
72
|
);
|
|
35
73
|
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
36
|
-
|
|
74
|
+
recreate = true;
|
|
37
75
|
}
|
|
38
76
|
}
|
|
39
77
|
}
|
|
78
|
+
if (recreate) {
|
|
79
|
+
const added = res.filter(
|
|
80
|
+
(query) => query.type === QueryType.AlterTable && Boolean(query.addColumn)
|
|
81
|
+
).map((query) => query.addColumn.name);
|
|
82
|
+
return recreateTable(schema, new Set(added));
|
|
83
|
+
}
|
|
84
|
+
const meta = schema.meta();
|
|
40
85
|
const indexNames = /* @__PURE__ */ new Set([
|
|
41
86
|
...Object.keys(local.indexes),
|
|
42
|
-
...Object.keys(
|
|
87
|
+
...Object.keys(meta.indexes)
|
|
43
88
|
]);
|
|
44
89
|
for (const indexName of indexNames) {
|
|
45
90
|
const localInstruction = local.indexes[indexName];
|
|
46
|
-
const schemaIndex =
|
|
91
|
+
const schemaIndex = meta.indexes[indexName];
|
|
47
92
|
if (!localInstruction) {
|
|
48
93
|
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
49
94
|
} else if (!schemaIndex) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Cursor } from './Cursor';
|
|
2
2
|
import type { Expr } from './Expr';
|
|
3
|
-
type SelectionBase = (() => any) | Expr<any> | Cursor.SelectMultiple<any> | Cursor.SelectSingle<any>;
|
|
3
|
+
type SelectionBase = unknown | (() => any) | Expr<any> | Cursor.SelectMultiple<any> | Cursor.SelectSingle<any>;
|
|
4
4
|
interface SelectionRecord extends Record<string, Selection> {
|
|
5
5
|
}
|
|
6
6
|
export type Selection = SelectionBase | SelectionRecord;
|
|
@@ -12,8 +12,8 @@ export declare namespace Selection {
|
|
|
12
12
|
} ? K : T extends {
|
|
13
13
|
[__cursorType](): infer K;
|
|
14
14
|
} ? K : T extends Expr<infer K> ? K : T extends Record<string, Selection> ? {
|
|
15
|
-
[K in keyof T
|
|
16
|
-
} : T extends () => any ? never : T;
|
|
15
|
+
[K in keyof T]: Infer<T[K]>;
|
|
16
|
+
} : T extends () => any ? never : unknown extends T ? never : T;
|
|
17
17
|
type With<A, B> = Expr<Combine<A, B>>;
|
|
18
18
|
type Combine<A, B> = Omit<A, keyof Infer<B>> & Infer<B>;
|
|
19
19
|
}
|
package/dist/define/Table.d.ts
CHANGED
|
@@ -1,62 +1,67 @@
|
|
|
1
|
-
import { Column,
|
|
1
|
+
import { Column, ColumnData, OptionalColumn, PrimaryColumn } from './Column';
|
|
2
2
|
import { Cursor } from './Cursor';
|
|
3
|
-
import { EV
|
|
4
|
-
import {
|
|
5
|
-
import { Index } from './Index';
|
|
6
|
-
import { Schema } from './Schema';
|
|
3
|
+
import { EV } from './Expr';
|
|
4
|
+
import { Index, IndexData } from './Index';
|
|
7
5
|
import { Selection } from './Selection';
|
|
8
|
-
|
|
9
|
-
export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>> {
|
|
10
|
-
[Selection.__tableType](): Table.Normalize<T>;
|
|
11
|
-
constructor(schema: Schema);
|
|
12
|
-
fetch(id: EV<T extends {
|
|
13
|
-
id: Column.IsPrimary<infer V, any>;
|
|
14
|
-
} ? V : never>): Cursor.SelectSingle<Table.Normalize<T> | undefined>;
|
|
15
|
-
insertOne(record: Table.Insert<T>): Cursor<Table.Normalize<T>>;
|
|
16
|
-
insertAll(data: Array<Table.Insert<T>>): Cursor.InsertValues;
|
|
17
|
-
set(data: Update<Table.Normalize<T>>): Cursor.Update<Table.Normalize<T>>;
|
|
18
|
-
delete(): Cursor.Delete;
|
|
19
|
-
createTable(): Cursor.CreateTable;
|
|
20
|
-
as(alias: string): this;
|
|
21
|
-
alias(): Record<string, this>;
|
|
22
|
-
get(name: string): Expr<any>;
|
|
23
|
-
toExpr(): Expr<Table.Normalize<T>>;
|
|
24
|
-
schema(): Schema;
|
|
6
|
+
interface TableDefinition {
|
|
25
7
|
}
|
|
8
|
+
export interface TableData {
|
|
9
|
+
name: string;
|
|
10
|
+
alias?: string;
|
|
11
|
+
definition: TableDefinition;
|
|
12
|
+
columns: Record<string, ColumnData>;
|
|
13
|
+
meta(): {
|
|
14
|
+
indexes: Record<string, IndexData>;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
interface TableProto<Definition> {
|
|
18
|
+
(conditions: {
|
|
19
|
+
[K in keyof Definition]?: Definition[K] extends Column<infer V> ? EV<V> : never;
|
|
20
|
+
}): Cursor.TableSelect<Definition>;
|
|
21
|
+
(...conditions: Array<EV<boolean>>): Cursor.TableSelect<Definition>;
|
|
22
|
+
}
|
|
23
|
+
declare class TableProto<Definition> {
|
|
24
|
+
[Selection.__tableType](): Table.Select<Definition>;
|
|
25
|
+
get [table.data](): TableData;
|
|
26
|
+
[table.meta](): TableMeta;
|
|
27
|
+
get name(): unknown;
|
|
28
|
+
get length(): unknown;
|
|
29
|
+
get call(): unknown;
|
|
30
|
+
get apply(): unknown;
|
|
31
|
+
get bind(): unknown;
|
|
32
|
+
get prototype(): unknown;
|
|
33
|
+
}
|
|
34
|
+
export type Table<Definition> = Definition & TableProto<Definition>;
|
|
26
35
|
export declare namespace Table {
|
|
27
|
-
type
|
|
28
|
-
[
|
|
29
|
-
} : never;
|
|
30
|
-
type OptionalKeys<T> = {
|
|
31
|
-
[K in keyof T]: null extends T[K] ? K : T[K] extends Column.IsPrimary<any, any> ? K : T[K] extends Column.IsOptional<any> ? K : never;
|
|
32
|
-
}[keyof T];
|
|
33
|
-
type RequiredKeys<T> = {
|
|
34
|
-
[K in keyof T]: null extends T[K] ? never : T[K] extends Column.IsPrimary<any, any> ? never : T[K] extends Column.IsOptional<any> ? never : K;
|
|
35
|
-
}[keyof T];
|
|
36
|
-
type Optionals<T> = {
|
|
37
|
-
[K in keyof T]?: T[K] extends Column.IsOptional<infer V> ? V : T[K] extends Column.IsPrimary<infer V, infer K> ? PrimaryKey<V, K> : T[K];
|
|
36
|
+
export type Select<Definition> = {
|
|
37
|
+
[K in keyof Definition as Definition[K] extends Column<any> ? K : never]: Definition[K] extends Column<infer T> ? T : never;
|
|
38
38
|
};
|
|
39
|
-
export type
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
export type Update<Definition> = Partial<{
|
|
40
|
+
[K in keyof Definition as Definition[K] extends Column<any> ? K : never]: Definition[K] extends Column<infer T> ? EV<T> : never;
|
|
41
|
+
}>;
|
|
42
|
+
type IsOptional<K> = K extends PrimaryColumn<any, any> ? true : K extends OptionalColumn<any> ? true : K extends Column<infer V> ? null extends V ? true : false : never;
|
|
43
|
+
export type Insert<Definition> = {
|
|
44
|
+
[K in keyof Definition as true extends IsOptional<Definition[K]> ? K : never]?: Definition[K] extends Column<infer V> ? EV<V> : never;
|
|
45
|
+
} & {
|
|
46
|
+
[K in keyof Definition as false extends IsOptional<Definition[K]> ? K : never]: Definition[K] extends Column<infer V> ? EV<V> : never;
|
|
42
47
|
};
|
|
43
|
-
export type Infer<T> = T extends Table<infer U> ? Normalize<U> : never;
|
|
44
48
|
export {};
|
|
45
49
|
}
|
|
46
|
-
export
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
indexes?: (this: Fields<T>) => Record<string, Index>;
|
|
50
|
+
export interface TableMeta {
|
|
51
|
+
indexes?: Record<string, Index>;
|
|
52
|
+
}
|
|
53
|
+
type Definition<T> = {
|
|
54
|
+
[K in keyof T as K extends string ? K : never]: Column<any> | (() => any);
|
|
53
55
|
};
|
|
54
|
-
|
|
56
|
+
interface Define<T> {
|
|
57
|
+
new (): T;
|
|
58
|
+
}
|
|
59
|
+
type Blueprint<T> = Definition<T>;
|
|
60
|
+
export type table<T> = T extends Table<infer D> ? Table.Select<D> : never;
|
|
61
|
+
export declare function createTable<Definition>(data: TableData): Table<Definition>;
|
|
62
|
+
export declare function table<T extends Blueprint<T>>(define: Record<string, T | Define<T>>): Table<T>;
|
|
55
63
|
export declare namespace table {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
[key: string]: (this: T, ...args: any[]) => any;
|
|
59
|
-
};
|
|
60
|
-
export function extend<T extends Table<any>, E extends Extensions<T>>(target: T, extensions: E): T & E;
|
|
61
|
-
export {};
|
|
64
|
+
const data: unique symbol;
|
|
65
|
+
const meta: unique symbol;
|
|
62
66
|
}
|
|
67
|
+
export {};
|
package/dist/define/Table.js
CHANGED
|
@@ -1,130 +1,173 @@
|
|
|
1
1
|
// src/define/Table.ts
|
|
2
|
+
import { Column } from "./Column.js";
|
|
2
3
|
import { Cursor } from "./Cursor.js";
|
|
3
|
-
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
-
import { Query } from "./Query.js";
|
|
4
|
+
import { BinOpType, Expr, ExprData } from "./Expr.js";
|
|
5
5
|
import { Selection } from "./Selection.js";
|
|
6
6
|
import { Target } from "./Target.js";
|
|
7
|
-
var
|
|
7
|
+
var {
|
|
8
|
+
keys,
|
|
9
|
+
entries,
|
|
10
|
+
fromEntries,
|
|
11
|
+
getOwnPropertyDescriptors,
|
|
12
|
+
assign,
|
|
13
|
+
getPrototypeOf,
|
|
14
|
+
setPrototypeOf,
|
|
15
|
+
getOwnPropertyDescriptor
|
|
16
|
+
} = Object;
|
|
17
|
+
var { ownKeys } = Reflect;
|
|
18
|
+
var TableProto = class {
|
|
8
19
|
[Selection.__tableType]() {
|
|
9
20
|
throw "assert";
|
|
10
21
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Query.Select({
|
|
14
|
-
from: Target.Table(schema),
|
|
15
|
-
selection: ExprData.Row(Target.Table(schema))
|
|
16
|
-
})
|
|
17
|
-
);
|
|
18
|
-
Object.defineProperty(this, "schema", {
|
|
19
|
-
enumerable: false,
|
|
20
|
-
value: () => schema
|
|
21
|
-
});
|
|
22
|
-
if (schema.columns)
|
|
23
|
-
for (const column of Object.keys(schema.columns)) {
|
|
24
|
-
Object.defineProperty(this, column, {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: () => this.get(column)
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
return new Proxy(this, {
|
|
30
|
-
get(target, key) {
|
|
31
|
-
return key in target ? target[key] : target.get(key);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
fetch(id) {
|
|
36
|
-
return this.where(this.get("id").is(id)).first();
|
|
37
|
-
}
|
|
38
|
-
insertOne(record) {
|
|
39
|
-
return new Cursor(
|
|
40
|
-
Query.Insert({
|
|
41
|
-
into: this.schema(),
|
|
42
|
-
data: [record],
|
|
43
|
-
selection: ExprData.Row(Target.Table(this.schema())),
|
|
44
|
-
singleResult: true
|
|
45
|
-
})
|
|
46
|
-
);
|
|
22
|
+
get [table.data]() {
|
|
23
|
+
throw "assert";
|
|
47
24
|
}
|
|
48
|
-
|
|
49
|
-
|
|
25
|
+
[table.meta]() {
|
|
26
|
+
throw "assert";
|
|
50
27
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
28
|
+
// Clear the Function prototype, not sure if there's a better way
|
|
29
|
+
// as mapped types (Omit) will remove the callable signature. We define them
|
|
30
|
+
// in a class getter since it's the only way to also mark them as non-enumarable
|
|
31
|
+
// Seems open: Microsoft/TypeScript#27575
|
|
32
|
+
get name() {
|
|
33
|
+
throw "assert";
|
|
57
34
|
}
|
|
58
|
-
|
|
59
|
-
|
|
35
|
+
get length() {
|
|
36
|
+
throw "assert";
|
|
60
37
|
}
|
|
61
|
-
|
|
62
|
-
|
|
38
|
+
get call() {
|
|
39
|
+
throw "assert";
|
|
63
40
|
}
|
|
64
|
-
|
|
65
|
-
|
|
41
|
+
get apply() {
|
|
42
|
+
throw "assert";
|
|
66
43
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
{},
|
|
70
|
-
{
|
|
71
|
-
get: (_, key) => {
|
|
72
|
-
return this.as(key);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
);
|
|
44
|
+
get bind() {
|
|
45
|
+
throw "assert";
|
|
76
46
|
}
|
|
77
|
-
get(
|
|
78
|
-
|
|
47
|
+
get prototype() {
|
|
48
|
+
throw "assert";
|
|
79
49
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
50
|
+
};
|
|
51
|
+
function keysOf(input) {
|
|
52
|
+
const methods = [];
|
|
53
|
+
while (input = getPrototypeOf(input)) {
|
|
54
|
+
const keys2 = ownKeys(input);
|
|
55
|
+
for (const key of keys2)
|
|
56
|
+
if (typeof key === "string")
|
|
57
|
+
methods.push(key);
|
|
84
58
|
}
|
|
85
|
-
|
|
86
|
-
|
|
59
|
+
return methods;
|
|
60
|
+
}
|
|
61
|
+
function createTable(data) {
|
|
62
|
+
const target = Target.Table(data);
|
|
63
|
+
const call = {
|
|
64
|
+
[data.name]: function(...args) {
|
|
65
|
+
const isConditionalRecord = args.length === 1 && typeof args[0] === "object" && !(args[0] instanceof Expr);
|
|
66
|
+
const conditions = isConditionalRecord ? entries(args[0]).map(([key, value]) => {
|
|
67
|
+
const column = data.columns[key];
|
|
68
|
+
if (!column)
|
|
69
|
+
throw new Error(`Column ${key} not found`);
|
|
70
|
+
return new Expr(
|
|
71
|
+
ExprData.BinOp(
|
|
72
|
+
BinOpType.Equals,
|
|
73
|
+
ExprData.Field(ExprData.Row(target), key),
|
|
74
|
+
ExprData.create(value)
|
|
75
|
+
)
|
|
76
|
+
);
|
|
77
|
+
}) : args;
|
|
78
|
+
return new Cursor.TableSelect(data, conditions);
|
|
79
|
+
}
|
|
80
|
+
}[data.name];
|
|
81
|
+
const cols = keys(data.columns);
|
|
82
|
+
const hasKeywords = cols.concat(keysOf(data.definition)).some((name) => name in Function);
|
|
83
|
+
const expressions = fromEntries(
|
|
84
|
+
cols.map((name) => [
|
|
85
|
+
name,
|
|
86
|
+
new Expr(ExprData.Field(ExprData.Row(target), name))
|
|
87
|
+
])
|
|
88
|
+
);
|
|
89
|
+
const toExpr = () => new Expr(ExprData.Row(target));
|
|
90
|
+
const ownKeys2 = ["prototype", ...cols];
|
|
91
|
+
let res;
|
|
92
|
+
if (!hasKeywords) {
|
|
93
|
+
res = assign(call, expressions, { [table.data]: data });
|
|
94
|
+
setPrototypeOf(call, getPrototypeOf(data.definition));
|
|
95
|
+
} else {
|
|
96
|
+
let get2 = function(key) {
|
|
97
|
+
return expressions[key] || data.definition[key];
|
|
98
|
+
};
|
|
99
|
+
var get = get2;
|
|
100
|
+
res = new Proxy(call, {
|
|
101
|
+
get(target2, key) {
|
|
102
|
+
if (key === table.data)
|
|
103
|
+
return data;
|
|
104
|
+
if (key === Expr.toExpr)
|
|
105
|
+
return toExpr;
|
|
106
|
+
return get2(key);
|
|
107
|
+
},
|
|
108
|
+
ownKeys(target2) {
|
|
109
|
+
return ownKeys2;
|
|
110
|
+
},
|
|
111
|
+
getOwnPropertyDescriptor(target2, key) {
|
|
112
|
+
if (key === "prototype")
|
|
113
|
+
return getOwnPropertyDescriptor(target2, key);
|
|
114
|
+
return {
|
|
115
|
+
value: get2(key),
|
|
116
|
+
enumerable: true,
|
|
117
|
+
configurable: true
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
});
|
|
87
121
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
122
|
+
return res;
|
|
123
|
+
}
|
|
124
|
+
function table(define) {
|
|
125
|
+
const names = keys(define);
|
|
126
|
+
if (names.length !== 1)
|
|
127
|
+
throw new Error("Table must have a single name");
|
|
128
|
+
const name = names[0];
|
|
129
|
+
const target = define[name];
|
|
130
|
+
const definition = "prototype" in target ? new target() : target;
|
|
131
|
+
const columns = definition;
|
|
132
|
+
const res = createTable({
|
|
133
|
+
name,
|
|
134
|
+
definition,
|
|
135
|
+
columns: fromEntries(
|
|
136
|
+
entries(getOwnPropertyDescriptors(columns)).map(([name2, descriptor]) => {
|
|
137
|
+
const column = columns[name2];
|
|
138
|
+
if (!(column instanceof Column))
|
|
139
|
+
throw new Error(`Property ${name2} is not a column`);
|
|
94
140
|
const { data } = column;
|
|
95
|
-
return [
|
|
141
|
+
return [
|
|
142
|
+
name2,
|
|
143
|
+
{
|
|
144
|
+
...data,
|
|
145
|
+
name: data.name || name2,
|
|
146
|
+
enumerable: descriptor.enumerable
|
|
147
|
+
}
|
|
148
|
+
];
|
|
96
149
|
})
|
|
97
150
|
),
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return new Table({
|
|
111
|
-
...schema,
|
|
112
|
-
indexes
|
|
151
|
+
meta() {
|
|
152
|
+
const createMeta = res[table.meta];
|
|
153
|
+
const meta = createMeta ? createMeta.apply(res) : {};
|
|
154
|
+
return {
|
|
155
|
+
indexes: fromEntries(
|
|
156
|
+
entries(meta?.indexes || {}).map(([key, index]) => {
|
|
157
|
+
const indexName = `${name}.${key}`;
|
|
158
|
+
return [indexName, { name: indexName, ...index.data }];
|
|
159
|
+
})
|
|
160
|
+
)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
113
163
|
});
|
|
164
|
+
return res;
|
|
114
165
|
}
|
|
115
166
|
((table2) => {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
get(target2, key) {
|
|
119
|
-
if (key === "as")
|
|
120
|
-
return (...args) => extend(target2.as(...args), extensions);
|
|
121
|
-
return key in extensions ? extensions[key].bind(target2) : target2[key];
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
table2.extend = extend;
|
|
167
|
+
table2.data = Symbol("data");
|
|
168
|
+
table2.meta = Symbol("meta");
|
|
126
169
|
})(table || (table = {}));
|
|
127
170
|
export {
|
|
128
|
-
|
|
171
|
+
createTable,
|
|
129
172
|
table
|
|
130
173
|
};
|
package/dist/define/Target.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExprData } from './Expr';
|
|
2
2
|
import { Query as QueryData } from './Query';
|
|
3
|
-
import {
|
|
3
|
+
import { TableData } from './Table';
|
|
4
4
|
export declare enum TargetType {
|
|
5
5
|
Expr = "Expr",
|
|
6
6
|
Query = "Query",
|
|
@@ -23,9 +23,9 @@ export declare namespace Target {
|
|
|
23
23
|
function Query(query: QueryData, alias?: string): Query;
|
|
24
24
|
interface Table {
|
|
25
25
|
type: TargetType.Table;
|
|
26
|
-
table:
|
|
26
|
+
table: TableData;
|
|
27
27
|
}
|
|
28
|
-
function Table(table:
|
|
28
|
+
function Table(table: TableData): Table;
|
|
29
29
|
interface Join {
|
|
30
30
|
type: TargetType.Join;
|
|
31
31
|
left: Target;
|
|
@@ -34,5 +34,5 @@ export declare namespace Target {
|
|
|
34
34
|
on: ExprData;
|
|
35
35
|
}
|
|
36
36
|
function Join(left: Target, right: Target, joinType: 'left' | 'inner', on: ExprData): Join;
|
|
37
|
-
function source(from: Target):
|
|
37
|
+
function source(from: Target): TableData | undefined;
|
|
38
38
|
}
|
package/dist/lib/Driver.js
CHANGED
|
@@ -4,6 +4,7 @@ import { Expr, ExprData } from "../define/Expr.js";
|
|
|
4
4
|
import { ParamData } from "../define/Param.js";
|
|
5
5
|
import { Query, QueryType } from "../define/Query.js";
|
|
6
6
|
import { Schema } from "../define/Schema.js";
|
|
7
|
+
import { table } from "../define/Table.js";
|
|
7
8
|
var Callable = class extends Function {
|
|
8
9
|
constructor(fn) {
|
|
9
10
|
super();
|
|
@@ -80,8 +81,8 @@ var SyncDriver = class extends DriverBase {
|
|
|
80
81
|
}
|
|
81
82
|
migrateSchema(...tables) {
|
|
82
83
|
const queries = [];
|
|
83
|
-
for (const
|
|
84
|
-
const schema = table.
|
|
84
|
+
for (const current of Object.values(tables)) {
|
|
85
|
+
const schema = current[table.data];
|
|
85
86
|
const localSchema = this.schemaInstructions(schema.name);
|
|
86
87
|
if (!localSchema) {
|
|
87
88
|
queries.push(...Schema.create(schema).queries);
|
|
@@ -186,8 +187,8 @@ var AsyncDriver = class extends DriverBase {
|
|
|
186
187
|
}
|
|
187
188
|
async migrateSchema(...tables) {
|
|
188
189
|
const queries = [];
|
|
189
|
-
for (const
|
|
190
|
-
const schema = table.
|
|
190
|
+
for (const current of Object.values(tables)) {
|
|
191
|
+
const schema = current[table.data];
|
|
191
192
|
const localSchema = await this.schemaInstructions(schema.name);
|
|
192
193
|
if (!localSchema) {
|
|
193
194
|
queries.push(...Schema.create(schema).queries);
|
package/dist/lib/Formatter.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface FormatContext {
|
|
|
22
22
|
formatAsInsert?: boolean;
|
|
23
23
|
formatAsIn?: boolean;
|
|
24
24
|
topLevel?: boolean;
|
|
25
|
+
selectAsColumns?: boolean;
|
|
25
26
|
}
|
|
26
27
|
type FormatSubject = (stmt: Statement, mkSubject: () => void) => void;
|
|
27
28
|
export interface CompileOptions extends Partial<FormatContext>, StatementOptions {
|
|
@@ -43,6 +44,7 @@ export declare abstract class Formatter implements Sanitizer {
|
|
|
43
44
|
formatAsInsert?: boolean | undefined;
|
|
44
45
|
formatAsIn?: boolean | undefined;
|
|
45
46
|
topLevel?: boolean | undefined;
|
|
47
|
+
selectAsColumns?: boolean | undefined;
|
|
46
48
|
skipNewlines?: boolean | undefined;
|
|
47
49
|
};
|
|
48
50
|
format<T>(ctx: FormatContext, query: Query<T>): Statement;
|
|
@@ -54,6 +56,7 @@ export declare abstract class Formatter implements Sanitizer {
|
|
|
54
56
|
formatCreateIndex(ctx: FormatContext, query: Query.CreateIndex): Statement;
|
|
55
57
|
formatDropIndex(ctx: FormatContext, query: Query.DropIndex): Statement;
|
|
56
58
|
formatAlterTable(ctx: FormatContext, query: Query.AlterTable): Statement;
|
|
59
|
+
formatDropTable(ctx: FormatContext, query: Query.DropTable): Statement;
|
|
57
60
|
formatTransaction(ctx: FormatContext, { op, id }: Query.Transaction): Statement;
|
|
58
61
|
formatBatch(ctx: FormatContext, { queries }: Query.Batch): Statement;
|
|
59
62
|
formatRaw(ctx: FormatContext, { strings, params }: Query.Raw): Statement;
|