Support of MongoDB for Node.js
CAST supports MongoDB via its com.castsoftware.nodejs extension. Details about the support provided for Node.js source code is discussed below.
Supported Libraries
The following libraries are supported:
- MongoDB
- Mongoose
- Prisma
Objects
Icon | Description |
---|---|
Node.js MongoDB connection | |
Node.js unknown MongoDB connection | |
Node.js MongoDB collection |
Supported MongoDB
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useUpdateLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useDeleteLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useSelectLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
Example
Taking the following codes:
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017/myproject';
var insertDocuments = function(db, callback) {
var collection = db.collection('documents');
collection.insertMany([
{a : 1}, {a : 2}, {a : 3}
], function(err, result) {
callback(result);
});
}
var updateDocument = function(db, callback) {
var collection = db.collection('documents');
for (i = 0; i < 2; i += 1) {
collection.updateOne({ a : 2 }
, { $set: { b : 1 } }, function(err, result) {
callback(result);
});
}
}
MongoClient.connect(url, function(err, db) {
insertDocuments(db, function() {
updateDocument(db, function() {
db.close();
});
});
});
In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useInsert’ link from function ‘insertDocuments’ to the collection ‘documents’ and a ‘useUpdate’ link from function ‘updateDocument’ to the collection ‘documents’:
Supported Mongoose
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useUpdateLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useDeleteLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
useSelectLink | Between JavaScript Function (JavaScript Initialisation also) and Node.js MongoDB collection |
|
Example
Taking the following codes:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/analyzerlauncher', function(err) {
if (err) { throw err; }
});
userModel = mongoose.model('users', userSchema);
function find(req,res) {
userModel.findOne(req.params.id, function (err, authorize) {})
}
In this example, a ‘Node.js MongoDB connection’ and a ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useSelect’ link from function ‘find’ to the collection ‘users’: