@@ -89,19 +89,18 @@ suite('DatabaseSync() constructor', () => {
8989
9090 test ( 'is not read-only by default' , ( t ) => {
9191 const dbPath = nextDb ( ) ;
92- const db = new DatabaseSync ( dbPath ) ;
92+ using db = new DatabaseSync ( dbPath ) ;
9393 db . exec ( 'CREATE TABLE foo (id INTEGER PRIMARY KEY)' ) ;
9494 } ) ;
9595
9696 test ( 'is read-only if readOnly is set' , ( t ) => {
9797 const dbPath = nextDb ( ) ;
9898 {
99- const db = new DatabaseSync ( dbPath ) ;
99+ using db = new DatabaseSync ( dbPath ) ;
100100 db . exec ( 'CREATE TABLE foo (id INTEGER PRIMARY KEY)' ) ;
101- db . close ( ) ;
102101 }
103102 {
104- const db = new DatabaseSync ( dbPath , { readOnly : true } ) ;
103+ using db = new DatabaseSync ( dbPath , { readOnly : true } ) ;
105104 t . assert . throws ( ( ) => {
106105 db . exec ( 'CREATE TABLE bar (id INTEGER PRIMARY KEY)' ) ;
107106 } , {
@@ -122,12 +121,11 @@ suite('DatabaseSync() constructor', () => {
122121
123122 test ( 'enables foreign key constraints by default' , ( t ) => {
124123 const dbPath = nextDb ( ) ;
125- const db = new DatabaseSync ( dbPath ) ;
124+ using db = new DatabaseSync ( dbPath ) ;
126125 db . exec ( `
127126 CREATE TABLE foo (id INTEGER PRIMARY KEY);
128127 CREATE TABLE bar (foo_id INTEGER REFERENCES foo(id));
129128 ` ) ;
130- t . after ( ( ) => { db . close ( ) ; } ) ;
131129 t . assert . throws ( ( ) => {
132130 db . exec ( 'INSERT INTO bar (foo_id) VALUES (1)' ) ;
133131 } , {
@@ -138,12 +136,11 @@ suite('DatabaseSync() constructor', () => {
138136
139137 test ( 'allows disabling foreign key constraints' , ( t ) => {
140138 const dbPath = nextDb ( ) ;
141- const db = new DatabaseSync ( dbPath , { enableForeignKeyConstraints : false } ) ;
139+ using db = new DatabaseSync ( dbPath , { enableForeignKeyConstraints : false } ) ;
142140 db . exec ( `
143141 CREATE TABLE foo (id INTEGER PRIMARY KEY);
144142 CREATE TABLE bar (foo_id INTEGER REFERENCES foo(id));
145143 ` ) ;
146- t . after ( ( ) => { db . close ( ) ; } ) ;
147144 db . exec ( 'INSERT INTO bar (foo_id) VALUES (1)' ) ;
148145 } ) ;
149146
@@ -158,8 +155,7 @@ suite('DatabaseSync() constructor', () => {
158155
159156 test ( 'disables double-quoted string literals by default' , ( t ) => {
160157 const dbPath = nextDb ( ) ;
161- const db = new DatabaseSync ( dbPath ) ;
162- t . after ( ( ) => { db . close ( ) ; } ) ;
158+ using db = new DatabaseSync ( dbPath ) ;
163159 t . assert . throws ( ( ) => {
164160 db . exec ( 'SELECT "foo";' ) ;
165161 } , {
@@ -170,8 +166,7 @@ suite('DatabaseSync() constructor', () => {
170166
171167 test ( 'allows enabling double-quoted string literals' , ( t ) => {
172168 const dbPath = nextDb ( ) ;
173- const db = new DatabaseSync ( dbPath , { enableDoubleQuotedStringLiterals : true } ) ;
174- t . after ( ( ) => { db . close ( ) ; } ) ;
169+ using db = new DatabaseSync ( dbPath , { enableDoubleQuotedStringLiterals : true } ) ;
175170 db . exec ( 'SELECT "foo";' ) ;
176171 } ) ;
177172
@@ -186,8 +181,7 @@ suite('DatabaseSync() constructor', () => {
186181
187182 test ( 'allows reading big integers' , ( t ) => {
188183 const dbPath = nextDb ( ) ;
189- const db = new DatabaseSync ( dbPath , { readBigInts : true } ) ;
190- t . after ( ( ) => { db . close ( ) ; } ) ;
184+ using db = new DatabaseSync ( dbPath , { readBigInts : true } ) ;
191185
192186 const setup = db . exec ( `
193187 CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;
@@ -216,8 +210,7 @@ suite('DatabaseSync() constructor', () => {
216210
217211 test ( 'allows returning arrays' , ( t ) => {
218212 const dbPath = nextDb ( ) ;
219- const db = new DatabaseSync ( dbPath , { returnArrays : true } ) ;
220- t . after ( ( ) => { db . close ( ) ; } ) ;
213+ using db = new DatabaseSync ( dbPath , { returnArrays : true } ) ;
221214 const setup = db . exec ( `
222215 CREATE TABLE data(key INTEGER PRIMARY KEY, val TEXT) STRICT;
223216 INSERT INTO data (key, val) VALUES (1, 'one');
@@ -240,8 +233,7 @@ suite('DatabaseSync() constructor', () => {
240233
241234 test ( 'throws if bare named parameters are used when option is false' , ( t ) => {
242235 const dbPath = nextDb ( ) ;
243- const db = new DatabaseSync ( dbPath , { allowBareNamedParameters : false } ) ;
244- t . after ( ( ) => { db . close ( ) ; } ) ;
236+ using db = new DatabaseSync ( dbPath , { allowBareNamedParameters : false } ) ;
245237 const setup = db . exec (
246238 'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
247239 ) ;
@@ -267,8 +259,7 @@ suite('DatabaseSync() constructor', () => {
267259
268260 test ( 'allows unknown named parameters' , ( t ) => {
269261 const dbPath = nextDb ( ) ;
270- const db = new DatabaseSync ( dbPath , { allowUnknownNamedParameters : true } ) ;
271- t . after ( ( ) => { db . close ( ) ; } ) ;
262+ using db = new DatabaseSync ( dbPath , { allowUnknownNamedParameters : true } ) ;
272263 const setup = db . exec (
273264 'CREATE TABLE data(key INTEGER, val INTEGER) STRICT;'
274265 ) ;
@@ -284,8 +275,7 @@ suite('DatabaseSync() constructor', () => {
284275
285276 test ( 'has sqlite-type symbol property' , ( t ) => {
286277 const dbPath = nextDb ( ) ;
287- const db = new DatabaseSync ( dbPath ) ;
288- t . after ( ( ) => { db . close ( ) ; } ) ;
278+ using db = new DatabaseSync ( dbPath ) ;
289279
290280 const sqliteTypeSymbol = Symbol . for ( 'sqlite-type' ) ;
291281 t . assert . strictEqual ( db [ sqliteTypeSymbol ] , 'node:sqlite' ) ;
@@ -295,8 +285,7 @@ suite('DatabaseSync() constructor', () => {
295285suite ( 'DatabaseSync.prototype.open()' , ( ) => {
296286 test ( 'opens a database connection' , ( t ) => {
297287 const dbPath = nextDb ( ) ;
298- const db = new DatabaseSync ( dbPath , { open : false } ) ;
299- t . after ( ( ) => { db . close ( ) ; } ) ;
288+ using db = new DatabaseSync ( dbPath , { open : false } ) ;
300289
301290 t . assert . strictEqual ( db . isOpen , false ) ;
302291 t . assert . strictEqual ( existsSync ( dbPath ) , false ) ;
@@ -306,8 +295,7 @@ suite('DatabaseSync.prototype.open()', () => {
306295 } ) ;
307296
308297 test ( 'throws if database is already open' , ( t ) => {
309- const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
310- t . after ( ( ) => { db . close ( ) ; } ) ;
298+ using db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
311299
312300 t . assert . strictEqual ( db . isOpen , false ) ;
313301 db . open ( ) ;
@@ -324,15 +312,15 @@ suite('DatabaseSync.prototype.open()', () => {
324312
325313suite ( 'DatabaseSync.prototype.close()' , ( ) => {
326314 test ( 'closes an open database connection' , ( t ) => {
327- const db = new DatabaseSync ( nextDb ( ) ) ;
315+ using db = new DatabaseSync ( nextDb ( ) ) ;
328316
329317 t . assert . strictEqual ( db . isOpen , true ) ;
330318 t . assert . strictEqual ( db . close ( ) , undefined ) ;
331319 t . assert . strictEqual ( db . isOpen , false ) ;
332320 } ) ;
333321
334322 test ( 'throws if database is not open' , ( t ) => {
335- const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
323+ using db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
336324
337325 t . assert . strictEqual ( db . isOpen , false ) ;
338326 t . assert . throws ( ( ) => {
@@ -347,14 +335,13 @@ suite('DatabaseSync.prototype.close()', () => {
347335
348336suite ( 'DatabaseSync.prototype.prepare()' , ( ) => {
349337 test ( 'returns a prepared statement' , ( t ) => {
350- const db = new DatabaseSync ( nextDb ( ) ) ;
351- t . after ( ( ) => { db . close ( ) ; } ) ;
338+ using db = new DatabaseSync ( nextDb ( ) ) ;
352339 const stmt = db . prepare ( 'CREATE TABLE webstorage(key TEXT)' ) ;
353340 t . assert . ok ( stmt instanceof StatementSync ) ;
354341 } ) ;
355342
356343 test ( 'throws if database is not open' , ( t ) => {
357- const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
344+ using db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
358345
359346 t . assert . throws ( ( ) => {
360347 db . prepare ( ) ;
@@ -365,8 +352,7 @@ suite('DatabaseSync.prototype.prepare()', () => {
365352 } ) ;
366353
367354 test ( 'throws if sql is not a string' , ( t ) => {
368- const db = new DatabaseSync ( nextDb ( ) ) ;
369- t . after ( ( ) => { db . close ( ) ; } ) ;
355+ using db = new DatabaseSync ( nextDb ( ) ) ;
370356
371357 t . assert . throws ( ( ) => {
372358 db . prepare ( ) ;
@@ -379,8 +365,7 @@ suite('DatabaseSync.prototype.prepare()', () => {
379365
380366suite ( 'DatabaseSync.prototype.exec()' , ( ) => {
381367 test ( 'executes SQL' , ( t ) => {
382- const db = new DatabaseSync ( nextDb ( ) ) ;
383- t . after ( ( ) => { db . close ( ) ; } ) ;
368+ using db = new DatabaseSync ( nextDb ( ) ) ;
384369 const result = db . exec ( `
385370 CREATE TABLE data(
386371 key INTEGER PRIMARY KEY,
@@ -398,8 +383,7 @@ suite('DatabaseSync.prototype.exec()', () => {
398383 } ) ;
399384
400385 test ( 'reports errors from SQLite' , ( t ) => {
401- const db = new DatabaseSync ( nextDb ( ) ) ;
402- t . after ( ( ) => { db . close ( ) ; } ) ;
386+ using db = new DatabaseSync ( nextDb ( ) ) ;
403387
404388 t . assert . throws ( ( ) => {
405389 db . exec ( 'CREATE TABLEEEE' ) ;
@@ -419,7 +403,7 @@ suite('DatabaseSync.prototype.exec()', () => {
419403 } ) ;
420404
421405 test ( 'throws if database is not open' , ( t ) => {
422- const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
406+ using db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
423407
424408 t . assert . throws ( ( ) => {
425409 db . exec ( ) ;
@@ -430,8 +414,7 @@ suite('DatabaseSync.prototype.exec()', () => {
430414 } ) ;
431415
432416 test ( 'throws if sql is not a string' , ( t ) => {
433- const db = new DatabaseSync ( nextDb ( ) ) ;
434- t . after ( ( ) => { db . close ( ) ; } ) ;
417+ using db = new DatabaseSync ( nextDb ( ) ) ;
435418
436419 t . assert . throws ( ( ) => {
437420 db . exec ( ) ;
@@ -444,7 +427,7 @@ suite('DatabaseSync.prototype.exec()', () => {
444427
445428suite ( 'DatabaseSync.prototype.isTransaction' , ( ) => {
446429 test ( 'correctly detects a committed transaction' , ( t ) => {
447- const db = new DatabaseSync ( ':memory:' ) ;
430+ using db = new DatabaseSync ( ':memory:' ) ;
448431
449432 t . assert . strictEqual ( db . isTransaction , false ) ;
450433 db . exec ( 'BEGIN' ) ;
@@ -456,7 +439,7 @@ suite('DatabaseSync.prototype.isTransaction', () => {
456439 } ) ;
457440
458441 test ( 'correctly detects a rolled back transaction' , ( t ) => {
459- const db = new DatabaseSync ( ':memory:' ) ;
442+ using db = new DatabaseSync ( ':memory:' ) ;
460443
461444 t . assert . strictEqual ( db . isTransaction , false ) ;
462445 db . exec ( 'BEGIN' ) ;
@@ -468,7 +451,7 @@ suite('DatabaseSync.prototype.isTransaction', () => {
468451 } ) ;
469452
470453 test ( 'throws if database is not open' , ( t ) => {
471- const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
454+ using db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
472455
473456 t . assert . throws ( ( ) => {
474457 return db . isTransaction ;
@@ -481,7 +464,7 @@ suite('DatabaseSync.prototype.isTransaction', () => {
481464
482465suite ( 'DatabaseSync.prototype.location()' , ( ) => {
483466 test ( 'throws if database is not open' , ( t ) => {
484- const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
467+ using db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
485468
486469 t . assert . throws ( ( ) => {
487470 db . location ( ) ;
@@ -492,8 +475,7 @@ suite('DatabaseSync.prototype.location()', () => {
492475 } ) ;
493476
494477 test ( 'throws if provided dbName is not string' , ( t ) => {
495- const db = new DatabaseSync ( nextDb ( ) ) ;
496- t . after ( ( ) => { db . close ( ) ; } ) ;
478+ using db = new DatabaseSync ( nextDb ( ) ) ;
497479
498480 t . assert . throws ( ( ) => {
499481 db . location ( null ) ;
@@ -504,24 +486,20 @@ suite('DatabaseSync.prototype.location()', () => {
504486 } ) ;
505487
506488 test ( 'returns null when connected to in-memory database' , ( t ) => {
507- const db = new DatabaseSync ( ':memory:' ) ;
489+ using db = new DatabaseSync ( ':memory:' ) ;
508490 t . assert . strictEqual ( db . location ( ) , null ) ;
509491 } ) ;
510492
511493 test ( 'returns db path when connected to a persistent database' , ( t ) => {
512494 const dbPath = nextDb ( ) ;
513- const db = new DatabaseSync ( dbPath ) ;
514- t . after ( ( ) => { db . close ( ) ; } ) ;
495+ using db = new DatabaseSync ( dbPath ) ;
515496 t . assert . strictEqual ( db . location ( ) , dbPath ) ;
516497 } ) ;
517498
518499 test ( 'returns that specific db path when attached' , ( t ) => {
519500 const dbPath = nextDb ( ) ;
520501 const otherPath = nextDb ( ) ;
521- const db = new DatabaseSync ( dbPath ) ;
522- t . after ( ( ) => { db . close ( ) ; } ) ;
523- const other = new DatabaseSync ( dbPath ) ;
524- t . after ( ( ) => { other . close ( ) ; } ) ;
502+ using db = new DatabaseSync ( dbPath ) ;
525503
526504 // Adding this escape because the test with unusual chars have a single quote which breaks the query
527505 const escapedPath = otherPath . replace ( "'" , "''" ) ;
@@ -530,3 +508,18 @@ suite('DatabaseSync.prototype.location()', () => {
530508 t . assert . strictEqual ( db . location ( 'other' ) , otherPath ) ;
531509 } ) ;
532510} ) ;
511+
512+ suite ( 'DatabaseSync.prototype[Symbol.dispose]' , ( ) => {
513+ test ( 'closes an open database' , ( t ) => {
514+ const db = new DatabaseSync ( nextDb ( ) ) ;
515+ t . assert . strictEqual ( db . isOpen , true ) ;
516+ db [ Symbol . dispose ] ( ) ;
517+ t . assert . strictEqual ( db . isOpen , false ) ;
518+ } ) ;
519+
520+ test ( 'does not throw on databases that are not open' , ( t ) => {
521+ const db = new DatabaseSync ( nextDb ( ) , { open : false } ) ;
522+ t . assert . strictEqual ( db . isOpen , false ) ;
523+ db [ Symbol . dispose ] ( ) ;
524+ } ) ;
525+ } ) ;
0 commit comments