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

refactor: refactor types #230

Merged
merged 7 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 7 additions & 6 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 { 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>;
Model: typeof Model;

/**
Expand Down Expand Up @@ -117,7 +118,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name: string, schema?: any) {
model(name: string, schema?: Schema | object): Model {
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> } {
const models = Object.keys(this._models)
.reduce((obj, key) => {
const value = this._models[key];
Expand Down
28 changes: 16 additions & 12 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 _model: Model;
_id!: string | number | undefined;
abstract _schema: Schema;
[key: PropertyKey]: any;

/**
* Document constructor.
*
* @param {object} data
*/
constructor(data) {
constructor(data?: object) {
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: Record<string, any>, 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: object | Document, 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,7 +68,7 @@ abstract class Document {
*
* @return {object}
*/
toObject() {
toObject(): object {
const keys = Object.keys(this);
const obj = {};

Expand All @@ -83,7 +87,7 @@ abstract class 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 {
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