
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
@storyous/mongodb
Advanced tools
mongodbimport { 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();
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');
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);
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);
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;
});
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);
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
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.
FAQs
## Usage of MongoClient
The npm package @storyous/mongodb receives a total of 347 weekly downloads. As such, @storyous/mongodb popularity was classified as not popular.
We found that @storyous/mongodb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.