You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Automatically starts/stops a local MongoDB instance
Automatically downloads MongoDB binaries on first use
Can be used as a local equivalent to DocumentDB/CosmosDB
Install
npm i -D serverless-mongodb-local
Usage
In serverless.yaml
plugins:
- serverless-mongodb-localcustom:
mongodb:
stages: # If you only want to use MongoDB Local in particular stages, declare them here as a string or regex
- dev
- 'dev-\d+'instance: # MongoMemoryServer() options and defaults https://github.com/nodkz/mongodb-memory-server#available-options-for-mongomemoryserverport: 1234dbName: MyDBdbPath: ./dbstorageEngine: wiredTiger # Set with `dbPath` to persists the database between instantiationsseed:
auto: truedataPath: ./test/data
In your handlers
const{ MongoClient }=require('mongodb');constclient=awaitMongoClient.connect(process.env.SLS_MONGODB_URI,// Provided as a convenience when using the plugin (local only, requires --localEnvironment flag to be set on the command line) {useUnifiedTopology: true});
Seeding data
By setting a mongodb.seed.dataPath any .json files in the folder will be imported as collections. The name of file being the name of the collection. The file should be an array of documents to load into the collection.