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,15 @@ 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 ,
85
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
75
86
onUpgrade ( ) { } ,
76
-
77
- onDowngrade ( ) { }
78
- } , options ) ;
87
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
88
+ onDowngrade ( ) { } ,
89
+ ...options
90
+ } ;
79
91
80
92
this . _models = { } ;
81
93
@@ -93,7 +105,7 @@ class Database {
93
105
* @param {Schema|object } [schema]
94
106
* @return {Model }
95
107
*/
96
- model ( name , schema ) {
108
+ model ( name : string , schema ?: any ) {
97
109
if ( this . _models [ name ] ) {
98
110
return this . _models [ name ] ;
99
111
}
@@ -132,9 +144,9 @@ class Database {
132
144
this . model ( data . key ) . _import ( data . value ) ;
133
145
} ) ;
134
146
135
- const rs = fs . createReadStream ( path , 'utf8' ) ;
147
+ const rs = createReadStream ( path , 'utf8' ) ;
136
148
137
- return pipeline ( rs , parseStream ) . then ( ( ) => {
149
+ return pipelineAsync ( rs , parseStream ) . then ( ( ) => {
138
150
if ( newVersion > oldVersion ) {
139
151
return onUpgrade ( oldVersion , newVersion ) ;
140
152
} else if ( newVersion < oldVersion ) {
@@ -153,7 +165,7 @@ class Database {
153
165
const { path } = this . options ;
154
166
155
167
if ( ! path ) throw new WarehouseError ( 'options.path is required' ) ;
156
- return Promise . resolve ( exportAsync ( this , path ) ) . asCallback ( callback ) ;
168
+ return Bluebird . resolve ( exportAsync ( this , path ) ) . asCallback ( callback ) ;
157
169
}
158
170
159
171
toJSON ( ) {
@@ -171,12 +183,15 @@ class Database {
171
183
} , models
172
184
} ;
173
185
}
186
+ static Schema = Schema ;
187
+ Schema : typeof Schema ;
188
+ static SchemaType = SchemaType ;
189
+ SchemaType : typeof SchemaType ;
190
+ static version : number ;
174
191
}
175
192
176
193
Database . prototype . Schema = Schema ;
177
- Database . Schema = Database . prototype . Schema ;
178
194
Database . prototype . SchemaType = SchemaType ;
179
- Database . SchemaType = Database . prototype . SchemaType ;
180
195
Database . version = pkg . version ;
181
196
182
- module . exports = Database ;
197
+ export = Database ;
0 commit comments