npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@plugcore/ds-mongodb

v1.4.9-3

Published

plugcore.com Mongodb datasource

Downloads

310

Readme

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:[email protected]: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 });
    }
 
}