GeezerVison is showing github.com — sqlite: add DatabaseSync.prototype[Symbol.dispose]() · nodejs/node@5c976f1
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/sqlite.md +11Lines changed: 11 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -281,6 +281,17 @@ targetDb.applyChangeset(changeset); |
281 | 281 | // Now that the changeset has been applied, targetDb contains the same data as sourceDb. |
282 | 282 | ``` |
283 | 283 |
|
| 284 | +### `database[Symbol.dispose]()` |
| 285 | + |
| 286 | +<!-- YAML |
| 287 | +added: REPLACEME |
| 288 | +--> |
| 289 | + |
| 290 | +> Stability: 1 - Experimental |
| 291 | +
|
| 292 | +Closes the database connection. If the database connection is already closed |
| 293 | +then this is a no-op. |
| 294 | + |
284 | 295 | ## Class: `Session` |
285 | 296 |
|
286 | 297 | <!-- YAML |
|
Collapse file
Copy file name to clipboard +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | 'use strict'; |
| 2 | +const { |
| 3 | + SymbolDispose, |
| 4 | +} = primordials; |
2 | 5 | const { emitExperimentalWarning } = require('internal/util'); |
| 6 | +const binding = internalBinding('sqlite'); |
3 | 7 |
|
4 | 8 | emitExperimentalWarning('SQLite'); |
5 | | -module.exports = internalBinding('sqlite'); |
| 9 | + |
| 10 | +// TODO(cjihrig): Move this to C++ once Symbol.dispose reaches Stage 4. |
| 11 | +binding.DatabaseSync.prototype[SymbolDispose] = function() { |
| 12 | + try { |
| 13 | + this.close(); |
| 14 | + } catch { |
| 15 | + // Ignore errors. |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +module.exports = binding; |
Collapse file
Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const tmpdir = require('../common/tmpdir'); |
| 4 | +const assert = require('node:assert'); |
| 5 | +const { join } = require('node:path'); |
| 6 | +const { DatabaseSync } = require('node:sqlite'); |
| 7 | +const { suite, test } = require('node:test'); |
| 8 | +let cnt = 0; |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +function nextDb() { |
| 13 | + return join(tmpdir.path, `database-${cnt++}.db`); |
| 14 | +} |
| 15 | + |
| 16 | +suite('DatabaseSync.prototype[Symbol.dispose]()', () => { |
| 17 | + test('closes an open database', () => { |
| 18 | + const db = new DatabaseSync(nextDb()); |
| 19 | + db[Symbol.dispose](); |
| 20 | + assert.throws(() => { |
| 21 | + db.close(); |
| 22 | + }, /database is not open/); |
| 23 | + }); |
| 24 | + |
| 25 | + test('supports databases that are not open', () => { |
| 26 | + const db = new DatabaseSync(nextDb(), { open: false }); |
| 27 | + db[Symbol.dispose](); |
| 28 | + assert.throws(() => { |
| 29 | + db.close(); |
| 30 | + }, /database is not open/); |
| 31 | + }); |
| 32 | +}); |