|
@@ -1,4 +1,4 @@
|
|
|
-import { Db, connect, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId } from 'mongodb';
|
|
|
|
|
|
|
+import { Db, Cursor, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId } from 'mongodb';
|
|
|
|
|
|
|
|
export interface IDBDocument<T extends {}> {
|
|
export interface IDBDocument<T extends {}> {
|
|
|
_id: ObjectID
|
|
_id: ObjectID
|
|
@@ -7,7 +7,8 @@ export interface IDBDocument<T extends {}> {
|
|
|
export interface IMongoService {
|
|
export interface IMongoService {
|
|
|
insertOne<T>(data: T): Promise<IDBDocument<T>>,
|
|
insertOne<T>(data: T): Promise<IDBDocument<T>>,
|
|
|
findOne<T>(query: FilterQuery<T>): Promise<IDBDocument<T>>,
|
|
findOne<T>(query: FilterQuery<T>): Promise<IDBDocument<T>>,
|
|
|
- findOneById<T>(idParam: string): Promise<IDBDocument<T>>
|
|
|
|
|
|
|
+ findOneById<T>(idParam: string): Promise<IDBDocument<T>>,
|
|
|
|
|
+ find<T>(query: FilterQuery<T>): Promise<IDBDocument<T>[]>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export const mongoService = (db: Db) => (col: string) => {
|
|
export const mongoService = (db: Db) => (col: string) => {
|
|
@@ -17,6 +18,7 @@ export const mongoService = (db: Db) => (col: string) => {
|
|
|
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]),
|
|
|
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>): Promise<IDBDocument<T>[]> => collection.find(query).toArray(),
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|