Skip to content

Commit

Permalink
Replace js to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
segayuu committed Jun 6, 2020
1 parent 50a6c59 commit d70896e
Show file tree
Hide file tree
Showing 43 changed files with 560 additions and 534 deletions.
14 changes: 0 additions & 14 deletions lib/types/index.js

This file was deleted.

190 changes: 0 additions & 190 deletions lib/util.js

This file was deleted.

65 changes: 39 additions & 26 deletions lib/database.js → src/database.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
'use strict';

const JSONStream = require('JSONStream');
const Promise = require('bluebird');
const fs = require('graceful-fs');
const Model = require('./model');
const Schema = require('./schema');
const SchemaType = require('./schematype');
const WarehouseError = require('./error');
import JSONStream = require('JSONStream');
import Bluebird = require('bluebird');
import { writev, promises as fsPromises, createReadStream } from 'graceful-fs';
import { pipeline, Stream } from 'stream';
import Model = require('./model');
import Schema = require('./schema');
import SchemaType = require('./schematype');
import WarehouseError = require('./error');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../package.json');
const { open } = fs.promises;
const pipeline = Promise.promisify(require('stream').pipeline);
const { open } = fsPromises;
const pipelineAsync = Bluebird.promisify(pipeline) as (...args: Stream[]) => Bluebird<unknown>;

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

if (typeof fs.writev === 'function') {
if (typeof writev === 'function') {
_writev = (handle, buffers) => handle.writev(buffers);
} else {
_writev = async (handle, buffers) => {
for (const buffer of buffers) await handle.write(buffer);
};
}

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

try {
Expand Down Expand Up @@ -58,7 +58,17 @@ async function exportAsync(database, path) {
}
}

type DatabaseOptions = {
version: number,
path: string,
onUpgrade: (...args: any[]) => any,
onDowngrade: (...args: any[]) => any
};

class Database {
options: DatabaseOptions;
_models: any;
Model: typeof Model;

/**
* Database constructor.
Expand All @@ -69,13 +79,13 @@ class Database {
* @param {function} [options.onUpgrade] Triggered when the database is upgraded
* @param {function} [options.onDowngrade] Triggered when the database is downgraded
*/
constructor(options) {
this.options = Object.assign({
constructor(options: { path: string } & Partial<DatabaseOptions>) {
this.options = {
version: 0,
onUpgrade() {},

onDowngrade() {}
}, options);
onDowngrade() {},
...options
};

this._models = {};

Expand All @@ -93,7 +103,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name, schema) {
model(name: string, schema?: any) {
if (this._models[name]) {
return this._models[name];
}
Expand Down Expand Up @@ -132,9 +142,9 @@ class Database {
this.model(data.key)._import(data.value);
});

const rs = fs.createReadStream(path, 'utf8');
const rs = createReadStream(path, 'utf8');

return pipeline(rs, parseStream).then(() => {
return pipelineAsync(rs, parseStream).then(() => {
if (newVersion > oldVersion) {
return onUpgrade(oldVersion, newVersion);
} else if (newVersion < oldVersion) {
Expand All @@ -153,7 +163,7 @@ class Database {
const { path } = this.options;

if (!path) throw new WarehouseError('options.path is required');
return Promise.resolve(exportAsync(this, path)).asCallback(callback);
return Bluebird.resolve(exportAsync(this, path)).asCallback(callback);
}

toJSON() {
Expand All @@ -171,12 +181,15 @@ class Database {
}, models
};
}
static Schema = Schema;
Schema: typeof Schema;
static SchemaType = SchemaType;
SchemaType: typeof SchemaType;
static version: number;
}

Database.prototype.Schema = Schema;
Database.Schema = Database.prototype.Schema;
Database.prototype.SchemaType = SchemaType;
Database.SchemaType = Database.prototype.SchemaType;
Database.version = pkg.version;

module.exports = Database;
export = Database;
12 changes: 7 additions & 5 deletions lib/document.js → src/document.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
import rfdc = require('rfdc');
const cloneDeep = rfdc();

const cloneDeep = require('rfdc')();

class Document {
abstract class Document {
abstract _model;
_id!: any;
abstract _schema;

/**
* Document constructor.
Expand Down Expand Up @@ -101,4 +103,4 @@ function isGetter(obj, key) {
return Object.getOwnPropertyDescriptor(obj, key).get;
}

module.exports = Document;
export = Document;
13 changes: 6 additions & 7 deletions lib/error.js → src/error.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
'use strict';

class WarehouseError extends Error {
code?: string;

/**
* WarehouseError constructor
*
* @param {string} msg
* @param {string} code
*/
constructor(msg, code) {
constructor(msg: string, code?: string) {
super(msg);

Error.captureStackTrace(this);

this.code = code;
}
static ID_EXIST = 'ID_EXIST';
static ID_NOT_EXIST = 'ID_NOT_EXIST';
static ID_UNDEFINED = 'ID_UNDEFINED';
}

WarehouseError.prototype.name = 'WarehouseError';
WarehouseError.ID_EXIST = 'ID_EXIST';
WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST';
WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED';

module.exports = WarehouseError;
export = WarehouseError;
6 changes: 2 additions & 4 deletions lib/error/population.js → src/error/population.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

const WarehouseError = require('../error');
import WarehouseError = require('../error');

class PopulationError extends WarehouseError {}

PopulationError.prototype.name = 'PopulationError';

module.exports = PopulationError;
export = PopulationError;
Loading

0 comments on commit d70896e

Please sign in to comment.