@uniflowed/sql 0.34.0 → 0.35.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/adapters/better-sqlite3.js +8 -8
- package/adapters/bun-sqlite.js +8 -8
- package/adapters/d1.js +2 -2
- package/adapters/mysql2.js +13 -13
- package/adapters/node-sqlite.js +7 -7
- package/adapters/pg.js +5 -5
- package/adapters/pglite.js +5 -5
- package/adapters/postgres.js +5 -5
- package/index.js +90 -90
- package/internal/shared.js +4 -4
- package/internal/statements.js +1 -1
- package/mysql.js +10 -10
- package/package.json +1 -1
- package/postgresql.js +14 -14
- package/sqlite.js +13 -13
|
@@ -22,7 +22,7 @@ export interface BetterSqlite3Statement {
|
|
|
22
22
|
readonly reader: boolean;
|
|
23
23
|
all(...params: $ReadOnlyArray<SqlParam>): $ReadOnlyArray<mixed>;
|
|
24
24
|
run(...params: $ReadOnlyArray<SqlParam>): {
|
|
25
|
-
readonly changes: number,
|
|
25
|
+
readonly changes : number,
|
|
26
26
|
readonly lastInsertRowid: number | bigint,
|
|
27
27
|
...
|
|
28
28
|
};
|
|
@@ -36,15 +36,15 @@ export interface BetterSqlite3Database {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
export type BetterSqlite3Options = {|
|
|
39
|
-
readonly begin
|
|
39
|
+
readonly begin? : string,
|
|
40
40
|
readonly statementCache?: number,
|
|
41
|
-
readonly maxParams
|
|
41
|
+
readonly maxParams? : number,
|
|
42
42
|
|};
|
|
43
43
|
|
|
44
44
|
/** A [`Queryable`] and a [`SyncQueryable`] over one better-sqlite3 `Database`. */
|
|
45
45
|
export function fromBetterSqlite3(
|
|
46
46
|
database: BetterSqlite3Database,
|
|
47
|
-
options: BetterSqlite3Options = {},
|
|
47
|
+
options : BetterSqlite3Options = {},
|
|
48
48
|
): Queryable & SyncQueryable {
|
|
49
49
|
const statement = statementCache((text) => {
|
|
50
50
|
const prepared = database.prepare(text);
|
|
@@ -70,16 +70,16 @@ export function fromBetterSqlite3(
|
|
|
70
70
|
}
|
|
71
71
|
const result = prepared.run(...params);
|
|
72
72
|
return {
|
|
73
|
-
rows: [],
|
|
73
|
+
rows : [],
|
|
74
74
|
rowsAffected: count(result.changes),
|
|
75
75
|
lastInsertId: rowId(result.lastInsertRowid),
|
|
76
76
|
};
|
|
77
77
|
};
|
|
78
78
|
return singleConnectionSync({
|
|
79
|
-
engine: "sqlite",
|
|
79
|
+
engine : "sqlite",
|
|
80
80
|
maxParams: options.maxParams ?? 32766,
|
|
81
|
-
begin: options.begin,
|
|
82
|
-
run: async (text, params, mode) => runSync(text, params, mode),
|
|
81
|
+
begin : options.begin,
|
|
82
|
+
run : async (text, params, mode) => runSync(text, params, mode),
|
|
83
83
|
runSync,
|
|
84
84
|
});
|
|
85
85
|
}
|
package/adapters/bun-sqlite.js
CHANGED
|
@@ -21,7 +21,7 @@ import { DEFAULT_STATEMENT_CACHE, count, rowId, statementCache } from "../intern
|
|
|
21
21
|
export interface BunSqliteStatement {
|
|
22
22
|
values(...params: $ReadOnlyArray<SqlParam>): $ReadOnlyArray<mixed>;
|
|
23
23
|
run(...params: $ReadOnlyArray<SqlParam>): {
|
|
24
|
-
readonly changes: number | bigint,
|
|
24
|
+
readonly changes : number | bigint,
|
|
25
25
|
readonly lastInsertRowid: number | bigint,
|
|
26
26
|
...
|
|
27
27
|
};
|
|
@@ -34,15 +34,15 @@ export interface BunSqliteDatabase {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export type BunSqliteOptions = {|
|
|
37
|
-
readonly begin
|
|
37
|
+
readonly begin? : string,
|
|
38
38
|
readonly statementCache?: number,
|
|
39
|
-
readonly maxParams
|
|
39
|
+
readonly maxParams? : number,
|
|
40
40
|
|};
|
|
41
41
|
|
|
42
42
|
/** A [`Queryable`] and a [`SyncQueryable`] over one `bun:sqlite` `Database`. */
|
|
43
43
|
export function fromBunSqlite(
|
|
44
44
|
database: BunSqliteDatabase,
|
|
45
|
-
options: BunSqliteOptions = {},
|
|
45
|
+
options : BunSqliteOptions = {},
|
|
46
46
|
): Queryable & SyncQueryable {
|
|
47
47
|
const statement = statementCache((text) => {
|
|
48
48
|
const prepared = database.prepare(text);
|
|
@@ -63,16 +63,16 @@ export function fromBunSqlite(
|
|
|
63
63
|
}
|
|
64
64
|
const result = prepared.run(...params);
|
|
65
65
|
return {
|
|
66
|
-
rows: [],
|
|
66
|
+
rows : [],
|
|
67
67
|
rowsAffected: count(result.changes),
|
|
68
68
|
lastInsertId: rowId(result.lastInsertRowid),
|
|
69
69
|
};
|
|
70
70
|
};
|
|
71
71
|
return singleConnectionSync({
|
|
72
|
-
engine: "sqlite",
|
|
72
|
+
engine : "sqlite",
|
|
73
73
|
maxParams: options.maxParams ?? 32766,
|
|
74
|
-
begin: options.begin,
|
|
75
|
-
run: async (text, params, mode) => runSync(text, params, mode),
|
|
74
|
+
begin : options.begin,
|
|
75
|
+
run : async (text, params, mode) => runSync(text, params, mode),
|
|
76
76
|
runSync,
|
|
77
77
|
});
|
|
78
78
|
}
|
package/adapters/d1.js
CHANGED
|
@@ -47,7 +47,7 @@ function toD1(param: SqlParam): mixed {
|
|
|
47
47
|
/** A [`Queryable`] over a D1 binding. */
|
|
48
48
|
export function fromD1(database: D1Database): Queryable {
|
|
49
49
|
return {
|
|
50
|
-
engine: "sqlite",
|
|
50
|
+
engine : "sqlite",
|
|
51
51
|
maxParams: 100,
|
|
52
52
|
query: async (text, params, mode) => {
|
|
53
53
|
const statement = database.prepare(text).bind(...params.map(toD1));
|
|
@@ -63,7 +63,7 @@ export function fromD1(database: D1Database): Queryable {
|
|
|
63
63
|
}
|
|
64
64
|
const { meta } = await statement.run();
|
|
65
65
|
return {
|
|
66
|
-
rows: [],
|
|
66
|
+
rows : [],
|
|
67
67
|
rowsAffected: count(meta.changes ?? 0),
|
|
68
68
|
lastInsertId: rowId(meta.last_row_id),
|
|
69
69
|
};
|
package/adapters/mysql2.js
CHANGED
|
@@ -34,13 +34,13 @@ export type Mysql2Field = {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
type Mysql2Options = {|
|
|
37
|
-
readonly sql: string,
|
|
38
|
-
readonly values: $ReadOnlyArray<mixed>,
|
|
39
|
-
readonly rowsAsArray: true,
|
|
37
|
+
readonly sql : string,
|
|
38
|
+
readonly values : $ReadOnlyArray<mixed>,
|
|
39
|
+
readonly rowsAsArray : true,
|
|
40
40
|
readonly supportBigNumbers: true,
|
|
41
|
-
readonly bigNumberStrings: true,
|
|
42
|
-
readonly dateStrings: true,
|
|
43
|
-
readonly typeCast: (field: Mysql2Field, next: () => mixed) => mixed,
|
|
41
|
+
readonly bigNumberStrings : true,
|
|
42
|
+
readonly dateStrings : true,
|
|
43
|
+
readonly typeCast : (field: Mysql2Field, next: () => mixed) => mixed,
|
|
44
44
|
|};
|
|
45
45
|
|
|
46
46
|
type Mysql2Result =
|
|
@@ -93,12 +93,12 @@ function lastId(value: number | string): bigint | null {
|
|
|
93
93
|
function runOn(connection: Mysql2Connection): Run {
|
|
94
94
|
return async (text, params, mode): Promise<QueryResult> => {
|
|
95
95
|
const options: Mysql2Options = {
|
|
96
|
-
sql: text,
|
|
97
|
-
values: params,
|
|
98
|
-
rowsAsArray: true,
|
|
96
|
+
sql : text,
|
|
97
|
+
values : params,
|
|
98
|
+
rowsAsArray : true,
|
|
99
99
|
supportBigNumbers: true,
|
|
100
|
-
bigNumberStrings: true,
|
|
101
|
-
dateStrings: true,
|
|
100
|
+
bigNumberStrings : true,
|
|
101
|
+
dateStrings : true,
|
|
102
102
|
typeCast,
|
|
103
103
|
};
|
|
104
104
|
const [result] =
|
|
@@ -125,9 +125,9 @@ function connection(client: Mysql2Connection): Connection {
|
|
|
125
125
|
/** A [`Queryable`] over a `mysql2/promise` pool. */
|
|
126
126
|
export function fromMysql2Pool(pool: Mysql2Pool): Queryable {
|
|
127
127
|
return {
|
|
128
|
-
engine: "mysql",
|
|
128
|
+
engine : "mysql",
|
|
129
129
|
maxParams: 65535,
|
|
130
|
-
query: runOn(pool),
|
|
130
|
+
query : runOn(pool),
|
|
131
131
|
transaction: async <T>(body: (tx: Queryable) => Promise<T>): Promise<T> => {
|
|
132
132
|
const client = await pool.getConnection();
|
|
133
133
|
try {
|
package/adapters/node-sqlite.js
CHANGED
|
@@ -22,7 +22,7 @@ import { DEFAULT_STATEMENT_CACHE, count, rowId, statementCache } from "../intern
|
|
|
22
22
|
export interface NodeSqliteStatement {
|
|
23
23
|
all(...params: $ReadOnlyArray<SqlParam>): $ReadOnlyArray<mixed>;
|
|
24
24
|
run(...params: $ReadOnlyArray<SqlParam>): {
|
|
25
|
-
readonly changes: number | bigint,
|
|
25
|
+
readonly changes : number | bigint,
|
|
26
26
|
readonly lastInsertRowid: number | bigint,
|
|
27
27
|
...
|
|
28
28
|
};
|
|
@@ -51,7 +51,7 @@ function prepared(database: NodeSqliteDatabase, text: string): NodeSqliteStateme
|
|
|
51
51
|
// failing on the call below.
|
|
52
52
|
if (!("setReturnArrays" in statement)) {
|
|
53
53
|
throw new SqlError({
|
|
54
|
-
kind: "unsupported",
|
|
54
|
+
kind : "unsupported",
|
|
55
55
|
feature: "node:sqlite without StatementSync#setReturnArrays (Node 24.0 or 22.16 and newer)",
|
|
56
56
|
});
|
|
57
57
|
}
|
|
@@ -62,7 +62,7 @@ function prepared(database: NodeSqliteDatabase, text: string): NodeSqliteStateme
|
|
|
62
62
|
/** A [`Queryable`] and a [`SyncQueryable`] over one `DatabaseSync`. */
|
|
63
63
|
export function fromNodeSqlite(
|
|
64
64
|
database: NodeSqliteDatabase,
|
|
65
|
-
options: NodeSqliteOptions = {},
|
|
65
|
+
options : NodeSqliteOptions = {},
|
|
66
66
|
): Queryable & SyncQueryable {
|
|
67
67
|
const statement = statementCache(
|
|
68
68
|
(text) => prepared(database, text),
|
|
@@ -83,16 +83,16 @@ export function fromNodeSqlite(
|
|
|
83
83
|
}
|
|
84
84
|
const result = prepared.run(...params);
|
|
85
85
|
return {
|
|
86
|
-
rows: [],
|
|
86
|
+
rows : [],
|
|
87
87
|
rowsAffected: count(result.changes),
|
|
88
88
|
lastInsertId: rowId(result.lastInsertRowid),
|
|
89
89
|
};
|
|
90
90
|
};
|
|
91
91
|
return singleConnectionSync({
|
|
92
|
-
engine: "sqlite",
|
|
92
|
+
engine : "sqlite",
|
|
93
93
|
maxParams: options.maxParams ?? 32766,
|
|
94
|
-
begin: options.begin,
|
|
95
|
-
run: async (text, params, mode) => runSync(text, params, mode),
|
|
94
|
+
begin : options.begin,
|
|
95
|
+
run : async (text, params, mode) => runSync(text, params, mode),
|
|
96
96
|
runSync,
|
|
97
97
|
});
|
|
98
98
|
}
|
package/adapters/pg.js
CHANGED
|
@@ -18,10 +18,10 @@ import { singleConnection, transactionOn } from "../index.js";
|
|
|
18
18
|
type PgResult = { readonly rows: $ReadOnlyArray<mixed>, readonly rowCount: number | null, ... };
|
|
19
19
|
|
|
20
20
|
type PgQuery = {|
|
|
21
|
-
readonly text: string,
|
|
22
|
-
readonly values: $ReadOnlyArray<mixed>,
|
|
21
|
+
readonly text : string,
|
|
22
|
+
readonly values : $ReadOnlyArray<mixed>,
|
|
23
23
|
readonly rowMode: "array",
|
|
24
|
-
readonly types: {| readonly getTypeParser: () => (value: string) => string |},
|
|
24
|
+
readonly types : {| readonly getTypeParser: () => (value: string) => string |},
|
|
25
25
|
|};
|
|
26
26
|
|
|
27
27
|
/** A connected `pg.Client` or a client checked out of a pool. */
|
|
@@ -67,9 +67,9 @@ export function fromPgClient(client: PgClient): Queryable {
|
|
|
67
67
|
/** A [`Queryable`] over a `pg.Pool`. */
|
|
68
68
|
export function fromPgPool(pool: PgPool): Queryable {
|
|
69
69
|
return {
|
|
70
|
-
engine: "postgresql",
|
|
70
|
+
engine : "postgresql",
|
|
71
71
|
maxParams: 65535,
|
|
72
|
-
query: runOn(pool),
|
|
72
|
+
query : runOn(pool),
|
|
73
73
|
transaction: async <T>(body: (tx: Queryable) => Promise<T>): Promise<T> => {
|
|
74
74
|
const client = await pool.connect();
|
|
75
75
|
let broken: mixed = undefined;
|
package/adapters/pglite.js
CHANGED
|
@@ -18,15 +18,15 @@ import { singleConnection } from "../index.js";
|
|
|
18
18
|
/** The part of a `PGlite` instance this uses. */
|
|
19
19
|
export interface PGliteDatabase {
|
|
20
20
|
query(
|
|
21
|
-
text: string,
|
|
21
|
+
text : string,
|
|
22
22
|
params: $ReadOnlyArray<SqlParam>,
|
|
23
23
|
options: {|
|
|
24
|
-
readonly rowMode: "array",
|
|
25
|
-
readonly parsers: { readonly [string]: (value: string) => mixed },
|
|
24
|
+
readonly rowMode : "array",
|
|
25
|
+
readonly parsers : { readonly [string]: (value: string) => mixed },
|
|
26
26
|
readonly serializers: { readonly [string]: (value: mixed) => mixed },
|
|
27
27
|
|},
|
|
28
28
|
): Promise<{ readonly rows: $ReadOnlyArray<mixed>, readonly affectedRows?: number, ... }>;
|
|
29
|
-
readonly parsers: { readonly [string]: mixed };
|
|
29
|
+
readonly parsers : { readonly [string]: mixed };
|
|
30
30
|
readonly serializers: { readonly [string]: mixed };
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -50,7 +50,7 @@ export function fromPGlite(database: PGliteDatabase): Queryable {
|
|
|
50
50
|
let parsers: { readonly [string]: (value: mixed) => mixed } = {};
|
|
51
51
|
let serializers: { readonly [string]: (value: mixed) => mixed } = {};
|
|
52
52
|
return singleConnection({
|
|
53
|
-
engine: "postgresql",
|
|
53
|
+
engine : "postgresql",
|
|
54
54
|
maxParams: 65535,
|
|
55
55
|
run: async (text, params) => {
|
|
56
56
|
const size = Object.keys(database.parsers).length + Object.keys(database.serializers).length;
|
package/adapters/postgres.js
CHANGED
|
@@ -30,8 +30,8 @@ type Unsafe = { raw(): RawRows, ... };
|
|
|
30
30
|
/** What postgres.js's `Sql` and its transaction handles have in common. */
|
|
31
31
|
export interface PostgresQueries {
|
|
32
32
|
unsafe(
|
|
33
|
-
text: string,
|
|
34
|
-
params: $ReadOnlyArray<mixed>,
|
|
33
|
+
text : string,
|
|
34
|
+
params : $ReadOnlyArray<mixed>,
|
|
35
35
|
options?: {| readonly prepare?: boolean |},
|
|
36
36
|
): Unsafe;
|
|
37
37
|
typed(value: SqlParam, oid: number): mixed;
|
|
@@ -83,7 +83,7 @@ async function scoped<T>(tx: PostgresTransaction, body: (tx: Queryable) => Promi
|
|
|
83
83
|
let open = true;
|
|
84
84
|
const query = run(tx);
|
|
85
85
|
const queryable: Queryable = {
|
|
86
|
-
engine: "postgresql",
|
|
86
|
+
engine : "postgresql",
|
|
87
87
|
maxParams: 65535,
|
|
88
88
|
query: (statement, params, mode) =>
|
|
89
89
|
open ? query(statement, params, mode) : Promise.reject(new SqlError({ kind: "closed" })),
|
|
@@ -102,9 +102,9 @@ async function scoped<T>(tx: PostgresTransaction, body: (tx: Queryable) => Promi
|
|
|
102
102
|
/** A [`Queryable`] over a postgres.js `Sql`. */
|
|
103
103
|
export function fromPostgres(sql: PostgresSql): Queryable {
|
|
104
104
|
return {
|
|
105
|
-
engine: "postgresql",
|
|
105
|
+
engine : "postgresql",
|
|
106
106
|
maxParams: 65535,
|
|
107
|
-
query: run(sql),
|
|
107
|
+
query : run(sql),
|
|
108
108
|
transaction: <T>(body: (tx: Queryable) => Promise<T>): Promise<T> =>
|
|
109
109
|
sql.begin((tx) => scoped(tx, body)),
|
|
110
110
|
};
|
package/index.js
CHANGED
|
@@ -91,7 +91,7 @@ export type Queryable = {
|
|
|
91
91
|
* body would deadlock the same way `await db.query` does inside `transaction`.
|
|
92
92
|
*/
|
|
93
93
|
export type SyncQueryable = {
|
|
94
|
-
readonly engine: Engine,
|
|
94
|
+
readonly engine : Engine,
|
|
95
95
|
readonly maxParams: number,
|
|
96
96
|
querySync(text: string, params: $ReadOnlyArray<SqlParam>, mode: QueryMode): QueryResult,
|
|
97
97
|
readonly transactionSync?: <T>(body: (tx: SyncQueryable) => T) => T,
|
|
@@ -155,19 +155,19 @@ function explain(failure: SqlFailure, query: string | null): string {
|
|
|
155
155
|
return (
|
|
156
156
|
where +
|
|
157
157
|
match (failure) {
|
|
158
|
-
{kind: "decode", expected: const expected, value: const value}
|
|
158
|
+
{kind: "decode", expected: const expected, value: const value} =>
|
|
159
159
|
`expected ${expected} from the database, got ${show(value)}`,
|
|
160
|
-
{kind: "encode", expected: const expected, value: const value}
|
|
160
|
+
{kind: "encode", expected: const expected, value: const value} =>
|
|
161
161
|
`expected ${expected} as a parameter, got ${show(value)}`,
|
|
162
162
|
{kind: "shape", expected: const expected, actual: const actual} =>
|
|
163
163
|
`the adapter returned ${actual} columns where the query has ${expected}; ` +
|
|
164
164
|
"regenerate with `uf sqlc generate`, or check that the adapter returns rows as arrays",
|
|
165
|
-
{kind: "closed"}
|
|
166
|
-
{kind: "unsupported", feature: const feature}
|
|
165
|
+
{kind: "closed"} => "the transaction has already committed or rolled back",
|
|
166
|
+
{kind: "unsupported", feature: const feature} =>
|
|
167
167
|
`${feature} is not supported by this adapter`,
|
|
168
|
-
{kind: "params", width: const width, max: const max}
|
|
168
|
+
{kind: "params", width: const width, max: const max} =>
|
|
169
169
|
`a :copyfrom row binds ${width} parameters and this adapter allows ${max}`,
|
|
170
|
-
{kind: "row", index: const index, length: const length}
|
|
170
|
+
{kind: "row", index: const index, length: const length} =>
|
|
171
171
|
`a :copyfrom row has ${length} fields and the statement reads index ${index}`,
|
|
172
172
|
}
|
|
173
173
|
);
|
|
@@ -229,11 +229,11 @@ function allRows<T>(rows: $ReadOnlyArray<Row>, width: number, decode: (row: Row)
|
|
|
229
229
|
|
|
230
230
|
/** `:one` — the first row, or `null`. */
|
|
231
231
|
export async function one<T>(
|
|
232
|
-
db: Queryable,
|
|
233
|
-
name: string,
|
|
234
|
-
text: string,
|
|
232
|
+
db : Queryable,
|
|
233
|
+
name : string,
|
|
234
|
+
text : string,
|
|
235
235
|
params: $ReadOnlyArray<SqlParam>,
|
|
236
|
-
width: number,
|
|
236
|
+
width : number,
|
|
237
237
|
decode: (row: Row) => T,
|
|
238
238
|
): Promise<T | null> {
|
|
239
239
|
try {
|
|
@@ -246,11 +246,11 @@ export async function one<T>(
|
|
|
246
246
|
|
|
247
247
|
/** `:one` on a [`SyncQueryable`]. */
|
|
248
248
|
export function oneSync<T>(
|
|
249
|
-
db: SyncQueryable,
|
|
250
|
-
name: string,
|
|
251
|
-
text: string,
|
|
249
|
+
db : SyncQueryable,
|
|
250
|
+
name : string,
|
|
251
|
+
text : string,
|
|
252
252
|
params: $ReadOnlyArray<SqlParam>,
|
|
253
|
-
width: number,
|
|
253
|
+
width : number,
|
|
254
254
|
decode: (row: Row) => T,
|
|
255
255
|
): T | null {
|
|
256
256
|
try {
|
|
@@ -263,11 +263,11 @@ export function oneSync<T>(
|
|
|
263
263
|
|
|
264
264
|
/** `:many` — every row. */
|
|
265
265
|
export async function many<T>(
|
|
266
|
-
db: Queryable,
|
|
267
|
-
name: string,
|
|
268
|
-
text: string,
|
|
266
|
+
db : Queryable,
|
|
267
|
+
name : string,
|
|
268
|
+
text : string,
|
|
269
269
|
params: $ReadOnlyArray<SqlParam>,
|
|
270
|
-
width: number,
|
|
270
|
+
width : number,
|
|
271
271
|
decode: (row: Row) => T,
|
|
272
272
|
): Promise<Array<T>> {
|
|
273
273
|
try {
|
|
@@ -280,11 +280,11 @@ export async function many<T>(
|
|
|
280
280
|
|
|
281
281
|
/** `:many` on a [`SyncQueryable`]. */
|
|
282
282
|
export function manySync<T>(
|
|
283
|
-
db: SyncQueryable,
|
|
284
|
-
name: string,
|
|
285
|
-
text: string,
|
|
283
|
+
db : SyncQueryable,
|
|
284
|
+
name : string,
|
|
285
|
+
text : string,
|
|
286
286
|
params: $ReadOnlyArray<SqlParam>,
|
|
287
|
-
width: number,
|
|
287
|
+
width : number,
|
|
288
288
|
decode: (row: Row) => T,
|
|
289
289
|
): Array<T> {
|
|
290
290
|
try {
|
|
@@ -296,9 +296,9 @@ export function manySync<T>(
|
|
|
296
296
|
}
|
|
297
297
|
|
|
298
298
|
async function run(
|
|
299
|
-
db: Queryable,
|
|
300
|
-
name: string,
|
|
301
|
-
text: string,
|
|
299
|
+
db : Queryable,
|
|
300
|
+
name : string,
|
|
301
|
+
text : string,
|
|
302
302
|
params: $ReadOnlyArray<SqlParam>,
|
|
303
303
|
): Promise<QueryResult> {
|
|
304
304
|
try {
|
|
@@ -309,9 +309,9 @@ async function run(
|
|
|
309
309
|
}
|
|
310
310
|
|
|
311
311
|
function executed(
|
|
312
|
-
db: SyncQueryable,
|
|
313
|
-
name: string,
|
|
314
|
-
text: string,
|
|
312
|
+
db : SyncQueryable,
|
|
313
|
+
name : string,
|
|
314
|
+
text : string,
|
|
315
315
|
params: $ReadOnlyArray<SqlParam>,
|
|
316
316
|
): QueryResult {
|
|
317
317
|
try {
|
|
@@ -323,9 +323,9 @@ function executed(
|
|
|
323
323
|
|
|
324
324
|
/** `:exec`. */
|
|
325
325
|
export async function exec(
|
|
326
|
-
db: Queryable,
|
|
327
|
-
name: string,
|
|
328
|
-
text: string,
|
|
326
|
+
db : Queryable,
|
|
327
|
+
name : string,
|
|
328
|
+
text : string,
|
|
329
329
|
params: $ReadOnlyArray<SqlParam>,
|
|
330
330
|
): Promise<void> {
|
|
331
331
|
await run(db, name, text, params);
|
|
@@ -333,9 +333,9 @@ export async function exec(
|
|
|
333
333
|
|
|
334
334
|
/** `:exec` on a [`SyncQueryable`]. */
|
|
335
335
|
export function execSync(
|
|
336
|
-
db: SyncQueryable,
|
|
337
|
-
name: string,
|
|
338
|
-
text: string,
|
|
336
|
+
db : SyncQueryable,
|
|
337
|
+
name : string,
|
|
338
|
+
text : string,
|
|
339
339
|
params: $ReadOnlyArray<SqlParam>,
|
|
340
340
|
): void {
|
|
341
341
|
executed(db, name, text, params);
|
|
@@ -343,9 +343,9 @@ export function execSync(
|
|
|
343
343
|
|
|
344
344
|
/** `:execrows` — how many rows the statement changed. */
|
|
345
345
|
export async function execRows(
|
|
346
|
-
db: Queryable,
|
|
347
|
-
name: string,
|
|
348
|
-
text: string,
|
|
346
|
+
db : Queryable,
|
|
347
|
+
name : string,
|
|
348
|
+
text : string,
|
|
349
349
|
params: $ReadOnlyArray<SqlParam>,
|
|
350
350
|
): Promise<number> {
|
|
351
351
|
return (await run(db, name, text, params)).rowsAffected;
|
|
@@ -353,9 +353,9 @@ export async function execRows(
|
|
|
353
353
|
|
|
354
354
|
/** `:execrows` on a [`SyncQueryable`]. */
|
|
355
355
|
export function execRowsSync(
|
|
356
|
-
db: SyncQueryable,
|
|
357
|
-
name: string,
|
|
358
|
-
text: string,
|
|
356
|
+
db : SyncQueryable,
|
|
357
|
+
name : string,
|
|
358
|
+
text : string,
|
|
359
359
|
params: $ReadOnlyArray<SqlParam>,
|
|
360
360
|
): number {
|
|
361
361
|
return executed(db, name, text, params).rowsAffected;
|
|
@@ -369,9 +369,9 @@ export type ExecResult = {|
|
|
|
369
369
|
|
|
370
370
|
/** `:execresult` — the affected-row count and, on MySQL and SQLite, the last id. */
|
|
371
371
|
export async function execResult(
|
|
372
|
-
db: Queryable,
|
|
373
|
-
name: string,
|
|
374
|
-
text: string,
|
|
372
|
+
db : Queryable,
|
|
373
|
+
name : string,
|
|
374
|
+
text : string,
|
|
375
375
|
params: $ReadOnlyArray<SqlParam>,
|
|
376
376
|
): Promise<ExecResult> {
|
|
377
377
|
const { rowsAffected, lastInsertId } = await run(db, name, text, params);
|
|
@@ -380,9 +380,9 @@ export async function execResult(
|
|
|
380
380
|
|
|
381
381
|
/** `:execresult` on a [`SyncQueryable`]. */
|
|
382
382
|
export function execResultSync(
|
|
383
|
-
db: SyncQueryable,
|
|
384
|
-
name: string,
|
|
385
|
-
text: string,
|
|
383
|
+
db : SyncQueryable,
|
|
384
|
+
name : string,
|
|
385
|
+
text : string,
|
|
386
386
|
params: $ReadOnlyArray<SqlParam>,
|
|
387
387
|
): ExecResult {
|
|
388
388
|
const { rowsAffected, lastInsertId } = executed(db, name, text, params);
|
|
@@ -403,9 +403,9 @@ function insertedId(result: QueryResult, name: string): bigint {
|
|
|
403
403
|
* `:one`, and this throws rather than resolve to a made-up zero.
|
|
404
404
|
*/
|
|
405
405
|
export async function execLastId(
|
|
406
|
-
db: Queryable,
|
|
407
|
-
name: string,
|
|
408
|
-
text: string,
|
|
406
|
+
db : Queryable,
|
|
407
|
+
name : string,
|
|
408
|
+
text : string,
|
|
409
409
|
params: $ReadOnlyArray<SqlParam>,
|
|
410
410
|
): Promise<bigint> {
|
|
411
411
|
return insertedId(await run(db, name, text, params), name);
|
|
@@ -413,9 +413,9 @@ export async function execLastId(
|
|
|
413
413
|
|
|
414
414
|
/** `:execlastid` on a [`SyncQueryable`]. */
|
|
415
415
|
export function execLastIdSync(
|
|
416
|
-
db: SyncQueryable,
|
|
417
|
-
name: string,
|
|
418
|
-
text: string,
|
|
416
|
+
db : SyncQueryable,
|
|
417
|
+
name : string,
|
|
418
|
+
text : string,
|
|
419
419
|
params: $ReadOnlyArray<SqlParam>,
|
|
420
420
|
): bigint {
|
|
421
421
|
return insertedId(executed(db, name, text, params), name);
|
|
@@ -453,7 +453,7 @@ export function slice(values: $ReadOnlyArray<SqlParam>): Slice {
|
|
|
453
453
|
* should match.
|
|
454
454
|
*/
|
|
455
455
|
export function expand(
|
|
456
|
-
parts: $ReadOnlyArray<string>,
|
|
456
|
+
parts : $ReadOnlyArray<string>,
|
|
457
457
|
values: $ReadOnlyArray<SqlParam | Slice>,
|
|
458
458
|
): {| readonly text: string, readonly params: $ReadOnlyArray<SqlParam> |} {
|
|
459
459
|
const params: Array<SqlParam> = [];
|
|
@@ -485,10 +485,10 @@ export function expand(
|
|
|
485
485
|
* `VALUES ($1, $2)` is `tuple: ["(", ", ", ")"]`, `refs: [0, 1]`.
|
|
486
486
|
*/
|
|
487
487
|
export type CopyPlan = {|
|
|
488
|
-
readonly head: string,
|
|
488
|
+
readonly head : string,
|
|
489
489
|
readonly tuple: $ReadOnlyArray<string>,
|
|
490
|
-
readonly refs: $ReadOnlyArray<number>,
|
|
491
|
-
readonly tail: string,
|
|
490
|
+
readonly refs : $ReadOnlyArray<number>,
|
|
491
|
+
readonly tail : string,
|
|
492
492
|
|};
|
|
493
493
|
|
|
494
494
|
/** The most rows one statement carries when a row has no parameters at all. */
|
|
@@ -496,9 +496,9 @@ const COPY_ROWS_WITHOUT_PARAMS = 1000;
|
|
|
496
496
|
|
|
497
497
|
function copyChunk(
|
|
498
498
|
engine: Engine,
|
|
499
|
-
plan: CopyPlan,
|
|
500
|
-
chunk: $ReadOnlyArray<$ReadOnlyArray<SqlParam>>,
|
|
501
|
-
name: string,
|
|
499
|
+
plan : CopyPlan,
|
|
500
|
+
chunk : $ReadOnlyArray<$ReadOnlyArray<SqlParam>>,
|
|
501
|
+
name : string,
|
|
502
502
|
): {| readonly text: string, readonly params: $ReadOnlyArray<SqlParam> |} {
|
|
503
503
|
const width = plan.refs.length;
|
|
504
504
|
const params: Array<SqlParam> = [];
|
|
@@ -536,7 +536,7 @@ function copyWidth(db: { readonly maxParams: number, ... }, plan: CopyPlan, name
|
|
|
536
536
|
* now several statements. Resolves to the number of rows inserted.
|
|
537
537
|
*/
|
|
538
538
|
export async function copyFrom(
|
|
539
|
-
db: Queryable,
|
|
539
|
+
db : Queryable,
|
|
540
540
|
name: string,
|
|
541
541
|
plan: CopyPlan,
|
|
542
542
|
rows: $ReadOnlyArray<$ReadOnlyArray<SqlParam>>,
|
|
@@ -562,7 +562,7 @@ export async function copyFrom(
|
|
|
562
562
|
|
|
563
563
|
/** `:copyfrom` on a [`SyncQueryable`]. */
|
|
564
564
|
export function copyFromSync(
|
|
565
|
-
db: SyncQueryable,
|
|
565
|
+
db : SyncQueryable,
|
|
566
566
|
name: string,
|
|
567
567
|
plan: CopyPlan,
|
|
568
568
|
rows: $ReadOnlyArray<$ReadOnlyArray<SqlParam>>,
|
|
@@ -598,9 +598,9 @@ export function copyFromSync(
|
|
|
598
598
|
* order without one, and the docs say so.
|
|
599
599
|
*/
|
|
600
600
|
export async function batch<A, R>(
|
|
601
|
-
db: Queryable,
|
|
601
|
+
db : Queryable,
|
|
602
602
|
items: $ReadOnlyArray<A>,
|
|
603
|
-
body: (q: Queryable, item: A) => Promise<R>,
|
|
603
|
+
body : (q: Queryable, item: A) => Promise<R>,
|
|
604
604
|
): Promise<Array<R>> {
|
|
605
605
|
const each = async (q: Queryable): Promise<Array<R>> => {
|
|
606
606
|
const out: Array<R> = [];
|
|
@@ -623,9 +623,9 @@ export async function batch<A, R>(
|
|
|
623
623
|
* items still run in order, on this thread.
|
|
624
624
|
*/
|
|
625
625
|
export function batchSync<A, R>(
|
|
626
|
-
db: SyncQueryable,
|
|
626
|
+
db : SyncQueryable,
|
|
627
627
|
items: $ReadOnlyArray<A>,
|
|
628
|
-
body: (q: SyncQueryable, item: A) => R,
|
|
628
|
+
body : (q: SyncQueryable, item: A) => R,
|
|
629
629
|
): Array<R> {
|
|
630
630
|
const each = (q: SyncQueryable): Array<R> => {
|
|
631
631
|
const out: Array<R> = [];
|
|
@@ -646,23 +646,23 @@ export function batchSync<A, R>(
|
|
|
646
646
|
|
|
647
647
|
/** How an adapter runs one statement on one connection. */
|
|
648
648
|
export type Run = (
|
|
649
|
-
text: string,
|
|
649
|
+
text : string,
|
|
650
650
|
params: $ReadOnlyArray<SqlParam>,
|
|
651
|
-
mode: QueryMode,
|
|
651
|
+
mode : QueryMode,
|
|
652
652
|
) => Promise<QueryResult>;
|
|
653
653
|
|
|
654
654
|
/** [`Run`] for a driver that finishes the statement before returning. */
|
|
655
655
|
export type RunSync = (
|
|
656
|
-
text: string,
|
|
656
|
+
text : string,
|
|
657
657
|
params: $ReadOnlyArray<SqlParam>,
|
|
658
|
-
mode: QueryMode,
|
|
658
|
+
mode : QueryMode,
|
|
659
659
|
) => QueryResult;
|
|
660
660
|
|
|
661
661
|
/** What [`transactionOn`] needs from an adapter. */
|
|
662
662
|
export type Connection = {|
|
|
663
|
-
readonly engine: Engine,
|
|
663
|
+
readonly engine : Engine,
|
|
664
664
|
readonly maxParams: number,
|
|
665
|
-
readonly run: Run,
|
|
665
|
+
readonly run : Run,
|
|
666
666
|
/** Present when the driver can also run a statement on the calling thread. */
|
|
667
667
|
readonly runSync?: RunSync,
|
|
668
668
|
/** The statement that opens a transaction; `BEGIN` unless the adapter knows better. */
|
|
@@ -681,7 +681,7 @@ export type Connection = {|
|
|
|
681
681
|
*/
|
|
682
682
|
export async function transactionOn<T>(
|
|
683
683
|
connection: Connection,
|
|
684
|
-
body: (tx: Queryable) => Promise<T>,
|
|
684
|
+
body : (tx: Queryable) => Promise<T>,
|
|
685
685
|
): Promise<T> {
|
|
686
686
|
await connection.run(connection.begin ?? "BEGIN", [], "exec");
|
|
687
687
|
return scoped(connection, 0, body, "COMMIT", "ROLLBACK");
|
|
@@ -689,10 +689,10 @@ export async function transactionOn<T>(
|
|
|
689
689
|
|
|
690
690
|
async function scoped<T>(
|
|
691
691
|
connection: Connection,
|
|
692
|
-
depth: number,
|
|
693
|
-
body: (tx: Queryable) => Promise<T>,
|
|
694
|
-
commit: string,
|
|
695
|
-
rollback: string,
|
|
692
|
+
depth : number,
|
|
693
|
+
body : (tx: Queryable) => Promise<T>,
|
|
694
|
+
commit : string,
|
|
695
|
+
rollback : string,
|
|
696
696
|
): Promise<T> {
|
|
697
697
|
let open = true;
|
|
698
698
|
const guarded: Run = (text, params, mode) => {
|
|
@@ -702,9 +702,9 @@ async function scoped<T>(
|
|
|
702
702
|
return connection.run(text, params, mode);
|
|
703
703
|
};
|
|
704
704
|
const tx: Queryable = {
|
|
705
|
-
engine: connection.engine,
|
|
705
|
+
engine : connection.engine,
|
|
706
706
|
maxParams: connection.maxParams,
|
|
707
|
-
query: guarded,
|
|
707
|
+
query : guarded,
|
|
708
708
|
transaction: async <U>(inner: (tx: Queryable) => Promise<U>): Promise<U> => {
|
|
709
709
|
const savepoint = `uf_sp_${depth + 1}`;
|
|
710
710
|
await guarded(`SAVEPOINT ${savepoint}`, [], "exec");
|
|
@@ -773,12 +773,12 @@ export function transactionOnSync<T>(connection: Connection, body: (tx: SyncQuer
|
|
|
773
773
|
}
|
|
774
774
|
|
|
775
775
|
function scopedSync<T>(
|
|
776
|
-
runSync: RunSync,
|
|
776
|
+
runSync : RunSync,
|
|
777
777
|
connection: Connection,
|
|
778
|
-
depth: number,
|
|
779
|
-
body: (tx: SyncQueryable) => T,
|
|
780
|
-
commit: string,
|
|
781
|
-
rollback: string,
|
|
778
|
+
depth : number,
|
|
779
|
+
body : (tx: SyncQueryable) => T,
|
|
780
|
+
commit : string,
|
|
781
|
+
rollback : string,
|
|
782
782
|
): T {
|
|
783
783
|
let open = true;
|
|
784
784
|
const guarded: RunSync = (text, params, mode) => {
|
|
@@ -788,7 +788,7 @@ function scopedSync<T>(
|
|
|
788
788
|
return runSync(text, params, mode);
|
|
789
789
|
};
|
|
790
790
|
const tx: SyncQueryable = {
|
|
791
|
-
engine: connection.engine,
|
|
791
|
+
engine : connection.engine,
|
|
792
792
|
maxParams: connection.maxParams,
|
|
793
793
|
querySync: guarded,
|
|
794
794
|
transactionSync: <U>(inner: (tx: SyncQueryable) => U): U => {
|
|
@@ -863,9 +863,9 @@ function connect(connection: Connection): Handle {
|
|
|
863
863
|
const runSync = connection.runSync;
|
|
864
864
|
|
|
865
865
|
const query = async (
|
|
866
|
-
text: string,
|
|
866
|
+
text : string,
|
|
867
867
|
params: $ReadOnlyArray<SqlParam>,
|
|
868
|
-
mode: QueryMode,
|
|
868
|
+
mode : QueryMode,
|
|
869
869
|
): Promise<QueryResult> => {
|
|
870
870
|
if (syncOpen) {
|
|
871
871
|
unsupported("an asynchronous query while a synchronous transaction is open");
|
|
@@ -894,9 +894,9 @@ function connect(connection: Connection): Handle {
|
|
|
894
894
|
}
|
|
895
895
|
};
|
|
896
896
|
const querySync = (
|
|
897
|
-
text: string,
|
|
897
|
+
text : string,
|
|
898
898
|
params: $ReadOnlyArray<SqlParam>,
|
|
899
|
-
mode: QueryMode,
|
|
899
|
+
mode : QueryMode,
|
|
900
900
|
): QueryResult => {
|
|
901
901
|
const run =
|
|
902
902
|
connection.runSync ?? unsupported("a synchronous query on an asynchronous connection");
|
|
@@ -930,7 +930,7 @@ function connect(connection: Connection): Handle {
|
|
|
930
930
|
}
|
|
931
931
|
};
|
|
932
932
|
return {
|
|
933
|
-
engine: connection.engine,
|
|
933
|
+
engine : connection.engine,
|
|
934
934
|
maxParams: connection.maxParams,
|
|
935
935
|
query,
|
|
936
936
|
transaction,
|
package/internal/shared.js
CHANGED
|
@@ -161,9 +161,9 @@ export function bytesParam(expected: string, value: mixed): Uint8Array {
|
|
|
161
161
|
|
|
162
162
|
/** One of an enum's labels. */
|
|
163
163
|
export function label<T extends string>(
|
|
164
|
-
values: $ReadOnlyArray<T>,
|
|
164
|
+
values : $ReadOnlyArray<T>,
|
|
165
165
|
expected: string,
|
|
166
|
-
value: mixed,
|
|
166
|
+
value : mixed,
|
|
167
167
|
): T {
|
|
168
168
|
const index = typeof value === "string" ? values.findIndex((item) => item === value) : -1;
|
|
169
169
|
if (index < 0) {
|
|
@@ -174,9 +174,9 @@ export function label<T extends string>(
|
|
|
174
174
|
|
|
175
175
|
/** Encode one of an enum's labels, refusing a string that is not one. */
|
|
176
176
|
export function labelParam<T extends string>(
|
|
177
|
-
values: $ReadOnlyArray<T>,
|
|
177
|
+
values : $ReadOnlyArray<T>,
|
|
178
178
|
expected: string,
|
|
179
|
-
value: mixed,
|
|
179
|
+
value : mixed,
|
|
180
180
|
): T {
|
|
181
181
|
const index = typeof value === "string" ? values.findIndex((item) => item === value) : -1;
|
|
182
182
|
if (index < 0) {
|
package/internal/statements.js
CHANGED
package/mysql.js
CHANGED
|
@@ -34,8 +34,8 @@ export type MysqlParam = null | string | Uint8Array;
|
|
|
34
34
|
/** How one MySQL type becomes a Flow value and back. */
|
|
35
35
|
export type Codec<T> = {|
|
|
36
36
|
readonly sqlType: string,
|
|
37
|
-
readonly decode: (value: mixed) => T,
|
|
38
|
-
readonly encode: (value: T) => MysqlParam,
|
|
37
|
+
readonly decode : (value: mixed) => T,
|
|
38
|
+
readonly encode : (value: T) => MysqlParam,
|
|
39
39
|
|};
|
|
40
40
|
|
|
41
41
|
/** `TINYINT` through `INT`, signed or unsigned, and `YEAR`: all fit a `number`. */
|
|
@@ -55,7 +55,7 @@ export function integer(sqlType: string): Codec<number> {
|
|
|
55
55
|
/** `BIGINT`, signed or unsigned, exactly. */
|
|
56
56
|
export const bigint: Codec<bigint> = {
|
|
57
57
|
sqlType: "bigint",
|
|
58
|
-
decode: (value) => bigintFrom("bigint", value),
|
|
58
|
+
decode : (value) => bigintFrom("bigint", value),
|
|
59
59
|
encode: (value) => {
|
|
60
60
|
if (typeof value !== "bigint") {
|
|
61
61
|
return encodeError("bigint (a bigint)", value);
|
|
@@ -169,14 +169,14 @@ export function bytes(sqlType: string): Codec<Uint8Array> {
|
|
|
169
169
|
/** `JSON`. */
|
|
170
170
|
export const json: Codec<JsonValue> = {
|
|
171
171
|
sqlType: "json",
|
|
172
|
-
decode: (value) => jsonFromText(value),
|
|
173
|
-
encode: (value) => jsonParam(value),
|
|
172
|
+
decode : (value) => jsonFromText(value),
|
|
173
|
+
encode : (value) => jsonParam(value),
|
|
174
174
|
};
|
|
175
175
|
|
|
176
176
|
/** A result sqlc could not type. */
|
|
177
177
|
export const unknown: Codec<mixed> = {
|
|
178
178
|
sqlType: "unknown",
|
|
179
|
-
decode: (value) => value,
|
|
179
|
+
decode : (value) => value,
|
|
180
180
|
encode: (value) => {
|
|
181
181
|
if (value === null || typeof value === "string" || value instanceof Uint8Array) {
|
|
182
182
|
return value;
|
|
@@ -194,7 +194,7 @@ export const unknown: Codec<mixed> = {
|
|
|
194
194
|
/** An `ENUM(…)` column, from its values in declaration order. */
|
|
195
195
|
export function enumeration<T extends string>(
|
|
196
196
|
sqlType: string,
|
|
197
|
-
values: $ReadOnlyArray<T>,
|
|
197
|
+
values : $ReadOnlyArray<T>,
|
|
198
198
|
): Codec<T> {
|
|
199
199
|
const expected = `${sqlType} (${enumName(values)})`;
|
|
200
200
|
return {
|
|
@@ -209,14 +209,14 @@ export function enumeration<T extends string>(
|
|
|
209
209
|
* value back to what `codec` writes.
|
|
210
210
|
*/
|
|
211
211
|
export function map<T, U>(
|
|
212
|
-
codec: Codec<T>,
|
|
212
|
+
codec : Codec<T>,
|
|
213
213
|
decode: (value: T) => U,
|
|
214
214
|
encode: (value: U) => T,
|
|
215
215
|
): Codec<U> {
|
|
216
216
|
return {
|
|
217
217
|
sqlType: codec.sqlType,
|
|
218
|
-
decode: (value) => decode(codec.decode(value)),
|
|
219
|
-
encode: (value) => codec.encode(encode(value)),
|
|
218
|
+
decode : (value) => decode(codec.decode(value)),
|
|
219
|
+
encode : (value) => codec.encode(encode(value)),
|
|
220
220
|
};
|
|
221
221
|
}
|
|
222
222
|
|
package/package.json
CHANGED
package/postgresql.js
CHANGED
|
@@ -39,8 +39,8 @@ export type ArrayNode = string | null | $ReadOnlyArray<ArrayNode>;
|
|
|
39
39
|
export type Codec<T> = {|
|
|
40
40
|
/** For messages: `int8`, `text[]`. */
|
|
41
41
|
readonly sqlType: string,
|
|
42
|
-
readonly decode: (value: mixed) => T,
|
|
43
|
-
readonly encode: (value: T) => string,
|
|
42
|
+
readonly decode : (value: mixed) => T,
|
|
43
|
+
readonly encode : (value: T) => string,
|
|
44
44
|
readonly element: (node: ArrayNode) => T,
|
|
45
45
|
/** The value as it appears inside `{…}`: quoted, or a nested literal. */
|
|
46
46
|
readonly literal: (value: T) => string,
|
|
@@ -54,9 +54,9 @@ function quote(text: string): string {
|
|
|
54
54
|
|
|
55
55
|
/** A scalar codec from its two text conversions. */
|
|
56
56
|
export function scalar<T>(
|
|
57
|
-
sqlType: string,
|
|
57
|
+
sqlType : string,
|
|
58
58
|
fromText: (text: string) => T,
|
|
59
|
-
toText: (value: T) => string,
|
|
59
|
+
toText : (value: T) => string,
|
|
60
60
|
): Codec<T> {
|
|
61
61
|
const decode = (value: mixed): T => {
|
|
62
62
|
if (typeof value !== "string") {
|
|
@@ -78,7 +78,7 @@ export function scalar<T>(
|
|
|
78
78
|
return fromText(node);
|
|
79
79
|
},
|
|
80
80
|
literal: (value) => quote(toText(value)),
|
|
81
|
-
depth: 0,
|
|
81
|
+
depth : 0,
|
|
82
82
|
};
|
|
83
83
|
}
|
|
84
84
|
|
|
@@ -381,7 +381,7 @@ export const timestamptz: Codec<Date> = scalar("timestamptz", instantFromText, i
|
|
|
381
381
|
/** A value sqlc could not type (`any`, `anyelement`, …): whatever the server printed. */
|
|
382
382
|
export const unknown: Codec<mixed> = {
|
|
383
383
|
sqlType: "unknown",
|
|
384
|
-
decode: (value) => value,
|
|
384
|
+
decode : (value) => value,
|
|
385
385
|
encode: (value) => {
|
|
386
386
|
if (typeof value === "string") {
|
|
387
387
|
return value;
|
|
@@ -396,7 +396,7 @@ export const unknown: Codec<mixed> = {
|
|
|
396
396
|
},
|
|
397
397
|
element: (node) => node,
|
|
398
398
|
literal: (value) => (value === null ? "NULL" : quote(String(value))),
|
|
399
|
-
depth: 0,
|
|
399
|
+
depth : 0,
|
|
400
400
|
};
|
|
401
401
|
|
|
402
402
|
// ---------------------------------------------------------------------------
|
|
@@ -405,7 +405,7 @@ export const unknown: Codec<mixed> = {
|
|
|
405
405
|
/** A codec for a `CREATE TYPE … AS ENUM`, from its labels in declaration order. */
|
|
406
406
|
export function enumeration<T extends string>(
|
|
407
407
|
sqlType: string,
|
|
408
|
-
values: $ReadOnlyArray<T>,
|
|
408
|
+
values : $ReadOnlyArray<T>,
|
|
409
409
|
): Codec<T> {
|
|
410
410
|
const expected = `${sqlType} (${enumName(values)})`;
|
|
411
411
|
return scalar(
|
|
@@ -550,10 +550,10 @@ export function array<T>(element: Codec<T>): Codec<$ReadOnlyArray<T>> {
|
|
|
550
550
|
// An empty array has no dimensions at all, whatever the column says.
|
|
551
551
|
return root.length === 0 ? [] : fromNode(root);
|
|
552
552
|
},
|
|
553
|
-
encode: toLiteral,
|
|
553
|
+
encode : toLiteral,
|
|
554
554
|
element: (node) => (Array.isArray(node) && node.length === 0 ? [] : fromNode(node)),
|
|
555
555
|
literal: toLiteral,
|
|
556
|
-
depth: element.depth + 1,
|
|
556
|
+
depth : element.depth + 1,
|
|
557
557
|
};
|
|
558
558
|
}
|
|
559
559
|
|
|
@@ -563,17 +563,17 @@ export function array<T>(element: Codec<T>): Codec<$ReadOnlyArray<T>> {
|
|
|
563
563
|
* options, where `decode` is the application's own check.
|
|
564
564
|
*/
|
|
565
565
|
export function map<T, U>(
|
|
566
|
-
codec: Codec<T>,
|
|
566
|
+
codec : Codec<T>,
|
|
567
567
|
decode: (value: T) => U,
|
|
568
568
|
encode: (value: U) => T,
|
|
569
569
|
): Codec<U> {
|
|
570
570
|
return {
|
|
571
571
|
sqlType: codec.sqlType,
|
|
572
|
-
decode: (value) => decode(codec.decode(value)),
|
|
573
|
-
encode: (value) => codec.encode(encode(value)),
|
|
572
|
+
decode : (value) => decode(codec.decode(value)),
|
|
573
|
+
encode : (value) => codec.encode(encode(value)),
|
|
574
574
|
element: (node) => decode(codec.element(node)),
|
|
575
575
|
literal: (value) => codec.literal(encode(value)),
|
|
576
|
-
depth: codec.depth,
|
|
576
|
+
depth : codec.depth,
|
|
577
577
|
};
|
|
578
578
|
}
|
|
579
579
|
|
package/sqlite.js
CHANGED
|
@@ -31,8 +31,8 @@ import {
|
|
|
31
31
|
/** How one declared type becomes a Flow value and back. */
|
|
32
32
|
export type Codec<T> = {|
|
|
33
33
|
readonly sqlType: string,
|
|
34
|
-
readonly decode: (value: mixed) => T,
|
|
35
|
-
readonly encode: (value: T) => SqlParam,
|
|
34
|
+
readonly decode : (value: mixed) => T,
|
|
35
|
+
readonly encode : (value: T) => SqlParam,
|
|
36
36
|
|};
|
|
37
37
|
|
|
38
38
|
/** `INTEGER` as a `number`, which throws past 2^53 instead of rounding. */
|
|
@@ -50,8 +50,8 @@ export const integer: Codec<number> = {
|
|
|
50
50
|
/** `INTEGER` as a `bigint`, for `sqliteInteger: "bigint"`. */
|
|
51
51
|
export const integerAsBigint: Codec<bigint> = {
|
|
52
52
|
sqlType: "INTEGER",
|
|
53
|
-
decode: (value) => bigintFrom("INTEGER", value),
|
|
54
|
-
encode: (value) => bigintParam("INTEGER (a bigint)", value),
|
|
53
|
+
decode : (value) => bigintFrom("INTEGER", value),
|
|
54
|
+
encode : (value) => bigintParam("INTEGER (a bigint)", value),
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
/** `REAL`, `DOUBLE`, `FLOAT`, and `NUMERIC`/`DECIMAL`, which SQLite stores as one or an INTEGER. */
|
|
@@ -106,21 +106,21 @@ export const string: Codec<string> = text("TEXT");
|
|
|
106
106
|
/** `BLOB`. */
|
|
107
107
|
export const blob: Codec<Uint8Array> = {
|
|
108
108
|
sqlType: "BLOB",
|
|
109
|
-
decode: (value) => bytesFrom("BLOB", value),
|
|
110
|
-
encode: (value) => bytesParam("BLOB (a Uint8Array)", value),
|
|
109
|
+
decode : (value) => bytesFrom("BLOB", value),
|
|
110
|
+
encode : (value) => bytesParam("BLOB (a Uint8Array)", value),
|
|
111
111
|
};
|
|
112
112
|
|
|
113
113
|
/** `JSON` and `JSONB` (sqlc reads the latter through `json()`), stored as text. */
|
|
114
114
|
export const json: Codec<JsonValue> = {
|
|
115
115
|
sqlType: "JSON",
|
|
116
|
-
decode: (value) => jsonFromText(value),
|
|
117
|
-
encode: (value) => jsonParam(value),
|
|
116
|
+
decode : (value) => jsonFromText(value),
|
|
117
|
+
encode : (value) => jsonParam(value),
|
|
118
118
|
};
|
|
119
119
|
|
|
120
120
|
/** A declared type with no affinity uf can type, or a result sqlc could not. */
|
|
121
121
|
export const unknown: Codec<mixed> = {
|
|
122
122
|
sqlType: "unknown",
|
|
123
|
-
decode: (value) => value,
|
|
123
|
+
decode : (value) => value,
|
|
124
124
|
encode: (value) => {
|
|
125
125
|
if (
|
|
126
126
|
value === null ||
|
|
@@ -141,7 +141,7 @@ export const unknown: Codec<mixed> = {
|
|
|
141
141
|
/** A `CHECK (x IN (…))`-style enum sqlc was told about through an override. */
|
|
142
142
|
export function enumeration<T extends string>(
|
|
143
143
|
sqlType: string,
|
|
144
|
-
values: $ReadOnlyArray<T>,
|
|
144
|
+
values : $ReadOnlyArray<T>,
|
|
145
145
|
): Codec<T> {
|
|
146
146
|
const expected = `${sqlType} (${enumName(values)})`;
|
|
147
147
|
return {
|
|
@@ -156,14 +156,14 @@ export function enumeration<T extends string>(
|
|
|
156
156
|
* value back to what `codec` writes.
|
|
157
157
|
*/
|
|
158
158
|
export function map<T, U>(
|
|
159
|
-
codec: Codec<T>,
|
|
159
|
+
codec : Codec<T>,
|
|
160
160
|
decode: (value: T) => U,
|
|
161
161
|
encode: (value: U) => T,
|
|
162
162
|
): Codec<U> {
|
|
163
163
|
return {
|
|
164
164
|
sqlType: codec.sqlType,
|
|
165
|
-
decode: (value) => decode(codec.decode(value)),
|
|
166
|
-
encode: (value) => codec.encode(encode(value)),
|
|
165
|
+
decode : (value) => decode(codec.decode(value)),
|
|
166
|
+
encode : (value) => codec.encode(encode(value)),
|
|
167
167
|
};
|
|
168
168
|
}
|
|
169
169
|
|