Skip to content

Commit b5bd593

Browse files
authored
TryGhost#1655 Database.backup() typings
1 parent 1980f10 commit b5bd593

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

lib/sqlite3.d.ts

+135-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,140 @@ export class Database extends events.EventEmitter {
139139
wait(callback?: (param: null) => void): this;
140140

141141
interrupt(): void;
142+
143+
backup(path:string, callback?: ()=>void): Backup
144+
backup(filename:string, destDbName:string, sourceDbName:string, filenameIsDest:boolean, callback?: ()=>void): Backup
145+
}
146+
147+
/**
148+
*
149+
* A class for managing an sqlite3_backup object. For consistency
150+
* with other node-sqlite3 classes, it maintains an internal queue
151+
* of calls.
152+
*
153+
* Intended usage from node:
154+
*
155+
* var db = new sqlite3.Database('live.db');
156+
* var backup = db.backup('backup.db');
157+
* ...
158+
* // in event loop, move backup forward when we have time.
159+
* if (backup.idle) { backup.step(NPAGES); }
160+
* if (backup.completed) { ... success ... }
161+
* if (backup.failed) { ... sadness ... }
162+
* // do other work in event loop - fine to modify live.db
163+
* ...
164+
*
165+
* Here is how sqlite's backup api is exposed:
166+
*
167+
* - `sqlite3_backup_init`: This is implemented as
168+
* `db.backup(filename, [callback])` or
169+
* `db.backup(filename, destDbName, sourceDbName, filenameIsDest, [callback])`.
170+
* - `sqlite3_backup_step`: `backup.step(pages, [callback])`.
171+
* - `sqlite3_backup_finish`: `backup.finish([callback])`.
172+
* - `sqlite3_backup_remaining`: `backup.remaining`.
173+
* - `sqlite3_backup_pagecount`: `backup.pageCount`.
174+
*
175+
* There are the following read-only properties:
176+
*
177+
* - `backup.completed` is set to `true` when the backup
178+
* succeeeds.
179+
* - `backup.failed` is set to `true` when the backup
180+
* has a fatal error.
181+
* - `backup.idle` is set to `true` when no operation
182+
* is currently in progress or queued for the backup.
183+
* - `backup.remaining` is an integer with the remaining
184+
* number of pages after the last call to `backup.step`
185+
* (-1 if `step` not yet called).
186+
* - `backup.pageCount` is an integer with the total number
187+
* of pages measured during the last call to `backup.step`
188+
* (-1 if `step` not yet called).
189+
*
190+
* There is the following writable property:
191+
*
192+
* - `backup.retryErrors`: an array of sqlite3 error codes
193+
* that are treated as non-fatal - meaning, if they occur,
194+
* backup.failed is not set, and the backup may continue.
195+
* By default, this is `[sqlite3.BUSY, sqlite3.LOCKED]`.
196+
*
197+
* The `db.backup(filename, [callback])` shorthand is sufficient
198+
* for making a backup of a database opened by node-sqlite3. If
199+
* using attached or temporary databases, or moving data in the
200+
* opposite direction, the more complete (but daunting)
201+
* `db.backup(filename, destDbName, sourceDbName, filenameIsDest, [callback])`
202+
* signature is provided.
203+
*
204+
* A backup will finish automatically when it succeeds or a fatal
205+
* error occurs, meaning it is not necessary to call `db.finish()`.
206+
* By default, SQLITE_LOCKED and SQLITE_BUSY errors are not
207+
* treated as failures, and the backup will continue if they
208+
* occur. The set of errors that are tolerated can be controlled
209+
* by setting `backup.retryErrors`. To disable automatic
210+
* finishing and stick strictly to sqlite's raw api, set
211+
* `backup.retryErrors` to `[]`. In that case, it is necessary
212+
* to call `backup.finish()`.
213+
*
214+
* In the same way as node-sqlite3 databases and statements,
215+
* backup methods can be called safely without callbacks, due
216+
* to an internal call queue. So for example this naive code
217+
* will correctly back up a db, if there are no errors:
218+
*
219+
* var backup = db.backup('backup.db');
220+
* backup.step(-1);
221+
* backup.finish();
222+
*
223+
*/
224+
export class Backup extends events.EventEmitter {
225+
/**
226+
* `true` when the backup is idle and ready for `step()` to be called, `false` when busy.
227+
*/
228+
idle: boolean
229+
230+
/**
231+
* `true` when the backup has completed, `false` otherwise.
232+
*/
233+
completed: boolean
234+
235+
/**
236+
* `true` when the backup has failed, `false` otherwise.
237+
*/
238+
failed: boolean
239+
240+
/**
241+
* The number of remaining pages after the last call to `step()`, or `-1` if `step()` has never been called.
242+
*/
243+
remaining: number
244+
245+
/**
246+
* The total number of pages measured during the last call to `step()`, or `-1` if `step()` has never been called.
247+
*/
248+
pageCount: number
249+
250+
251+
/**
252+
* An array of sqlite3 error codes that are treated as non-fatal - meaning, if they occur, `Backup.failed` is not set, and the backup may continue. By default, this is `[sqlite3.BUSY, sqlite3.LOCKED]`.
253+
*/
254+
retryErrors: number[]
255+
256+
/**
257+
* Asynchronously finalize the backup (required).
258+
*
259+
* @param callback Called when the backup is finalized.
260+
*/
261+
finish(callback?: ()=>void): void
262+
263+
/**
264+
* Asynchronously perform an incremental segment of the backup.
265+
*
266+
* Example:
267+
*
268+
* ```
269+
* backup.step(5)
270+
* ```
271+
*
272+
* @param nPages Number of pages to process (5 recommended).
273+
* @param callback Called when the step is completed.
274+
*/
275+
step(nPages: number,callback?: ()=>void): void
142276
}
143277

144278
export function verbose(): sqlite3;
@@ -202,4 +336,4 @@ export interface sqlite3 {
202336
Statement: typeof Statement;
203337
Database: typeof Database;
204338
verbose(): this;
205-
}
339+
}

0 commit comments

Comments
 (0)