Skip to content

Commit

Permalink
refactor: refactor types (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon authored Jan 12, 2024
1 parent 25dca28 commit 2209f06
Show file tree
Hide file tree
Showing 35 changed files with 596 additions and 452 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ node_modules/
*.log
docs/
coverage/
dist
dist
yarn.lock
package-lock.json
pnpm-lock.yaml
15 changes: 8 additions & 7 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Schema from './schema';
import SchemaType from './schematype';
import WarehouseError from './error';
import { logger } from 'hexo-log';
import type { AddSchemaTypeOptions, NodeJSLikeCallback } from './types';

const log = logger();
const pkg = require('../package.json');
Expand All @@ -23,7 +24,7 @@ if (typeof writev === 'function') {
};
}

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

try {
Expand Down Expand Up @@ -79,7 +80,7 @@ interface DatabaseOptions {

class Database {
options: DatabaseOptions;
_models: any;
_models: Record<string, Model<any>>;
Model: typeof Model;

/**
Expand All @@ -103,7 +104,7 @@ class Database {

this._models = {};

class _Model extends Model {}
class _Model extends Model<any> {}

this.Model = _Model;

Expand All @@ -117,7 +118,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name: string, schema?: any) {
model(name: string, schema?: Schema | Record<string, AddSchemaTypeOptions>): Model<any> {
if (this._models[name]) {
return this._models[name];
}
Expand All @@ -133,7 +134,7 @@ class Database {
* @param {function} [callback]
* @return {Promise}
*/
load(callback?) {
load(callback?: NodeJSLikeCallback<any>): Bluebird<any> {
const { path, onUpgrade, onDowngrade, version: newVersion } = this.options;

if (!path) throw new WarehouseError('options.path is required');
Expand Down Expand Up @@ -173,14 +174,14 @@ class Database {
* @param {function} [callback]
* @return {Promise}
*/
save(callback?) {
save(callback?: NodeJSLikeCallback<any>): Bluebird<void> {
const { path } = this.options;

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

toJSON() {
toJSON(): { meta: { version: number, warehouse: string }, models: Record<string, Model<any>> } {
const models = Object.keys(this._models)
.reduce((obj, key) => {
const value = this._models[key];
Expand Down
34 changes: 19 additions & 15 deletions src/document.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import rfdc from 'rfdc';
import type Model from './model';
import type Schema from './schema';
import type { NodeJSLikeCallback } from './types';
const cloneDeep = rfdc();

abstract class Document {
abstract _model;
_id!: any;
abstract _schema;
abstract class Document<T> {
abstract _model: Model<T>;
_id!: string | number | undefined;
abstract _schema: Schema;
[key : string]: any;

/**
* Document constructor.
*
* @param {object} data
*/
constructor(data) {
constructor(data?: T) {
if (data) {
Object.assign(this, data);
}
Expand All @@ -23,7 +27,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
save(callback) {
save(callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.save(this, callback);
}

Expand All @@ -34,7 +38,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
update(data, callback) {
update(data: object, callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.updateById(this._id, data, callback);
}

Expand All @@ -45,7 +49,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
replace(data, callback) {
replace(data: T | Document<T>, callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.replaceById(this._id, data, callback);
}

Expand All @@ -55,7 +59,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
remove(callback) {
remove(callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.removeById(this._id, callback);
}

Expand All @@ -64,9 +68,9 @@ abstract class Document {
*
* @return {object}
*/
toObject() {
toObject(): T {
const keys = Object.keys(this);
const obj = {};
const obj: Partial<T> = {};

for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
Expand All @@ -75,15 +79,15 @@ abstract class Document {
obj[key] = isGetter(this, key) ? this[key] : cloneDeep(this[key]);
}

return obj;
return obj as T;
}

/**
* Returns a string representing the document.
*
* @return {String}
*/
toString() {
toString(): string {
return JSON.stringify(this);
}

Expand All @@ -93,13 +97,13 @@ abstract class Document {
* @param {String|Object} expr
* @return {Document}
*/
populate(expr) {
populate(expr: string | any[] | { path?: string; model?: any; [key: PropertyKey]: any }): Document<T> {
const stack = this._schema._parsePopulate(expr);
return this._model._populate(this, stack);
}
}

function isGetter(obj, key) {
function isGetter(obj: any, key: PropertyKey): any {
return Object.getOwnPropertyDescriptor(obj, key).get;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/jsonstream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Parser from 'jsonparse';
* @param {*} y
* @returns {boolean}
*/
const check = (x, y) => {
const check = (x, y): boolean => {
if (typeof x === 'string') {
return y === x;
}
Expand All @@ -27,7 +27,7 @@ const check = (x, y) => {
return false;
};

export function parse(path, map = null) {
export function parse(path: string | any[], map = null) {
let header, footer;

const parser = new Parser();
Expand Down
Loading

0 comments on commit 2209f06

Please sign in to comment.