|
@@ -1,4 +1,4 @@
|
|
|
-import { Db, Cursor, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId, UpdateWriteOpResult, WriteOpResult } from 'mongodb';
|
|
|
|
|
|
|
+import { Db, Cursor, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId, UpdateWriteOpResult, WriteOpResult, CollectionAggregationOptions, AggregationCursorResult } from 'mongodb';
|
|
|
|
|
|
|
|
export interface IDDocument {
|
|
export interface IDDocument {
|
|
|
_id: ObjectID
|
|
_id: ObjectID
|
|
@@ -12,11 +12,12 @@ export interface IMongoService {
|
|
|
findOne<T>(query: FilterQuery<T>): Promise<IDBDocument<T>|null>,
|
|
findOne<T>(query: FilterQuery<T>): Promise<IDBDocument<T>|null>,
|
|
|
findOneById<T>(idParam: string): Promise<IDBDocument<T>|null>,
|
|
findOneById<T>(idParam: string): Promise<IDBDocument<T>|null>,
|
|
|
find<T>(query: FilterQuery<T>, optionalQuery?: FilterQuery<T>): Promise<IDBDocument<T>[]>,
|
|
find<T>(query: FilterQuery<T>, optionalQuery?: FilterQuery<T>): Promise<IDBDocument<T>[]>,
|
|
|
- update<T>(id: string, data: T): Promise<WriteOpResult>
|
|
|
|
|
|
|
+ update<T>(id: string, data: T): Promise<WriteOpResult>,
|
|
|
|
|
+ aggregate<T>(pipeline: Object[]): Promise<T[]>
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export const mongoService = (db: Db) => (col: string): IMongoService => {
|
|
export const mongoService = (db: Db) => (col: string): IMongoService => {
|
|
|
- const collection:Collection = db.collection(col);
|
|
|
|
|
|
|
+ const collection: Collection = db.collection(col);
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
insertOne: <T>(data: T): Promise<IDBDocument<T>> => collection.insertOne(data).then(insertOneResult => insertOneResult.ops[0]),
|
|
insertOne: <T>(data: T): Promise<IDBDocument<T>> => collection.insertOne(data).then(insertOneResult => insertOneResult.ops[0]),
|
|
@@ -24,7 +25,8 @@ export const mongoService = (db: Db) => (col: string): IMongoService => {
|
|
|
findOne: <T>(query: FilterQuery<T>) => collection.findOne(query),
|
|
findOne: <T>(query: FilterQuery<T>) => collection.findOne(query),
|
|
|
findOneById: (idParam: string) => collection.findOne({ _id: new ObjectId(idParam) }),
|
|
findOneById: (idParam: string) => collection.findOne({ _id: new ObjectId(idParam) }),
|
|
|
find: <T>(query: FilterQuery<T>, optionalQuery: FilterQuery<T> = {} ): Promise<IDBDocument<T>[]> => collection.find(query, optionalQuery).toArray(),
|
|
find: <T>(query: FilterQuery<T>, optionalQuery: FilterQuery<T> = {} ): Promise<IDBDocument<T>[]> => collection.find(query, optionalQuery).toArray(),
|
|
|
- update: <T>(id: string, data: T) => collection.update({_id: new ObjectId(id)}, data)
|
|
|
|
|
|
|
+ update: <T>(id: string, data: T) => collection.update({_id: new ObjectId(id)}, data),
|
|
|
|
|
+ aggregate: <T>(pipeline: Object[]) => collection.aggregate<T>(pipeline).toArray()
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|