@helenejs/data
Advanced tools
Comparing version 1.3.0-alpha.24 to 1.3.0-alpha.25
import { Index } from './indexes'; | ||
import { Persistence } from './persistence'; | ||
import { Cursor, Projection, Query } from './cursor'; | ||
import { Cursor, Projection, Query, SortQuery } from './cursor'; | ||
import { EventEmitter2 } from 'eventemitter2'; | ||
@@ -148,3 +148,3 @@ import { IStorage } from './types'; | ||
find(query?: Query, projection?: Projection): Cursor<CT>; | ||
findOne(query: Query, projection?: Projection): Cursor<CT>; | ||
findOne(query: Query, projection?: Projection, sort?: SortQuery): Promise<CT>; | ||
update(query: Query, updateQuery: UpdateQuery, options?: UpdateOptions): Promise<any>; | ||
@@ -151,0 +151,0 @@ updateOne(query: Query, updateQuery: UpdateQuery, options?: Omit<UpdateOptions, 'multi'>): Promise<any>; |
@@ -321,15 +321,12 @@ "use strict"; | ||
find(query, projection) { | ||
const cursor = new cursor_1.Cursor(this, query, async (docs) => docs.map(model_1.deepCopy)); | ||
return cursor.projection(projection); | ||
const cursor = new cursor_1.Cursor(this, query); | ||
if (projection) { | ||
return cursor.projection(projection); | ||
} | ||
return cursor; | ||
} | ||
findOne(query, projection) { | ||
const cursor = new cursor_1.Cursor(this, query, async (docs) => { | ||
if (docs.length === 1) { | ||
return (0, model_1.deepCopy)(docs[0]); | ||
} | ||
else { | ||
return null; | ||
} | ||
}); | ||
return cursor.projection(projection).limit(1); | ||
async findOne(query, projection, sort) { | ||
const cursor = new cursor_1.Cursor(this, query); | ||
const result = await cursor.projection(projection).sort(sort).limit(1); | ||
return result[0] ?? null; | ||
} | ||
@@ -336,0 +333,0 @@ async update(query, updateQuery, options) { |
@@ -11,12 +11,10 @@ import { BaseDocument, Collection } from './collection'; | ||
}; | ||
export type ExecFn = (data: any) => Promise<any>; | ||
export declare class Cursor<T extends BaseDocument = BaseDocument> implements PromiseLike<T[]> { | ||
db: Collection; | ||
query: Query; | ||
execFn: ExecFn; | ||
_limit: number; | ||
_skip: number; | ||
_sort: any; | ||
_projection: any; | ||
constructor(db: Collection, query?: Query, execFn?: ExecFn); | ||
_sort: SortQuery; | ||
_projection: Projection; | ||
constructor(db: Collection, query?: Query); | ||
limit(limit: number): this; | ||
@@ -26,6 +24,6 @@ skip(skip: number): this; | ||
projection(projection: Projection): this; | ||
project(candidates: any[]): any[]; | ||
exec(): Promise<any[]>; | ||
map(fn: any): Promise<any[]>; | ||
then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: any[]) => PromiseLike<TResult1> | TResult1) | undefined | null, onrejected?: ((reason: any) => PromiseLike<TResult2> | TResult2) | undefined | null): PromiseLike<TResult1 | TResult2>; | ||
project(candidates: T[]): any[]; | ||
exec(): Promise<T[]>; | ||
map(fn: (item: T) => Promise<any> | any): Promise<any[]>; | ||
then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: T[]) => PromiseLike<TResult1> | TResult1) | undefined | null, onrejected?: ((reason: any) => PromiseLike<TResult2> | TResult2) | undefined | null): PromiseLike<TResult1 | TResult2>; | ||
} |
@@ -11,8 +11,5 @@ "use strict"; | ||
class Cursor { | ||
constructor(db, query, execFn) { | ||
constructor(db, query) { | ||
this.db = db; | ||
this.query = query || {}; | ||
if (execFn) { | ||
this.execFn = execFn; | ||
} | ||
} | ||
@@ -28,3 +25,5 @@ limit(limit) { | ||
sort(sortQuery) { | ||
this._sort = sortQuery; | ||
if (sortQuery) { | ||
this._sort = sortQuery; | ||
} | ||
return this; | ||
@@ -38,10 +37,9 @@ } | ||
const res = []; | ||
let action; | ||
if ((0, isEmpty_1.default)(this._projection)) { | ||
return candidates; | ||
} | ||
const keepId = this._projection._id !== 0; | ||
const keepId = this._projection._id === 1 || this._projection._id === undefined; | ||
this._projection = (0, omit_1.default)(this._projection, '_id'); | ||
// Check for consistency | ||
const keys = Object.keys(this._projection); | ||
let action = keys.length === 0 ? 1 : undefined; | ||
for (const k of keys) { | ||
@@ -53,31 +51,24 @@ if (action !== undefined && this._projection[k] !== action) { | ||
} | ||
// Do the actual projection | ||
for (const candidate of candidates) { | ||
let toPush; | ||
if (action === 1) { | ||
// pick-type projection | ||
toPush = { $set: {} }; | ||
if (action === 1) { | ||
for (const candidate of candidates) { | ||
const modifier = { $set: {} }; | ||
for (const k of keys) { | ||
const value = (0, model_1.getDotValue)(candidate, k); | ||
if (value !== undefined) { | ||
toPush.$set[k] = value; | ||
modifier.$set[k] = value; | ||
} | ||
} | ||
toPush = (0, model_1.modify)({}, toPush); | ||
res.push((0, model_1.modify)({ | ||
...(keepId && { _id: candidate._id }), | ||
}, modifier)); | ||
} | ||
else { | ||
// omit-type projection | ||
toPush = { $unset: {} }; | ||
} | ||
else if (action === 0) { | ||
for (const candidate of candidates) { | ||
const modifier = { $unset: {} }; | ||
for (const k of keys) { | ||
toPush.$unset[k] = true; | ||
modifier.$unset[k] = true; | ||
} | ||
toPush = (0, model_1.modify)(candidate, toPush); | ||
res.push((0, model_1.modify)(keepId ? candidate : (0, omit_1.default)(candidate, '_id'), modifier)); | ||
} | ||
if (keepId) { | ||
toPush._id = candidate._id; | ||
} | ||
else { | ||
delete toPush._id; | ||
} | ||
res.push(toPush); | ||
} | ||
@@ -137,5 +128,3 @@ return res; | ||
res = self.project(res); | ||
if (this.execFn) { | ||
res = await this.execFn(res); | ||
} | ||
res = res.map(doc => (0, model_1.deepCopy)(doc)); | ||
return res; | ||
@@ -142,0 +131,0 @@ } |
{ | ||
"name": "@helenejs/data", | ||
"version": "1.3.0-alpha.24", | ||
"version": "1.3.0-alpha.25", | ||
"description": "Real-time Web Apps for Node.js", | ||
@@ -50,3 +50,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "ffaefc241becd1d686fdf54d72b9f2e4c5cc3282" | ||
"gitHead": "c16682f26329a2a7080102501dfe03e4bcf988f5" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
234957
3914