Skip to content

Commit 24fc225

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

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

+564
-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: 41 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,15 @@ 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,
85+
// eslint-disable-next-line @typescript-eslint/no-empty-function
7586
onUpgrade() {},
76-
77-
onDowngrade() {}
78-
}, options);
87+
// eslint-disable-next-line @typescript-eslint/no-empty-function
88+
onDowngrade() {},
89+
...options
90+
};
7991

8092
this._models = {};
8193

@@ -93,7 +105,7 @@ class Database {
93105
* @param {Schema|object} [schema]
94106
* @return {Model}
95107
*/
96-
model(name, schema) {
108+
model(name: string, schema?: any) {
97109
if (this._models[name]) {
98110
return this._models[name];
99111
}
@@ -132,9 +144,9 @@ class Database {
132144
this.model(data.key)._import(data.value);
133145
});
134146

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

137-
return pipeline(rs, parseStream).then(() => {
149+
return pipelineAsync(rs, parseStream).then(() => {
138150
if (newVersion > oldVersion) {
139151
return onUpgrade(oldVersion, newVersion);
140152
} else if (newVersion < oldVersion) {
@@ -153,7 +165,7 @@ class Database {
153165
const { path } = this.options;
154166

155167
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);
157169
}
158170

159171
toJSON() {
@@ -171,12 +183,15 @@ class Database {
171183
}, models
172184
};
173185
}
186+
static Schema = Schema;
187+
Schema: typeof Schema;
188+
static SchemaType = SchemaType;
189+
SchemaType: typeof SchemaType;
190+
static version: number;
174191
}
175192

176193
Database.prototype.Schema = Schema;
177-
Database.Schema = Database.prototype.Schema;
178194
Database.prototype.SchemaType = SchemaType;
179-
Database.SchemaType = Database.prototype.SchemaType;
180195
Database.version = pkg.version;
181196

182-
module.exports = Database;
197+
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;

0 commit comments

Comments
 (0)