|
@@ -1,4 +1,4 @@
|
|
|
-import { Db, Cursor, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId, UpdateWriteOpResult, WriteOpResult, CollectionAggregationOptions, AggregationCursorResult } from 'mongodb';
|
|
|
|
|
|
|
+import { Db, Collection, ObjectID, FilterQuery, ObjectId, WriteOpResult, FindOneOptions } from 'mongodb';
|
|
|
|
|
|
|
|
export interface IDDocument {
|
|
export interface IDDocument {
|
|
|
_id: ObjectID
|
|
_id: ObjectID
|
|
@@ -11,7 +11,7 @@ export interface IMongoService {
|
|
|
insertMany<T>(data: T[]): Promise<IDBDocument<T>[]|any[]>,
|
|
insertMany<T>(data: T[]): Promise<IDBDocument<T>[]|any[]>,
|
|
|
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?: FindOneOptions): 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[]>
|
|
aggregate<T>(pipeline: Object[]): Promise<T[]>
|
|
|
}
|
|
}
|
|
@@ -20,13 +20,13 @@ 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]),
|
|
|
|
|
- insertMany: <T>(data: T[]): Promise<any[]|IDBDocument<T>[]> => collection.insertMany(data).then(insertManyResult => insertManyResult.ops),
|
|
|
|
|
- findOne: <T>(query: FilterQuery<T>) => collection.findOne(query),
|
|
|
|
|
|
|
+ insertOne: data => collection.insertOne(data).then(insertOneResult => insertOneResult.ops[0]),
|
|
|
|
|
+ insertMany: data => collection.insertMany(data).then(insertManyResult => insertManyResult.ops),
|
|
|
|
|
+ findOne: query => 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(),
|
|
|
|
|
- update: <T>(id: string, data: T) => collection.update({_id: new ObjectId(id)}, data),
|
|
|
|
|
- aggregate: <T>(pipeline: Object[]) => collection.aggregate<T>(pipeline).toArray()
|
|
|
|
|
|
|
+ find: (query, optionalQuery) => collection.find(query, optionalQuery).toArray(),
|
|
|
|
|
+ update: (id, data) => collection.update({_id: new ObjectId(id)}, data),
|
|
|
|
|
+ aggregate: pipeline => collection.aggregate(pipeline).toArray()
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|