Skip to content

Commit 64d795e

Browse files
committed
BAAS-33449: add boilerplate snippets
1 parent 2fa9f62 commit 64d795e

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{
44
"metadata": "/snippets/functions/third-party/manifest.json"
55
},
6+
{
7+
"metadata": "/snippets/functions/boilerplate/manifest.json"
8+
},
69
{
710
"metadata": "/snippets/functions/mongodb-crud/manifest.json"
811
},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
exports = async function (changeEvent) {
2+
// A Database Trigger will always call a function with a changeEvent.
3+
// Documentation on ChangeEvents: https://www.mongodb.com/docs/manual/reference/change-events
4+
5+
// This sample function will listen for events and replicate them to a collection in a different Database
6+
7+
// Access the _id of the changed document:
8+
const docId = changeEvent.documentKey._id;
9+
10+
// Get the MongoDB service you want to use (see "Linked Data Sources" tab)
11+
const serviceName = "mongodb-atlas";
12+
const databaseName = "other_db_name";
13+
const collection = context.services
14+
.get(serviceName)
15+
.db(databaseName)
16+
.collection(changeEvent.ns.coll);
17+
18+
// Get the "FullDocument" present in the Insert/Replace/Update ChangeEvents
19+
try {
20+
// If this is a "delete" event, delete the document in the other collection
21+
if (changeEvent.operationType === "delete") {
22+
await collection.deleteOne({ _id: docId });
23+
}
24+
25+
// If this is an "insert" event, insert the document into the other collection
26+
else if (changeEvent.operationType === "insert") {
27+
await collection.insertOne(changeEvent.fullDocument);
28+
}
29+
30+
// If this is an "update" or "replace" event, then replace the document in the other collection
31+
else if (
32+
changeEvent.operationType === "update" ||
33+
changeEvent.operationType === "replace"
34+
) {
35+
await collection.replaceOne({ _id: docId }, changeEvent.fullDocument);
36+
}
37+
} catch (err) {
38+
console.log("error performing mongodb write: ", err.message);
39+
}
40+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
exports = async function (error, changeEvent) {
2+
// This sample function will log additional details if the error is not
3+
// a DOCUMENT_TOO_LARGE error
4+
if (error.code === "DOCUMENT_TOO_LARGE") {
5+
console.log("Document too large error");
6+
7+
// Comment out the line below in order to skip this event and not suspend the Trigger
8+
throw new Error(`Encountered error: ${error.code}`);
9+
}
10+
11+
console.log("Error sending event to EventBridge");
12+
console.log(`DB: ${changeEvent.ns.db}`);
13+
console.log(`Collection: ${changeEvent.ns.coll}`);
14+
console.log(`Operation type: ${changeEvent.operationType}`);
15+
16+
// Throw an error in your function to suspend the trigger and stop processing additional events
17+
throw new Error(`Encountered error: ${error.message}`);
18+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
exports = async function(arg){
2+
// This default function will get a value and find a document in MongoDB
3+
// To see plenty more examples of what you can do with functions see:
4+
// https://www.mongodb.com/docs/atlas/app-services/functions/
5+
6+
7+
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
8+
var serviceName = "mongodb-atlas";
9+
10+
11+
// Update these to reflect your db/collection
12+
var dbName = "db_name";
13+
var collName = "coll_name";
14+
15+
16+
// Get a collection from the context
17+
var collection = context.services.get(serviceName).db(dbName).collection(collName);
18+
19+
20+
var findResult;
21+
try {
22+
// Get a value from the context (see "Values" tab)
23+
// Update this to reflect your value's name.
24+
var valueName = "value_name";
25+
var value = context.values.get(valueName);
26+
27+
28+
// Execute a FindOne in MongoDB
29+
findResult = await collection.findOne(
30+
{ owner_id: context.user.id, "fieldName": value, "argField": arg},
31+
);
32+
33+
34+
} catch(err) {
35+
console.log("Error occurred while executing findOne:", err.message);
36+
37+
38+
return { error: err.message };
39+
}
40+
41+
42+
// To call other named functions:
43+
// var result = context.functions.execute("function_name", arg1, arg2);
44+
45+
46+
return { result: findResult };
47+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"category": "Boilerplate Template by Function Type",
3+
"categoryId": "4756072f-b3c7-4469-b038-cdf6b346789c",
4+
"viewType": "functionSnippet",
5+
"snippets": [
6+
{
7+
"id": "7c84ed29-b8b2-466c-af3d-c240889d3112",
8+
"title": "Database Trigger",
9+
"snippet": "/databaseTrigger.js",
10+
"description": "Example shows a template for a DB trigger function, which always accepts a changeEvent in the header."
11+
},
12+
{
13+
"id": "ba6113d7-5e52-48e4-8ab4-03df30c0d61a",
14+
"title": "Scheduled Trigger",
15+
"snippet": "/scheduledTrigger.js",
16+
"description": "Shows an example of a Scheduled Trigger function, which will always call a function without arguments."
17+
},
18+
{
19+
"id": "1dca4a58-c150-4a8e-b602-9edf5c338f3d",
20+
"title": "General Function",
21+
"snippet": "/generalFunction.js",
22+
"description": "Example of a default function that will get a value and find a document in MongoDB, the parameters passed in the header differs from other templates."
23+
},
24+
{
25+
"id": "b43b9abb-dde7-4389-880e-38ef995d501a",
26+
"title": "Eventbridge Custom Error Function",
27+
"snippet": "/eventbridgeErrorFunction.js",
28+
"description": "Example of a custom error function, which takes in the error and changeEvent as arguments."
29+
}
30+
]
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exports = async function () {
2+
// A Scheduled Trigger will always call a function without arguments.
3+
// Documentation on Triggers: https://www.mongodb.com/docs/atlas/app-services/triggers/
4+
5+
// Functions run by Triggers are run as System users and have full access to Services, Functions, and MongoDB Data.
6+
7+
// Get the MongoDB service you want to use (see "Linked Data Sources" tab)
8+
const serviceName = "mongodb-atlas";
9+
const databaseName = "db_name";
10+
const collectionName = "coll_name";
11+
const collection = context.services
12+
.get(serviceName)
13+
.db(databaseName)
14+
.collection(collectionName);
15+
16+
try {
17+
const doc = await collection.findOne({ name: "mongodb" });
18+
} catch (err) {
19+
console.log("error performing mongodb findOne: ", err.message);
20+
}
21+
};

0 commit comments

Comments
 (0)