Skip to content

Commit

Permalink
refactor: refactor types (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon authored May 4, 2024
1 parent 2c89f53 commit 83df5a1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name: string, schema?: Schema | Record<string, AddSchemaTypeOptions>): Model<any> {
model<T = any>(name: string, schema?: Schema<T> | Record<string, AddSchemaTypeOptions>): Model<any> {
if (this._models[name]) {
return this._models[name];
}
Expand Down
2 changes: 1 addition & 1 deletion src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const cloneDeep = rfdc();
abstract class Document<T> {
abstract _model: Model<T>;
_id!: string | number | undefined;
abstract _schema: Schema;
abstract _schema: Schema<T>;
[key : string]: any;

/**
Expand Down
12 changes: 6 additions & 6 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { AddSchemaTypeOptions, NodeJSLikeCallback, Options, PopulateResult
class Model<T> extends EventEmitter {
_mutex = new Mutex();
data: Record<PropertyKey, T> = {};
schema: Schema;
schema: Schema<T>;
length = 0;
Document;
Query;
Expand All @@ -28,10 +28,10 @@ class Model<T> extends EventEmitter {
* @param {string} name Model name
* @param {Schema|object} [schema_] Schema
*/
constructor(public name: string, schema_: Schema | Record<string, AddSchemaTypeOptions>) {
constructor(public name: string, schema_: Schema<T> | Record<string, AddSchemaTypeOptions>) {
super();

let schema: Schema;
let schema: Schema<T>;

// Define schema
if (schema_ instanceof Schema) {
Expand All @@ -51,7 +51,7 @@ class Model<T> extends EventEmitter {

class _Document<T> extends Document<T> {
_model!: Model<T>;
_schema!: Schema;
_schema!: Schema<T>;
constructor(data: T) {
super(data);

Expand All @@ -67,7 +67,7 @@ class Model<T> extends EventEmitter {

class _Query<T> extends Query<T> {
_model!: Model<T>;
_schema!: Schema;
_schema!: Schema<T>;
}

this.Query = _Query;
Expand Down Expand Up @@ -963,7 +963,7 @@ class Model<T> extends EventEmitter {

Model.prototype.get = Model.prototype.findById;

function execHooks(schema: Schema, type: string, event: string, data: any): BluebirdPromise<any> {
function execHooks<T>(schema: Schema<T>, type: string, event: string, data: any): BluebirdPromise<any> {
const hooks = schema.hooks[type][event] as ((data: any) => BluebirdPromise<void> | void)[];
if (!hooks.length) return BluebirdPromise.resolve(data);

Expand Down
2 changes: 1 addition & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class Query<T> {
data: Document<T>[];
length: number;
abstract _model: Model<T>;
abstract _schema: Schema;
abstract _schema: Schema<T>;

/**
* Query constructor.
Expand Down
14 changes: 8 additions & 6 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import PopulationError from './error/population';
import SchemaTypeVirtual from './types/virtual';
import { isPlainObject } from 'is-plain-object';
import type { AddSchemaTypeLoopOptions, AddSchemaTypeOptions, PopulateResult, SchemaTypeOptions } from './types';
import type Model from './model';
import type Document from './document';

/**
* @callback queryFilterCallback
Expand Down Expand Up @@ -379,10 +381,10 @@ class QueryParser {
}


class Schema {
class Schema<T = any> {
paths: Record<string, SchemaType<any>> = {};
statics: Record<string, (...args: any[]) => any> = {};
methods: Record<string, (...args: any[]) => any> = {};
statics: Record<string, (this: Model<T>, ...args: any[]) => any> = {};
methods: Record<string, (this: T, ...args: any[]) => any> = {};
hooks: {
pre: {
save: ((...args: any[]) => BluebirdPromise<any>)[]
Expand Down Expand Up @@ -557,7 +559,7 @@ class Schema {
* @param {Function} [getter]
* @return {SchemaType.Virtual}
*/
virtual(name: string, getter?: () => any): SchemaTypeVirtual {
virtual(name: string, getter?: (this: T) => any): SchemaTypeVirtual<T> {
const virtual = new Types.Virtual(name, {});
if (getter) virtual.get(getter);

Expand Down Expand Up @@ -598,7 +600,7 @@ class Schema {
* @param {String} name
* @param {Function} fn
*/
method(name: string, fn: (...args: any[]) => any) {
method(name: string, fn: (this: T, ...args: any[]) => any) {
if (!name) throw new TypeError('Method name is required!');

if (typeof fn !== 'function') {
Expand All @@ -614,7 +616,7 @@ class Schema {
* @param {String} name
* @param {Function} fn
*/
static(name: string, fn: (...args: any[]) => any) {
static(name: string, fn: (this: Model<T>, ...args: any[]) => any) {
if (!name) throw new TypeError('Method name is required!');

if (typeof fn !== 'function') {
Expand Down
8 changes: 4 additions & 4 deletions src/types/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { setGetter } from '../util';
/**
* Virtual schema type.
*/
class SchemaTypeVirtual extends SchemaType<any> {
getter: (() => any) | undefined;
class SchemaTypeVirtual<T = any> extends SchemaType<any> {
getter: ((this: T) => any) | undefined;
setter: ((value: any) => void) | undefined;

/**
Expand All @@ -14,7 +14,7 @@ class SchemaTypeVirtual extends SchemaType<any> {
* @param {Function} fn
* @chainable
*/
get(fn: () => any): SchemaTypeVirtual {
get(fn: (this: T) => any): SchemaTypeVirtual<T> {
if (typeof fn !== 'function') {
throw new TypeError('Getter must be a function!');
}
Expand All @@ -30,7 +30,7 @@ class SchemaTypeVirtual extends SchemaType<any> {
* @param {Function} fn
* @chainable
*/
set(fn: (value: any) => void): SchemaTypeVirtual {
set(fn: (this: T, value: any) => void): SchemaTypeVirtual<T> {
if (typeof fn !== 'function') {
throw new TypeError('Setter must be a function!');
}
Expand Down
10 changes: 9 additions & 1 deletion test/scripts/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ describe('Model', () => {

const db = new Database();

const userSchema = new Schema({
const userSchema = new Schema<{
name: {
first: string;
last: string;
}
email: string;
age: number;
posts: string[];
}>({
name: {
first: String,
last: String
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/types/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const should = chai.should(); // eslint-disable-line
import SchemaTypeVirtual from '../../../dist/types/virtual';

describe('SchemaTypeVirtual', () => {
const type = new SchemaTypeVirtual('test');
const type = new SchemaTypeVirtual<any>('test');

it('get()', () => {
const getter = () => 'foo';
Expand Down

0 comments on commit 83df5a1

Please sign in to comment.