rado 0.1.24 → 0.1.26
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/driver/better-sqlite3.d.ts +5 -7
- package/dist/driver/better-sqlite3.js +25 -18
- package/dist/driver/sql.js.d.ts +5 -7
- package/dist/driver/sql.js.js +29 -26
- package/dist/driver/sqlite3.d.ts +6 -8
- package/dist/driver/sqlite3.js +40 -29
- package/dist/lib/Cursor.d.ts +2 -1
- package/dist/lib/Cursor.js +3 -0
- package/dist/lib/Driver.d.ts +39 -23
- package/dist/lib/Driver.js +72 -28
- package/dist/lib/Formatter.d.ts +2 -1
- package/dist/lib/Formatter.js +14 -4
- package/dist/lib/Index.d.ts +2 -2
- package/dist/lib/Index.js +3 -3
- package/dist/lib/Ops.d.ts +1 -1
- package/dist/lib/Param.d.ts +5 -0
- package/dist/lib/Param.js +10 -1
- package/dist/lib/Sanitizer.d.ts +1 -0
- package/dist/lib/Schema.js +2 -2
- package/dist/lib/Statement.d.ts +14 -4
- package/dist/lib/Statement.js +48 -5
- package/dist/lib/Table.d.ts +6 -5
- package/dist/lib/Table.js +4 -7
- package/dist/sqlite/SqliteFormatter.d.ts +1 -0
- package/dist/sqlite/SqliteFormatter.js +11 -0
- package/dist/sqlite/SqliteSchema.d.ts +4 -3
- package/dist/sqlite/SqliteSchema.js +5 -4
- package/package.json +1 -1
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import type { Database } from 'better-sqlite3';
|
|
2
2
|
import { Driver } from '../lib/Driver';
|
|
3
3
|
import { SchemaInstructions } from '../lib/Schema';
|
|
4
|
-
import {
|
|
4
|
+
import { CompiledStatement } from '../lib/Statement';
|
|
5
|
+
import { SqliteSchema } from '../sqlite/SqliteSchema';
|
|
5
6
|
export declare class BetterSqlite3Driver extends Driver.Sync {
|
|
6
7
|
db: Database;
|
|
8
|
+
tableData: (tableName: string) => Array<SqliteSchema.Column>;
|
|
9
|
+
indexData: (tableName: string) => Array<SqliteSchema.Index>;
|
|
7
10
|
constructor(db: Database);
|
|
8
|
-
|
|
9
|
-
values([sql, params]: Statement.Compiled): Array<Array<any>>;
|
|
10
|
-
execute([sql, params]: Statement.Compiled): void;
|
|
11
|
-
mutate([sql, params]: Statement.Compiled): {
|
|
12
|
-
rowsAffected: number;
|
|
13
|
-
};
|
|
11
|
+
prepareStatement(stmt: CompiledStatement): Driver.Sync.PreparedStatement;
|
|
14
12
|
schemaInstructions(tableName: string): SchemaInstructions | undefined;
|
|
15
13
|
export(): Uint8Array;
|
|
16
14
|
}
|
|
@@ -2,31 +2,38 @@
|
|
|
2
2
|
import { Driver } from "../lib/Driver.js";
|
|
3
3
|
import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
|
|
4
4
|
import { SqliteSchema } from "../sqlite/SqliteSchema.js";
|
|
5
|
+
var PreparedStatement = class {
|
|
6
|
+
constructor(stmt) {
|
|
7
|
+
this.stmt = stmt;
|
|
8
|
+
}
|
|
9
|
+
all(params = []) {
|
|
10
|
+
return this.stmt.all(...params);
|
|
11
|
+
}
|
|
12
|
+
run(params = []) {
|
|
13
|
+
return { rowsAffected: this.stmt.run(...params).changes };
|
|
14
|
+
}
|
|
15
|
+
get(params = []) {
|
|
16
|
+
return this.stmt.get(...params);
|
|
17
|
+
}
|
|
18
|
+
execute(params = []) {
|
|
19
|
+
this.stmt.run(...params);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
5
22
|
var BetterSqlite3Driver = class extends Driver.Sync {
|
|
6
23
|
constructor(db) {
|
|
7
24
|
super(new SqliteFormatter());
|
|
8
25
|
this.db = db;
|
|
26
|
+
this.tableData = this.prepare(SqliteSchema.tableData);
|
|
27
|
+
this.indexData = this.prepare(SqliteSchema.indexData);
|
|
9
28
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return this.db.prepare(sql).raw().all(...params);
|
|
15
|
-
}
|
|
16
|
-
execute([sql, params]) {
|
|
17
|
-
this.db.prepare(sql).run(...params);
|
|
18
|
-
}
|
|
19
|
-
mutate([sql, params]) {
|
|
20
|
-
const { changes } = this.db.prepare(sql).run(...params);
|
|
21
|
-
return { rowsAffected: changes };
|
|
29
|
+
tableData;
|
|
30
|
+
indexData;
|
|
31
|
+
prepareStatement(stmt) {
|
|
32
|
+
return new PreparedStatement(this.db.prepare(stmt.sql));
|
|
22
33
|
}
|
|
23
34
|
schemaInstructions(tableName) {
|
|
24
|
-
const columnData = this.
|
|
25
|
-
|
|
26
|
-
);
|
|
27
|
-
const indexData = this.rows(
|
|
28
|
-
SqliteSchema.indexData(tableName).compile(this.formatter)
|
|
29
|
-
);
|
|
35
|
+
const columnData = this.tableData(tableName);
|
|
36
|
+
const indexData = this.indexData(tableName);
|
|
30
37
|
return SqliteSchema.createInstructions(columnData, indexData);
|
|
31
38
|
}
|
|
32
39
|
export() {
|
package/dist/driver/sql.js.d.ts
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import type { Database } from 'sql.js';
|
|
2
2
|
import { Driver } from '../lib/Driver';
|
|
3
3
|
import { SchemaInstructions } from '../lib/Schema';
|
|
4
|
-
import {
|
|
4
|
+
import { CompiledStatement } from '../lib/Statement';
|
|
5
|
+
import { SqliteSchema } from '../sqlite/SqliteSchema';
|
|
5
6
|
export declare class SqlJsDriver extends Driver.Sync {
|
|
6
7
|
db: Database;
|
|
8
|
+
tableData: (tableName: string) => Array<SqliteSchema.Column>;
|
|
9
|
+
indexData: (tableName: string) => Array<SqliteSchema.Index>;
|
|
7
10
|
constructor(db: Database);
|
|
8
|
-
|
|
9
|
-
values([sql, params]: Statement.Compiled): Array<Array<any>>;
|
|
10
|
-
execute([sql, params]: Statement.Compiled): void;
|
|
11
|
-
mutate([sql, params]: Statement.Compiled): {
|
|
12
|
-
rowsAffected: number;
|
|
13
|
-
};
|
|
11
|
+
prepareStatement(stmt: CompiledStatement): Driver.Sync.PreparedStatement;
|
|
14
12
|
schemaInstructions(tableName: string): SchemaInstructions | undefined;
|
|
15
13
|
export(): Uint8Array;
|
|
16
14
|
}
|
package/dist/driver/sql.js.js
CHANGED
|
@@ -2,41 +2,44 @@
|
|
|
2
2
|
import { Driver } from "../lib/Driver.js";
|
|
3
3
|
import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
|
|
4
4
|
import { SqliteSchema } from "../sqlite/SqliteSchema.js";
|
|
5
|
-
var
|
|
6
|
-
constructor(db) {
|
|
7
|
-
super(new SqliteFormatter());
|
|
5
|
+
var PreparedStatement = class {
|
|
6
|
+
constructor(db, stmt) {
|
|
8
7
|
this.db = db;
|
|
8
|
+
this.stmt = stmt;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
stmt.bind(params);
|
|
10
|
+
all(params) {
|
|
11
|
+
this.stmt.bind(params);
|
|
13
12
|
const res = [];
|
|
14
|
-
while (stmt.step())
|
|
15
|
-
res.push(stmt.getAsObject());
|
|
13
|
+
while (this.stmt.step())
|
|
14
|
+
res.push(this.stmt.getAsObject());
|
|
16
15
|
return res;
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const res = [];
|
|
22
|
-
while (stmt.step())
|
|
23
|
-
res.push(stmt.get());
|
|
24
|
-
return res;
|
|
17
|
+
run(params) {
|
|
18
|
+
this.stmt.run(params);
|
|
19
|
+
return { rowsAffected: this.db.getRowsModified() };
|
|
25
20
|
}
|
|
26
|
-
|
|
27
|
-
this.
|
|
21
|
+
get(params) {
|
|
22
|
+
return this.all(params)[0];
|
|
28
23
|
}
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
|
|
24
|
+
execute(params) {
|
|
25
|
+
this.stmt.run(params);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var SqlJsDriver = class extends Driver.Sync {
|
|
29
|
+
constructor(db) {
|
|
30
|
+
super(new SqliteFormatter());
|
|
31
|
+
this.db = db;
|
|
32
|
+
this.tableData = this.prepare(SqliteSchema.tableData);
|
|
33
|
+
this.indexData = this.prepare(SqliteSchema.indexData);
|
|
34
|
+
}
|
|
35
|
+
tableData;
|
|
36
|
+
indexData;
|
|
37
|
+
prepareStatement(stmt) {
|
|
38
|
+
return new PreparedStatement(this.db, this.db.prepare(stmt.sql));
|
|
32
39
|
}
|
|
33
40
|
schemaInstructions(tableName) {
|
|
34
|
-
const columnData = this.
|
|
35
|
-
|
|
36
|
-
);
|
|
37
|
-
const indexData = this.rows(
|
|
38
|
-
SqliteSchema.indexData(tableName).compile(this.formatter)
|
|
39
|
-
);
|
|
41
|
+
const columnData = this.tableData(tableName);
|
|
42
|
+
const indexData = this.indexData(tableName);
|
|
40
43
|
return SqliteSchema.createInstructions(columnData, indexData);
|
|
41
44
|
}
|
|
42
45
|
export() {
|
package/dist/driver/sqlite3.d.ts
CHANGED
|
@@ -2,18 +2,16 @@ import type { Database } from 'sqlite3';
|
|
|
2
2
|
import { Driver } from '../lib/Driver';
|
|
3
3
|
import { Query } from '../lib/Query';
|
|
4
4
|
import { SchemaInstructions } from '../lib/Schema';
|
|
5
|
-
import {
|
|
5
|
+
import { CompiledStatement } from '../lib/Statement';
|
|
6
|
+
import { SqliteSchema } from '../sqlite/SqliteSchema';
|
|
6
7
|
export declare class Sqlite3Driver extends Driver.Async {
|
|
7
8
|
private db;
|
|
8
9
|
lock: Promise<void> | undefined;
|
|
10
|
+
tableData: (tableName: string) => Promise<Array<SqliteSchema.Column>>;
|
|
11
|
+
indexData: (tableName: string) => Promise<Array<SqliteSchema.Index>>;
|
|
9
12
|
constructor(db: Database);
|
|
10
|
-
executeQuery<T>(query: Query<T
|
|
11
|
-
|
|
12
|
-
values(stmt: Statement.Compiled): Promise<Array<Array<any>>>;
|
|
13
|
-
execute([sql, params]: Statement.Compiled): Promise<void>;
|
|
14
|
-
mutate([sql, params]: Statement.Compiled): Promise<{
|
|
15
|
-
rowsAffected: number;
|
|
16
|
-
}>;
|
|
13
|
+
executeQuery<T>(query: Query<T>, stmt?: Driver.Async.PreparedStatement, params?: any[] | undefined): Promise<T>;
|
|
14
|
+
prepareStatement(stmt: CompiledStatement): Driver.Async.PreparedStatement;
|
|
17
15
|
schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
18
16
|
isolate(): [connection: Driver.Async, release: () => Promise<void>];
|
|
19
17
|
}
|
package/dist/driver/sqlite3.js
CHANGED
|
@@ -2,20 +2,13 @@
|
|
|
2
2
|
import { Driver } from "../lib/Driver.js";
|
|
3
3
|
import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
|
|
4
4
|
import { SqliteSchema } from "../sqlite/SqliteSchema.js";
|
|
5
|
-
var
|
|
6
|
-
constructor(
|
|
7
|
-
|
|
8
|
-
this.db = db;
|
|
9
|
-
}
|
|
10
|
-
lock;
|
|
11
|
-
async executeQuery(query) {
|
|
12
|
-
await this.lock;
|
|
13
|
-
return super.executeQuery(query);
|
|
5
|
+
var PreparedStatement = class {
|
|
6
|
+
constructor(stmt) {
|
|
7
|
+
this.stmt = stmt;
|
|
14
8
|
}
|
|
15
|
-
|
|
9
|
+
all(params) {
|
|
16
10
|
return new Promise((resolve, reject) => {
|
|
17
|
-
|
|
18
|
-
stmt.all(params, (err, rows) => {
|
|
11
|
+
this.stmt.all(params, (err, rows) => {
|
|
19
12
|
if (err)
|
|
20
13
|
reject(err);
|
|
21
14
|
else
|
|
@@ -23,39 +16,57 @@ var Sqlite3Driver = class extends Driver.Async {
|
|
|
23
16
|
});
|
|
24
17
|
});
|
|
25
18
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
run(params) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
this.stmt.run(params, function(err) {
|
|
22
|
+
if (err)
|
|
23
|
+
reject(err);
|
|
24
|
+
else
|
|
25
|
+
resolve({ rowsAffected: this.changes });
|
|
26
|
+
});
|
|
27
|
+
});
|
|
29
28
|
}
|
|
30
|
-
|
|
29
|
+
get(params) {
|
|
31
30
|
return new Promise((resolve, reject) => {
|
|
32
|
-
|
|
33
|
-
stmt.run(params, (err) => {
|
|
31
|
+
this.stmt.get(params, (err, row) => {
|
|
34
32
|
if (err)
|
|
35
33
|
reject(err);
|
|
36
34
|
else
|
|
37
|
-
resolve();
|
|
35
|
+
resolve(row);
|
|
38
36
|
});
|
|
39
37
|
});
|
|
40
38
|
}
|
|
41
|
-
|
|
39
|
+
execute(params) {
|
|
42
40
|
return new Promise((resolve, reject) => {
|
|
43
|
-
|
|
44
|
-
stmt.run(params, function(err) {
|
|
41
|
+
this.stmt.run(params, (err) => {
|
|
45
42
|
if (err)
|
|
46
43
|
reject(err);
|
|
47
44
|
else
|
|
48
|
-
resolve(
|
|
45
|
+
resolve();
|
|
49
46
|
});
|
|
50
47
|
});
|
|
51
48
|
}
|
|
49
|
+
};
|
|
50
|
+
var Sqlite3Driver = class extends Driver.Async {
|
|
51
|
+
constructor(db) {
|
|
52
|
+
super(new SqliteFormatter());
|
|
53
|
+
this.db = db;
|
|
54
|
+
this.tableData = this.prepare(SqliteSchema.tableData);
|
|
55
|
+
this.indexData = this.prepare(SqliteSchema.indexData);
|
|
56
|
+
}
|
|
57
|
+
lock;
|
|
58
|
+
tableData;
|
|
59
|
+
indexData;
|
|
60
|
+
async executeQuery(query, stmt, params) {
|
|
61
|
+
await this.lock;
|
|
62
|
+
return super.executeQuery(query, stmt, params);
|
|
63
|
+
}
|
|
64
|
+
prepareStatement(stmt) {
|
|
65
|
+
return new PreparedStatement(this.db.prepare(stmt.sql));
|
|
66
|
+
}
|
|
52
67
|
async schemaInstructions(tableName) {
|
|
53
|
-
const columnData = await this.
|
|
54
|
-
|
|
55
|
-
);
|
|
56
|
-
const indexData = await this.rows(
|
|
57
|
-
SqliteSchema.indexData(tableName).compile(this.formatter)
|
|
58
|
-
);
|
|
68
|
+
const columnData = await this.tableData(tableName);
|
|
69
|
+
const indexData = await this.indexData(tableName);
|
|
59
70
|
return SqliteSchema.createInstructions(columnData, indexData);
|
|
60
71
|
}
|
|
61
72
|
isolate() {
|
package/dist/lib/Cursor.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { Update as UpdateSet } from './Update';
|
|
|
8
8
|
export declare class Cursor<T> {
|
|
9
9
|
[Selection.__cursorType](): T;
|
|
10
10
|
constructor(query: Query<T>);
|
|
11
|
+
static all(strings: TemplateStringsArray, ...params: Array<any>): Cursor<any>;
|
|
11
12
|
query(): Query<T>;
|
|
12
13
|
toJSON(): Query<T>;
|
|
13
14
|
}
|
|
@@ -19,7 +20,7 @@ export declare namespace Cursor {
|
|
|
19
20
|
class Filterable<T> extends Limitable<T> {
|
|
20
21
|
where(...where: Array<EV<boolean>>): Filterable<T>;
|
|
21
22
|
}
|
|
22
|
-
class Delete
|
|
23
|
+
class Delete extends Filterable<{
|
|
23
24
|
rowsAffected: number;
|
|
24
25
|
}> {
|
|
25
26
|
query(): Query.Delete;
|
package/dist/lib/Cursor.js
CHANGED
package/dist/lib/Driver.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Cursor } from './Cursor';
|
|
2
|
+
import { Expr } from './Expr';
|
|
2
3
|
import { Formatter } from './Formatter';
|
|
3
4
|
import { Query } from './Query';
|
|
4
5
|
import { SchemaInstructions } from './Schema';
|
|
5
|
-
import {
|
|
6
|
+
import { CompiledStatement } from './Statement';
|
|
6
7
|
import { Table } from './Table';
|
|
7
8
|
declare class Callable extends Function {
|
|
8
9
|
constructor(fn: Function);
|
|
@@ -10,12 +11,18 @@ declare class Callable extends Function {
|
|
|
10
11
|
declare abstract class DriverBase extends Callable {
|
|
11
12
|
formatter: Formatter;
|
|
12
13
|
constructor(formatter: Formatter);
|
|
14
|
+
compile<T extends Array<Expr<any>>, R>(create: (...params: T) => Cursor<R>): [Query<T>, CompiledStatement];
|
|
13
15
|
all(...args: Array<any>): any;
|
|
14
16
|
get(...args: Array<any>): any;
|
|
15
17
|
sql(strings: TemplateStringsArray, ...params: Array<any>): Cursor<unknown>;
|
|
16
18
|
executeTemplate(expectedReturn: Query.RawReturn, strings: TemplateStringsArray, ...params: Array<any>): any;
|
|
17
19
|
abstract executeQuery(query: Query<any>): any;
|
|
18
20
|
}
|
|
21
|
+
type ParamTypes<Params extends [...any[]]> = {
|
|
22
|
+
[K in keyof Params]: Params[K] extends Expr<infer T> ? T : never;
|
|
23
|
+
} & {
|
|
24
|
+
length: Params['length'];
|
|
25
|
+
};
|
|
19
26
|
interface SyncDriver {
|
|
20
27
|
<T>(query: Cursor<T>): T;
|
|
21
28
|
<T>(...queries: Array<Cursor<any>>): T;
|
|
@@ -23,17 +30,13 @@ interface SyncDriver {
|
|
|
23
30
|
}
|
|
24
31
|
declare abstract class SyncDriver extends DriverBase {
|
|
25
32
|
transactionId: number;
|
|
26
|
-
abstract
|
|
27
|
-
abstract values(stmt: Statement.Compiled): Array<Array<any>>;
|
|
28
|
-
abstract execute(stmt: Statement.Compiled): void;
|
|
29
|
-
abstract mutate(stmt: Statement.Compiled): {
|
|
30
|
-
rowsAffected: number;
|
|
31
|
-
};
|
|
33
|
+
abstract prepareStatement(stmt: CompiledStatement): SyncPreparedStatement;
|
|
32
34
|
abstract schemaInstructions(tableName: string): SchemaInstructions | undefined;
|
|
35
|
+
prepare<T extends Array<Expr<any>>, R>(create: (...params: T) => Cursor<R>): (...params: ParamTypes<T>) => R;
|
|
33
36
|
migrateSchema(...tables: Array<Table<any>>): unknown;
|
|
34
|
-
executeQuery<T>(query: Query<T>): T;
|
|
37
|
+
executeQuery<T>(query: Query<T>, stmt?: SyncPreparedStatement, params?: Array<any>): T;
|
|
35
38
|
all<T>(cursor: Cursor.SelectSingle<T>): Array<T>;
|
|
36
|
-
all<T>(cursor: Cursor<T>):
|
|
39
|
+
all<T>(cursor: Cursor<T>): T;
|
|
37
40
|
all<T>(strings: TemplateStringsArray, ...params: Array<any>): Array<T>;
|
|
38
41
|
get<T>(cursor: Cursor.SelectMultiple<T>): T | null;
|
|
39
42
|
get<T>(cursor: Cursor<T>): T;
|
|
@@ -41,6 +44,14 @@ declare abstract class SyncDriver extends DriverBase {
|
|
|
41
44
|
transaction<T>(run: (query: SyncDriver) => T): T;
|
|
42
45
|
toAsync(): SyncWrapper;
|
|
43
46
|
}
|
|
47
|
+
interface SyncPreparedStatement {
|
|
48
|
+
run(params?: Array<any>): {
|
|
49
|
+
rowsAffected: number;
|
|
50
|
+
};
|
|
51
|
+
all<T>(params?: Array<any>): Array<T>;
|
|
52
|
+
get<T>(params?: Array<any>): T;
|
|
53
|
+
execute(params?: Array<any>): void;
|
|
54
|
+
}
|
|
44
55
|
interface AsyncDriver {
|
|
45
56
|
<T>(query: Cursor<T>): Promise<T>;
|
|
46
57
|
<T>(...queries: Array<Cursor<any>>): Promise<T>;
|
|
@@ -49,15 +60,11 @@ interface AsyncDriver {
|
|
|
49
60
|
declare abstract class AsyncDriver extends DriverBase {
|
|
50
61
|
transactionId: number;
|
|
51
62
|
abstract isolate(): [connection: AsyncDriver, release: () => Promise<void>];
|
|
52
|
-
abstract
|
|
53
|
-
abstract values(stmt: Statement.Compiled): Promise<Array<Array<any>>>;
|
|
54
|
-
abstract execute(stmt: Statement.Compiled): Promise<void>;
|
|
55
|
-
abstract mutate(stmt: Statement.Compiled): Promise<{
|
|
56
|
-
rowsAffected: number;
|
|
57
|
-
}>;
|
|
63
|
+
abstract prepareStatement(stmt: CompiledStatement): AsyncPreparedStatement;
|
|
58
64
|
abstract schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
65
|
+
prepare<T extends Array<Expr<any>>, R>(create: (...params: T) => Cursor<R>): (...params: ParamTypes<T>) => Promise<R>;
|
|
59
66
|
migrateSchema(...tables: Array<Table<any>>): Promise<unknown>;
|
|
60
|
-
executeQuery<T>(query: Query<T>): Promise<T>;
|
|
67
|
+
executeQuery<T>(query: Query<T>, stmt?: AsyncPreparedStatement, params?: Array<any>): Promise<T>;
|
|
61
68
|
all<T>(cursor: Cursor.SelectSingle<T>): Promise<Array<T>>;
|
|
62
69
|
all<T>(cursor: Cursor<T>): Promise<T>;
|
|
63
70
|
all<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<Array<T>>;
|
|
@@ -70,20 +77,29 @@ declare class SyncWrapper extends AsyncDriver {
|
|
|
70
77
|
private sync;
|
|
71
78
|
lock: Promise<void> | undefined;
|
|
72
79
|
constructor(sync: SyncDriver);
|
|
73
|
-
executeQuery<T>(query: Query<T>): Promise<T>;
|
|
74
|
-
|
|
75
|
-
values(stmt: Statement.Compiled): Promise<Array<Array<any>>>;
|
|
76
|
-
execute(stmt: Statement.Compiled): Promise<void>;
|
|
77
|
-
mutate(stmt: Statement.Compiled): Promise<{
|
|
78
|
-
rowsAffected: number;
|
|
79
|
-
}>;
|
|
80
|
+
executeQuery<T>(query: Query<T>, stmt?: AsyncPreparedStatement, params?: Array<any>): Promise<T>;
|
|
81
|
+
prepareStatement(stmt: CompiledStatement): AsyncPreparedStatement;
|
|
80
82
|
schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
81
83
|
isolate(): [connection: AsyncDriver, release: () => Promise<void>];
|
|
82
84
|
}
|
|
85
|
+
interface AsyncPreparedStatement {
|
|
86
|
+
run(params?: Array<any>): Promise<{
|
|
87
|
+
rowsAffected: number;
|
|
88
|
+
}>;
|
|
89
|
+
all<T>(params?: Array<any>): Promise<Array<T>>;
|
|
90
|
+
get<T>(params?: Array<any>): Promise<T>;
|
|
91
|
+
execute(params?: Array<any>): Promise<void>;
|
|
92
|
+
}
|
|
83
93
|
export declare namespace Driver {
|
|
84
94
|
type Sync = SyncDriver;
|
|
85
95
|
const Sync: typeof SyncDriver;
|
|
96
|
+
namespace Sync {
|
|
97
|
+
type PreparedStatement = SyncPreparedStatement;
|
|
98
|
+
}
|
|
86
99
|
type Async = AsyncDriver;
|
|
87
100
|
const Async: typeof AsyncDriver;
|
|
101
|
+
namespace Async {
|
|
102
|
+
type PreparedStatement = AsyncPreparedStatement;
|
|
103
|
+
}
|
|
88
104
|
}
|
|
89
105
|
export {};
|
package/dist/lib/Driver.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// src/lib/Driver.ts
|
|
2
2
|
import { Cursor } from "./Cursor.js";
|
|
3
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
+
import { ParamData } from "./Param.js";
|
|
3
5
|
import { Query, QueryType } from "./Query.js";
|
|
4
6
|
import { Schema } from "./Schema.js";
|
|
5
7
|
var Callable = class extends Function {
|
|
@@ -31,6 +33,16 @@ var DriverBase = class extends Callable {
|
|
|
31
33
|
});
|
|
32
34
|
this.formatter = formatter;
|
|
33
35
|
}
|
|
36
|
+
compile(create) {
|
|
37
|
+
const { length } = create;
|
|
38
|
+
const paramNames = Array.from({ length }, (_, i) => `p${i}`);
|
|
39
|
+
const params = paramNames.map(
|
|
40
|
+
(name) => new Expr(ExprData.Param(ParamData.Named(name)))
|
|
41
|
+
);
|
|
42
|
+
const cursor = create(...params);
|
|
43
|
+
const query = cursor.query();
|
|
44
|
+
return [query, this.formatter.compile(query)];
|
|
45
|
+
}
|
|
34
46
|
all(...args) {
|
|
35
47
|
const [input, ...rest] = args;
|
|
36
48
|
if (input instanceof Cursor.SelectSingle)
|
|
@@ -56,6 +68,16 @@ var DriverBase = class extends Callable {
|
|
|
56
68
|
};
|
|
57
69
|
var SyncDriver = class extends DriverBase {
|
|
58
70
|
transactionId = 0;
|
|
71
|
+
prepare(create) {
|
|
72
|
+
const [query, compiled] = this.compile(create);
|
|
73
|
+
const prepared = this.prepareStatement(compiled);
|
|
74
|
+
return (...params) => {
|
|
75
|
+
const namedParams = Object.fromEntries(
|
|
76
|
+
params.map((value, i) => [`p${i}`, value])
|
|
77
|
+
);
|
|
78
|
+
return this.executeQuery(query, prepared, compiled.params(namedParams));
|
|
79
|
+
};
|
|
80
|
+
}
|
|
59
81
|
migrateSchema(...tables) {
|
|
60
82
|
const queries = [];
|
|
61
83
|
for (const table of Object.values(tables)) {
|
|
@@ -71,7 +93,7 @@ var SyncDriver = class extends DriverBase {
|
|
|
71
93
|
}
|
|
72
94
|
return this.executeQuery(Query.Batch({ queries }));
|
|
73
95
|
}
|
|
74
|
-
executeQuery(query) {
|
|
96
|
+
executeQuery(query, stmt, params) {
|
|
75
97
|
switch (query.type) {
|
|
76
98
|
case QueryType.Batch:
|
|
77
99
|
let result;
|
|
@@ -86,24 +108,26 @@ var SyncDriver = class extends DriverBase {
|
|
|
86
108
|
return result;
|
|
87
109
|
});
|
|
88
110
|
default:
|
|
89
|
-
const
|
|
111
|
+
const compiled = stmt ? void 0 : this.formatter.compile(query);
|
|
112
|
+
stmt = stmt || this.prepareStatement(compiled);
|
|
113
|
+
params = params || compiled.params();
|
|
90
114
|
if ("selection" in query) {
|
|
91
|
-
const res =
|
|
115
|
+
const res = stmt.all(params).map((row) => JSON.parse(row.result).result);
|
|
92
116
|
if (query.singleResult)
|
|
93
117
|
return res[0];
|
|
94
118
|
return res;
|
|
95
119
|
} else if (query.type === QueryType.Raw) {
|
|
96
120
|
switch (query.expectedReturn) {
|
|
97
121
|
case "row":
|
|
98
|
-
return
|
|
122
|
+
return stmt.all(params)[0];
|
|
99
123
|
case "rows":
|
|
100
|
-
return
|
|
124
|
+
return stmt.all(params);
|
|
101
125
|
default:
|
|
102
|
-
|
|
126
|
+
stmt.execute(params);
|
|
103
127
|
return void 0;
|
|
104
128
|
}
|
|
105
129
|
} else {
|
|
106
|
-
return
|
|
130
|
+
return stmt.run(params);
|
|
107
131
|
}
|
|
108
132
|
}
|
|
109
133
|
}
|
|
@@ -137,6 +161,16 @@ var SyncDriver = class extends DriverBase {
|
|
|
137
161
|
};
|
|
138
162
|
var AsyncDriver = class extends DriverBase {
|
|
139
163
|
transactionId = 0;
|
|
164
|
+
prepare(create) {
|
|
165
|
+
const [query, compiled] = this.compile(create);
|
|
166
|
+
const prepared = this.prepareStatement(compiled);
|
|
167
|
+
return (...params) => {
|
|
168
|
+
const namedParams = Object.fromEntries(
|
|
169
|
+
params.map((value, i) => [`p${i}`, value])
|
|
170
|
+
);
|
|
171
|
+
return this.executeQuery(query, prepared, compiled.params(namedParams));
|
|
172
|
+
};
|
|
173
|
+
}
|
|
140
174
|
async migrateSchema(...tables) {
|
|
141
175
|
const queries = [];
|
|
142
176
|
for (const table of Object.values(tables)) {
|
|
@@ -152,7 +186,7 @@ var AsyncDriver = class extends DriverBase {
|
|
|
152
186
|
}
|
|
153
187
|
return this.executeQuery(Query.Batch({ queries }));
|
|
154
188
|
}
|
|
155
|
-
async executeQuery(query) {
|
|
189
|
+
async executeQuery(query, stmt, params) {
|
|
156
190
|
switch (query.type) {
|
|
157
191
|
case QueryType.Batch:
|
|
158
192
|
let result;
|
|
@@ -167,10 +201,12 @@ var AsyncDriver = class extends DriverBase {
|
|
|
167
201
|
return result;
|
|
168
202
|
});
|
|
169
203
|
default:
|
|
170
|
-
const
|
|
204
|
+
const compiled = stmt ? void 0 : this.formatter.compile(query);
|
|
205
|
+
stmt = stmt || this.prepareStatement(compiled);
|
|
206
|
+
params = params || compiled.params();
|
|
171
207
|
if ("selection" in query) {
|
|
172
|
-
const res = (await
|
|
173
|
-
(
|
|
208
|
+
const res = (await stmt.all(params)).map(
|
|
209
|
+
(item) => JSON.parse(item.result).result
|
|
174
210
|
);
|
|
175
211
|
if (query.singleResult)
|
|
176
212
|
return res[0];
|
|
@@ -178,15 +214,15 @@ var AsyncDriver = class extends DriverBase {
|
|
|
178
214
|
} else if (query.type === QueryType.Raw) {
|
|
179
215
|
switch (query.expectedReturn) {
|
|
180
216
|
case "row":
|
|
181
|
-
return (await
|
|
217
|
+
return (await stmt.all(params))[0];
|
|
182
218
|
case "rows":
|
|
183
|
-
return await
|
|
219
|
+
return await stmt.all(params);
|
|
184
220
|
default:
|
|
185
|
-
await
|
|
221
|
+
await stmt.execute(params);
|
|
186
222
|
return void 0;
|
|
187
223
|
}
|
|
188
224
|
} else {
|
|
189
|
-
return await
|
|
225
|
+
return await stmt.run(params);
|
|
190
226
|
}
|
|
191
227
|
}
|
|
192
228
|
}
|
|
@@ -218,27 +254,35 @@ var AsyncDriver = class extends DriverBase {
|
|
|
218
254
|
}
|
|
219
255
|
}
|
|
220
256
|
};
|
|
257
|
+
var SyncPreparedStatementWrapper = class {
|
|
258
|
+
constructor(stmt) {
|
|
259
|
+
this.stmt = stmt;
|
|
260
|
+
}
|
|
261
|
+
async run(params) {
|
|
262
|
+
return this.stmt.run(params);
|
|
263
|
+
}
|
|
264
|
+
async all(params) {
|
|
265
|
+
return this.stmt.all(params);
|
|
266
|
+
}
|
|
267
|
+
async get(params) {
|
|
268
|
+
return this.stmt.get(params);
|
|
269
|
+
}
|
|
270
|
+
async execute(params) {
|
|
271
|
+
return this.stmt.execute(params);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
221
274
|
var SyncWrapper = class extends AsyncDriver {
|
|
222
275
|
constructor(sync) {
|
|
223
276
|
super(sync.formatter);
|
|
224
277
|
this.sync = sync;
|
|
225
278
|
}
|
|
226
279
|
lock;
|
|
227
|
-
async executeQuery(query) {
|
|
280
|
+
async executeQuery(query, stmt, params) {
|
|
228
281
|
await this.lock;
|
|
229
|
-
return super.executeQuery(query);
|
|
230
|
-
}
|
|
231
|
-
async rows(stmt) {
|
|
232
|
-
return this.sync.rows(stmt);
|
|
233
|
-
}
|
|
234
|
-
async values(stmt) {
|
|
235
|
-
return this.sync.values(stmt);
|
|
236
|
-
}
|
|
237
|
-
async execute(stmt) {
|
|
238
|
-
return this.sync.execute(stmt);
|
|
282
|
+
return super.executeQuery(query, stmt, params);
|
|
239
283
|
}
|
|
240
|
-
|
|
241
|
-
return this.sync.
|
|
284
|
+
prepareStatement(stmt) {
|
|
285
|
+
return new SyncPreparedStatementWrapper(this.sync.prepareStatement(stmt));
|
|
242
286
|
}
|
|
243
287
|
async schemaInstructions(tableName) {
|
|
244
288
|
return this.sync.schemaInstructions(tableName);
|
package/dist/lib/Formatter.d.ts
CHANGED
|
@@ -16,10 +16,11 @@ export declare abstract class Formatter implements Sanitizer {
|
|
|
16
16
|
constructor();
|
|
17
17
|
abstract escapeValue(value: any): string;
|
|
18
18
|
abstract escapeIdentifier(ident: string): string;
|
|
19
|
+
abstract formatParamValue(paramValue: any): any;
|
|
19
20
|
abstract formatSqlAccess(on: Statement, field: string): Statement;
|
|
20
21
|
abstract formatJsonAccess(on: Statement, field: string): Statement;
|
|
21
22
|
formatAccess(on: Statement, field: string, formatAsJson?: boolean): Statement;
|
|
22
|
-
compile<T>(query: Query<T>, formatInline?: boolean): Statement.
|
|
23
|
+
compile<T>(query: Query<T>, formatInline?: boolean): import("./Statement").CompiledStatement;
|
|
23
24
|
format<T>(query: Query<T>, ctx?: FormatContext): Statement;
|
|
24
25
|
formatSelect(query: Query.Select, ctx: FormatContext): Statement;
|
|
25
26
|
formatInsert(query: Query.Insert, ctx: FormatContext): Statement;
|
package/dist/lib/Formatter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/lib/Formatter.ts
|
|
2
2
|
import { ColumnType } from "./Column.js";
|
|
3
|
-
import { BinOp, ExprData, ExprType, UnOp } from "./Expr.js";
|
|
3
|
+
import { BinOp, Expr, ExprData, ExprType, UnOp } from "./Expr.js";
|
|
4
4
|
import { OrderDirection } from "./OrderBy.js";
|
|
5
5
|
import { ParamType } from "./Param.js";
|
|
6
6
|
import { Query, QueryType } from "./Query.js";
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
call,
|
|
10
10
|
identifier,
|
|
11
11
|
newline,
|
|
12
|
+
param,
|
|
12
13
|
parenthesis,
|
|
13
14
|
raw,
|
|
14
15
|
separated,
|
|
@@ -163,7 +164,14 @@ var Formatter = class {
|
|
|
163
164
|
);
|
|
164
165
|
}
|
|
165
166
|
formatRaw({ strings, params }, ctx) {
|
|
166
|
-
return Statement.tag(
|
|
167
|
+
return Statement.tag(
|
|
168
|
+
strings,
|
|
169
|
+
params.map((param2) => {
|
|
170
|
+
if (param2 instanceof Expr)
|
|
171
|
+
return this.formatExpr(param2.expr, ctx);
|
|
172
|
+
return this.formatValue(param2, ctx);
|
|
173
|
+
})
|
|
174
|
+
);
|
|
167
175
|
}
|
|
168
176
|
formatColumn(column) {
|
|
169
177
|
return identifier(column.name).add(this.formatType(column.type)).addIf(column.unique, "UNIQUE").addIf(column.autoIncrement, "AUTOINCREMENT").addIf(column.primaryKey, "PRIMARY KEY").addIf(!column.nullable, "NOT NULL").addIf(
|
|
@@ -209,6 +217,8 @@ var Formatter = class {
|
|
|
209
217
|
);
|
|
210
218
|
}
|
|
211
219
|
formatColumnValue(column, columnValue) {
|
|
220
|
+
if (columnValue instanceof Expr)
|
|
221
|
+
return this.formatExprValue(columnValue.expr, {});
|
|
212
222
|
const isNull = columnValue === void 0 || columnValue === null;
|
|
213
223
|
const isOptional = column.nullable || column.autoIncrement || column.primaryKey;
|
|
214
224
|
if (isNull) {
|
|
@@ -231,7 +241,7 @@ var Formatter = class {
|
|
|
231
241
|
case ColumnType.Boolean:
|
|
232
242
|
if (typeof columnValue !== "boolean")
|
|
233
243
|
throw new TypeError(`Expected boolean for column ${column.name}`);
|
|
234
|
-
return
|
|
244
|
+
return raw(this.escapeValue(columnValue));
|
|
235
245
|
case ColumnType.Json:
|
|
236
246
|
if (typeof columnValue !== "object")
|
|
237
247
|
throw new TypeError(`Expected object for column ${column.name}`);
|
|
@@ -409,7 +419,7 @@ var Formatter = class {
|
|
|
409
419
|
case ParamType.Value:
|
|
410
420
|
return this.formatValue(expr.param.value, ctx);
|
|
411
421
|
case ParamType.Named:
|
|
412
|
-
|
|
422
|
+
return param(expr.param.name);
|
|
413
423
|
}
|
|
414
424
|
case ExprType.Field:
|
|
415
425
|
return this.formatField(expr.expr, expr.field, ctx);
|
package/dist/lib/Index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Expr, ExprData } from './Expr';
|
|
2
2
|
interface PartialIndexData {
|
|
3
3
|
on: Array<ExprData>;
|
|
4
|
-
where?: ExprData;
|
|
5
4
|
unique?: boolean;
|
|
5
|
+
where?: ExprData;
|
|
6
6
|
}
|
|
7
7
|
export interface IndexData extends PartialIndexData {
|
|
8
8
|
name: string;
|
|
@@ -10,8 +10,8 @@ export interface IndexData extends PartialIndexData {
|
|
|
10
10
|
export declare class Index {
|
|
11
11
|
data: PartialIndexData;
|
|
12
12
|
constructor(data: PartialIndexData);
|
|
13
|
-
where(where: Expr<boolean>): Index;
|
|
14
13
|
unique(): Index;
|
|
14
|
+
where(where: Expr<boolean>): Index;
|
|
15
15
|
}
|
|
16
16
|
export declare function index(...on: Array<Expr<any>>): Index;
|
|
17
17
|
export {};
|
package/dist/lib/Index.js
CHANGED
|
@@ -4,12 +4,12 @@ var Index = class {
|
|
|
4
4
|
constructor(data) {
|
|
5
5
|
this.data = data;
|
|
6
6
|
}
|
|
7
|
-
where(where) {
|
|
8
|
-
return new Index({ ...this.data, where: ExprData.create(where) });
|
|
9
|
-
}
|
|
10
7
|
unique() {
|
|
11
8
|
return new Index({ ...this.data, unique: true });
|
|
12
9
|
}
|
|
10
|
+
where(where) {
|
|
11
|
+
return new Index({ ...this.data, where: ExprData.create(where) });
|
|
12
|
+
}
|
|
13
13
|
};
|
|
14
14
|
function index(...on) {
|
|
15
15
|
return new Index({ on: on.map(ExprData.create) });
|
package/dist/lib/Ops.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ export declare function selectAll<Row>(table: Table<Row>): Cursor.SelectMultiple
|
|
|
4
4
|
export declare function selectFirst<Row>(table: Table<Row>): Cursor.SelectSingle<Row>;
|
|
5
5
|
export declare function update<Row>(table: Table<Row>): Cursor.Update<Row>;
|
|
6
6
|
export declare function insertInto<Row>(table: Table<Row>): Cursor.Insert<Row>;
|
|
7
|
-
export declare function deleteFrom<Row>(table: Table<Row>): Cursor.Delete
|
|
7
|
+
export declare function deleteFrom<Row>(table: Table<Row>): Cursor.Delete;
|
|
8
8
|
export declare function create(...tables: Array<Table<any>>): Cursor.Batch;
|
package/dist/lib/Param.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Expr } from './Expr';
|
|
1
2
|
export declare enum ParamType {
|
|
2
3
|
Value = "Value",
|
|
3
4
|
Named = "Named"
|
|
@@ -13,3 +14,7 @@ export declare const ParamData: {
|
|
|
13
14
|
Value(value: any): ParamData;
|
|
14
15
|
Named(name: string): ParamData;
|
|
15
16
|
};
|
|
17
|
+
export type Params<T> = {
|
|
18
|
+
[K in keyof T]: Expr<T[K]>;
|
|
19
|
+
};
|
|
20
|
+
export declare function createParams<T>(): Params<T>;
|
package/dist/lib/Param.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/lib/Param.ts
|
|
2
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
2
3
|
var ParamType = /* @__PURE__ */ ((ParamType2) => {
|
|
3
4
|
ParamType2["Value"] = "Value";
|
|
4
5
|
ParamType2["Named"] = "Named";
|
|
@@ -12,7 +13,15 @@ var ParamData = {
|
|
|
12
13
|
return { type: "Named" /* Named */, name };
|
|
13
14
|
}
|
|
14
15
|
};
|
|
16
|
+
function createParams() {
|
|
17
|
+
return new Proxy({}, {
|
|
18
|
+
get(target, prop) {
|
|
19
|
+
return new Expr(ExprData.Param(ParamData.Named(prop)));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
15
23
|
export {
|
|
16
24
|
ParamData,
|
|
17
|
-
ParamType
|
|
25
|
+
ParamType,
|
|
26
|
+
createParams
|
|
18
27
|
};
|
package/dist/lib/Sanitizer.d.ts
CHANGED
package/dist/lib/Schema.js
CHANGED
|
@@ -27,7 +27,7 @@ var Schema;
|
|
|
27
27
|
} else if (!schemaCol) {
|
|
28
28
|
res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
|
|
29
29
|
} else {
|
|
30
|
-
const
|
|
30
|
+
const { sql: instruction } = formatter.formatColumn({ ...schemaCol, references: void 0 }).compile(formatter);
|
|
31
31
|
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
32
32
|
res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
|
|
33
33
|
}
|
|
@@ -45,7 +45,7 @@ var Schema;
|
|
|
45
45
|
} else if (!schemaIndex) {
|
|
46
46
|
res.unshift(Query.DropIndex({ table: schema, name: indexName }));
|
|
47
47
|
} else {
|
|
48
|
-
const
|
|
48
|
+
const { sql: instruction } = formatter.formatCreateIndex(
|
|
49
49
|
Query.CreateIndex({ table: schema, index: schemaIndex })
|
|
50
50
|
).compile(formatter);
|
|
51
51
|
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
package/dist/lib/Statement.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { ParamData } from './Param';
|
|
1
2
|
import { Sanitizer } from './Sanitizer';
|
|
2
3
|
declare enum TokenType {
|
|
3
4
|
Raw = "Raw",
|
|
4
5
|
Identifier = "Identifier",
|
|
5
6
|
Value = "Value",
|
|
7
|
+
Param = "Param",
|
|
6
8
|
Indent = "Indent",
|
|
7
9
|
Dedent = "Dedent"
|
|
8
10
|
}
|
|
@@ -15,6 +17,7 @@ declare class Token {
|
|
|
15
17
|
static Dedent(): Token;
|
|
16
18
|
static Identifier(data: string): Token;
|
|
17
19
|
static Value(data: any): Token;
|
|
20
|
+
static Param(name: string): Token;
|
|
18
21
|
}
|
|
19
22
|
export interface CompileOptions {
|
|
20
23
|
formatInline?: boolean;
|
|
@@ -25,7 +28,7 @@ export declare class Statement {
|
|
|
25
28
|
constructor(tokens: Array<Token>);
|
|
26
29
|
concat(...tokens: Array<Token | Statement>): Statement;
|
|
27
30
|
static create(from: string | Statement): Statement;
|
|
28
|
-
static tag(strings: ReadonlyArray<string>,
|
|
31
|
+
static tag(strings: ReadonlyArray<string>, params: Array<Statement>): Statement;
|
|
29
32
|
space(): Statement;
|
|
30
33
|
call(method: string, ...args: Array<Statement>): Statement;
|
|
31
34
|
addCall(method: string, ...args: Array<Statement>): Statement;
|
|
@@ -39,21 +42,28 @@ export declare class Statement {
|
|
|
39
42
|
addIdentifier(name: string): Statement;
|
|
40
43
|
value(value: any): Statement;
|
|
41
44
|
addValue(value: any): Statement;
|
|
45
|
+
param(name: string): Statement;
|
|
46
|
+
addParam(name: string): Statement;
|
|
42
47
|
raw(query: string): Statement;
|
|
43
48
|
parenthesis(inner: string | Statement): Statement;
|
|
44
49
|
addParenthesis(stmnt: string | Statement): Statement;
|
|
45
50
|
separated(input: Array<Statement>, separator?: string): Statement;
|
|
46
51
|
addSeparated(input: Array<Statement>, separator?: string): Statement;
|
|
47
52
|
isEmpty(): boolean;
|
|
48
|
-
compile(sanitizer: Sanitizer, formatInline?: boolean):
|
|
53
|
+
compile(sanitizer: Sanitizer, formatInline?: boolean): CompiledStatement;
|
|
49
54
|
}
|
|
50
|
-
export declare
|
|
51
|
-
|
|
55
|
+
export declare class CompiledStatement {
|
|
56
|
+
sql: string;
|
|
57
|
+
private paramData;
|
|
58
|
+
constructor(sql: string, paramData: Array<ParamData>);
|
|
59
|
+
params(input?: Record<string, any>): Array<any>;
|
|
60
|
+
toString(): string;
|
|
52
61
|
}
|
|
53
62
|
export declare function newline(): Statement;
|
|
54
63
|
export declare function raw(raw: string): Statement;
|
|
55
64
|
export declare function identifier(name: string): Statement;
|
|
56
65
|
export declare function value(value: any): Statement;
|
|
66
|
+
export declare function param(name: string): Statement;
|
|
57
67
|
export declare function empty(): Statement;
|
|
58
68
|
export declare function parenthesis(stmnt: Statement): Statement;
|
|
59
69
|
export declare function call(method: string, ...args: Array<Statement>): Statement;
|
package/dist/lib/Statement.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/lib/Statement.ts
|
|
2
|
+
import { ParamData, ParamType } from "./Param.js";
|
|
2
3
|
var Token = class {
|
|
3
4
|
constructor(type, data) {
|
|
4
5
|
this.type = type;
|
|
@@ -19,6 +20,9 @@ var Token = class {
|
|
|
19
20
|
static Value(data) {
|
|
20
21
|
return new Token("Value" /* Value */, data);
|
|
21
22
|
}
|
|
23
|
+
static Param(name) {
|
|
24
|
+
return new Token("Param" /* Param */, name);
|
|
25
|
+
}
|
|
22
26
|
};
|
|
23
27
|
var SEPARATE = ",";
|
|
24
28
|
var WHITESPACE = " ";
|
|
@@ -37,9 +41,12 @@ var Statement = class {
|
|
|
37
41
|
static create(from) {
|
|
38
42
|
return typeof from === "string" ? raw(from) : from;
|
|
39
43
|
}
|
|
40
|
-
static tag(strings,
|
|
44
|
+
static tag(strings, params) {
|
|
41
45
|
return new Statement(
|
|
42
|
-
strings.flatMap((s, i) =>
|
|
46
|
+
strings.flatMap((s, i) => {
|
|
47
|
+
const param2 = params[i];
|
|
48
|
+
return [Token.Raw(s)].concat(param2 ? param2.tokens : []);
|
|
49
|
+
})
|
|
43
50
|
);
|
|
44
51
|
}
|
|
45
52
|
space() {
|
|
@@ -93,6 +100,12 @@ var Statement = class {
|
|
|
93
100
|
addValue(value2) {
|
|
94
101
|
return this.space().value(value2);
|
|
95
102
|
}
|
|
103
|
+
param(name) {
|
|
104
|
+
return this.concat(Token.Param(name));
|
|
105
|
+
}
|
|
106
|
+
addParam(name) {
|
|
107
|
+
return this.space().param(name);
|
|
108
|
+
}
|
|
96
109
|
raw(query) {
|
|
97
110
|
if (!query)
|
|
98
111
|
return this;
|
|
@@ -118,7 +131,7 @@ var Statement = class {
|
|
|
118
131
|
return this.tokens.length === 0 || this.tokens.length === 1 && this.tokens[0].type === "Raw" /* Raw */ && this.tokens[0].data === "";
|
|
119
132
|
}
|
|
120
133
|
compile(sanitizer, formatInline = false) {
|
|
121
|
-
let sql = "",
|
|
134
|
+
let sql = "", paramData = [], indent = "";
|
|
122
135
|
for (const token of this.tokens) {
|
|
123
136
|
switch (token.type) {
|
|
124
137
|
case "Raw" /* Raw */:
|
|
@@ -135,9 +148,15 @@ var Statement = class {
|
|
|
135
148
|
sql += sanitizer.escapeValue(token.data);
|
|
136
149
|
} else {
|
|
137
150
|
sql += "?";
|
|
138
|
-
|
|
151
|
+
paramData.push(
|
|
152
|
+
ParamData.Value(sanitizer.formatParamValue(token.data))
|
|
153
|
+
);
|
|
139
154
|
}
|
|
140
155
|
break;
|
|
156
|
+
case "Param" /* Param */:
|
|
157
|
+
sql += "?";
|
|
158
|
+
paramData.push(ParamData.Named(token.data));
|
|
159
|
+
break;
|
|
141
160
|
case "Indent" /* Indent */:
|
|
142
161
|
indent += " ";
|
|
143
162
|
break;
|
|
@@ -146,7 +165,26 @@ var Statement = class {
|
|
|
146
165
|
break;
|
|
147
166
|
}
|
|
148
167
|
}
|
|
149
|
-
return
|
|
168
|
+
return new CompiledStatement(sql, paramData);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
var CompiledStatement = class {
|
|
172
|
+
constructor(sql, paramData) {
|
|
173
|
+
this.sql = sql;
|
|
174
|
+
this.paramData = paramData;
|
|
175
|
+
}
|
|
176
|
+
params(input) {
|
|
177
|
+
return this.paramData.map((param2) => {
|
|
178
|
+
if (param2.type === ParamType.Named) {
|
|
179
|
+
if (input && param2.name in input)
|
|
180
|
+
return input[param2.name];
|
|
181
|
+
throw new TypeError(`Missing parameter ${param2.name}`);
|
|
182
|
+
}
|
|
183
|
+
return param2.value;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
toString() {
|
|
187
|
+
return this.sql;
|
|
150
188
|
}
|
|
151
189
|
};
|
|
152
190
|
function newline() {
|
|
@@ -161,6 +199,9 @@ function identifier(name) {
|
|
|
161
199
|
function value(value2) {
|
|
162
200
|
return new Statement([Token.Value(value2)]);
|
|
163
201
|
}
|
|
202
|
+
function param(name) {
|
|
203
|
+
return new Statement([Token.Param(name)]);
|
|
204
|
+
}
|
|
164
205
|
function empty() {
|
|
165
206
|
return new Statement([]);
|
|
166
207
|
}
|
|
@@ -174,11 +215,13 @@ function separated(input, separator = SEPARATE) {
|
|
|
174
215
|
return empty().separated(input, separator);
|
|
175
216
|
}
|
|
176
217
|
export {
|
|
218
|
+
CompiledStatement,
|
|
177
219
|
Statement,
|
|
178
220
|
call,
|
|
179
221
|
empty,
|
|
180
222
|
identifier,
|
|
181
223
|
newline,
|
|
224
|
+
param,
|
|
182
225
|
parenthesis,
|
|
183
226
|
raw,
|
|
184
227
|
separated,
|
package/dist/lib/Table.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Column, PrimaryKey } from './Column';
|
|
2
2
|
import { Cursor } from './Cursor';
|
|
3
|
-
import { Expr } from './Expr';
|
|
3
|
+
import { EV, Expr } from './Expr';
|
|
4
4
|
import { Fields } from './Fields';
|
|
5
5
|
import { Index } from './Index';
|
|
6
6
|
import { Schema } from './Schema';
|
|
@@ -12,6 +12,7 @@ export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>>
|
|
|
12
12
|
insertOne(record: Table.Insert<T>): Cursor.Batch<Table.Normalize<T>>;
|
|
13
13
|
insertAll(data: Array<Table.Insert<T>>): Cursor.InsertValues;
|
|
14
14
|
set(data: Update<Table.Normalize<T>>): Cursor.Update<Table.Normalize<T>>;
|
|
15
|
+
delete(): Cursor.Delete;
|
|
15
16
|
createTable(): Cursor.Create;
|
|
16
17
|
as(alias: string): this;
|
|
17
18
|
alias(): Record<string, this>;
|
|
@@ -21,7 +22,7 @@ export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>>
|
|
|
21
22
|
}
|
|
22
23
|
export declare namespace Table {
|
|
23
24
|
type Intersection<A, B> = A & B extends infer U ? {
|
|
24
|
-
[P in keyof U]: U[P]
|
|
25
|
+
[P in keyof U]: EV<U[P]>;
|
|
25
26
|
} : never;
|
|
26
27
|
type OptionalKeys<T> = {
|
|
27
28
|
[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;
|
|
@@ -39,15 +40,15 @@ export declare namespace Table {
|
|
|
39
40
|
export type Infer<T> = T extends Table<infer U> ? Normalize<U> : never;
|
|
40
41
|
export {};
|
|
41
42
|
}
|
|
42
|
-
export
|
|
43
|
+
export type TableOptions<T, R> = {
|
|
43
44
|
name: string;
|
|
44
45
|
alias?: string;
|
|
45
46
|
columns: {
|
|
46
47
|
[K in keyof T]: Column<T[K]>;
|
|
47
48
|
};
|
|
48
49
|
indexes?: (this: Fields<T>) => Record<string, Index>;
|
|
49
|
-
}
|
|
50
|
-
export declare function table<T extends {}>(options: TableOptions<T>): Table<T> & Fields<T>;
|
|
50
|
+
};
|
|
51
|
+
export declare function table<T extends {}, R>(options: TableOptions<T, R>): Table<T> & Fields<T>;
|
|
51
52
|
export declare namespace table {
|
|
52
53
|
export type infer<T> = Table.Infer<T>;
|
|
53
54
|
type Extensions<T> = {
|
package/dist/lib/Table.js
CHANGED
|
@@ -52,6 +52,9 @@ var Table = class extends Cursor.SelectMultiple {
|
|
|
52
52
|
})
|
|
53
53
|
).set(data);
|
|
54
54
|
}
|
|
55
|
+
delete() {
|
|
56
|
+
return new Cursor.Delete(Query.Delete({ table: this.schema() }));
|
|
57
|
+
}
|
|
55
58
|
createTable() {
|
|
56
59
|
return new Cursor.Create(this.schema());
|
|
57
60
|
}
|
|
@@ -98,13 +101,7 @@ function table(options) {
|
|
|
98
101
|
) : {}
|
|
99
102
|
).map(([key, index]) => {
|
|
100
103
|
const indexName = `${schema.name}.${key}`;
|
|
101
|
-
return [
|
|
102
|
-
indexName,
|
|
103
|
-
{
|
|
104
|
-
name: indexName,
|
|
105
|
-
...index.data
|
|
106
|
-
}
|
|
107
|
-
];
|
|
104
|
+
return [indexName, { name: indexName, ...index.data }];
|
|
108
105
|
})
|
|
109
106
|
);
|
|
110
107
|
return new Table({
|
|
@@ -2,6 +2,7 @@ import { ExprData } from '../lib/Expr';
|
|
|
2
2
|
import { FormatContext, Formatter } from '../lib/Formatter';
|
|
3
3
|
import { Statement } from '../lib/Statement';
|
|
4
4
|
export declare class SqliteFormatter extends Formatter {
|
|
5
|
+
formatParamValue(paramValue: any): any;
|
|
5
6
|
escapeValue(value: any): string;
|
|
6
7
|
escapeIdentifier(input: string): string;
|
|
7
8
|
escapeString(input: string): string;
|
|
@@ -15,6 +15,17 @@ function escapeWithin(input, outer) {
|
|
|
15
15
|
return buf;
|
|
16
16
|
}
|
|
17
17
|
var SqliteFormatter = class extends Formatter {
|
|
18
|
+
formatParamValue(paramValue) {
|
|
19
|
+
if (paramValue === null || paramValue === void 0)
|
|
20
|
+
return null;
|
|
21
|
+
if (typeof paramValue === "boolean")
|
|
22
|
+
return paramValue ? 1 : 0;
|
|
23
|
+
if (typeof paramValue === "number")
|
|
24
|
+
return paramValue;
|
|
25
|
+
if (typeof paramValue === "string")
|
|
26
|
+
return paramValue;
|
|
27
|
+
return JSON.stringify(paramValue);
|
|
28
|
+
}
|
|
18
29
|
escapeValue(value) {
|
|
19
30
|
if (value === null || value === void 0)
|
|
20
31
|
return "null";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Cursor } from '../lib/Cursor';
|
|
2
|
+
import { Expr } from '../lib/Expr';
|
|
1
3
|
import { SchemaInstructions } from '../lib/Schema';
|
|
2
|
-
import { Statement } from '../lib/Statement';
|
|
3
4
|
export declare namespace SqliteSchema {
|
|
4
5
|
type Column = {
|
|
5
6
|
cid: number;
|
|
@@ -16,8 +17,8 @@ export declare namespace SqliteSchema {
|
|
|
16
17
|
rootpage: number;
|
|
17
18
|
sql: string;
|
|
18
19
|
}
|
|
19
|
-
function tableData(tableName: string):
|
|
20
|
-
function indexData(tableName: string):
|
|
20
|
+
function tableData(tableName: Expr<string>): Cursor<Array<SqliteSchema.Column>>;
|
|
21
|
+
function indexData(tableName: Expr<string>): Cursor<Array<SqliteSchema.Index>>;
|
|
21
22
|
function createInstructions(columnData: Array<Column>, indexData: Array<Index>): SchemaInstructions | undefined;
|
|
22
23
|
function columnInstruction(column: Column): [string, string];
|
|
23
24
|
function indexInstruction(index: Index): [string, string];
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
// src/sqlite/SqliteSchema.ts
|
|
2
|
-
import {
|
|
2
|
+
import { Cursor } from "../lib/Cursor.js";
|
|
3
|
+
import { identifier, raw } from "../lib/Statement.js";
|
|
3
4
|
import { SqliteFormatter } from "./SqliteFormatter.js";
|
|
4
5
|
var SqliteSchema;
|
|
5
6
|
((SqliteSchema2) => {
|
|
6
7
|
const formatter = new SqliteFormatter();
|
|
7
8
|
function tableData(tableName) {
|
|
8
|
-
return
|
|
9
|
+
return Cursor.all`select * from pragma_table_info(${tableName}) order by cid`;
|
|
9
10
|
}
|
|
10
11
|
SqliteSchema2.tableData = tableData;
|
|
11
12
|
function indexData(tableName) {
|
|
12
|
-
return
|
|
13
|
+
return Cursor.all`select * from sqlite_master where type='index' and tbl_name=${tableName}`;
|
|
13
14
|
}
|
|
14
15
|
SqliteSchema2.indexData = indexData;
|
|
15
16
|
function createInstructions(columnData, indexData2) {
|
|
@@ -29,7 +30,7 @@ var SqliteSchema;
|
|
|
29
30
|
identifier(column.name).add(column.type).addIf(column.pk === 1, "PRIMARY KEY").addIf(column.notnull === 1, "NOT NULL").addIf(
|
|
30
31
|
column.dflt_value !== null,
|
|
31
32
|
raw("DEFAULT").addParenthesis(column.dflt_value)
|
|
32
|
-
).compile(formatter)
|
|
33
|
+
).compile(formatter).sql
|
|
33
34
|
];
|
|
34
35
|
}
|
|
35
36
|
SqliteSchema2.columnInstruction = columnInstruction;
|