@storyous/mongodb
v1.10.0
Published
## Usage of MongoClient
Downloads
109
Keywords
Readme
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.