From 64d795e7f06541214088604f1578f1db332b9ce7 Mon Sep 17 00:00:00 2001 From: Joey Wong Date: Mon, 19 Aug 2024 21:49:14 -0400 Subject: [PATCH 01/13] BAAS-33449: add boilerplate snippets --- manifest.json | 3 ++ .../functions/boilerplate/databaseTrigger.js | 40 ++++++++++++++++ .../boilerplate/eventbridgeErrorFunction.js | 18 +++++++ .../functions/boilerplate/generalFunction.js | 47 +++++++++++++++++++ snippets/functions/boilerplate/manifest.json | 31 ++++++++++++ .../functions/boilerplate/scheduledTrigger.js | 21 +++++++++ 6 files changed, 160 insertions(+) create mode 100644 snippets/functions/boilerplate/databaseTrigger.js create mode 100644 snippets/functions/boilerplate/eventbridgeErrorFunction.js create mode 100644 snippets/functions/boilerplate/generalFunction.js create mode 100644 snippets/functions/boilerplate/manifest.json create mode 100644 snippets/functions/boilerplate/scheduledTrigger.js diff --git a/manifest.json b/manifest.json index 53237de..8eb16ef 100644 --- a/manifest.json +++ b/manifest.json @@ -3,6 +3,9 @@ { "metadata": "/snippets/functions/third-party/manifest.json" }, + { + "metadata": "/snippets/functions/boilerplate/manifest.json" + }, { "metadata": "/snippets/functions/mongodb-crud/manifest.json" }, diff --git a/snippets/functions/boilerplate/databaseTrigger.js b/snippets/functions/boilerplate/databaseTrigger.js new file mode 100644 index 0000000..508921d --- /dev/null +++ b/snippets/functions/boilerplate/databaseTrigger.js @@ -0,0 +1,40 @@ +exports = async function (changeEvent) { + // A Database Trigger will always call a function with a changeEvent. + // Documentation on ChangeEvents: https://www.mongodb.com/docs/manual/reference/change-events + + // This sample function will listen for events and replicate them to a collection in a different Database + + // Access the _id of the changed document: + const docId = changeEvent.documentKey._id; + + // Get the MongoDB service you want to use (see "Linked Data Sources" tab) + const serviceName = "mongodb-atlas"; + const databaseName = "other_db_name"; + const collection = context.services + .get(serviceName) + .db(databaseName) + .collection(changeEvent.ns.coll); + + // Get the "FullDocument" present in the Insert/Replace/Update ChangeEvents + try { + // If this is a "delete" event, delete the document in the other collection + if (changeEvent.operationType === "delete") { + await collection.deleteOne({ _id: docId }); + } + + // If this is an "insert" event, insert the document into the other collection + else if (changeEvent.operationType === "insert") { + await collection.insertOne(changeEvent.fullDocument); + } + + // If this is an "update" or "replace" event, then replace the document in the other collection + else if ( + changeEvent.operationType === "update" || + changeEvent.operationType === "replace" + ) { + await collection.replaceOne({ _id: docId }, changeEvent.fullDocument); + } + } catch (err) { + console.log("error performing mongodb write: ", err.message); + } +}; diff --git a/snippets/functions/boilerplate/eventbridgeErrorFunction.js b/snippets/functions/boilerplate/eventbridgeErrorFunction.js new file mode 100644 index 0000000..fb8c4f5 --- /dev/null +++ b/snippets/functions/boilerplate/eventbridgeErrorFunction.js @@ -0,0 +1,18 @@ +exports = async function (error, changeEvent) { + // This sample function will log additional details if the error is not + // a DOCUMENT_TOO_LARGE error + if (error.code === "DOCUMENT_TOO_LARGE") { + console.log("Document too large error"); + + // Comment out the line below in order to skip this event and not suspend the Trigger + throw new Error(`Encountered error: ${error.code}`); + } + + console.log("Error sending event to EventBridge"); + console.log(`DB: ${changeEvent.ns.db}`); + console.log(`Collection: ${changeEvent.ns.coll}`); + console.log(`Operation type: ${changeEvent.operationType}`); + + // Throw an error in your function to suspend the trigger and stop processing additional events + throw new Error(`Encountered error: ${error.message}`); +}; diff --git a/snippets/functions/boilerplate/generalFunction.js b/snippets/functions/boilerplate/generalFunction.js new file mode 100644 index 0000000..56ed7be --- /dev/null +++ b/snippets/functions/boilerplate/generalFunction.js @@ -0,0 +1,47 @@ +exports = async function(arg){ + // This default function will get a value and find a document in MongoDB + // To see plenty more examples of what you can do with functions see: + // https://www.mongodb.com/docs/atlas/app-services/functions/ + + + // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) + var serviceName = "mongodb-atlas"; + + + // Update these to reflect your db/collection + var dbName = "db_name"; + var collName = "coll_name"; + + + // Get a collection from the context + var collection = context.services.get(serviceName).db(dbName).collection(collName); + + + var findResult; + try { + // Get a value from the context (see "Values" tab) + // Update this to reflect your value's name. + var valueName = "value_name"; + var value = context.values.get(valueName); + + + // Execute a FindOne in MongoDB + findResult = await collection.findOne( + { owner_id: context.user.id, "fieldName": value, "argField": arg}, + ); + + + } catch(err) { + console.log("Error occurred while executing findOne:", err.message); + + + return { error: err.message }; + } + + + // To call other named functions: + // var result = context.functions.execute("function_name", arg1, arg2); + + + return { result: findResult }; + \ No newline at end of file diff --git a/snippets/functions/boilerplate/manifest.json b/snippets/functions/boilerplate/manifest.json new file mode 100644 index 0000000..8294726 --- /dev/null +++ b/snippets/functions/boilerplate/manifest.json @@ -0,0 +1,31 @@ +{ + "category": "Boilerplate Template by Function Type", + "categoryId": "4756072f-b3c7-4469-b038-cdf6b346789c", + "viewType": "functionSnippet", + "snippets": [ + { + "id": "7c84ed29-b8b2-466c-af3d-c240889d3112", + "title": "Database Trigger", + "snippet": "/databaseTrigger.js", + "description": "Example shows a template for a DB trigger function, which always accepts a changeEvent in the header." + }, + { + "id": "ba6113d7-5e52-48e4-8ab4-03df30c0d61a", + "title": "Scheduled Trigger", + "snippet": "/scheduledTrigger.js", + "description": "Shows an example of a Scheduled Trigger function, which will always call a function without arguments." + }, + { + "id": "1dca4a58-c150-4a8e-b602-9edf5c338f3d", + "title": "General Function", + "snippet": "/generalFunction.js", + "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." + }, + { + "id": "b43b9abb-dde7-4389-880e-38ef995d501a", + "title": "Eventbridge Custom Error Function", + "snippet": "/eventbridgeErrorFunction.js", + "description": "Example of a custom error function, which takes in the error and changeEvent as arguments." + } + ] +} diff --git a/snippets/functions/boilerplate/scheduledTrigger.js b/snippets/functions/boilerplate/scheduledTrigger.js new file mode 100644 index 0000000..df4d635 --- /dev/null +++ b/snippets/functions/boilerplate/scheduledTrigger.js @@ -0,0 +1,21 @@ +exports = async function () { + // A Scheduled Trigger will always call a function without arguments. + // Documentation on Triggers: https://www.mongodb.com/docs/atlas/app-services/triggers/ + + // Functions run by Triggers are run as System users and have full access to Services, Functions, and MongoDB Data. + + // Get the MongoDB service you want to use (see "Linked Data Sources" tab) + const serviceName = "mongodb-atlas"; + const databaseName = "db_name"; + const collectionName = "coll_name"; + const collection = context.services + .get(serviceName) + .db(databaseName) + .collection(collectionName); + + try { + const doc = await collection.findOne({ name: "mongodb" }); + } catch (err) { + console.log("error performing mongodb findOne: ", err.message); + } +}; From ccbfc7a281a4e1d30504acf4fd40adf2c8d7c2b1 Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 20 Aug 2024 01:54:04 +0000 Subject: [PATCH 02/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 8eb16ef..3a4a46d 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "7e5bf28650dfc9e8d3981859adeaa8525fdb2034" + "version": "64d795e7f06541214088604f1578f1db332b9ce7" } From 27f1b237149cb5b613f03e50ebe0861fefcdc4af Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 20 Aug 2024 01:56:38 +0000 Subject: [PATCH 03/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 2602a2a..85af845 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "d04d084298c27e7c755e36c9f61c00ac9e836f56" + "version": "16e53ff6726b3a514bf5343b5ea1664dac8ae5a7" } From 963efdc5d61b754f4e690c537942c55db8677a75 Mon Sep 17 00:00:00 2001 From: jwongmongodb <116834447+jwongmongodb@users.noreply.github.com> Date: Mon, 19 Aug 2024 22:00:46 -0400 Subject: [PATCH 04/13] Update generalFunction.js add missing bracket --- .../functions/boilerplate/generalFunction.js | 89 +++++++++---------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/snippets/functions/boilerplate/generalFunction.js b/snippets/functions/boilerplate/generalFunction.js index 56ed7be..16ba3d2 100644 --- a/snippets/functions/boilerplate/generalFunction.js +++ b/snippets/functions/boilerplate/generalFunction.js @@ -1,47 +1,42 @@ -exports = async function(arg){ - // This default function will get a value and find a document in MongoDB - // To see plenty more examples of what you can do with functions see: - // https://www.mongodb.com/docs/atlas/app-services/functions/ - - - // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) - var serviceName = "mongodb-atlas"; - - - // Update these to reflect your db/collection - var dbName = "db_name"; - var collName = "coll_name"; - - - // Get a collection from the context - var collection = context.services.get(serviceName).db(dbName).collection(collName); - - - var findResult; - try { - // Get a value from the context (see "Values" tab) - // Update this to reflect your value's name. - var valueName = "value_name"; - var value = context.values.get(valueName); - - - // Execute a FindOne in MongoDB - findResult = await collection.findOne( - { owner_id: context.user.id, "fieldName": value, "argField": arg}, - ); - - - } catch(err) { - console.log("Error occurred while executing findOne:", err.message); - - - return { error: err.message }; - } - - - // To call other named functions: - // var result = context.functions.execute("function_name", arg1, arg2); - - - return { result: findResult }; - \ No newline at end of file +exports = async function (arg) { + // This default function will get a value and find a document in MongoDB + // To see plenty more examples of what you can do with functions see: + // https://www.mongodb.com/docs/atlas/app-services/functions/ + + // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) + var serviceName = "mongodb-atlas"; + + // Update these to reflect your db/collection + var dbName = "db_name"; + var collName = "coll_name"; + + // Get a collection from the context + var collection = context.services + .get(serviceName) + .db(dbName) + .collection(collName); + + var findResult; + try { + // Get a value from the context (see "Values" tab) + // Update this to reflect your value's name. + var valueName = "value_name"; + var value = context.values.get(valueName); + + // Execute a FindOne in MongoDB + findResult = await collection.findOne({ + owner_id: context.user.id, + fieldName: value, + argField: arg, + }); + } catch (err) { + console.log("Error occurred while executing findOne:", err.message); + + return { error: err.message }; + } + + // To call other named functions: + // var result = context.functions.execute("function_name", arg1, arg2); + + return { result: findResult }; +}; From 7a0488afbbc0b507c18e4b8133454ab7df397285 Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 20 Aug 2024 02:01:06 +0000 Subject: [PATCH 05/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 85af845..d3597a8 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "16e53ff6726b3a514bf5343b5ea1664dac8ae5a7" + "version": "963efdc5d61b754f4e690c537942c55db8677a75" } From 225c694859eed0dbf9e3ba84966e2220d3cca332 Mon Sep 17 00:00:00 2001 From: jwongmongodb <116834447+jwongmongodb@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:49:38 -0400 Subject: [PATCH 06/13] Update manifest.json to use consistent descriptions for change events --- snippets/functions/boilerplate/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/functions/boilerplate/manifest.json b/snippets/functions/boilerplate/manifest.json index 8294726..465e880 100644 --- a/snippets/functions/boilerplate/manifest.json +++ b/snippets/functions/boilerplate/manifest.json @@ -7,7 +7,7 @@ "id": "7c84ed29-b8b2-466c-af3d-c240889d3112", "title": "Database Trigger", "snippet": "/databaseTrigger.js", - "description": "Example shows a template for a DB trigger function, which always accepts a changeEvent in the header." + "description": "Example shows a template for a DB trigger function, which always accepts a changeEvent as arguments." }, { "id": "ba6113d7-5e52-48e4-8ab4-03df30c0d61a", From 5f32a691329fa9c40e36f245cf90c487547957b5 Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 3 Sep 2024 20:49:51 +0000 Subject: [PATCH 07/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index d3597a8..ca34bad 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "963efdc5d61b754f4e690c537942c55db8677a75" + "version": "225c694859eed0dbf9e3ba84966e2220d3cca332" } From 04370ebb3605edbde110f8c018211c97697d127f Mon Sep 17 00:00:00 2001 From: jwongmongodb <116834447+jwongmongodb@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:50:27 -0400 Subject: [PATCH 08/13] fix grammar --- snippets/functions/boilerplate/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/functions/boilerplate/manifest.json b/snippets/functions/boilerplate/manifest.json index 465e880..d39d8fd 100644 --- a/snippets/functions/boilerplate/manifest.json +++ b/snippets/functions/boilerplate/manifest.json @@ -7,7 +7,7 @@ "id": "7c84ed29-b8b2-466c-af3d-c240889d3112", "title": "Database Trigger", "snippet": "/databaseTrigger.js", - "description": "Example shows a template for a DB trigger function, which always accepts a changeEvent as arguments." + "description": "Example shows a template for a DB trigger function, which always accepts a changeEvent as an argument." }, { "id": "ba6113d7-5e52-48e4-8ab4-03df30c0d61a", From f022f51f76e9aecf4942f023134685c3b1e05e63 Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 3 Sep 2024 20:50:37 +0000 Subject: [PATCH 09/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index ca34bad..ae97c29 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "225c694859eed0dbf9e3ba84966e2220d3cca332" + "version": "04370ebb3605edbde110f8c018211c97697d127f" } From 0d755bf3c9e841c96bfc7b360711e8f635b7773b Mon Sep 17 00:00:00 2001 From: jwongmongodb <116834447+jwongmongodb@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:51:40 -0400 Subject: [PATCH 10/13] use more modern js variable syntax Co-authored-by: Gabriele Cimato --- .../functions/boilerplate/generalFunction.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/snippets/functions/boilerplate/generalFunction.js b/snippets/functions/boilerplate/generalFunction.js index 16ba3d2..3c6055f 100644 --- a/snippets/functions/boilerplate/generalFunction.js +++ b/snippets/functions/boilerplate/generalFunction.js @@ -4,24 +4,24 @@ exports = async function (arg) { // https://www.mongodb.com/docs/atlas/app-services/functions/ // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) - var serviceName = "mongodb-atlas"; + const serviceName = "mongodb-atlas"; // Update these to reflect your db/collection - var dbName = "db_name"; - var collName = "coll_name"; + const dbName = "db_name"; + const collName = "coll_name"; // Get a collection from the context - var collection = context.services + const collection = context.services .get(serviceName) .db(dbName) .collection(collName); - var findResult; + let findResult; try { // Get a value from the context (see "Values" tab) // Update this to reflect your value's name. - var valueName = "value_name"; - var value = context.values.get(valueName); + const valueName = "value_name"; + const value = context.values.get(valueName); // Execute a FindOne in MongoDB findResult = await collection.findOne({ @@ -36,7 +36,7 @@ exports = async function (arg) { } // To call other named functions: - // var result = context.functions.execute("function_name", arg1, arg2); + // const result = context.functions.execute("function_name", arg1, arg2); return { result: findResult }; }; From 435676c1b136d9fd7ad4594c47124598966fa484 Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 3 Sep 2024 20:51:51 +0000 Subject: [PATCH 11/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index ae97c29..1099af6 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "04370ebb3605edbde110f8c018211c97697d127f" + "version": "0d755bf3c9e841c96bfc7b360711e8f635b7773b" } From 86e6c130f442b25a8cabba7e140acde80e58f085 Mon Sep 17 00:00:00 2001 From: jwongmongodb <116834447+jwongmongodb@users.noreply.github.com> Date: Tue, 3 Sep 2024 17:06:23 -0400 Subject: [PATCH 12/13] grammar update Co-authored-by: Gabriele Cimato --- snippets/functions/boilerplate/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/functions/boilerplate/manifest.json b/snippets/functions/boilerplate/manifest.json index d39d8fd..63348fa 100644 --- a/snippets/functions/boilerplate/manifest.json +++ b/snippets/functions/boilerplate/manifest.json @@ -19,7 +19,7 @@ "id": "1dca4a58-c150-4a8e-b602-9edf5c338f3d", "title": "General Function", "snippet": "/generalFunction.js", - "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." + "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." }, { "id": "b43b9abb-dde7-4389-880e-38ef995d501a", From 035b40e9decccb7f2eef768eb937266a1c53ca82 Mon Sep 17 00:00:00 2001 From: ActionBot Date: Tue, 3 Sep 2024 21:06:33 +0000 Subject: [PATCH 13/13] update commit hash --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 1099af6..46d7e33 100644 --- a/manifest.json +++ b/manifest.json @@ -22,5 +22,5 @@ "metadata": "/snippets/triggers/project/manifest.json" } ], - "version": "0d755bf3c9e841c96bfc7b360711e8f635b7773b" + "version": "86e6c130f442b25a8cabba7e140acde80e58f085" }