Skip to content

Commit

Permalink
refactor: generic
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Dec 23, 2023
1 parent 2cb780e commit 2762d05
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 213 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
10 changes: 5 additions & 5 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Schema from './schema';
import SchemaType from './schematype';
import WarehouseError from './error';
import { logger } from 'hexo-log';
import type { NodeJSLikeCallback } from './types';
import type { AddSchemaTypeOptions, NodeJSLikeCallback } from './types';

const log = logger();
const pkg = require('../package.json');
Expand Down Expand Up @@ -80,7 +80,7 @@ interface DatabaseOptions {

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

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

this._models = {};

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

this.Model = _Model;

Expand All @@ -118,7 +118,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name: string, schema?: Schema | object): Model {
model(name: string, schema?: Schema | Record<string, AddSchemaTypeOptions>): Model<any> {
if (this._models[name]) {
return this._models[name];
}
Expand Down Expand Up @@ -181,7 +181,7 @@ class Database {
return Bluebird.resolve(exportAsync(this, path)).asCallback(callback);
}

toJSON(): { meta: { version: number, warehouse: string }, models: Record<string, Model> } {
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
20 changes: 10 additions & 10 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import type Schema from './schema';
import type { NodeJSLikeCallback } from './types';
const cloneDeep = rfdc();

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

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

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

Expand All @@ -68,9 +68,9 @@ abstract class Document {
*
* @return {object}
*/
toObject(): object {
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 @@ -79,7 +79,7 @@ abstract class Document {
obj[key] = isGetter(this, key) ? this[key] : cloneDeep(this[key]);
}

return obj;
return obj as T;
}

/**
Expand All @@ -97,7 +97,7 @@ abstract class Document {
* @param {String|Object} expr
* @return {Document}
*/
populate(expr: string | any[] | { path?: string; model?: any; [key: PropertyKey]: any }): Document {
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);
}
Expand Down
Loading

0 comments on commit 2762d05

Please sign in to comment.