Skip to content

Commit

Permalink
Merge branch 'master' into refactor/type
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jan 11, 2025
2 parents 87413b5 + ca7f851 commit 4be4b07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/node": "^18.11.7",
"@types/sinon": "^17.0.3",
"@types/through2": "^2.0.36",
"c8": "^9.0.0",
"c8": "^10.1.3",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"eslint": "^8.26.0",
Expand Down
35 changes: 27 additions & 8 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Model<T> extends EventEmitter {
_database: Database;
[key : string]: any;

_dataKeys: string[] = [];
dirty = false;

/**
* Model constructor.
*
Expand Down Expand Up @@ -83,6 +86,17 @@ class Model<T> extends EventEmitter {
Object.assign(_Document.prototype, schema.methods);
}

/**
* Returns the cached data keys.
*/
get dataKeys(): string[] {
if (this.dirty) {
this._dataKeys = Object.keys(this.data);
this.dirty = false;
}
return this._dataKeys;
}

/**
* Creates a new document.
*
Expand Down Expand Up @@ -174,6 +188,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'save', data).then(data => {
// Insert data
this.dirty = true;
this.data[id] = result;
this.length++;

Expand Down Expand Up @@ -264,6 +279,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'save', doc).then(data => {
// Update data
this.dirty = true;
this.data[id] = result;

this.emit('update', data);
Expand Down Expand Up @@ -325,6 +341,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'save', data).then(data => {
// Replace data
this.dirty = true;
this.data[id] = result;

this.emit('update', data);
Expand Down Expand Up @@ -375,6 +392,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'remove', data).then(data => {
// Remove data
this.dirty = true;
this.data[id] = null;
this.length--;

Expand Down Expand Up @@ -433,7 +451,7 @@ class Model<T> extends EventEmitter {
forEach(iterator: (value: Document<T>, index: number) => void, options: Partial<Omit<Options, 'lean'>> & { lean?: undefined }): void;
forEach(iterator: ((value: T, index: number) => void) | ((value: Document<T>, index: number) => void), options: Partial<Options>): void;
forEach(iterator: ((value: T, index: number) => void) | ((value: Document<T>, index: number) => void), options?: Partial<Options>): void {
const keys = Object.keys(this.data);
const keys = this.dataKeys;
let num = 0;

for (let i = 0, len = keys.length; i < len; i++) {
Expand Down Expand Up @@ -480,7 +498,7 @@ class Model<T> extends EventEmitter {
find(query: object, options: Partial<Options>): Query<T> | T[];
find(query: object, options: Partial<Options> = {}): Query<T> | T[] {
const filter = this.schema._execQuery(query);
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;
let limit = options.limit || this.length;
let skip = options.skip;
Expand Down Expand Up @@ -558,7 +576,7 @@ class Model<T> extends EventEmitter {
eq(i_: number, options?: Partial<Options>): Document<T> | T {
let index = i_ < 0 ? this.length + i_ : i_;
const data = this.data;
const keys = Object.keys(data);
const keys = this.dataKeys;

for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
Expand Down Expand Up @@ -626,7 +644,7 @@ class Model<T> extends EventEmitter {
if (!len) return new this.Query([]);

const arr = new Array(len);
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const keysLen = keys.length;
let num = 0;

Expand Down Expand Up @@ -696,7 +714,7 @@ class Model<T> extends EventEmitter {
map<R>(iterator: ((value: T, index: number) => R) | ((value: Document<T>, index: number) => R), options: Partial<Options>): R[];
map<R>(iterator: ((value: T, index: number) => R) | ((value: Document<T>, index: number) => R), options?: Partial<Options>): R[] {
const result = new Array(this.length);
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;

for (let i = 0, num = 0; i < len; i++) {
Expand Down Expand Up @@ -797,7 +815,7 @@ class Model<T> extends EventEmitter {
* @return {Boolean}
*/
every(iterator: (value: Document<T>, index: number) => boolean): boolean {
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;
let num = 0;

Expand All @@ -822,7 +840,7 @@ class Model<T> extends EventEmitter {
* @return {Boolean}
*/
some(iterator: (value: Document<T>, index: number) => boolean): boolean {
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;
let num = 0;

Expand Down Expand Up @@ -975,6 +993,7 @@ class Model<T> extends EventEmitter {

for (let i = 0; i < len; i++) {
const item = arr[i];
this.dirty = true;
data[item._id] = schema._parseDatabase(item) as T;
}

Expand All @@ -994,7 +1013,7 @@ class Model<T> extends EventEmitter {
toJSON(): any[] {
const result = new Array(this.length);
const { data, schema } = this;
const keys = Object.keys(data);
const keys = this.dataKeys;
const { length } = keys;

for (let i = 0, num = 0; i < length; i++) {
Expand Down

0 comments on commit 4be4b07

Please sign in to comment.