@ix-xs/node-comfort 1.1.31 → 2.0.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/CHANGELOG.md +190 -0
- package/LICENSE +21 -0
- package/README.md +210 -748
- package/index.js +84 -13
- package/package.json +201 -21
- package/src/Arr.js +901 -0
- package/src/Async.js +585 -0
- package/src/Cache.js +329 -0
- package/src/Checker.js +979 -188
- package/src/Cli.js +1034 -0
- package/src/Color.js +415 -0
- package/src/Crypto.js +707 -0
- package/src/Emitter.js +289 -0
- package/src/Env.js +555 -0
- package/src/FS.js +1483 -768
- package/src/Func.js +692 -0
- package/src/Http.js +538 -0
- package/src/Id.js +418 -0
- package/src/Logger.js +739 -307
- package/src/Num.js +849 -0
- package/src/Obj.js +904 -0
- package/src/SQLite.js +1308 -994
- package/src/Schema.js +1586 -0
- package/src/Str.js +999 -0
- package/src/Sys.js +511 -0
- package/src/Time.js +1323 -0
- package/src/Utils.js +321 -325
- package/src/errors.js +292 -0
- package/types/index.d.ts +693 -120
- package/types/src/Arr.d.ts +562 -0
- package/types/src/Arr.d.ts.map +1 -0
- package/types/src/Async.d.ts +321 -0
- package/types/src/Async.d.ts.map +1 -0
- package/types/src/Cache.d.ts +195 -0
- package/types/src/Cache.d.ts.map +1 -0
- package/types/src/Checker.d.ts +609 -81
- package/types/src/Checker.d.ts.map +1 -1
- package/types/src/Cli.d.ts +582 -0
- package/types/src/Cli.d.ts.map +1 -0
- package/types/src/Color.d.ts +294 -0
- package/types/src/Color.d.ts.map +1 -0
- package/types/src/Crypto.d.ts +534 -0
- package/types/src/Crypto.d.ts.map +1 -0
- package/types/src/Emitter.d.ts +170 -0
- package/types/src/Emitter.d.ts.map +1 -0
- package/types/src/Env.d.ts +382 -0
- package/types/src/Env.d.ts.map +1 -0
- package/types/src/FS.d.ts +709 -201
- package/types/src/FS.d.ts.map +1 -1
- package/types/src/Func.d.ts +455 -0
- package/types/src/Func.d.ts.map +1 -0
- package/types/src/Http.d.ts +374 -0
- package/types/src/Http.d.ts.map +1 -0
- package/types/src/Id.d.ts +200 -0
- package/types/src/Id.d.ts.map +1 -0
- package/types/src/Logger.d.ts +374 -24
- package/types/src/Logger.d.ts.map +1 -1
- package/types/src/Num.d.ts +578 -0
- package/types/src/Num.d.ts.map +1 -0
- package/types/src/Obj.d.ts +475 -0
- package/types/src/Obj.d.ts.map +1 -0
- package/types/src/SQLite.d.ts +647 -305
- package/types/src/SQLite.d.ts.map +1 -1
- package/types/src/Schema.d.ts +940 -0
- package/types/src/Schema.d.ts.map +1 -0
- package/types/src/Str.d.ts +696 -0
- package/types/src/Str.d.ts.map +1 -0
- package/types/src/Sys.d.ts +269 -0
- package/types/src/Sys.d.ts.map +1 -0
- package/types/src/Time.d.ts +664 -0
- package/types/src/Time.d.ts.map +1 -0
- package/types/src/Utils.d.ts +154 -102
- package/types/src/Utils.d.ts.map +1 -1
- package/types/src/errors.d.ts +292 -0
- package/types/src/errors.d.ts.map +1 -0
- package/tsconfig.json +0 -18
- package/types/index.d.ts.map +0 -1
package/types/src/SQLite.d.ts
CHANGED
|
@@ -1,391 +1,733 @@
|
|
|
1
1
|
export = SQLite;
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* A SQLite database connection.
|
|
4
|
+
*
|
|
5
|
+
* @template [R=Record<string, any>] The row type read methods return by default. Each call can override it.
|
|
6
|
+
*/
|
|
7
|
+
declare class SQLite<R = Record<string, any>> {
|
|
3
8
|
/**
|
|
4
|
-
*
|
|
9
|
+
* Opens a database, creating the file and its folders if needed. Use
|
|
10
|
+
* `":memory:"` for a throwaway in-memory database.
|
|
5
11
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
12
|
+
* @example
|
|
13
|
+
* const db = new nc.SQLite(); // ./db.sqlite
|
|
14
|
+
* const app = new nc.SQLite("data/app.sqlite", { dates: true });
|
|
15
|
+
* const mem = new nc.SQLite(":memory:");
|
|
8
16
|
*
|
|
9
|
-
* @param {string} [path]
|
|
17
|
+
* @param {string} [path="db.sqlite"] Relative to the working directory.
|
|
18
|
+
* @param {SQLiteOptions} [options]
|
|
19
|
+
* @throws {SQLiteError} If the file can't be opened.
|
|
20
|
+
*/
|
|
21
|
+
constructor(path?: string, options?: SQLiteOptions);
|
|
22
|
+
/**
|
|
23
|
+
* Runs SQL that doesn't return rows. Without parameters, you can pass
|
|
24
|
+
* several statements at once.
|
|
10
25
|
*
|
|
11
26
|
* @example
|
|
12
|
-
*
|
|
27
|
+
* db.exec("UPDATE users SET active = ? WHERE last_login < ?", [false, cutoff]);
|
|
28
|
+
* db.exec("DELETE FROM sessions WHERE user_id = $id", { $id: 42 });
|
|
29
|
+
* db.exec(`
|
|
30
|
+
* CREATE TABLE tags (id INTEGER PRIMARY KEY, label TEXT UNIQUE);
|
|
31
|
+
* CREATE INDEX idx_tags_label ON tags (label);
|
|
32
|
+
* `);
|
|
33
|
+
*
|
|
34
|
+
* @param {string} sql
|
|
35
|
+
* @param {unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null} [params] An array, named parameters like `{ $id: 1 }`, or a single value.
|
|
36
|
+
* @returns {{ changes: number, lastInsertRowid: number | bigint }}
|
|
37
|
+
* @throws {SQLiteError}
|
|
38
|
+
*/
|
|
39
|
+
exec(sql: string, params?: unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null): {
|
|
40
|
+
changes: number;
|
|
41
|
+
lastInsertRowid: number | bigint;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Runs a query and returns the first row. When it reads a single table,
|
|
45
|
+
* JSON and boolean columns are converted.
|
|
13
46
|
*
|
|
14
47
|
* @example
|
|
15
|
-
* const
|
|
48
|
+
* const user = db.queryOne("SELECT * FROM users WHERE email = ?", [email]);
|
|
49
|
+
*
|
|
50
|
+
* @template [T=R]
|
|
51
|
+
* @param {string} sql
|
|
52
|
+
* @param {unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null} [params]
|
|
53
|
+
* @returns {T | undefined}
|
|
54
|
+
* @throws {SQLiteError}
|
|
16
55
|
*/
|
|
17
|
-
|
|
56
|
+
queryOne<T = R>(sql: string, params?: unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null): T | undefined;
|
|
18
57
|
/**
|
|
19
|
-
*
|
|
58
|
+
* Runs a query and returns every row.
|
|
20
59
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* - `INSERT`
|
|
24
|
-
* - `UPDATE`
|
|
25
|
-
* - `DELETE`
|
|
26
|
-
* - `CREATE INDEX`
|
|
27
|
-
* - PRAGMA writes
|
|
60
|
+
* @example
|
|
61
|
+
* const stats = db.queryAll("SELECT role, COUNT(*) AS n FROM users GROUP BY role");
|
|
28
62
|
*
|
|
29
|
-
* @
|
|
30
|
-
* @param {
|
|
31
|
-
* @
|
|
63
|
+
* @template [T=R]
|
|
64
|
+
* @param {string} sql
|
|
65
|
+
* @param {unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null} [params]
|
|
66
|
+
* @returns {T[]}
|
|
67
|
+
* @throws {SQLiteError}
|
|
32
68
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @
|
|
44
|
-
* @
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* @param {any[]|any} [params=[]] - Parameters to bind.
|
|
54
|
-
* @returns {Promise<object[]|{ error: string }>}
|
|
55
|
-
*/
|
|
56
|
-
queryAll(sql: string, params?: any[] | any): Promise<object[] | {
|
|
57
|
-
error: string;
|
|
58
|
-
}>;
|
|
59
|
-
/**
|
|
60
|
-
* Creates a table and stores its schema in memory.
|
|
61
|
-
*
|
|
62
|
-
* This method supports both the new `columns` property and the legacy
|
|
63
|
-
* `options` property for backward compatibility.
|
|
64
|
-
*
|
|
65
|
-
* @param {{
|
|
66
|
-
* name: string,
|
|
67
|
-
* columns?: Record<string, {
|
|
68
|
-
* type: "INTEGER"|"TEXT"|"REAL"|"BLOB"|"NUMERIC",
|
|
69
|
-
* primaryKey?: boolean,
|
|
70
|
-
* autoincrement?: boolean,
|
|
71
|
-
* notNull?: boolean,
|
|
72
|
-
* unique?: boolean,
|
|
73
|
-
* defaultValue?: any,
|
|
74
|
-
* values?: any[],
|
|
75
|
-
* references?: { table: string, column: string },
|
|
76
|
-
* onDelete?: string,
|
|
77
|
-
* onUpdate?: string
|
|
78
|
-
* }>,
|
|
79
|
-
* options?: Record<string, any>,
|
|
80
|
-
* constraints?: Array<
|
|
81
|
-
* | { type: "unique", columns: string[] }
|
|
82
|
-
* | { type: "primaryKey", columns: string[] }
|
|
83
|
-
* | { type: "check", expression: string }
|
|
84
|
-
* | {
|
|
85
|
-
* type: "foreignKey",
|
|
86
|
-
* columns: string[],
|
|
87
|
-
* references: { table: string, columns: string[] },
|
|
88
|
-
* onDelete?: string,
|
|
89
|
-
* onUpdate?: string
|
|
90
|
-
* }
|
|
91
|
-
* >,
|
|
92
|
-
* indexes?: Array<{
|
|
93
|
-
* name?: string,
|
|
94
|
-
* columns: string[],
|
|
95
|
-
* unique?: boolean
|
|
96
|
-
* }>
|
|
97
|
-
* }} table - Table definition.
|
|
98
|
-
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
69
|
+
queryAll<T = R>(sql: string, params?: unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null): T[];
|
|
70
|
+
/**
|
|
71
|
+
* Goes through the rows of a query one at a time, without loading them all.
|
|
72
|
+
* For big exports.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* for (const row of db.iterate("SELECT * FROM events WHERE day = ?", [day])) {
|
|
76
|
+
* stream.write(JSON.stringify(row) + "\n");
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* @template [T=R]
|
|
80
|
+
* @param {string} sql
|
|
81
|
+
* @param {unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null} [params]
|
|
82
|
+
* @returns {Generator<T, void, undefined>}
|
|
83
|
+
* @throws {SQLiteError}
|
|
84
|
+
*/
|
|
85
|
+
iterate<T = R>(sql: string, params?: unknown[] | Record<string, unknown> | string | number | bigint | boolean | Date | null): Generator<T, void, undefined>;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a table with its constraints and indexes, unless it already
|
|
88
|
+
* exists. To change an existing table, use `migrate()`.
|
|
99
89
|
*
|
|
100
90
|
* @example
|
|
101
|
-
*
|
|
102
|
-
* name: "
|
|
91
|
+
* db.createTable({
|
|
92
|
+
* name: "posts",
|
|
103
93
|
* columns: {
|
|
104
94
|
* id: { type: "INTEGER", primaryKey: true, autoincrement: true },
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* references: { table: "User", column: "id" },
|
|
109
|
-
* onDelete: "CASCADE"
|
|
110
|
-
* },
|
|
111
|
-
* title: { type: "TEXT", notNull: true }
|
|
95
|
+
* user_id: { type: "INTEGER", notNull: true, references: { table: "users", column: "id" }, onDelete: "CASCADE" },
|
|
96
|
+
* status: { type: "TEXT", values: ["draft", "published"], defaultValue: "draft" },
|
|
97
|
+
* tags: { type: "JSON", defaultValue: [] },
|
|
112
98
|
* },
|
|
113
|
-
* indexes: [{ columns: ["
|
|
99
|
+
* indexes: [{ columns: ["user_id", "status"] }],
|
|
114
100
|
* });
|
|
101
|
+
*
|
|
102
|
+
* @param {TableDefinition} table
|
|
103
|
+
* @returns {{ ok: true }}
|
|
104
|
+
* @throws {SQLiteError} If the definition is invalid.
|
|
115
105
|
*/
|
|
116
|
-
createTable(table: {
|
|
117
|
-
name: string;
|
|
118
|
-
columns?: Record<string, {
|
|
119
|
-
type: "INTEGER" | "TEXT" | "REAL" | "BLOB" | "NUMERIC";
|
|
120
|
-
primaryKey?: boolean;
|
|
121
|
-
autoincrement?: boolean;
|
|
122
|
-
notNull?: boolean;
|
|
123
|
-
unique?: boolean;
|
|
124
|
-
defaultValue?: any;
|
|
125
|
-
values?: any[];
|
|
126
|
-
references?: {
|
|
127
|
-
table: string;
|
|
128
|
-
column: string;
|
|
129
|
-
};
|
|
130
|
-
onDelete?: string;
|
|
131
|
-
onUpdate?: string;
|
|
132
|
-
}>;
|
|
133
|
-
options?: Record<string, any>;
|
|
134
|
-
constraints?: Array<{
|
|
135
|
-
type: "unique";
|
|
136
|
-
columns: string[];
|
|
137
|
-
} | {
|
|
138
|
-
type: "primaryKey";
|
|
139
|
-
columns: string[];
|
|
140
|
-
} | {
|
|
141
|
-
type: "check";
|
|
142
|
-
expression: string;
|
|
143
|
-
} | {
|
|
144
|
-
type: "foreignKey";
|
|
145
|
-
columns: string[];
|
|
146
|
-
references: {
|
|
147
|
-
table: string;
|
|
148
|
-
columns: string[];
|
|
149
|
-
};
|
|
150
|
-
onDelete?: string;
|
|
151
|
-
onUpdate?: string;
|
|
152
|
-
}>;
|
|
153
|
-
indexes?: Array<{
|
|
154
|
-
name?: string;
|
|
155
|
-
columns: string[];
|
|
156
|
-
unique?: boolean;
|
|
157
|
-
}>;
|
|
158
|
-
}): Promise<{
|
|
106
|
+
createTable(table: TableDefinition): {
|
|
159
107
|
ok: true;
|
|
160
|
-
}
|
|
161
|
-
error: string;
|
|
162
|
-
}>;
|
|
108
|
+
};
|
|
163
109
|
/**
|
|
164
|
-
* Creates an index
|
|
165
|
-
*
|
|
166
|
-
* Composite indexes are supported by passing multiple columns.
|
|
167
|
-
*
|
|
168
|
-
* @param {string} tableName - Table name.
|
|
169
|
-
* @param {{
|
|
170
|
-
* name?: string,
|
|
171
|
-
* columns: string[],
|
|
172
|
-
* unique?: boolean
|
|
173
|
-
* }} index - Index definition.
|
|
174
|
-
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
110
|
+
* Creates an index, unless it already exists.
|
|
175
111
|
*
|
|
176
112
|
* @example
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* }
|
|
113
|
+
* db.createIndex("users", { columns: ["email"], unique: true });
|
|
114
|
+
* db.createIndex("posts", { columns: ["slug"], unique: true, where: "deleted_at IS NULL" });
|
|
115
|
+
*
|
|
116
|
+
* @param {string} tableName
|
|
117
|
+
* @param {IndexDefinition} index
|
|
118
|
+
* @returns {{ ok: true }}
|
|
119
|
+
* @throws {SQLiteError}
|
|
181
120
|
*/
|
|
182
|
-
createIndex(tableName: string, index: {
|
|
183
|
-
name?: string;
|
|
184
|
-
columns: string[];
|
|
185
|
-
unique?: boolean;
|
|
186
|
-
}): Promise<{
|
|
121
|
+
createIndex(tableName: string, index: IndexDefinition): {
|
|
187
122
|
ok: true;
|
|
188
|
-
}
|
|
189
|
-
error: string;
|
|
190
|
-
}>;
|
|
123
|
+
};
|
|
191
124
|
/**
|
|
192
|
-
* Drops a table and
|
|
125
|
+
* Drops a table and everything in it.
|
|
193
126
|
*
|
|
194
|
-
* @param {string} tableName
|
|
195
|
-
* @returns {
|
|
127
|
+
* @param {string} tableName
|
|
128
|
+
* @returns {{ ok: true }}
|
|
129
|
+
* @throws {SQLiteError} If the table doesn't exist.
|
|
196
130
|
*/
|
|
197
|
-
deleteTable(tableName: string):
|
|
131
|
+
deleteTable(tableName: string): {
|
|
198
132
|
ok: true;
|
|
199
|
-
}
|
|
200
|
-
error: string;
|
|
201
|
-
}>;
|
|
133
|
+
};
|
|
202
134
|
/**
|
|
203
|
-
* Deletes
|
|
135
|
+
* Deletes every row, keeping the table.
|
|
204
136
|
*
|
|
205
|
-
* @param {string} tableName
|
|
206
|
-
* @returns {
|
|
137
|
+
* @param {string} tableName
|
|
138
|
+
* @returns {{ ok: true }}
|
|
139
|
+
* @throws {SQLiteError} If the table doesn't exist.
|
|
207
140
|
*/
|
|
208
|
-
clearTable(tableName: string):
|
|
141
|
+
clearTable(tableName: string): {
|
|
209
142
|
ok: true;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Does the table exist?
|
|
146
|
+
*
|
|
147
|
+
* @param {string} tableName
|
|
148
|
+
* @returns {boolean}
|
|
149
|
+
*/
|
|
150
|
+
hasTable(tableName: string): boolean;
|
|
213
151
|
/**
|
|
214
|
-
*
|
|
152
|
+
* The tables in the database, sorted.
|
|
215
153
|
*
|
|
216
|
-
* @
|
|
217
|
-
* @param {Record<string, any>} [where={}] - Conditions.
|
|
218
|
-
* @returns {Promise<object|undefined|{ error: string }>}
|
|
154
|
+
* @returns {string[]}
|
|
219
155
|
*/
|
|
220
|
-
|
|
221
|
-
error: string;
|
|
222
|
-
}>;
|
|
156
|
+
tables(): string[];
|
|
223
157
|
/**
|
|
224
|
-
*
|
|
158
|
+
* The columns of a table.
|
|
225
159
|
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
* - pagination via `limit` and `offset`
|
|
160
|
+
* @example
|
|
161
|
+
* db.columns("users");
|
|
162
|
+
* // [{ name: "id", type: "INTEGER", notNull: false, defaultValue: null, primaryKey: true }, ...]
|
|
230
163
|
*
|
|
231
|
-
* @param {string} tableName
|
|
232
|
-
* @
|
|
233
|
-
* @
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
*
|
|
238
|
-
* }} [options={}] - Query options.
|
|
239
|
-
* @returns {Promise<object[] | { error: string }>}
|
|
164
|
+
* @param {string} tableName
|
|
165
|
+
* @returns {ColumnInfo[]}
|
|
166
|
+
* @throws {SQLiteError} If the table doesn't exist.
|
|
167
|
+
*/
|
|
168
|
+
columns(tableName: string): ColumnInfo[];
|
|
169
|
+
/**
|
|
170
|
+
* The first row that matches.
|
|
240
171
|
*
|
|
241
172
|
* @example
|
|
242
|
-
* const
|
|
173
|
+
* const user = db.get("users", { email: "ada@example.com" });
|
|
174
|
+
* const latest = db.get("posts", { status: "published" }, { orderBy: "created_at", direction: "DESC" });
|
|
175
|
+
*
|
|
176
|
+
* @template [T=R]
|
|
177
|
+
* @param {string} tableName
|
|
178
|
+
* @param {Where<R>} [where]
|
|
179
|
+
* @param {Pick<QueryOptions, "columns" | "orderBy" | "direction">} [options]
|
|
180
|
+
* @returns {T | undefined}
|
|
181
|
+
* @throws {SQLiteError}
|
|
182
|
+
*/
|
|
183
|
+
get<T = R>(tableName: string, where?: Where<R>, options?: Pick<QueryOptions, "columns" | "orderBy" | "direction">): T | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* The rows that match, with sorting and pagination.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* db.getAll("users");
|
|
189
|
+
* db.getAll("users", { role: ["admin", "editor"], age: { gte: 18 } });
|
|
190
|
+
* db.getAll("posts", { $or: [{ pinned: true }, { views: { gt: 1000 } }] }, {
|
|
191
|
+
* orderBy: [["views", "DESC"], "title"],
|
|
243
192
|
* limit: 20,
|
|
244
|
-
* offset:
|
|
245
|
-
* orderBy: "createdAt",
|
|
246
|
-
* direction: "DESC"
|
|
193
|
+
* offset: 40,
|
|
247
194
|
* });
|
|
195
|
+
*
|
|
196
|
+
* @template [T=R]
|
|
197
|
+
* @param {string} tableName
|
|
198
|
+
* @param {Where<R>} [where]
|
|
199
|
+
* @param {QueryOptions} [options]
|
|
200
|
+
* @returns {T[]}
|
|
201
|
+
* @throws {SQLiteError}
|
|
248
202
|
*/
|
|
249
|
-
getAll(tableName: string, where?:
|
|
250
|
-
limit?: number;
|
|
251
|
-
offset?: number;
|
|
252
|
-
orderBy?: string;
|
|
253
|
-
direction?: "ASC" | "DESC";
|
|
254
|
-
}): Promise<object[] | {
|
|
255
|
-
error: string;
|
|
256
|
-
}>;
|
|
203
|
+
getAll<T = R>(tableName: string, where?: Where<R>, options?: QueryOptions): T[];
|
|
257
204
|
/**
|
|
258
|
-
*
|
|
205
|
+
* Counts the rows that match.
|
|
259
206
|
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
207
|
+
* @example
|
|
208
|
+
* db.count("users", { active: true });
|
|
262
209
|
*
|
|
263
|
-
* @param {string} tableName
|
|
264
|
-
* @param {
|
|
265
|
-
* @returns {
|
|
210
|
+
* @param {string} tableName
|
|
211
|
+
* @param {Where<R>} [where]
|
|
212
|
+
* @returns {number}
|
|
213
|
+
* @throws {SQLiteError}
|
|
266
214
|
*/
|
|
267
|
-
|
|
268
|
-
ok: true;
|
|
269
|
-
lastInsertRowid: bigint | number;
|
|
270
|
-
changes: number;
|
|
271
|
-
} | {
|
|
272
|
-
error: string;
|
|
273
|
-
}>;
|
|
215
|
+
count(tableName: string, where?: Where<R>): number;
|
|
274
216
|
/**
|
|
275
|
-
*
|
|
217
|
+
* Does any row match?
|
|
276
218
|
*
|
|
277
|
-
*
|
|
219
|
+
* @example
|
|
220
|
+
* if (db.exists("users", { email })) throw new Error("Email already registered");
|
|
278
221
|
*
|
|
279
|
-
* @param {string} tableName
|
|
280
|
-
* @param {
|
|
281
|
-
* @
|
|
282
|
-
* @
|
|
222
|
+
* @param {string} tableName
|
|
223
|
+
* @param {Where<R>} [where]
|
|
224
|
+
* @returns {boolean}
|
|
225
|
+
* @throws {SQLiteError}
|
|
283
226
|
*/
|
|
284
|
-
|
|
285
|
-
ok: true;
|
|
286
|
-
changes: number;
|
|
287
|
-
} | {
|
|
288
|
-
error: string;
|
|
289
|
-
}>;
|
|
227
|
+
exists(tableName: string, where?: Where<R>): boolean;
|
|
290
228
|
/**
|
|
291
|
-
* Inserts
|
|
229
|
+
* Inserts a row. Objects and arrays are stored as JSON, booleans as 1 and
|
|
230
|
+
* 0, dates as ISO strings. `undefined` values are skipped, so column
|
|
231
|
+
* defaults apply.
|
|
292
232
|
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
233
|
+
* @example
|
|
234
|
+
* const { lastInsertRowid } = db.insert("users", { email: "ada@example.com", settings: { theme: "dark" } });
|
|
295
235
|
*
|
|
296
|
-
* @param {string} tableName
|
|
297
|
-
* @param {Record<string,
|
|
298
|
-
* @
|
|
299
|
-
* @
|
|
236
|
+
* @param {string} tableName
|
|
237
|
+
* @param {Partial<R> & Record<string, unknown>} data
|
|
238
|
+
* @returns {InsertResult}
|
|
239
|
+
* @throws {SQLiteError} On a constraint failure; `sqliteCode` tells you which, like `"SQLITE_CONSTRAINT_UNIQUE"`.
|
|
240
|
+
*/
|
|
241
|
+
insert(tableName: string, data: Partial<R> & Record<string, unknown>): InsertResult;
|
|
242
|
+
/**
|
|
243
|
+
* Inserts many rows in one transaction: all or nothing, and much faster
|
|
244
|
+
* than one by one.
|
|
300
245
|
*
|
|
301
246
|
* @example
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
* }
|
|
247
|
+
* db.insertMany("products", rowsFromCsv);
|
|
248
|
+
*
|
|
249
|
+
* @param {string} tableName
|
|
250
|
+
* @param {Array<Partial<R> & Record<string, unknown>>} rows
|
|
251
|
+
* @returns {ChangeResult}
|
|
252
|
+
* @throws {SQLiteError} If a row fails; nothing is inserted then.
|
|
307
253
|
*/
|
|
308
|
-
|
|
309
|
-
ok: true;
|
|
310
|
-
changes: number;
|
|
311
|
-
lastInsertRowid: bigint | number;
|
|
312
|
-
} | {
|
|
313
|
-
error: string;
|
|
314
|
-
}>;
|
|
254
|
+
insertMany(tableName: string, rows: Array<Partial<R> & Record<string, unknown>>): ChangeResult;
|
|
315
255
|
/**
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
* Behavior:
|
|
319
|
-
* - If `where` is provided, the existing row is loaded and merged with `data`,
|
|
320
|
-
* then updated if found or inserted if not found.
|
|
321
|
-
* - If `where` is not provided but `data.id` exists, lookup is performed on `id`.
|
|
322
|
-
* - If no existing row is found, a new row is inserted.
|
|
256
|
+
* Updates the rows that match. The filter is required, so a forgotten
|
|
257
|
+
* condition can't rewrite the whole table.
|
|
323
258
|
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
259
|
+
* @example
|
|
260
|
+
* db.update("users", { active: false }, { last_login: { lt: "2025-01-01" } });
|
|
326
261
|
*
|
|
327
|
-
* @param {string} tableName
|
|
328
|
-
* @param {Record<string,
|
|
329
|
-
* @param {
|
|
330
|
-
* @returns {
|
|
262
|
+
* @param {string} tableName
|
|
263
|
+
* @param {Partial<R> & Record<string, unknown>} data `undefined` values are skipped.
|
|
264
|
+
* @param {Where<R>} where
|
|
265
|
+
* @returns {ChangeResult}
|
|
266
|
+
* @throws {SQLiteError}
|
|
331
267
|
*/
|
|
332
|
-
|
|
333
|
-
ok: true;
|
|
334
|
-
} | {
|
|
335
|
-
error: string;
|
|
336
|
-
}>;
|
|
268
|
+
update(tableName: string, data: Partial<R> & Record<string, unknown>, where: Where<R>): ChangeResult;
|
|
337
269
|
/**
|
|
338
|
-
*
|
|
270
|
+
* Inserts a row, or updates it in place if it clashes with an existing one
|
|
271
|
+
* on the `conflict` columns.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* db.upsert("settings", { user_id: 1, key: "theme", value: "dark" }, ["user_id", "key"]);
|
|
339
275
|
*
|
|
340
|
-
* @param {string} tableName
|
|
341
|
-
* @param {Record<string,
|
|
342
|
-
* @
|
|
276
|
+
* @param {string} tableName
|
|
277
|
+
* @param {Partial<R> & Record<string, unknown>} data
|
|
278
|
+
* @param {string[]} conflict The unique columns that identify the row.
|
|
279
|
+
* @returns {InsertResult}
|
|
280
|
+
* @throws {SQLiteError} If `conflict` doesn't match a unique constraint.
|
|
281
|
+
*/
|
|
282
|
+
upsert(tableName: string, data: Partial<R> & Record<string, unknown>, conflict: string[]): InsertResult;
|
|
283
|
+
/**
|
|
284
|
+
* Updates the row matching `where` (or `{ id: data.id }`), or inserts it.
|
|
285
|
+
* Kept from 1.x; `upsert()` does the same in one atomic statement.
|
|
286
|
+
*
|
|
287
|
+
* @param {string} tableName
|
|
288
|
+
* @param {Partial<R> & Record<string, unknown>} data
|
|
289
|
+
* @param {Where<R> | null} [where]
|
|
290
|
+
* @returns {{ ok: true }}
|
|
291
|
+
* @throws {SQLiteError}
|
|
343
292
|
*/
|
|
344
|
-
|
|
293
|
+
set(tableName: string, data: Partial<R> & Record<string, unknown>, where?: Where<R> | null): {
|
|
345
294
|
ok: true;
|
|
346
|
-
|
|
347
|
-
} | {
|
|
348
|
-
error: string;
|
|
349
|
-
}>;
|
|
295
|
+
};
|
|
350
296
|
/**
|
|
351
|
-
*
|
|
297
|
+
* Deletes the rows that match. The filter is required; `clearTable()`
|
|
298
|
+
* deletes everything on purpose.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* db.delete("sessions", { expires_at: { lt: new Date() } });
|
|
352
302
|
*
|
|
353
|
-
* @param {string} tableName
|
|
354
|
-
* @param {
|
|
355
|
-
* @returns {
|
|
303
|
+
* @param {string} tableName
|
|
304
|
+
* @param {Where<R>} where
|
|
305
|
+
* @returns {ChangeResult}
|
|
306
|
+
* @throws {SQLiteError}
|
|
356
307
|
*/
|
|
357
|
-
|
|
358
|
-
error: string;
|
|
359
|
-
}>;
|
|
308
|
+
delete(tableName: string, where: Where<R>): ChangeResult;
|
|
360
309
|
/**
|
|
361
|
-
*
|
|
310
|
+
* Shortcuts bound to one table, so you don't repeat its name.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* const users = db.table("users"); // in TypeScript: db.table<User>("users")
|
|
314
|
+
* users.insert({ email: "ada@example.com" });
|
|
315
|
+
* users.count({ active: true });
|
|
362
316
|
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
317
|
+
* @template [T=R]
|
|
318
|
+
* @param {string} tableName The table can be created later.
|
|
319
|
+
* @returns {TableHandle<T>}
|
|
320
|
+
*/
|
|
321
|
+
table<T = R>(tableName: string): TableHandle<T>;
|
|
322
|
+
/**
|
|
323
|
+
* Runs `callback` in a transaction: all its changes are saved together, or
|
|
324
|
+
* none are if it throws. The error is then thrown again. Transactions can
|
|
325
|
+
* nest; an inner failure can be caught without losing the outer work.
|
|
365
326
|
*
|
|
366
|
-
*
|
|
327
|
+
* @example
|
|
328
|
+
* db.transaction(() => {
|
|
329
|
+
* db.update("accounts", { balance: from.balance - amount }, { id: from.id });
|
|
330
|
+
* db.update("accounts", { balance: to.balance + amount }, { id: to.id });
|
|
331
|
+
* });
|
|
367
332
|
*
|
|
368
333
|
* @template T
|
|
369
|
-
* @param {() => T
|
|
370
|
-
* @returns {
|
|
334
|
+
* @param {() => T} callback Can be async; the commit then waits for it.
|
|
335
|
+
* @returns {T}
|
|
336
|
+
* @throws Whatever `callback` threw, after rolling back.
|
|
337
|
+
*/
|
|
338
|
+
transaction<T>(callback: () => T): T;
|
|
339
|
+
/** Whether a transaction is open. */
|
|
340
|
+
get inTransaction(): boolean;
|
|
341
|
+
/**
|
|
342
|
+
* Applies migrations in order, each one once. The current version is kept
|
|
343
|
+
* in the database, and each migration runs in its own transaction. Only
|
|
344
|
+
* ever add migrations at the end of the list.
|
|
371
345
|
*
|
|
372
346
|
* @example
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
347
|
+
* db.migrate([
|
|
348
|
+
* "CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE)",
|
|
349
|
+
* "ALTER TABLE users ADD COLUMN name TEXT",
|
|
350
|
+
* (db) => db.update("users", { name: "unknown" }, { name: null }),
|
|
351
|
+
* ]);
|
|
352
|
+
*
|
|
353
|
+
* @param {Migration[]} migrations All of them, from the first.
|
|
354
|
+
* @returns {{ from: number, to: number }} The version before and after.
|
|
355
|
+
* @throws {SQLiteError} If a migration fails. It's rolled back; earlier ones stay.
|
|
378
356
|
*/
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
357
|
+
migrate(migrations: Migration[]): {
|
|
358
|
+
from: number;
|
|
359
|
+
to: number;
|
|
360
|
+
};
|
|
361
|
+
/** The migration version, stored in `PRAGMA user_version`. */
|
|
362
|
+
get version(): number;
|
|
382
363
|
/**
|
|
383
|
-
*
|
|
364
|
+
* Copies the database to a new file. Safe while the app is running.
|
|
384
365
|
*
|
|
385
366
|
* @example
|
|
386
|
-
* db.
|
|
367
|
+
* db.backup(`backups/app-${nc.time.format(new Date(), "YYYY-MM-DD")}.sqlite`);
|
|
368
|
+
*
|
|
369
|
+
* @param {string} path Must not exist yet.
|
|
370
|
+
* @returns {string} The absolute path of the copy.
|
|
371
|
+
* @throws {SQLiteError} If the file exists or can't be written.
|
|
372
|
+
*/
|
|
373
|
+
backup(path: string): string;
|
|
374
|
+
/**
|
|
375
|
+
* Compacts the database file to reclaim unused space.
|
|
376
|
+
*
|
|
377
|
+
* @returns {void}
|
|
378
|
+
*/
|
|
379
|
+
vacuum(): void;
|
|
380
|
+
/**
|
|
381
|
+
* Makes a JavaScript function callable from SQL.
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* db.fn("slugify", (text) => nc.str.slugify(String(text)));
|
|
385
|
+
* db.queryAll("SELECT slugify(title) AS slug FROM posts");
|
|
386
|
+
*
|
|
387
|
+
* @param {string} name
|
|
388
|
+
* @param {(...args: any[]) => null | number | bigint | string | Uint8Array} implementation
|
|
389
|
+
* @param {{ deterministic?: boolean, varargs?: boolean }} [options] `deterministic` (on by default) lets indexes use it; `varargs` accepts any number of arguments.
|
|
390
|
+
* @returns {this}
|
|
391
|
+
*/
|
|
392
|
+
fn(name: string, implementation: (...args: any[]) => null | number | bigint | string | Uint8Array, options?: {
|
|
393
|
+
deterministic?: boolean;
|
|
394
|
+
varargs?: boolean;
|
|
395
|
+
}): this;
|
|
396
|
+
/** The absolute path of the database file, or `":memory:"`. */
|
|
397
|
+
get path(): string;
|
|
398
|
+
/** Whether the connection is open. */
|
|
399
|
+
get isOpen(): boolean;
|
|
400
|
+
/**
|
|
401
|
+
* The underlying `node:sqlite` database, for anything not covered here.
|
|
402
|
+
* @returns {import("node:sqlite").DatabaseSync}
|
|
403
|
+
*/
|
|
404
|
+
get native(): import("node:sqlite").DatabaseSync;
|
|
405
|
+
/**
|
|
406
|
+
* Closes the connection. Calling it twice is fine.
|
|
407
|
+
*
|
|
408
|
+
* @returns {void}
|
|
387
409
|
*/
|
|
388
410
|
close(): void;
|
|
411
|
+
/**
|
|
412
|
+
* Closes the connection at the end of a `using` block.
|
|
413
|
+
* @returns {void}
|
|
414
|
+
*/
|
|
415
|
+
[Symbol.dispose](): void;
|
|
389
416
|
#private;
|
|
390
417
|
}
|
|
418
|
+
declare namespace SQLite {
|
|
419
|
+
export { ColumnType, ForeignKeyAction, ColumnDefinition, TableConstraint, IndexDefinition, TableDefinition, WhereOperators, Where, WhereValue, SortDirection, QueryOptions, SQLiteOptions, InsertResult, ChangeResult, ColumnInfo, Migration, TableHandle };
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* A column type. `JSON` and `BOOLEAN` columns give you back objects and
|
|
423
|
+
* `true`/`false`.
|
|
424
|
+
*/
|
|
425
|
+
type ColumnType = "INTEGER" | "TEXT" | "REAL" | "BLOB" | "NUMERIC" | "JSON" | "BOOLEAN" | "DATETIME" | (string & {});
|
|
426
|
+
/**
|
|
427
|
+
* What happens to a row when the row it references changes.
|
|
428
|
+
*/
|
|
429
|
+
type ForeignKeyAction = "CASCADE" | "SET NULL" | "SET DEFAULT" | "RESTRICT" | "NO ACTION";
|
|
430
|
+
/**
|
|
431
|
+
* A column.
|
|
432
|
+
*/
|
|
433
|
+
type ColumnDefinition = {
|
|
434
|
+
/**
|
|
435
|
+
* Like `"INTEGER"`, `"TEXT"`, `"JSON"` or `"VARCHAR(255)"`.
|
|
436
|
+
*/
|
|
437
|
+
type: ColumnType;
|
|
438
|
+
primaryKey?: boolean | undefined;
|
|
439
|
+
/**
|
|
440
|
+
* With an `INTEGER` primary key: ids are never reused.
|
|
441
|
+
*/
|
|
442
|
+
autoincrement?: boolean | undefined;
|
|
443
|
+
notNull?: boolean | undefined;
|
|
444
|
+
unique?: boolean | undefined;
|
|
445
|
+
/**
|
|
446
|
+
* Objects work for JSON columns, booleans for BOOLEAN ones.
|
|
447
|
+
*/
|
|
448
|
+
defaultValue?: unknown;
|
|
449
|
+
/**
|
|
450
|
+
* Allowed values.
|
|
451
|
+
*/
|
|
452
|
+
values?: unknown[] | undefined;
|
|
453
|
+
/**
|
|
454
|
+
* A `CHECK` expression, like `"price >= 0"`.
|
|
455
|
+
*/
|
|
456
|
+
check?: string | undefined;
|
|
457
|
+
/**
|
|
458
|
+
* Foreign key target.
|
|
459
|
+
*/
|
|
460
|
+
references?: {
|
|
461
|
+
table: string;
|
|
462
|
+
column: string;
|
|
463
|
+
} | undefined;
|
|
464
|
+
/**
|
|
465
|
+
* When the referenced row is deleted.
|
|
466
|
+
*/
|
|
467
|
+
onDelete?: ForeignKeyAction | undefined;
|
|
468
|
+
/**
|
|
469
|
+
* When the referenced key changes.
|
|
470
|
+
*/
|
|
471
|
+
onUpdate?: ForeignKeyAction | undefined;
|
|
472
|
+
};
|
|
473
|
+
/**
|
|
474
|
+
* A constraint on several columns.
|
|
475
|
+
*/
|
|
476
|
+
type TableConstraint = {
|
|
477
|
+
type: "unique";
|
|
478
|
+
columns: string[];
|
|
479
|
+
} | {
|
|
480
|
+
type: "primaryKey";
|
|
481
|
+
columns: string[];
|
|
482
|
+
} | {
|
|
483
|
+
type: "check";
|
|
484
|
+
expression: string;
|
|
485
|
+
} | {
|
|
486
|
+
type: "foreignKey";
|
|
487
|
+
columns: string[];
|
|
488
|
+
references: {
|
|
489
|
+
table: string;
|
|
490
|
+
columns: string[];
|
|
491
|
+
};
|
|
492
|
+
onDelete?: ForeignKeyAction;
|
|
493
|
+
onUpdate?: ForeignKeyAction;
|
|
494
|
+
};
|
|
495
|
+
/**
|
|
496
|
+
* An index.
|
|
497
|
+
*/
|
|
498
|
+
type IndexDefinition = {
|
|
499
|
+
/**
|
|
500
|
+
* Several columns make a composite index.
|
|
501
|
+
*/
|
|
502
|
+
columns: string[];
|
|
503
|
+
unique?: boolean | undefined;
|
|
504
|
+
/**
|
|
505
|
+
* Defaults to `idx_<table>_<columns>`.
|
|
506
|
+
*/
|
|
507
|
+
name?: string | undefined;
|
|
508
|
+
/**
|
|
509
|
+
* Makes it a partial index, like `"deleted_at IS NULL"`.
|
|
510
|
+
*/
|
|
511
|
+
where?: string | undefined;
|
|
512
|
+
};
|
|
513
|
+
/**
|
|
514
|
+
* A table for `createTable()`.
|
|
515
|
+
*/
|
|
516
|
+
type TableDefinition = {
|
|
517
|
+
name: string;
|
|
518
|
+
columns?: Record<string, ColumnDefinition> | undefined;
|
|
519
|
+
/**
|
|
520
|
+
* Old name for `columns`, still accepted.
|
|
521
|
+
*/
|
|
522
|
+
options?: Record<string, ColumnDefinition> | undefined;
|
|
523
|
+
constraints?: TableConstraint[] | undefined;
|
|
524
|
+
indexes?: IndexDefinition[] | undefined;
|
|
525
|
+
};
|
|
526
|
+
/**
|
|
527
|
+
* Operators for a column in a filter.
|
|
528
|
+
*/
|
|
529
|
+
type WhereOperators = {
|
|
530
|
+
/**
|
|
531
|
+
* Equal. `null` means `IS NULL`.
|
|
532
|
+
*/
|
|
533
|
+
eq?: unknown;
|
|
534
|
+
/**
|
|
535
|
+
* Not equal. Rows where the column is `NULL` match too.
|
|
536
|
+
*/
|
|
537
|
+
ne?: unknown;
|
|
538
|
+
/**
|
|
539
|
+
* Greater than.
|
|
540
|
+
*/
|
|
541
|
+
gt?: string | number | bigint | Date | undefined;
|
|
542
|
+
/**
|
|
543
|
+
* Greater than or equal.
|
|
544
|
+
*/
|
|
545
|
+
gte?: string | number | bigint | Date | undefined;
|
|
546
|
+
/**
|
|
547
|
+
* Less than.
|
|
548
|
+
*/
|
|
549
|
+
lt?: string | number | bigint | Date | undefined;
|
|
550
|
+
/**
|
|
551
|
+
* Less than or equal.
|
|
552
|
+
*/
|
|
553
|
+
lte?: string | number | bigint | Date | undefined;
|
|
554
|
+
/**
|
|
555
|
+
* One of these values.
|
|
556
|
+
*/
|
|
557
|
+
in?: unknown[] | undefined;
|
|
558
|
+
/**
|
|
559
|
+
* None of these values.
|
|
560
|
+
*/
|
|
561
|
+
notIn?: unknown[] | undefined;
|
|
562
|
+
/**
|
|
563
|
+
* A `LIKE` pattern: `%` any text, `_` one character.
|
|
564
|
+
*/
|
|
565
|
+
like?: string | undefined;
|
|
566
|
+
notLike?: string | undefined;
|
|
567
|
+
/**
|
|
568
|
+
* A case-sensitive pattern: `*` any text, `?` one character.
|
|
569
|
+
*/
|
|
570
|
+
glob?: string | undefined;
|
|
571
|
+
/**
|
|
572
|
+
* From `min` to `max`, both included.
|
|
573
|
+
*/
|
|
574
|
+
between?: [unknown, unknown] | undefined;
|
|
575
|
+
/**
|
|
576
|
+
* `true` for `IS NULL`, `false` for `IS NOT NULL`.
|
|
577
|
+
*/
|
|
578
|
+
isNull?: boolean | undefined;
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* A filter. Each key is a column: a value means "equals", `null` means
|
|
582
|
+
* `IS NULL`, an array means "one of", and an object uses operators. Keys
|
|
583
|
+
* combine with AND; use `$or` and `$and` for the rest.
|
|
584
|
+
*/
|
|
585
|
+
type Where<R = Record<string, any>> = { [K in keyof R]?: WhereValue; } & {
|
|
586
|
+
$or?: Where<R>[];
|
|
587
|
+
$and?: Where<R>[];
|
|
588
|
+
};
|
|
589
|
+
/**
|
|
590
|
+
* The condition for one column: a value, `null`, an array, or operators.
|
|
591
|
+
*/
|
|
592
|
+
type WhereValue = null | string | number | bigint | boolean | Date | Uint8Array | readonly unknown[] | WhereOperators;
|
|
593
|
+
/**
|
|
594
|
+
* Sort direction.
|
|
595
|
+
*/
|
|
596
|
+
type SortDirection = "ASC" | "DESC" | "asc" | "desc";
|
|
597
|
+
/**
|
|
598
|
+
* Options for `getAll()`.
|
|
599
|
+
*/
|
|
600
|
+
type QueryOptions = {
|
|
601
|
+
/**
|
|
602
|
+
* Like `"created_at"` or `[["age", "DESC"], "name"]`.
|
|
603
|
+
*/
|
|
604
|
+
orderBy?: string | (string | [string, SortDirection])[] | undefined;
|
|
605
|
+
/**
|
|
606
|
+
* For a single `orderBy` column. Defaults to `"ASC"`.
|
|
607
|
+
*/
|
|
608
|
+
direction?: SortDirection | undefined;
|
|
609
|
+
limit?: number | undefined;
|
|
610
|
+
/**
|
|
611
|
+
* Rows to skip, for pagination.
|
|
612
|
+
*/
|
|
613
|
+
offset?: number | undefined;
|
|
614
|
+
/**
|
|
615
|
+
* Columns to return. Defaults to all.
|
|
616
|
+
*/
|
|
617
|
+
columns?: string[] | undefined;
|
|
618
|
+
};
|
|
619
|
+
/**
|
|
620
|
+
* Options for `new SQLite()`.
|
|
621
|
+
*/
|
|
622
|
+
type SQLiteOptions = {
|
|
623
|
+
/**
|
|
624
|
+
* Parse `JSON` columns. Defaults to `true`.
|
|
625
|
+
*/
|
|
626
|
+
json?: boolean | undefined;
|
|
627
|
+
/**
|
|
628
|
+
* Return `BOOLEAN` columns as `true`/`false`. Defaults to `true`.
|
|
629
|
+
*/
|
|
630
|
+
booleans?: boolean | undefined;
|
|
631
|
+
/**
|
|
632
|
+
* Return `DATE`, `DATETIME` and `TIMESTAMP` columns as `Date`s.
|
|
633
|
+
*/
|
|
634
|
+
dates?: boolean | undefined;
|
|
635
|
+
/**
|
|
636
|
+
* Write-ahead logging: faster, and reads don't wait for writes. Defaults to `true`.
|
|
637
|
+
*/
|
|
638
|
+
wal?: boolean | undefined;
|
|
639
|
+
/**
|
|
640
|
+
* Enforce foreign keys. Defaults to `true`.
|
|
641
|
+
*/
|
|
642
|
+
foreignKeys?: boolean | undefined;
|
|
643
|
+
/**
|
|
644
|
+
* How long to wait for a lock held by another connection, in ms. Defaults to `5000`.
|
|
645
|
+
*/
|
|
646
|
+
busyTimeout?: number | undefined;
|
|
647
|
+
readOnly?: boolean | undefined;
|
|
648
|
+
};
|
|
649
|
+
/**
|
|
650
|
+
* What an insert returns.
|
|
651
|
+
*/
|
|
652
|
+
type InsertResult = {
|
|
653
|
+
ok: true;
|
|
654
|
+
changes: number;
|
|
655
|
+
lastInsertRowid: number | bigint;
|
|
656
|
+
};
|
|
657
|
+
/**
|
|
658
|
+
* What an update or delete returns.
|
|
659
|
+
*/
|
|
660
|
+
type ChangeResult = {
|
|
661
|
+
ok: true;
|
|
662
|
+
changes: number;
|
|
663
|
+
};
|
|
664
|
+
/**
|
|
665
|
+
* A column, as returned by `columns()`.
|
|
666
|
+
*/
|
|
667
|
+
type ColumnInfo = {
|
|
668
|
+
name: string;
|
|
669
|
+
/**
|
|
670
|
+
* `""` when there's none.
|
|
671
|
+
*/
|
|
672
|
+
type: string;
|
|
673
|
+
notNull: boolean;
|
|
674
|
+
/**
|
|
675
|
+
* As SQL text, or `null`.
|
|
676
|
+
*/
|
|
677
|
+
defaultValue: unknown;
|
|
678
|
+
primaryKey: boolean;
|
|
679
|
+
};
|
|
680
|
+
/**
|
|
681
|
+
* A migration: SQL (several statements are fine), or a function that gets
|
|
682
|
+
* the database.
|
|
683
|
+
*/
|
|
684
|
+
type Migration = string | ((db: SQLite<any>) => void);
|
|
685
|
+
/**
|
|
686
|
+
* Shortcuts bound to one table, from `table()`.
|
|
687
|
+
*/
|
|
688
|
+
type TableHandle<R = Record<string, any>> = {
|
|
689
|
+
name: string;
|
|
690
|
+
/**
|
|
691
|
+
* The first matching row.
|
|
692
|
+
*/
|
|
693
|
+
get: (where?: Where<R>, options?: Pick<QueryOptions, "columns" | "orderBy" | "direction">) => R | undefined;
|
|
694
|
+
/**
|
|
695
|
+
* All matching rows.
|
|
696
|
+
*/
|
|
697
|
+
getAll: (where?: Where<R>, options?: QueryOptions) => R[];
|
|
698
|
+
/**
|
|
699
|
+
* Inserts a row.
|
|
700
|
+
*/
|
|
701
|
+
insert: (data: Partial<R>) => InsertResult;
|
|
702
|
+
/**
|
|
703
|
+
* Inserts rows in one transaction.
|
|
704
|
+
*/
|
|
705
|
+
insertMany: (rows: Array<Partial<R>>) => ChangeResult;
|
|
706
|
+
/**
|
|
707
|
+
* Updates matching rows.
|
|
708
|
+
*/
|
|
709
|
+
update: (data: Partial<R>, where: Where<R>) => ChangeResult;
|
|
710
|
+
/**
|
|
711
|
+
* Inserts, or updates on conflict.
|
|
712
|
+
*/
|
|
713
|
+
upsert: (data: Partial<R>, conflict: string[]) => InsertResult;
|
|
714
|
+
/**
|
|
715
|
+
* Deletes matching rows.
|
|
716
|
+
*/
|
|
717
|
+
delete: (where: Where<R>) => ChangeResult;
|
|
718
|
+
/**
|
|
719
|
+
* Counts matching rows.
|
|
720
|
+
*/
|
|
721
|
+
count: (where?: Where<R>) => number;
|
|
722
|
+
/**
|
|
723
|
+
* Does any row match?
|
|
724
|
+
*/
|
|
725
|
+
exists: (where?: Where<R>) => boolean;
|
|
726
|
+
/**
|
|
727
|
+
* Deletes every row.
|
|
728
|
+
*/
|
|
729
|
+
clear: () => {
|
|
730
|
+
ok: true;
|
|
731
|
+
};
|
|
732
|
+
};
|
|
391
733
|
//# sourceMappingURL=SQLite.d.ts.map
|