Nonvoluntary Professional Mangling

    TypeScript icon, indicating that this package has built-in type declarations

    1.2.8 • Public • Published


    Documentation can be found at the wiki.

    Datasource: MongoDB

    This utility will help us connect to a Mongodb server using the datsources system, where very instance of the client will target a specific database.

    Internally it uses the MongoDB connector without any layers on top of it, this means with no other framework, this is because we believe the MongoDB connector is already high level enough and we want to remove as many layers as possible.


    Inside our configuration file, for example {PROJECT_ROOT}/configuration/configuration.json, we will have to add a new entry for each database we want to connect, like in this example:

        "connections": {
            "mymongo": { // Connection id
                "type": "mongodb", // Datasource type
                "url": "mongodb://", // As defined in
                "databaseName": "mydatabase" // Database name that we want to connect


    Once the connection is defined in the configuration we are ready to create a service with the dependency @InjectConnection('mymongo') private mongoDbConnection: MongoDbDatasource that will be able access the MongoDB. From the datasource wi will have to preload which collections we want to use and then with those collection instance we will be able to call all the MongoDB actions such as find, update, etc.

    Basic example:

    // First we should create somewhere an interface that will define the collections object DEFINITION
    interface Vehicle {
        id: string;
        year: number;
        model: string
    // We are going to need to execute some async operations before marking this service as ready,
    // for this we have to implement the OnInit interface
    export class VehicleService implements OnInit { 
        // Here is where we are going to store the collection reference
        private collection: Collection<Vehicle>;
            // Connection to "mymongo"
            @InjectConnection('mymongo') private mongoDbConnection: MongoDbDatasource
        ) {}
        // This method will be executed before marking this service as ready,
        // we could load the collections that we are going to need
        public async onInit() {
            // For the method "getCollection" we can use either a class or a string. 
            // If a class is provided, then the collection will be called as the class, and it will
            // also serve as a type for the collection
            // If a string is provided, then we can use any type we want for the collection
            this.collection = await this.mongoDbConnection.getCollection<Vehicle>('Vehicle');
        // Basic CRUD example
        public async create(vehicle: Vehicle) {
            return this.collection.insert(vehicle);
        public async findById(id: string): Promise<Vehicle | null> {
            return this.collection.findOne({ id });
        public async update(vehicle: Vehicle) {
            return this.collection.updateOne({ id: }, vehicle);
        public async remove(id: string) {
            return this.collection.remove({ id });




    npm i @plugcore/ds-mongodb

    DownloadsWeekly Downloads






    Unpacked Size

    37.6 kB

    Total Files


    Last publish


    • plugcorecom