Skip to content

Commit d70896e

Browse files
committed
Replace js to ts
1 parent 50a6c59 commit d70896e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+560
-534
lines changed

lib/types/index.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/util.js

Lines changed: 0 additions & 190 deletions
This file was deleted.

lib/database.js renamed to src/database.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
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
1010
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>;
1313

14-
let _writev;
14+
let _writev: (handle: fsPromises.FileHandle, buffers: Buffer[]) => Promise<unknown>;
1515

16-
if (typeof fs.writev === 'function') {
16+
if (typeof writev === 'function') {
1717
_writev = (handle, buffers) => handle.writev(buffers);
1818
} else {
1919
_writev = async (handle, buffers) => {
2020
for (const buffer of buffers) await handle.write(buffer);
2121
};
2222
}
2323

24-
async function exportAsync(database, path) {
24+
async function exportAsync(database: Database, path: string) {
2525
const handle = await open(path, 'w');
2626

2727
try {
@@ -58,7 +58,17 @@ async function exportAsync(database, path) {
5858
}
5959
}
6060

61+
type DatabaseOptions = {
62+
version: number,
63+
path: string,
64+
onUpgrade: (...args: any[]) => any,
65+
onDowngrade: (...args: any[]) => any
66+
};
67+
6168
class Database {
69+
options: DatabaseOptions;
70+
_models: any;
71+
Model: typeof Model;
6272

6373
/**
6474
* Database constructor.
@@ -69,13 +79,13 @@ class Database {
6979
* @param {function} [options.onUpgrade] Triggered when the database is upgraded
7080
* @param {function} [options.onDowngrade] Triggered when the database is downgraded
7181
*/
72-
constructor(options) {
73-
this.options = Object.assign({
82+
constructor(options: { path: string } & Partial<DatabaseOptions>) {
83+
this.options = {
7484
version: 0,
7585
onUpgrade() {},
76-
77-
onDowngrade() {}
78-
}, options);
86+
onDowngrade() {},
87+
...options
88+
};
7989

8090
this._models = {};
8191

@@ -93,7 +103,7 @@ class Database {
93103
* @param {Schema|object} [schema]
94104
* @return {Model}
95105
*/
96-
model(name, schema) {
106+
model(name: string, schema?: any) {
97107
if (this._models[name]) {
98108
return this._models[name];
99109
}
@@ -132,9 +142,9 @@ class Database {
132142
this.model(data.key)._import(data.value);
133143
});
134144

135-
const rs = fs.createReadStream(path, 'utf8');
145+
const rs = createReadStream(path, 'utf8');
136146

137-
return pipeline(rs, parseStream).then(() => {
147+
return pipelineAsync(rs, parseStream).then(() => {
138148
if (newVersion > oldVersion) {
139149
return onUpgrade(oldVersion, newVersion);
140150
} else if (newVersion < oldVersion) {
@@ -153,7 +163,7 @@ class Database {
153163
const { path } = this.options;
154164

155165
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);
157167
}
158168

159169
toJSON() {
@@ -171,12 +181,15 @@ class Database {
171181
}, models
172182
};
173183
}
184+
static Schema = Schema;
185+
Schema: typeof Schema;
186+
static SchemaType = SchemaType;
187+
SchemaType: typeof SchemaType;
188+
static version: number;
174189
}
175190

176191
Database.prototype.Schema = Schema;
177-
Database.Schema = Database.prototype.Schema;
178192
Database.prototype.SchemaType = SchemaType;
179-
Database.SchemaType = Database.prototype.SchemaType;
180193
Database.version = pkg.version;
181194

182-
module.exports = Database;
195+
export = Database;

lib/document.js renamed to src/document.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
'use strict';
1+
import rfdc = require('rfdc');
2+
const cloneDeep = rfdc();
23

3-
const cloneDeep = require('rfdc')();
4-
5-
class Document {
4+
abstract class Document {
5+
abstract _model;
6+
_id!: any;
7+
abstract _schema;
68

79
/**
810
* Document constructor.
@@ -101,4 +103,4 @@ function isGetter(obj, key) {
101103
return Object.getOwnPropertyDescriptor(obj, key).get;
102104
}
103105

104-
module.exports = Document;
106+
export = Document;
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
'use strict';
2-
31
class WarehouseError extends Error {
2+
code?: string;
43

54
/**
65
* WarehouseError constructor
76
*
87
* @param {string} msg
98
* @param {string} code
109
*/
11-
constructor(msg, code) {
10+
constructor(msg: string, code?: string) {
1211
super(msg);
1312

1413
Error.captureStackTrace(this);
1514

1615
this.code = code;
1716
}
17+
static ID_EXIST = 'ID_EXIST';
18+
static ID_NOT_EXIST = 'ID_NOT_EXIST';
19+
static ID_UNDEFINED = 'ID_UNDEFINED';
1820
}
1921

2022
WarehouseError.prototype.name = 'WarehouseError';
21-
WarehouseError.ID_EXIST = 'ID_EXIST';
22-
WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST';
23-
WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED';
2423

25-
module.exports = WarehouseError;
24+
export = WarehouseError;
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const WarehouseError = require('../error');
1+
import WarehouseError = require('../error');
42

53
class PopulationError extends WarehouseError {}
64

75
PopulationError.prototype.name = 'PopulationError';
86

9-
module.exports = PopulationError;
7+
export = PopulationError;

0 commit comments

Comments
 (0)