@plugcore/ds-mongodb
TypeScript icon, indicating that this package has built-in type declarations

1.4.12 • Public • Published

plugcore.com

@plugcore/ds-mongodb

https://nodei.co/npm/@plugcore/ds-mongodb.png?downloads=false&downloadRank=false&stars=false

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.

Configuration

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://myuser:mypassword@mongodb0.example.com:27017/admin", // As defined in https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format
            "databaseName": "mydatabase" // Database name that we want to connect
        },
        ...
    },
    ...
}

Usage

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
}
 
@Service()
// 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>;
 
    constructor(
        // 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.id }, vehicle);
    }
 
    public async remove(id: string) {
        return this.collection.remove({ id });
    }
 
}

Readme

Keywords

none

Package Sidebar

Install

npm i @plugcore/ds-mongodb

Weekly Downloads

1

Version

1.4.12

License

ISC

Unpacked Size

42.5 kB

Total Files

16

Last publish

Collaborators

  • plugcorecom