rado 0.2.13 → 0.2.15
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/Column.d.ts +22 -21
- package/dist/define/Column.js +42 -42
- package/dist/define/Cursor.d.ts +15 -7
- package/dist/define/Cursor.js +84 -86
- package/dist/define/Expr.d.ts +42 -29
- package/dist/define/Expr.js +203 -148
- package/dist/define/Functions.js +1 -1
- package/dist/define/Ops.js +6 -6
- package/dist/define/Schema.js +1 -1
- package/dist/define/Selection.d.ts +4 -4
- package/dist/define/Table.d.ts +8 -6
- package/dist/define/Table.js +26 -13
- package/dist/lib/Driver.d.ts +1 -2
- package/dist/lib/Driver.js +16 -16
- package/dist/lib/Formatter.js +5 -5
- package/dist/util/Callable.d.ts +1 -3
- package/dist/util/Callable.js +12 -11
- package/package.json +2 -2
package/dist/define/Table.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// src/define/Table.ts
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Column,
|
|
4
|
+
ColumnType
|
|
5
|
+
} from "./Column.js";
|
|
3
6
|
import { Cursor } from "./Cursor.js";
|
|
4
7
|
import { BinOpType, Expr, ExprData } from "./Expr.js";
|
|
5
8
|
import { Target } from "./Target.js";
|
|
9
|
+
var DATA = Symbol("Table.Data");
|
|
10
|
+
var META = Symbol("Table.Meta");
|
|
6
11
|
var {
|
|
7
12
|
keys,
|
|
8
13
|
entries,
|
|
@@ -14,6 +19,11 @@ var {
|
|
|
14
19
|
getOwnPropertyDescriptor
|
|
15
20
|
} = Object;
|
|
16
21
|
var { ownKeys } = Reflect;
|
|
22
|
+
var Table;
|
|
23
|
+
((Table2) => {
|
|
24
|
+
Table2.Data = DATA;
|
|
25
|
+
Table2.Meta = META;
|
|
26
|
+
})(Table || (Table = {}));
|
|
17
27
|
function keysOf(input) {
|
|
18
28
|
const methods = [];
|
|
19
29
|
while (input = getPrototypeOf(input)) {
|
|
@@ -28,7 +38,7 @@ function createTable(data) {
|
|
|
28
38
|
const target = Target.Table(data);
|
|
29
39
|
const call = {
|
|
30
40
|
[data.name]: function(...args) {
|
|
31
|
-
const isConditionalRecord = args.length === 1 &&
|
|
41
|
+
const isConditionalRecord = args.length === 1 && !Expr.isExpr(args[0]);
|
|
32
42
|
const conditions = isConditionalRecord ? entries(args[0]).map(([key, value]) => {
|
|
33
43
|
const column = data.columns[key];
|
|
34
44
|
if (!column)
|
|
@@ -46,17 +56,20 @@ function createTable(data) {
|
|
|
46
56
|
}[data.name];
|
|
47
57
|
const cols = keys(data.columns);
|
|
48
58
|
const hasKeywords = cols.concat(keysOf(data.definition)).some((name) => name in Function);
|
|
59
|
+
const row = ExprData.Row(target);
|
|
49
60
|
const expressions = fromEntries(
|
|
50
|
-
cols.map((name) =>
|
|
51
|
-
name
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
cols.map((name) => {
|
|
62
|
+
let expr = new Expr(ExprData.Field(row, name));
|
|
63
|
+
if (data.columns[name].type === ColumnType.Json)
|
|
64
|
+
expr = expr.dynamic();
|
|
65
|
+
return [name, expr];
|
|
66
|
+
})
|
|
54
67
|
);
|
|
55
|
-
const toExpr = () => new Expr(
|
|
68
|
+
const toExpr = () => new Expr(row);
|
|
56
69
|
const ownKeys2 = ["prototype", ...cols];
|
|
57
70
|
let res;
|
|
58
71
|
if (!hasKeywords) {
|
|
59
|
-
res = assign(call, expressions, { [
|
|
72
|
+
res = assign(call, expressions, { [DATA]: data, [Expr.ToExpr]: toExpr });
|
|
60
73
|
setPrototypeOf(call, getPrototypeOf(data.definition));
|
|
61
74
|
} else {
|
|
62
75
|
let get2 = function(key) {
|
|
@@ -65,9 +78,9 @@ function createTable(data) {
|
|
|
65
78
|
var get = get2;
|
|
66
79
|
res = new Proxy(call, {
|
|
67
80
|
get(target2, key) {
|
|
68
|
-
if (key ===
|
|
81
|
+
if (key === DATA)
|
|
69
82
|
return data;
|
|
70
|
-
if (key === Expr.
|
|
83
|
+
if (key === Expr.ToExpr)
|
|
71
84
|
return toExpr;
|
|
72
85
|
return get2(key);
|
|
73
86
|
},
|
|
@@ -101,7 +114,7 @@ function table(define) {
|
|
|
101
114
|
columns: fromEntries(
|
|
102
115
|
entries(getOwnPropertyDescriptors(columns)).map(([name2, descriptor]) => {
|
|
103
116
|
const column = columns[name2];
|
|
104
|
-
const data = column[Column.
|
|
117
|
+
const data = column[Column.Data];
|
|
105
118
|
if (!data.type)
|
|
106
119
|
throw new Error(`Column ${name2} has no type`);
|
|
107
120
|
return [
|
|
@@ -131,10 +144,10 @@ function table(define) {
|
|
|
131
144
|
return res;
|
|
132
145
|
}
|
|
133
146
|
((table2) => {
|
|
134
|
-
table2.
|
|
135
|
-
table2.meta = Symbol("meta");
|
|
147
|
+
table2.meta = META;
|
|
136
148
|
})(table || (table = {}));
|
|
137
149
|
export {
|
|
150
|
+
Table,
|
|
138
151
|
createTable,
|
|
139
152
|
table
|
|
140
153
|
};
|
package/dist/lib/Driver.d.ts
CHANGED
|
@@ -3,10 +3,9 @@ import { Expr } from '../define/Expr';
|
|
|
3
3
|
import { Query } from '../define/Query';
|
|
4
4
|
import { SchemaInstructions } from '../define/Schema';
|
|
5
5
|
import { Table } from '../define/Table';
|
|
6
|
-
import { Callable } from '../util/Callable';
|
|
7
6
|
import { Formatter } from './Formatter';
|
|
8
7
|
import { Statement } from './Statement';
|
|
9
|
-
declare abstract class DriverBase
|
|
8
|
+
declare abstract class DriverBase {
|
|
10
9
|
formatter: Formatter;
|
|
11
10
|
constructor(formatter: Formatter);
|
|
12
11
|
compile<T extends Array<Expr<any>>, R>(create: (...params: T) => Cursor<R>): [Query<T>, Statement];
|
package/dist/lib/Driver.js
CHANGED
|
@@ -4,26 +4,26 @@ 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 {
|
|
8
|
-
import {
|
|
7
|
+
import { Table } from "../define/Table.js";
|
|
8
|
+
import { callable } from "../util/Callable.js";
|
|
9
9
|
function isTemplateStringsArray(input) {
|
|
10
10
|
return Boolean(Array.isArray(input) && input.raw);
|
|
11
11
|
}
|
|
12
|
-
var DriverBase = class
|
|
12
|
+
var DriverBase = class {
|
|
13
13
|
constructor(formatter) {
|
|
14
|
-
|
|
14
|
+
this.formatter = formatter;
|
|
15
|
+
return callable(this, (...args) => {
|
|
15
16
|
const [input, ...rest] = args;
|
|
16
17
|
if (input instanceof Cursor && rest.length === 0)
|
|
17
|
-
return this.executeQuery(input.
|
|
18
|
+
return this.executeQuery(input[Cursor.Query]);
|
|
18
19
|
if (isTemplateStringsArray(input))
|
|
19
20
|
return this.executeTemplate(void 0, input, ...rest);
|
|
20
21
|
return this.executeQuery(
|
|
21
22
|
Query.Batch({
|
|
22
|
-
queries: args.filter((arg) => arg instanceof Cursor).map((arg) => arg.
|
|
23
|
+
queries: args.filter((arg) => arg instanceof Cursor).map((arg) => arg[Cursor.Query])
|
|
23
24
|
})
|
|
24
25
|
);
|
|
25
26
|
});
|
|
26
|
-
this.formatter = formatter;
|
|
27
27
|
}
|
|
28
28
|
compile(create) {
|
|
29
29
|
const { length } = create;
|
|
@@ -32,23 +32,23 @@ var DriverBase = class extends Callable {
|
|
|
32
32
|
(name) => new Expr(ExprData.Param(ParamData.Named(name)))
|
|
33
33
|
);
|
|
34
34
|
const cursor = create(...params);
|
|
35
|
-
const query = cursor.
|
|
35
|
+
const query = cursor[Cursor.Query];
|
|
36
36
|
return [query, this.formatter.compile(query)];
|
|
37
37
|
}
|
|
38
38
|
all(...args) {
|
|
39
39
|
const [input, ...rest] = args;
|
|
40
40
|
if (input instanceof Cursor.SelectSingle)
|
|
41
|
-
return this.executeQuery(input.all().
|
|
41
|
+
return this.executeQuery(input.all()[Cursor.Query]);
|
|
42
42
|
if (input instanceof Cursor)
|
|
43
|
-
return this.executeQuery(input.
|
|
43
|
+
return this.executeQuery(input[Cursor.Query]);
|
|
44
44
|
return this.executeTemplate("rows", input, ...rest);
|
|
45
45
|
}
|
|
46
46
|
get(...args) {
|
|
47
47
|
const [input, ...rest] = args;
|
|
48
48
|
if (input instanceof Cursor.SelectMultiple)
|
|
49
|
-
return this.executeQuery(input.first().
|
|
49
|
+
return this.executeQuery(input.first()[Cursor.Query]);
|
|
50
50
|
if (input instanceof Cursor)
|
|
51
|
-
return this.executeQuery(input.
|
|
51
|
+
return this.executeQuery(input[Cursor.Query]);
|
|
52
52
|
return this.executeTemplate("row", input, ...rest);
|
|
53
53
|
}
|
|
54
54
|
sql(strings, ...params) {
|
|
@@ -73,7 +73,7 @@ var SyncDriver = class extends DriverBase {
|
|
|
73
73
|
migrateSchema(...tables) {
|
|
74
74
|
const queries = [];
|
|
75
75
|
for (const current of Object.values(tables)) {
|
|
76
|
-
const schema = current[
|
|
76
|
+
const schema = current[Table.Data];
|
|
77
77
|
const localSchema = this.schemaInstructions(schema.name);
|
|
78
78
|
if (!localSchema) {
|
|
79
79
|
queries.push(...Schema.create(schema).queries);
|
|
@@ -135,7 +135,7 @@ var SyncDriver = class extends DriverBase {
|
|
|
135
135
|
}
|
|
136
136
|
*iterate(cursor) {
|
|
137
137
|
const stmt = this.prepareStatement(
|
|
138
|
-
this.formatter.compile(cursor.
|
|
138
|
+
this.formatter.compile(cursor[Cursor.Query]),
|
|
139
139
|
true
|
|
140
140
|
);
|
|
141
141
|
for (const row of stmt.iterate()) {
|
|
@@ -179,7 +179,7 @@ var AsyncDriver = class extends DriverBase {
|
|
|
179
179
|
async migrateSchema(...tables) {
|
|
180
180
|
const queries = [];
|
|
181
181
|
for (const current of Object.values(tables)) {
|
|
182
|
-
const schema = current[
|
|
182
|
+
const schema = current[Table.Data];
|
|
183
183
|
const localSchema = await this.schemaInstructions(schema.name);
|
|
184
184
|
if (!localSchema) {
|
|
185
185
|
queries.push(...Schema.create(schema).queries);
|
|
@@ -243,7 +243,7 @@ var AsyncDriver = class extends DriverBase {
|
|
|
243
243
|
}
|
|
244
244
|
async *iterate(cursor) {
|
|
245
245
|
const stmt = this.prepareStatement(
|
|
246
|
-
this.formatter.compile(cursor.
|
|
246
|
+
this.formatter.compile(cursor[Cursor.Query]),
|
|
247
247
|
true
|
|
248
248
|
);
|
|
249
249
|
for await (const row of stmt.iterate()) {
|
package/dist/lib/Formatter.js
CHANGED
|
@@ -134,7 +134,7 @@ var Formatter = class {
|
|
|
134
134
|
for (const key of stmt.separate(keys)) {
|
|
135
135
|
stmt.identifier(key).add("=").space();
|
|
136
136
|
const value = data[key];
|
|
137
|
-
if (value
|
|
137
|
+
if (Expr.isExpr(value))
|
|
138
138
|
this.formatExprJson(ctx, ExprData.create(data[key]));
|
|
139
139
|
else
|
|
140
140
|
this.formatValue({ ...ctx, formatAsInsert: true }, value);
|
|
@@ -245,8 +245,8 @@ var Formatter = class {
|
|
|
245
245
|
ctx.stmt.raw(strings[i]);
|
|
246
246
|
if (i < params.length) {
|
|
247
247
|
const param = params[i];
|
|
248
|
-
if (param
|
|
249
|
-
this.formatExpr(ctx, param.
|
|
248
|
+
if (Expr.isExpr(param))
|
|
249
|
+
this.formatExpr(ctx, param[Expr.Data]);
|
|
250
250
|
else
|
|
251
251
|
this.formatValue(ctx, param);
|
|
252
252
|
}
|
|
@@ -316,8 +316,8 @@ var Formatter = class {
|
|
|
316
316
|
}
|
|
317
317
|
formatColumnValue(ctx, column, columnValue) {
|
|
318
318
|
const { stmt } = ctx;
|
|
319
|
-
if (columnValue
|
|
320
|
-
return this.formatExprValue(ctx, columnValue.
|
|
319
|
+
if (Expr.isExpr(columnValue))
|
|
320
|
+
return this.formatExprValue(ctx, columnValue[Expr.Data]);
|
|
321
321
|
const isNull = columnValue === void 0 || columnValue === null;
|
|
322
322
|
const isOptional = column.nullable || column.autoIncrement || column.primaryKey;
|
|
323
323
|
if (isNull) {
|
package/dist/util/Callable.d.ts
CHANGED
package/dist/util/Callable.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
// src/util/Callable.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
2
|
+
function callable(target, call) {
|
|
3
|
+
return new Proxy(call, {
|
|
4
|
+
set(_, prop, value) {
|
|
5
|
+
target[prop] = value;
|
|
6
|
+
return true;
|
|
7
|
+
},
|
|
8
|
+
get(_, prop) {
|
|
9
|
+
return target[prop];
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
}
|
|
12
13
|
export {
|
|
13
|
-
|
|
14
|
+
callable
|
|
14
15
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rado",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"speedscope": "^1.15.0",
|
|
47
47
|
"sql.js": "^1.8.0",
|
|
48
48
|
"sqlite3": "^5.1.4",
|
|
49
|
-
"tsx": "^3.12.
|
|
49
|
+
"tsx": "^3.12.3",
|
|
50
50
|
"typescript": "next",
|
|
51
51
|
"uvu": "^0.5.6"
|
|
52
52
|
},
|