@peerbit/indexer-sqlite3 1.1.2-c679a0d → 1.1.3
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/peerbit/sqlite3.min.js +88 -30
- package/dist/src/engine.d.ts.map +1 -1
- package/dist/src/engine.js +23 -12
- package/dist/src/engine.js.map +1 -1
- package/dist/src/schema.d.ts +7 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/schema.js +47 -14
- package/dist/src/schema.js.map +1 -1
- package/dist/src/sqlite3.d.ts.map +1 -1
- package/dist/src/sqlite3.js +2 -1
- package/dist/src/sqlite3.js.map +1 -1
- package/package.json +78 -78
- package/src/engine.ts +36 -18
- package/src/schema.ts +63 -13
- package/src/sqlite3.ts +2 -1
package/src/schema.ts
CHANGED
|
@@ -54,17 +54,27 @@ export type BindableValue =
|
|
|
54
54
|
| ArrayBuffer
|
|
55
55
|
| null;
|
|
56
56
|
|
|
57
|
+
export const u64ToI64 = (u64: bigint | number) => {
|
|
58
|
+
return (typeof u64 === "number" ? BigInt(u64) : u64) - 9223372036854775808n;
|
|
59
|
+
};
|
|
60
|
+
export const i64ToU64 = (i64: number | bigint) =>
|
|
61
|
+
(typeof i64 === "number" ? BigInt(i64) : i64) + 9223372036854775808n;
|
|
62
|
+
|
|
57
63
|
export const convertToSQLType = (
|
|
58
64
|
value: boolean | bigint | string | number | Uint8Array,
|
|
59
65
|
type?: FieldType,
|
|
60
66
|
): BindableValue => {
|
|
61
67
|
// add bigint when https://github.com/TryGhost/node-sqlite3/pull/1501 fixed
|
|
62
68
|
|
|
63
|
-
if (
|
|
64
|
-
if (
|
|
69
|
+
if (value != null) {
|
|
70
|
+
if (type === "bool") {
|
|
65
71
|
return value ? 1 : 0;
|
|
66
72
|
}
|
|
67
|
-
|
|
73
|
+
if (type === "u64") {
|
|
74
|
+
// shift to fit in i64
|
|
75
|
+
|
|
76
|
+
return u64ToI64(value as number | bigint);
|
|
77
|
+
}
|
|
68
78
|
}
|
|
69
79
|
return value as BindableValue;
|
|
70
80
|
};
|
|
@@ -101,9 +111,15 @@ export const convertFromSQLType = (
|
|
|
101
111
|
: nullAsUndefined(value);
|
|
102
112
|
}
|
|
103
113
|
if (type === "u64") {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
114
|
+
if (typeof value === "number" || typeof value === "bigint") {
|
|
115
|
+
return i64ToU64(value as number | bigint); // TODO is not always value type bigint?
|
|
116
|
+
}
|
|
117
|
+
if (value == null) {
|
|
118
|
+
return nullAsUndefined(value);
|
|
119
|
+
}
|
|
120
|
+
throw new Error(
|
|
121
|
+
`Unexpected value type for value ${value} expected number or bigint for u64 field`,
|
|
122
|
+
);
|
|
107
123
|
}
|
|
108
124
|
return nullAsUndefined(value);
|
|
109
125
|
};
|
|
@@ -145,7 +161,8 @@ export interface Table {
|
|
|
145
161
|
name: string;
|
|
146
162
|
ctor: Constructor<any>;
|
|
147
163
|
primary: string | false;
|
|
148
|
-
primaryIndex: number;
|
|
164
|
+
primaryIndex: number; // can be -1 for nested tables TODO make it more clear
|
|
165
|
+
primaryField?: SQLField; // can be undefined for nested tables TODO make it required
|
|
149
166
|
path: string[];
|
|
150
167
|
parentPath: string[] | undefined; // field path of the parent where this table originates from
|
|
151
168
|
fields: SQLField[];
|
|
@@ -195,6 +212,7 @@ export const getSQLTable = (
|
|
|
195
212
|
ctor,
|
|
196
213
|
parentPath: path,
|
|
197
214
|
path: newPath,
|
|
215
|
+
primaryField: fields.find((x) => x.isPrimary)!,
|
|
198
216
|
primary,
|
|
199
217
|
primaryIndex: fields.findIndex((x) => x.isPrimary),
|
|
200
218
|
children: dependencies,
|
|
@@ -1254,8 +1272,16 @@ export const convertSumRequestToQuery = (
|
|
|
1254
1272
|
tables,
|
|
1255
1273
|
table,
|
|
1256
1274
|
);
|
|
1275
|
+
|
|
1276
|
+
const inlineName = getInlineTableFieldName(request.key);
|
|
1277
|
+
const field = table.fields.find((x) => x.name === inlineName);
|
|
1278
|
+
if (unwrapNestedType(field!.from!.type) === "u64") {
|
|
1279
|
+
throw new Error("Summing is not supported for u64 fields");
|
|
1280
|
+
}
|
|
1281
|
+
const column = `${table.name}.${getInlineTableFieldName(request.key)}`;
|
|
1282
|
+
|
|
1257
1283
|
return {
|
|
1258
|
-
sql: `SELECT SUM(${
|
|
1284
|
+
sql: `SELECT SUM(${column}) as sum FROM ${table.name} ${query}`,
|
|
1259
1285
|
bindable,
|
|
1260
1286
|
};
|
|
1261
1287
|
};
|
|
@@ -1281,7 +1307,10 @@ export const convertSearchRequestToQuery = (
|
|
|
1281
1307
|
request: types.IterateOptions | undefined,
|
|
1282
1308
|
tables: Map<string, Table>,
|
|
1283
1309
|
rootTables: Table[],
|
|
1284
|
-
|
|
1310
|
+
options?: {
|
|
1311
|
+
shape?: types.Shape | undefined;
|
|
1312
|
+
stable?: boolean;
|
|
1313
|
+
},
|
|
1285
1314
|
): { sql: string; bindable: any[] } => {
|
|
1286
1315
|
let unionBuilder = "";
|
|
1287
1316
|
let orderByClause: string = "";
|
|
@@ -1289,7 +1318,7 @@ export const convertSearchRequestToQuery = (
|
|
|
1289
1318
|
let matchedOnce = false;
|
|
1290
1319
|
let lastError: Error | undefined = undefined;
|
|
1291
1320
|
|
|
1292
|
-
const selectsPerTable = selectAllFieldsFromTables(rootTables, shape);
|
|
1321
|
+
const selectsPerTable = selectAllFieldsFromTables(rootTables, options?.shape);
|
|
1293
1322
|
let bindableBuilder: any[] = [];
|
|
1294
1323
|
for (const [i, table] of rootTables.entries()) {
|
|
1295
1324
|
const { selects, joins: joinFromSelect } = selectsPerTable[i];
|
|
@@ -1301,6 +1330,10 @@ export const convertSearchRequestToQuery = (
|
|
|
1301
1330
|
tables,
|
|
1302
1331
|
table,
|
|
1303
1332
|
joinFromSelect,
|
|
1333
|
+
[],
|
|
1334
|
+
{
|
|
1335
|
+
stable: options?.stable,
|
|
1336
|
+
},
|
|
1304
1337
|
);
|
|
1305
1338
|
unionBuilder += `${unionBuilder.length > 0 ? " UNION ALL " : ""} ${selectQuery} ${query}`;
|
|
1306
1339
|
orderByClause =
|
|
@@ -1358,6 +1391,9 @@ const convertRequestToQuery = <
|
|
|
1358
1391
|
table: Table,
|
|
1359
1392
|
extraJoin?: Map<string, JoinTable>,
|
|
1360
1393
|
path: string[] = [],
|
|
1394
|
+
options?: {
|
|
1395
|
+
stable?: boolean;
|
|
1396
|
+
},
|
|
1361
1397
|
): R => {
|
|
1362
1398
|
let whereBuilder = "";
|
|
1363
1399
|
let bindableBuilder: any[] = [];
|
|
@@ -1388,8 +1424,16 @@ const convertRequestToQuery = <
|
|
|
1388
1424
|
}
|
|
1389
1425
|
|
|
1390
1426
|
if (isIterateRequest(request, type)) {
|
|
1391
|
-
|
|
1392
|
-
|
|
1427
|
+
let sort = request?.sort;
|
|
1428
|
+
if (!sort && options?.stable) {
|
|
1429
|
+
sort =
|
|
1430
|
+
table.primary && path.length === 0
|
|
1431
|
+
? [{ key: [table.primary], direction: types.SortDirection.ASC }]
|
|
1432
|
+
: undefined;
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
if (sort) {
|
|
1436
|
+
let sortArr = Array.isArray(sort) ? sort : [sort];
|
|
1393
1437
|
if (sortArr.length > 0) {
|
|
1394
1438
|
orderByBuilder = "";
|
|
1395
1439
|
let once = false;
|
|
@@ -1793,7 +1837,13 @@ const convertStateFieldQuery = (
|
|
|
1793
1837
|
} else {
|
|
1794
1838
|
throw new Error(`Unsupported compare type: ${query.compare}`);
|
|
1795
1839
|
}
|
|
1796
|
-
|
|
1840
|
+
|
|
1841
|
+
if (unwrapNestedType(tableField.from!.type) === "u64") {
|
|
1842
|
+
// shift left because that is how we insert the value
|
|
1843
|
+
bindable.push(u64ToI64(query.value.value));
|
|
1844
|
+
} else {
|
|
1845
|
+
bindable.push(query.value.value);
|
|
1846
|
+
}
|
|
1797
1847
|
}
|
|
1798
1848
|
} else if (query instanceof types.IsNull) {
|
|
1799
1849
|
where = `${keyWithTable} IS NULL`;
|
package/src/sqlite3.ts
CHANGED
|
@@ -40,7 +40,8 @@ let create = async (directory?: string) => {
|
|
|
40
40
|
fileMustExist: false,
|
|
41
41
|
readonly: false /* , verbose: (message) => console.log(message) */,
|
|
42
42
|
});
|
|
43
|
-
|
|
43
|
+
// TODO this test makes things faster, but for benchmarking it might yield wierd results where some runs are faster than others
|
|
44
|
+
db.pragma("journal_mode = WAL");
|
|
44
45
|
db.pragma("foreign_keys = on");
|
|
45
46
|
db.defaultSafeIntegers(true);
|
|
46
47
|
};
|