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

WIP: refactor: migrate to ESM #130

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{
"root": true,
"extends": "hexo"
"extends": "hexo",
// TODO: should remove `@babel/core`, `@babel/eslint-parser` and `@babel/plugin-syntax-import-assertions`
// after the ESLint supports `import.meta.url` and `assert { type: "json" }`
// https://github.com/eslint/eslint/discussions/15305
// https://github.com/babel/babel/blob/ad194013881727ca6b17a0e72cfea2d552eed049/eslint/babel-eslint-parser/README.md
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false,
"sourceType": "module",
"ecmaVersion": "2020",
"babelOptions": {
"plugins": [
"@babel/plugin-syntax-import-assertions"
]
}
}
}
3 changes: 3 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
color: true
reporter: spec
opts: false
spec: "test/scripts/**/*.js"
28 changes: 13 additions & 15 deletions lib/database.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'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');
const pkg = require('../package.json');
import JSONStream from 'JSONStream';
import Promise from 'bluebird';
import fs from 'graceful-fs';
import Model from './model.js';
import Schema from './schema.js';
import SchemaType from './schematype.js';
import WarehouseError from './error.js';
import pkg from '../package.json' assert { type: "json" };
import log from 'hexo-log';
import { Stream } from 'stream';

const pipeline = Promise.promisify(Stream.pipeline);
const { open } = fs.promises;
const pipeline = Promise.promisify(require('stream').pipeline);
const log = require('hexo-log')();

let _writev;

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

class Database {
export default class Database {

/**
* Database constructor.
Expand Down Expand Up @@ -189,5 +189,3 @@ Database.Schema = Database.prototype.Schema;
Database.prototype.SchemaType = SchemaType;
Database.SchemaType = Database.prototype.SchemaType;
Database.version = pkg.version;

module.exports = Database;
7 changes: 3 additions & 4 deletions lib/document.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

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

class Document {

Expand Down Expand Up @@ -101,4 +100,4 @@ function isGetter(obj, key) {
return Object.getOwnPropertyDescriptor(obj, key).get;
}

module.exports = Document;
export default Document;
4 changes: 1 addition & 3 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

class WarehouseError extends Error {

/**
Expand All @@ -22,4 +20,4 @@ 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
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

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

class PopulationError extends WarehouseError {}

PopulationError.prototype.name = 'PopulationError';

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

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

class ValidationError extends WarehouseError {}

ValidationError.prototype.name = 'ValidationError';

module.exports = ValidationError;
export default ValidationError;
27 changes: 13 additions & 14 deletions lib/model.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

const { EventEmitter } = require('events');
const cloneDeep = require('rfdc')();
const Promise = require('bluebird');
const { parseArgs, getProp, setGetter, shuffle } = require('./util');
const Document = require('./document');
const Query = require('./query');
const Schema = require('./schema');
const Types = require('./types');
const WarehouseError = require('./error');
const PopulationError = require('./error/population');
const Mutex = require('./mutex');
import { EventEmitter } from 'events';
import rfdc from 'rfdc';
const cloneDeep = rfdc();
import Promise from 'bluebird';
import { parseArgs, getProp, setGetter, shuffle } from './util.js';
import Document from './document.js';
import Query from './query.js';
import Schema from './schema.js';
import * as Types from './types/index.js';
import WarehouseError from './error.js';
import PopulationError from './error/population.js';
import Mutex from './mutex.js';

class Model extends EventEmitter {

Expand Down Expand Up @@ -963,4 +962,4 @@ Model.prototype.each = Model.prototype.forEach;

Model.prototype.random = Model.prototype.shuffle;

module.exports = Model;
export default Model;
4 changes: 1 addition & 3 deletions lib/mutex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

class Mutex {
constructor() {
this._locked = false;
Expand Down Expand Up @@ -29,4 +27,4 @@ class Mutex {
}
}

module.exports = Mutex;
export default Mutex;
8 changes: 3 additions & 5 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const Promise = require('bluebird');
const { parseArgs, shuffle } = require('./util');
import Promise from 'bluebird';
import { parseArgs, shuffle } from './util.js';

class Query {

Expand Down Expand Up @@ -387,4 +385,4 @@ Query.prototype.each = Query.prototype.forEach;

Query.prototype.random = Query.prototype.shuffle;

module.exports = Query;
export default Query;
16 changes: 7 additions & 9 deletions lib/schema.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

const SchemaType = require('./schematype');
const Types = require('./types');
const Promise = require('bluebird');
const { getProp, setProp, delProp } = require('./util');
const PopulationError = require('./error/population');
const { isPlainObject } = require('is-plain-object');
import SchemaType from './schematype.js';
import * as Types from './types/index.js';
import Promise from 'bluebird';
import { getProp, setProp, delProp } from './util.js';
import PopulationError from './error/population.js';
import { isPlainObject } from 'is-plain-object';

/**
* @callback queryFilterCallback
Expand Down Expand Up @@ -792,4 +790,4 @@ class Schema {
Schema.prototype.Types = Types;
Schema.Types = Schema.prototype.Types;

module.exports = Schema;
export default Schema;
8 changes: 3 additions & 5 deletions lib/schematype.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const { setProp } = require('./util');
const ValidationError = require('./error/validation');
import { setProp } from './util.js';
import ValidationError from './error/validation.js';

/**
* This is the basic schema type.
Expand Down Expand Up @@ -292,4 +290,4 @@ SchemaType.prototype.q$max = SchemaType.prototype.q$lte;

SchemaType.prototype.q$min = SchemaType.prototype.q$gte;

module.exports = SchemaType;
export default SchemaType;
8 changes: 3 additions & 5 deletions lib/types/array.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaType = require('../schematype');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import ValidationError from '../error/validation.js';

const { isArray } = Array;

Expand Down Expand Up @@ -378,4 +376,4 @@ SchemaTypeArray.prototype.u$append = SchemaTypeArray.prototype.u$push;

SchemaTypeArray.prototype.u$prepend = SchemaTypeArray.prototype.u$unshift;

module.exports = SchemaTypeArray;
export default SchemaTypeArray;
8 changes: 3 additions & 5 deletions lib/types/boolean.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaType = require('../schematype');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import ValidationError from '../error/validation.js';

/**
* Boolean schema type.
Expand Down Expand Up @@ -63,4 +61,4 @@ class SchemaTypeBoolean extends SchemaType {
}
}

module.exports = SchemaTypeBoolean;
export default SchemaTypeBoolean;
8 changes: 3 additions & 5 deletions lib/types/buffer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaType = require('../schematype');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import ValidationError from '../error/validation.js';

/**
* Boolean schema type.
Expand Down Expand Up @@ -107,4 +105,4 @@ class SchemaTypeBuffer extends SchemaType {
}
}

module.exports = SchemaTypeBuffer;
export default SchemaTypeBuffer;
10 changes: 4 additions & 6 deletions lib/types/cuid.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const SchemaType = require('../schematype');
const cuid = require('cuid');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import cuid from 'cuid';
import ValidationError from '../error/validation.js';

/**
* [CUID](https://github.com/ericelliott/cuid) schema type.
Expand Down Expand Up @@ -41,4 +39,4 @@ class SchemaTypeCUID extends SchemaType {
}
}

module.exports = SchemaTypeCUID;
export default SchemaTypeCUID;
8 changes: 3 additions & 5 deletions lib/types/date.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaType = require('../schematype');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import ValidationError from '../error/validation.js';

/**
* Date schema type.
Expand Down Expand Up @@ -154,4 +152,4 @@ class SchemaTypeDate extends SchemaType {
}
}

module.exports = SchemaTypeDate;
export default SchemaTypeDate;
8 changes: 3 additions & 5 deletions lib/types/enum.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaType = require('../schematype');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import ValidationError from '../error/validation.js';

/**
* Enum schema type.
Expand Down Expand Up @@ -41,4 +39,4 @@ class SchemaTypeEnum extends SchemaType {
}
}

module.exports = SchemaTypeEnum;
export default SchemaTypeEnum;
39 changes: 26 additions & 13 deletions lib/types/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
'use strict';
import SchemaType from '../schematype.js';
import SchemaTypeString from './string.js';
import SchemaTypeNumber from './number.js';
import SchemaTypeBoolean from './boolean.js';
import SchemaTypeArray from './array.js';
import SchemaTypeObject from './object.js';
import SchemaTypeDate from './date.js';
import SchemaTypeVirtual from './virtual.js';
import SchemaTypeCUID from './cuid.js';
import SchemaTypeEnum from './enum.js';
import SchemaTypeInteger from './integer.js';
import SchemaTypeBuffer from './buffer.js';

exports.Mixed = require('../schematype');
exports.String = require('./string');
exports.Number = require('./number');
exports.Boolean = require('./boolean');
exports.Array = require('./array');
exports.Object = require('./object');
exports.Date = require('./date');
exports.Virtual = require('./virtual');
exports.CUID = require('./cuid');
exports.Enum = require('./enum');
exports.Integer = require('./integer');
exports.Buffer = require('./buffer');
export {
SchemaType as Mixed,
SchemaTypeString as String,
SchemaTypeNumber as Number,
SchemaTypeBoolean as Boolean,
SchemaTypeArray as Array,
SchemaTypeObject as Object,
SchemaTypeDate as Date,
SchemaTypeVirtual as Virtual,
SchemaTypeCUID as CUID,
SchemaTypeEnum as Enum,
SchemaTypeInteger as Integer,
SchemaTypeBuffer as Buffer
};
8 changes: 3 additions & 5 deletions lib/types/integer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaTypeNumber = require('./number');
const ValidationError = require('../error/validation');
import SchemaTypeNumber from './number.js';
import ValidationError from '../error/validation.js';

/**
* Integer schema type.
Expand Down Expand Up @@ -39,4 +37,4 @@ class SchemaTypeInteger extends SchemaTypeNumber {
}
}

module.exports = SchemaTypeInteger;
export default SchemaTypeInteger;
8 changes: 3 additions & 5 deletions lib/types/number.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const SchemaType = require('../schematype');
const ValidationError = require('../error/validation');
import SchemaType from '../schematype.js';
import ValidationError from '../error/validation.js';

/**
* Number schema type.
Expand Down Expand Up @@ -125,4 +123,4 @@ class SchemaTypeNumber extends SchemaType {
}
}

module.exports = SchemaTypeNumber;
export default SchemaTypeNumber;
Loading