-
Notifications
You must be signed in to change notification settings - Fork 4
BAAS-34011: add boilerplate snippets #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
64d795e
BAAS-33449: add boilerplate snippets
jwongmongodb ccbfc7a
update commit hash
MongoCaleb 16e53ff
Merge branch 'development' into BAAS-33449
jwongmongodb 27f1b23
update commit hash
MongoCaleb 963efdc
Update generalFunction.js
jwongmongodb 7a0488a
update commit hash
MongoCaleb 225c694
Update manifest.json to use consistent descriptions for change events
jwongmongodb 5f32a69
update commit hash
MongoCaleb 04370eb
fix grammar
jwongmongodb f022f51
update commit hash
MongoCaleb 0d755bf
use more modern js variable syntax
jwongmongodb 435676c
update commit hash
MongoCaleb 86e6c13
grammar update
jwongmongodb 035b40e
update commit hash
MongoCaleb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |
18 changes: 18 additions & 0 deletions
18
snippets/functions/boilerplate/eventbridgeErrorFunction.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 }; | ||
}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." | ||
Gabri3l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ | ||
"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." | ||
jwongmongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ | ||
"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." | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.