Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace typescript #60

Merged
merged 34 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
087e789
Add tsc
segayuu Jun 5, 2020
8e8aa31
Replace js to ts
segayuu Jun 6, 2020
d739088
chore(deps): update devDependencies
yoshinorin Jun 16, 2022
b1a4c57
refactor: use import syntax
yoshinorin Jun 22, 2022
fc5ff42
chore: format `.tsconfig.json`
yoshinorin Jun 22, 2022
9b82a79
chore: update `tsconfig.json`
yoshinorin Jun 22, 2022
d278763
Revert only under the `test` dir: "refactor: use import syntax"
yoshinorin Jun 22, 2022
38e36cb
chore: built to dist
yoshinorin Jun 22, 2022
7baec35
chore: update `.eslint.json` extends from `hexo/ts.js`
yoshinorin Jun 22, 2022
3abf305
chore(deps): update dependencies
yoshinorin Jun 22, 2022
232613a
fix; `rfdc` import
yoshinorin Jun 22, 2022
1786e37
fix: exports of types
yoshinorin Jun 22, 2022
989a7df
test: `js` to `ts`
yoshinorin Jun 22, 2022
4c4037c
fix: allow empty argument for Database constructor and some methods
yoshinorin Jun 22, 2022
c2fbe63
refactor: `export` to `export default`
yoshinorin Jun 22, 2022
24e23dd
refactor: util export
yoshinorin Jun 22, 2022
8fd8cc2
fix: import `isPlainObject` instead of const
yoshinorin Jun 22, 2022
22d2ba7
chore: once ignore eslint for test
yoshinorin Jun 22, 2022
7a92cc1
chore: add .tsconfig.json for test
yoshinorin Jul 23, 2022
18a2105
chore: .tsconfig.json to tsconfig.json
yoshinorin Jul 23, 2022
57d207f
chore: delete tsconfig.json for test
yoshinorin Jul 23, 2022
428c145
Merge branch 'master' into replace-typescript
yoshinorin Jul 23, 2022
3ee2546
update each type functions number of argument
yoshinorin Jul 23, 2022
935fa4d
test(eslint): fix and ignore eslint some rules
yoshinorin Jul 23, 2022
aa58e27
chore: specify node 18.4 in ci.yml
yoshinorin Jul 23, 2022
418c334
chore: workaround for `Error [ERR_STREAM_PREMATURE_CLOSE]` in node 18.6
yoshinorin Jul 23, 2022
c8176d2
Merge branch 'master' into replace-typescript
yoshinorin Jul 28, 2022
8a2e372
chore: delete require nodejs`<18.5`
yoshinorin Jul 28, 2022
64e134d
migrate jsonstream from js to ts
yoshinorin Aug 10, 2022
fa1eb2a
fix lint
yoshinorin Aug 10, 2022
984b4c5
Update src/database.ts
yoshinorin Aug 11, 2022
76599f2
chore(deps): add `@types/through2`
yoshinorin Aug 11, 2022
e5f66c5
chore: add ts-ignore
yoshinorin Aug 12, 2022
0e23108
chore: allow ts-ignore
yoshinorin Aug 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"root": true,
"extends": "hexo"
}
"extends": "hexo/ts.js",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
},
"rules": {
"@typescript-eslint/no-unused-vars": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-var-requires": 0,
"no-use-before-define": 0
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules/
.nyc_output/
*.log
docs/
coverage/
coverage/
dist
19 changes: 19 additions & 0 deletions .tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
]
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
14 changes: 0 additions & 14 deletions lib/types/index.js

This file was deleted.

48 changes: 31 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
"name": "warehouse",
"version": "4.0.1",
"description": "Simple JSON-based database",
"main": "lib/database",
"main": "dist/database",
"directories": {
"lib": "./lib"
"lib": "./dist"
},
"files": [
"lib/"
"dist/"
],
"scripts": {
"eslint": "eslint lib test",
"test": "mocha \"test/scripts/**/*.js\"",
"pretest": "npm run typescript",
"typescript": "tsc --build .tsconfig.json",
"eslint": "eslint src",
"test": "mocha -r ts-node/register 'test/scripts/**/*.ts'",
"test-cov": "nyc --reporter=lcovonly --reporter=text-summary npm test",
"jsdoc": "jsdoc --configure .jsdoc.json"
"jsdoc": "jsdoc --configure .jsdoc.json",
"prepublish": "npm run typescript"
},
"repository": "hexojs/warehouse",
"keywords": [
Expand All @@ -24,25 +27,36 @@
"author": "Tommy Chen <tommy351@gmail.com> (http://zespia.tw)",
"license": "MIT",
"dependencies": {
"bluebird": "^3.2.2",
"cuid": "^2.1.4",
"graceful-fs": "^4.1.3",
"bluebird": "^3.7.2",
"cuid": "^2.1.8",
"graceful-fs": "^4.2.10",
"hexo-log": "^3.0.0",
"is-plain-object": "^5.0.0",
"JSONStream": "^1.0.7",
"rfdc": "^1.1.4"
"JSONStream": "^1.3.5",
"rfdc": "^1.3.0"
},
"devDependencies": {
"chai": "^4.2.0",
"@types/bluebird": "^3.5.36",
"@types/chai": "^4.3.1",
"@types/graceful-fs": "^4.1.5",
"@types/jsonstream": "^0.8.30",
"@types/mocha": "^9.1.1",
"@types/node": "^18.0.0",
"@types/sinon": "^10.0.11",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"eslint": "^8.1.0",
"eslint": "^8.18.0",
"eslint-config-hexo": "^5.0.0",
"jsdoc": "^3.4.0",
"lodash": "^4.17.15",
"minami": "^1.1.1",
"jsdoc": "^3.6.10",
"lodash": "^4.17.21",
"minami": "^1.2.3",
"mocha": "^10.0.0",
"nyc": "^15.0.0",
"sinon": "^14.0.0"
"sinon": "^14.0.0",
"ts-node": "^10.8.1",
"typescript": "^4.7.4"
},
"engines": {
"node": ">=14"
Expand Down
73 changes: 44 additions & 29 deletions lib/database.js → src/database.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
'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 from 'JSONStream';
import Bluebird from 'bluebird';
import { writev, promises as fsPromises, createReadStream } from 'graceful-fs';
import { pipeline, Stream } from 'stream';
import Model from './model';
import Schema from './schema';
import SchemaType from './schematype';
import WarehouseError from './error';
import log from 'hexo-log';

const pkg = require('../package.json');
const { open } = fs.promises;
const pipeline = Promise.promisify(require('stream').pipeline);
const log = require('hexo-log')();
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 @@ -69,7 +69,17 @@ async function exportAsync(database, path) {
}
}

type DatabaseOptions = {
version: number,
path: string,
onUpgrade: (...args: any[]) => any,
onDowngrade: (...args: any[]) => any
};
yoshinorin marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* Database constructor.
Expand All @@ -80,13 +90,15 @@ 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,
// eslint-disable-next-line @typescript-eslint/no-empty-function
onUpgrade() {},

onDowngrade() {}
}, options);
// eslint-disable-next-line @typescript-eslint/no-empty-function
onDowngrade() {},
...options
};

this._models = {};

Expand All @@ -104,7 +116,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 All @@ -120,7 +132,7 @@ class Database {
* @param {function} [callback]
* @return {Promise}
*/
load(callback) {
load(callback?) {
const { path, onUpgrade, onDowngrade, version: newVersion } = this.options;

if (!path) throw new WarehouseError('options.path is required');
Expand All @@ -143,9 +155,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 @@ -160,11 +172,11 @@ class Database {
* @param {function} [callback]
* @return {Promise}
*/
save(callback) {
save(callback?) {
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 @@ -182,12 +194,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 default 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 from '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 default 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 default 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 from '../error';

class PopulationError extends WarehouseError {}

PopulationError.prototype.name = 'PopulationError';

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

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

class ValidationError extends WarehouseError {}

ValidationError.prototype.name = 'ValidationError';

module.exports = ValidationError;
export default ValidationError;
Loading