|
| 1 | +/** |
| 2 | + * Promise-based wrapper for the Backup class from node-sqlite3 |
| 3 | + * @module promise/backup |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A thin wrapper for the 'Backup' class from 'node-sqlite3' using Promises |
| 10 | + * instead of callbacks |
| 11 | + * |
| 12 | + * @see https://github.com/mapbox/node-sqlite3/wiki/API |
| 13 | + */ |
| 14 | +class SqliteBackup { |
| 15 | + /** |
| 16 | + * Creates an instance of SqliteBackup. |
| 17 | + * |
| 18 | + * @param {import('../sqlite3.js').Backup} backup - The underlying Backup instance |
| 19 | + */ |
| 20 | + constructor(backup) { |
| 21 | + /** |
| 22 | + * @type {import('../sqlite3.js').Backup} |
| 23 | + * @private |
| 24 | + */ |
| 25 | + this._backup = backup; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns true if the backup is idle (not actively copying) |
| 30 | + * |
| 31 | + * @returns {boolean} |
| 32 | + */ |
| 33 | + get idle() { |
| 34 | + return this._backup.idle; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns true if the backup is completed |
| 39 | + * |
| 40 | + * @returns {boolean} |
| 41 | + */ |
| 42 | + get completed() { |
| 43 | + return this._backup.completed; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns true if the backup has failed |
| 48 | + * |
| 49 | + * @returns {boolean} |
| 50 | + */ |
| 51 | + get failed() { |
| 52 | + return this._backup.failed; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the remaining number of pages left to copy |
| 57 | + * Returns -1 if `step` not yet called |
| 58 | + * |
| 59 | + * @returns {number} |
| 60 | + */ |
| 61 | + get remaining() { |
| 62 | + return this._backup.remaining; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the total number of pages |
| 67 | + * Returns -1 if `step` not yet called |
| 68 | + * |
| 69 | + * @returns {number} |
| 70 | + */ |
| 71 | + get pageCount() { |
| 72 | + return this._backup.pageCount; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the progress (percentage completion) |
| 77 | + * |
| 78 | + * @returns {number} Progress as percentage (0-100) |
| 79 | + */ |
| 80 | + get progress() { |
| 81 | + const pageCount = this.pageCount; |
| 82 | + const remaining = this.remaining; |
| 83 | + if (pageCount === -1 || remaining === -1) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + return pageCount === 0 ? 100 : ((pageCount - remaining) / pageCount) * 100; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Copy the next page or all remaining pages of the backup |
| 91 | + * |
| 92 | + * @param {number} [pages=-1] - Number of pages to copy (-1 for all remaining) |
| 93 | + * @returns {Promise<void>} |
| 94 | + */ |
| 95 | + step(pages) { |
| 96 | + return new Promise((resolve, reject) => { |
| 97 | + if (!this._backup) { |
| 98 | + reject(new Error('backup handle not open')); |
| 99 | + return; |
| 100 | + } |
| 101 | + this._backup.step(pages === undefined ? -1 : pages, (err) => { |
| 102 | + if (err) { |
| 103 | + reject(err); |
| 104 | + } else { |
| 105 | + resolve(); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Finish the backup (synchronous) |
| 113 | + */ |
| 114 | + finish() { |
| 115 | + if (this._backup) { |
| 116 | + this._backup.finish(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +module.exports = { SqliteBackup }; |
0 commit comments