Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ targetDb.applyChangeset(changeset);
// Now that the changeset has been applied, targetDb contains the same data as sourceDb.
```

### `database[Symbol.dispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

Closes the database connection. If the database connection is already closed
then this is a no-op.

## Class: `Session`

<!-- YAML
Expand Down
16 changes: 15 additions & 1 deletion lib/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
'use strict';
const {
SymbolDispose,
} = primordials;
const { emitExperimentalWarning } = require('internal/util');
const binding = internalBinding('sqlite');

emitExperimentalWarning('SQLite');
module.exports = internalBinding('sqlite');

// TODO(cjihrig): Move this to C++ once Symbol.dispose reaches Stage 4.
binding.DatabaseSync.prototype[SymbolDispose] = function() {
try {
this.close();
} catch {
// Ignore errors.
}
};

module.exports = binding;
32 changes: 32 additions & 0 deletions test/parallel/test-sqlite-database-sync-dispose.js
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a test that calling db[Symbol.dispose](); twice doesn't throw

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('node:assert');
const { join } = require('node:path');
const { DatabaseSync } = require('node:sqlite');
const { suite, test } = require('node:test');
let cnt = 0;

tmpdir.refresh();

function nextDb() {
return join(tmpdir.path, `database-${cnt++}.db`);
}

suite('DatabaseSync.prototype[Symbol.dispose]()', () => {
test('closes an open database', () => {
const db = new DatabaseSync(nextDb());
db[Symbol.dispose]();
assert.throws(() => {
db.close();
}, /database is not open/);
});

test('supports databases that are not open', () => {
const db = new DatabaseSync(nextDb(), { open: false });
db[Symbol.dispose]();
assert.throws(() => {
db.close();
}, /database is not open/);
});
});
Loading