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 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
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this can be modified to use the indexes in the generalized type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* 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
Loading