Skip to content

Commit be56f0b

Browse files
authored
Merge branch 'development' into fix-prod-workflow
2 parents d0f352d + c7d2aa0 commit be56f0b

File tree

8 files changed

+162
-7
lines changed

8 files changed

+162
-7
lines changed

manifest.json

Lines changed: 4 additions & 1 deletion
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
},
@@ -19,5 +22,5 @@
1922
"metadata": "/snippets/triggers/project/manifest.json"
2023
}
2124
],
22-
"version": "33962567db27d1f4ec53ce4b3813b0dc8754f632"
25+
"version": "7bd6521bcefa9ffb7dcfb7ad653044950d6027d1"
2326
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
7+
const serviceName = "mongodb-atlas";
8+
9+
// Update these to reflect your db/collection
10+
const dbName = "db_name";
11+
const collName = "coll_name";
12+
13+
// Get a collection from the context
14+
const collection = context.services
15+
.get(serviceName)
16+
.db(dbName)
17+
.collection(collName);
18+
19+
let findResult;
20+
try {
21+
// Get a value from the context (see "Values" tab)
22+
// Update this to reflect your value's name.
23+
const valueName = "value_name";
24+
const value = context.values.get(valueName);
25+
26+
// Execute a FindOne in MongoDB
27+
findResult = await collection.findOne({
28+
owner_id: context.user.id,
29+
fieldName: value,
30+
argField: arg,
31+
});
32+
} catch (err) {
33+
console.log("Error occurred while executing findOne:", err.message);
34+
35+
return { error: err.message };
36+
}
37+
38+
// To call other named functions:
39+
// const result = context.functions.execute("function_name", arg1, arg2);
40+
41+
return { result: findResult };
42+
};
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 as an argument."
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 differ 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+
};

snippets/triggers/match/manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
"snippets": [
66
{
77
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a081",
8-
"title": "Updates",
8+
"title": "Update",
99
"snippet": "/update.json",
1010
"description": "This trigger will only execute if the `storeLocation` array has changed to \"East Langley\" and the \"storeItems\" field has been removed."
1111
},
1212
{
1313
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a082",
14-
"title": "Deletes",
14+
"title": "Delete",
1515
"snippet": "/delete.json",
1616
"description": "This trigger will only execute if the document (before deletion) has a userName of \"Alice Smith\""
1717
},
1818
{
1919
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a083",
20-
"title": "Inserts",
20+
"title": "Insert",
2121
"snippet": "/insert.json",
2222
"description": "This trigger will only execute if the document has a userName of \"Alice Smith\" and a department of \"engineering\""
2323
},

snippets/triggers/project/manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
"snippets": [
66
{
77
"id": "7dd66555-8efb-4a76-8920-64c0874e0214",
8-
"title": "Updates",
8+
"title": "Update",
99
"snippet": "/update.json",
1010
"description": "This trigger will return the \"operationType\" and \"updateDescription.updateFields.storeLocation\" field values."
1111
},
1212
{
1313
"id": "7c9b14f4-30ec-4865-b153-8bd52c6566ad",
14-
"title": "Deletes",
14+
"title": "Delete",
1515
"snippet": "/delete.json",
1616
"description": "This trigger will return the \"documentKey._id\", \"operationType\", and \"wallTime\" field values."
1717
},
1818
{
1919
"id": "e040947b-08a4-4a8f-9f82-1bccd7826892",
20-
"title": "Inserts",
20+
"title": "Insert",
2121
"snippet": "/insert.json",
2222
"description": "This trigger will return the \"baseSalary\", \"department\", \"operationType\", and \"documentKey.userName\" field values."
2323
},

0 commit comments

Comments
 (0)