4.2.36 • Published 2 years ago

@jorgelg/mongodb v4.2.36

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
2 years ago

title: 'MongoDB Database Integration'

excerpt: 'The MongoDB Jovo integration allows you to store user specific data and more in a MongoDB database.'

MongoDB Database Integration

This database integration allows you to store user specific data in a MongoDB table.

Introduction

MongoDB is ... Shared Atlas clusters. Easy config.

Installation

You can install the plugin like this:

$ npm install @jovotech/db-mongodb

Add it as plugin to any stage you like, e.g. app.prod.ts:

import { MongoDb } from '@jovotech/db-mongodb';

// ...

app.configure({
  plugins: [
    MongoDb.instance({
      // Configuration
    }),
    // ...
  ],
});

Once the configuration is done, the MongoDB database integration will create a MongoDB database and a collection on the first read/write attempt (might take some seconds). No need for you to create the database.

The rest of this section provides an introduction to the steps you need to take depending on where you host your Jovo app:

The configuration section then provides a detailed overview of all configuration options.

Configuration

The following configurations can be added:

MongoDb.instance({
  connectionString: string;
  databaseName?: string;
  collectionName?: string;
} as MongoDbConfig),
  • MongoDbConfig:
  • connectionString: Specify username, password and clusterUrl. Additional parameters can also be added. Have a look at the MongoDB documentation for more details.
  • databaseName: The name of the database we want to use. If not provided, use database name from connection string. A new database is created if doesn't exist yet.
  • collectionName: A new collection is created with that name if doesn't exist yet.

You should specify a dataBase either in connectionString or in databaseName. The second takes precedence. The default collection name is users_all.

Note that you can add timeout or other configuration by adding parameters in the connectionString.

Examples of use

For better performance, you can reuse the connection in any component like in this example:

async START() {
    // You can read data from another user
    const users = mongoDb.jovoUsers();
    const otherUserData = (await users).find({ _id: '<another_id>' });

    // Also store documents in other collections in the same DB Jovo handles users
    const defaultDb = await mongoDb.jovoDb();
    await defaultDb.collection('my-collection').insertOne({ foo: 'bar' });

    // Or just get the single client to open a transaction
    const client = await mongoDb.client;
    const transactionResults = await client.startSession().withTransaction(async () => {
      //  Modify some data
      // ....
    });

    // Or create a new DB
    const newDb = client.db("my_new_db");
}

For the next example, although Jovo will persist it asynchronously during the lifecycle, it will use the same connection pool from the examples above. Reusing the client will get better response times and lower costs.

this.$user.data.foo = 'bar';
4.2.36

2 years ago

4.2.35

2 years ago

4.2.34

2 years ago

4.2.33

2 years ago

4.2.32

2 years ago

4.2.30

2 years ago

4.2.29

2 years ago

4.2.28

2 years ago

4.2.27

2 years ago

4.2.26

2 years ago

4.2.25

2 years ago

4.2.24

2 years ago

4.2.23

2 years ago

4.2.22

2 years ago

4.2.21

2 years ago

4.2.20

2 years ago

4.2.19

2 years ago

4.2.18

2 years ago

4.2.17

2 years ago

4.2.16

2 years ago

4.2.15

2 years ago

4.2.14

2 years ago

4.2.13

2 years ago

4.2.12

2 years ago

4.2.11

2 years ago

4.2.10

2 years ago

4.2.9

2 years ago

4.2.8

2 years ago

4.2.7

2 years ago

4.2.6

2 years ago

4.2.5

2 years ago

4.2.4

2 years ago

4.2.3

2 years ago

4.2.2

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago