@@ -9,37 +9,58 @@ const {
9
9
} = require ( 'bindings' ) ( 'better_sqlite3.node' ) ;
10
10
11
11
function Database ( filenameGiven , options ) {
12
+ if ( new . target !== Database ) {
13
+ return new Database ( filenameGiven , options ) ;
14
+ }
15
+
16
+ // Apply defaults
12
17
if ( filenameGiven == null ) filenameGiven = '' ;
13
18
if ( options == null ) options = { } ;
19
+
20
+ // Validate arguments
14
21
if ( typeof filenameGiven !== 'string' ) throw new TypeError ( 'Expected first argument to be a string' ) ;
15
22
if ( typeof options !== 'object' ) throw new TypeError ( 'Expected second argument to be an options object' ) ;
16
23
if ( 'readOnly' in options ) throw new TypeError ( 'Misspelled option "readOnly" should be "readonly"' ) ;
17
24
if ( 'memory' in options ) throw new TypeError ( 'Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)' ) ;
18
25
26
+ // Interpret options
19
27
const filename = filenameGiven . trim ( ) ;
20
28
const anonymous = filename === '' || filename === ':memory:' ;
21
29
const readonly = util . getBooleanOption ( options , 'readonly' ) ;
22
30
const fileMustExist = util . getBooleanOption ( options , 'fileMustExist' ) ;
23
31
const timeout = 'timeout' in options ? options . timeout : 5000 ;
24
32
const verbose = 'verbose' in options ? options . verbose : null ;
25
33
34
+ // Validate interpreted options
26
35
if ( readonly && anonymous ) throw new TypeError ( 'In-memory/temporary databases cannot be readonly' ) ;
27
36
if ( ! Number . isInteger ( timeout ) || timeout < 0 ) throw new TypeError ( 'Expected the "timeout" option to be a positive integer' ) ;
28
37
if ( timeout > 0x7fffffff ) throw new RangeError ( 'Option "timeout" cannot be greater than 2147483647' ) ;
29
38
if ( verbose != null && typeof verbose !== 'function' ) throw new TypeError ( 'Expected the "verbose" option to be a function' ) ;
30
39
40
+ // Make sure the specified directory exists
31
41
if ( ! anonymous && ! fs . existsSync ( path . dirname ( filename ) ) ) {
32
42
throw new TypeError ( 'Cannot open database because the directory does not exist' ) ;
33
43
}
34
- return new CPPDatabase ( filename , filenameGiven , anonymous , readonly , fileMustExist , timeout , verbose || null ) ;
44
+
45
+ Object . defineProperties ( this , {
46
+ [ util . cppdb ] : { value : new CPPDatabase ( filename , filenameGiven , anonymous , readonly , fileMustExist , timeout , verbose || null ) } ,
47
+ ...wrappers . getters ,
48
+ } ) ;
35
49
}
36
50
37
- setErrorConstructor ( require ( './sqlite-error' ) ) ;
38
- util . wrap ( CPPDatabase , 'pragma' , require ( './pragma' ) ) ;
39
- util . wrap ( CPPDatabase , 'function' , require ( './function' ) ) ;
40
- util . wrap ( CPPDatabase , 'aggregate' , require ( './aggregate' ) ) ;
41
- util . wrap ( CPPDatabase , 'backup' , require ( './backup' ) ) ;
42
- CPPDatabase . prototype . transaction = require ( './transaction' ) ;
43
- CPPDatabase . prototype . constructor = Database ;
44
- Database . prototype = CPPDatabase . prototype ;
51
+ const wrappers = require ( './methods/wrappers' ) ;
52
+ Database . prototype . prepare = wrappers . prepare ;
53
+ Database . prototype . transaction = require ( './methods/transaction' ) ;
54
+ Database . prototype . pragma = require ( './methods/pragma' ) ;
55
+ Database . prototype . backup = require ( './methods/backup' ) ;
56
+ Database . prototype . function = require ( './methods/function' ) ;
57
+ Database . prototype . aggregate = require ( './methods/aggregate' ) ;
58
+ Database . prototype . loadExtension = wrappers . loadExtension ;
59
+ Database . prototype . exec = wrappers . exec ;
60
+ Database . prototype . close = wrappers . close ;
61
+ Database . prototype . defaultSafeIntegers = wrappers . defaultSafeIntegers ;
62
+ Database . prototype . unsafeMode = wrappers . unsafeMode ;
63
+ Database . prototype [ util . inspect ] = require ( './methods/inspect' ) ;
64
+
45
65
module . exports = Database ;
66
+ setErrorConstructor ( require ( './sqlite-error' ) ) ;
0 commit comments