1
- 'use strict' ;
2
-
3
- const JSONStream = require ( 'JSONStream' ) ;
4
- const Promise = require ( 'bluebird' ) ;
5
- const fs = require ( 'graceful-fs ' ) ;
6
- const Model = require ( './model ' ) ;
7
- const Schema = require ( './schema ' ) ;
8
- const SchemaType = require ( './schematype ' ) ;
9
- const WarehouseError = require ( './error' ) ;
1
+ import JSONStream = require ( 'JSONStream' ) ;
2
+ import Bluebird = require ( 'bluebird' ) ;
3
+ import { writev , promises as fsPromises , createReadStream } from 'graceful-fs' ;
4
+ import { pipeline , Stream } from 'stream' ;
5
+ import Model = require( './model ' ) ;
6
+ import Schema = require( './schema ' ) ;
7
+ import SchemaType = require( './schematype ' ) ;
8
+ import WarehouseError = require( './error ' ) ;
9
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
10
10
const pkg = require ( '../package.json' ) ;
11
- const { open } = fs . promises ;
12
- const pipeline = Promise . promisify ( require ( 'stream' ) . pipeline ) ;
11
+ const { open } = fsPromises ;
12
+ const pipelineAsync = Bluebird . promisify ( pipeline ) as ( ... args : Stream [ ] ) => Bluebird < unknown > ;
13
13
14
- let _writev ;
14
+ let _writev : ( handle : fsPromises . FileHandle , buffers : Buffer [ ] ) => Promise < unknown > ;
15
15
16
- if ( typeof fs . writev === 'function' ) {
16
+ if ( typeof writev === 'function' ) {
17
17
_writev = ( handle , buffers ) => handle . writev ( buffers ) ;
18
18
} else {
19
19
_writev = async ( handle , buffers ) => {
20
20
for ( const buffer of buffers ) await handle . write ( buffer ) ;
21
21
} ;
22
22
}
23
23
24
- async function exportAsync ( database , path ) {
24
+ async function exportAsync ( database : Database , path : string ) {
25
25
const handle = await open ( path , 'w' ) ;
26
26
27
27
try {
@@ -58,7 +58,17 @@ async function exportAsync(database, path) {
58
58
}
59
59
}
60
60
61
+ type DatabaseOptions = {
62
+ version : number ,
63
+ path : string ,
64
+ onUpgrade : ( ...args : any [ ] ) => any ,
65
+ onDowngrade : ( ...args : any [ ] ) => any
66
+ } ;
67
+
61
68
class Database {
69
+ options : DatabaseOptions ;
70
+ _models : any ;
71
+ Model : typeof Model ;
62
72
63
73
/**
64
74
* Database constructor.
@@ -69,13 +79,13 @@ class Database {
69
79
* @param {function } [options.onUpgrade] Triggered when the database is upgraded
70
80
* @param {function } [options.onDowngrade] Triggered when the database is downgraded
71
81
*/
72
- constructor ( options ) {
73
- this . options = Object . assign ( {
82
+ constructor ( options : { path : string } & Partial < DatabaseOptions > ) {
83
+ this . options = {
74
84
version : 0 ,
75
85
onUpgrade ( ) { } ,
76
-
77
- onDowngrade ( ) { }
78
- } , options ) ;
86
+ onDowngrade ( ) { } ,
87
+ ... options
88
+ } ;
79
89
80
90
this . _models = { } ;
81
91
@@ -93,7 +103,7 @@ class Database {
93
103
* @param {Schema|object } [schema]
94
104
* @return {Model }
95
105
*/
96
- model ( name , schema ) {
106
+ model ( name : string , schema ?: any ) {
97
107
if ( this . _models [ name ] ) {
98
108
return this . _models [ name ] ;
99
109
}
@@ -132,9 +142,9 @@ class Database {
132
142
this . model ( data . key ) . _import ( data . value ) ;
133
143
} ) ;
134
144
135
- const rs = fs . createReadStream ( path , 'utf8' ) ;
145
+ const rs = createReadStream ( path , 'utf8' ) ;
136
146
137
- return pipeline ( rs , parseStream ) . then ( ( ) => {
147
+ return pipelineAsync ( rs , parseStream ) . then ( ( ) => {
138
148
if ( newVersion > oldVersion ) {
139
149
return onUpgrade ( oldVersion , newVersion ) ;
140
150
} else if ( newVersion < oldVersion ) {
@@ -153,7 +163,7 @@ class Database {
153
163
const { path } = this . options ;
154
164
155
165
if ( ! path ) throw new WarehouseError ( 'options.path is required' ) ;
156
- return Promise . resolve ( exportAsync ( this , path ) ) . asCallback ( callback ) ;
166
+ return Bluebird . resolve ( exportAsync ( this , path ) ) . asCallback ( callback ) ;
157
167
}
158
168
159
169
toJSON ( ) {
@@ -171,12 +181,15 @@ class Database {
171
181
} , models
172
182
} ;
173
183
}
184
+ static Schema = Schema ;
185
+ Schema : typeof Schema ;
186
+ static SchemaType = SchemaType ;
187
+ SchemaType : typeof SchemaType ;
188
+ static version : number ;
174
189
}
175
190
176
191
Database . prototype . Schema = Schema ;
177
- Database . Schema = Database . prototype . Schema ;
178
192
Database . prototype . SchemaType = SchemaType ;
179
- Database . SchemaType = Database . prototype . SchemaType ;
180
193
Database . version = pkg . version ;
181
194
182
- module . exports = Database ;
195
+ export = Database ;
0 commit comments