Skip to content

Commit e8b60a2

Browse files
committed
promise api
1 parent 0cf28e4 commit e8b60a2

12 files changed

Lines changed: 2437 additions & 30 deletions

File tree

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ trim_trailing_whitespace = true
1313
insert_final_newline = true
1414

1515
# Matches multiple files with the same settings
16-
[*.js]
17-
indent_size = 2
16+
[*.js;*.ts]
17+
indent_size = 4
1818

1919
[*.json]
2020
indent_size = 2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ local.env
2626
setup.sh
2727
/build-tmp-napi-v3
2828
/build-tmp-napi-v6
29+
/.nyc_output
2930
*.tgz
3031
package-lock.json
3132
prebuilds

docs/DEVELOP.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ This project uses [npm version](https://docs.npmjs.com/cli/v10/commands/npm-vers
2727
- Publish the package to npm
2828

2929
3. ** Edit the generated Pre-release
30-
- select "Generate release notes"
30+
- select "Generate release notes" and adjust them to your needs
3131
- uncheck "Set as pre-release"
3232
- check "Set as latest release" if it is
33-
- click "Update Releasse
33+
- click "Update Release
3434

3535
### Version format
3636

lib/promise/backup.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)