Skip to content

Mongodb Interface

Connecting to the database

ts
export const db = await urn.db('mongodb://localhost:27017', 'default') // db as mongodb interface

How to use

Import first

ts
import { db } from '../index'

Here are the methods of this class

ts
add(collection: string, object: object, doSanitize: boolean = true): Promise<InsertOneResult>
upd(collection: string, objectID: ObjectId, updateObject: object, doSanitize: boolean = true): Promise<UpdateResult>
del(collection: string, objectID: ObjectId): Promise<DeleteResult>
get<T>(collection: string, queryObject: ObjectAny, optObject?: MongoIntFind): Promise<WithId<T>[] | null[]>
createCollection(collection: string): Promise<Collection<Document>> 
getCollection(): Promise<(CollectionInfo | Pick<CollectionInfo, "name" | "type">)[]>
custom() // This method enables you to interact with the original db object
// example:
// db.custom().dropDatabase()

Update

42.1.3 BREAKING CHANGE!

Now @interface-mongo/get() requires a Type defined and it will returns WithId<T>[] instead of WithId<object>

42.0.3 BREAKING CHANGE!

Use optObj instead of opt args at @interface-mongo/get()

41.1.4

You should use queryObject instead of a single query key when calling get() method

41.1.6

doSanitize all interface input by default, use it out of the interface class with import