Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite: expose backup api #56253

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
23 changes: 23 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ added: v22.5.0

Constructs a new `DatabaseSync` instance.

### `database.backup(destination[, options])`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something that needs to happen in this PR necessarily, but we will need to rename the DatabaseSync class now that an async operation is being added. My plan was always to keep the sync and async APIs separated, similar to the fs module. Since we are breaking from that, I'll leave it up to others to decide on naming, keeping in mind that there is already a feature request for an async API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it could be a start for that async API.

#54307

I asked about this in the issue but I didn't get an answer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, @cjihrig . In your plans, would DatabaseSync have a backup feature?

Personally, I have no problem moving this to a Database class (without the Sync) as a start for that async API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be a method on DatabaseSync? Could it be a separate API exposed on the node:sqlite module? e.g.

const { backup, DatabaseSync } = require('node:sqlite');

const db = new DatabaseSync(...);
// ...
await backup(db);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea!


<!-- YAML
added: REPLACEME
-->

* `destination` {string} The path where the backup will be created. If the file already exists, the contents will be
overwritten.
* `options` {Object} Optional configuration for the backup. The
following properties are supported:
* `source` {string} Name of the source database. **Default:** `'main'`.
* `target` {string} Name of the target database. **Default:** `'main'`.
* `rate` {number} Number of pages to be transmitted in each batch of the backup. **Default:** `100`.
* `progress` {Function} Callback function that will be called with the number of pages copied and the total number of
pages.
* Returns: {Promise} A promise that resolves when the backup is completed and rejects if an error occurs.

This method makes a database backup. This method abstracts the [`sqlite3_backup_init()`][], [`sqlite3_backup_step()`][]
and [`sqlite3_backup_finish()`][] functions.

### `database.close()`

<!-- YAML
Expand Down Expand Up @@ -604,6 +624,9 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
[`SQLITE_DETERMINISTIC`]: https://www.sqlite.org/c3ref/c_deterministic.html
[`SQLITE_DIRECTONLY`]: https://www.sqlite.org/c3ref/c_deterministic.html
[`database.applyChangeset()`]: #databaseapplychangesetchangeset-options
[`sqlite3_backup_finish()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
[`sqlite3_backup_init()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
[`sqlite3_backup_step()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
[`sqlite3_changes64()`]: https://www.sqlite.org/c3ref/changes.html
[`sqlite3_close_v2()`]: https://www.sqlite.org/c3ref/close.html
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html
Expand Down
3 changes: 3 additions & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
V(primordials_string, "primordials") \
V(priority_string, "priority") \
V(process_string, "process") \
V(progress_string, "progress") \
V(promise_string, "promise") \
V(prototype_string, "prototype") \
V(psk_string, "psk") \
Expand All @@ -306,6 +307,7 @@
V(reason_string, "reason") \
V(refresh_string, "refresh") \
V(regexp_string, "regexp") \
V(remaining_pages_string, "remainingPages") \
V(rename_string, "rename") \
V(replacement_string, "replacement") \
V(required_module_facade_url_string, \
Expand Down Expand Up @@ -357,6 +359,7 @@
V(time_to_first_byte_sent_string, "timeToFirstByteSent") \
V(time_to_first_header_string, "timeToFirstHeader") \
V(tls_ticket_string, "tlsTicket") \
V(total_pages_string, "totalPages") \
V(transfer_string, "transfer") \
V(transfer_unsupported_type_str, \
"Cannot transfer object of unsupported type.") \
Expand Down
Loading
Loading