1.8.1 • Published 1 month ago

@storyous/mongodb v1.8.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

mongodb

Usage of MongoClient

import { mongoClient } from '@storyous/mongodb';

// Initialize and connect MongoDB
mongoClient.init('mongodb://127.0.0.1:27017');
await mongoClient.getClient().connect();

// Useage of MongoClient
const user = mongoClient.getClient().db('test').collection('users').find();

// Close current connection
await mongoClient.getClient().close();

Usage of getMongoConfig

import { getMongoConfig, mongoClient } from '@storyous/mongodb';

// Set following env variables:
// DOCUMENTDB_ENGINE="mongodb" (or "documentdb")
// DOCUMENTDB_ENDPOINT="127.0.0.1"
// DOCUMENTDB_USER="user"
// DOCUMENTDB_PASSWORD="password"
//
// Optional env variables (and default values)
// DOCUMENTDB_DATABASE=""
// DOCUMENTDB_PORT="27017"
// DOCUMENTDB_PROTOCOL="mongodb"
// DOCUMENTDB_QUERY="?retryWrites=false"

// Get config
const config = {
    database: {
        ...getMongoConfig(),
    }
};

// Initialize and connect MongoDB
mongoClient.init(config.database.mongodbUrl, config.database.mongoOptions);

You can also use custom prefix for environment variables (e.g. MY_DB_ENGINE, MY_DB_ENDPOINT, ...):

getMongoConfig('MY_DB');

Usage of MongoLocker

import { getMongoLocker, mongoLocker } from '@storyous/mongodb';

// default mongoLocker instance
const locker = mongoLocker();
const callback = () => {}
const result1 = await locker('someKey', callback);

// custom mongoLocker instance
const locker = getMongoLocker('nameOfMyCollecrion');
const callback = () => {}
const result1 = await locker('someKey', callback);

Usage of Fixtures

import { fixtures, mongoClient } from '@storyous/mongodb';

const collection = mongoClient.getClient().db().collection('currency');
const currencyFixtures = [
    { _id: new ObjectId(), name: 'CZK' },
    { _id: new ObjectId(), name: 'EUR' },
];
await fixtures.ensureInCollection(currencyFixtures, collection);

Usage of WithTransaction

import { mongoClient, withTransaction } from '@storyous/mongodb';

const collection = mongoClient.getClient().db().collection('currency');
const returnValue = await withTransaction(async (session) => {
    await testCollection.insertOne({}, { session });
    
    return true;
});

Usage of AppData

import { appData } from '@storyous/mongodb';

const testDocument = { _id: new ObjectId() };
await appData.ensureDocumentExists(testDocument);
await appData.updateDocument(testDocument._id, { $set: { foo: 'bar' } });
await appData.getDocument(testDocument._id);

Usage of Migrations

Generation of new migration

Add this target into your package.json:

"migrate:create": "migrate create --migrations-dir ./migrations --template-file ./node_modules/@storyous/mongodb/lib/migrations/template.ts"

Run target by npm:

npm run migrate:create nameOfMyMigration

Run migration

import { runMigrations } from '@storyous/mongodb';

await runMigrations(createLogger(), `${__dirname}/migrations`, {
     safeMigration: config.mongoSafeMigration
});

note: The "safeMigration" needs to be false on testing and localhost environment. And it should be true on TEST and PROD.

safeMigration: true does not run migration if it does not find any migration data in database or it is trying to run all migrations from beginning.

On first deploy to TEST/PROD you need to setup it to "false". Then it should be changed to "true"

Reason: Once it happened on production that the migrations history were erased for unknown reason and the migration started again - which caused a lot of issues. safeMigration: true should prevent it.

1.8.1

1 month ago

1.7.4

1 month ago

1.7.3

4 months ago

1.7.2

5 months ago

1.7.1

8 months ago

1.7.0

8 months ago

1.6.0

8 months ago

1.5.0

8 months ago

1.4.0

8 months ago

1.2.0

11 months ago

1.3.0

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

1 year ago

1.0.10-beta.1

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.7

1 year ago

1.0.6

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago