Skip to content

Commit 5867022

Browse files
authored
update main (#2)
1 parent 18fcd0f commit 5867022

File tree

9 files changed

+217
-62
lines changed

9 files changed

+217
-62
lines changed

.github/workflows/zip-and-upload-to-s3-production.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: Zip and Upload to S3 for Development
1+
name: Zip and Upload to S3 for Production
22

33
on:
44
push:
55
branches:
6-
- production
6+
- main
77

88
jobs:
99
deploy-after-testing:
@@ -21,4 +21,4 @@ jobs:
2121
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
2222
aws_bucket: ${{ secrets.AWS_BUCKET }}
2323
source_dir: ./zip
24-
destination_dir: "v1/production"
24+
destination_dir: "v1/production"

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": "a1c17cc564ffff9fe3413fdba419df03c8e34778"
25+
"version": "7395fc1e8214723dc941ed9e718b4ea36d303e1c"
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: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
{
2-
"category": "MatchTriggerExpressions",
3-
"categoryId": "ed4b3598-6800-413f-9158-d9b1b2a2a080",
4-
"viewType": "triggerMatch",
5-
"snippets": [
6-
{
7-
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a081",
8-
"title": "Match Expression for Updates",
9-
"snippet": "/update.json",
10-
"description": "This trigger will only execute if the `storeLocation` array has changed to \"East Langley\" and the \"storeItems\" field has been removed."
11-
},
12-
{
13-
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a082",
14-
"title": "Match Expression for Deletes",
15-
"snippet": "/delete.json",
16-
"description": "This trigger will only execute if the document (before deletion) has a userName of \"Alice Smith\""
17-
},
18-
{
19-
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a083",
20-
"title": "Match Expression for Inserts",
21-
"snippet": "/insert.json",
22-
"description": "This trigger will only execute if the document has a userName of \"Alice Smith\" and a department of \"engineering\""
23-
},
24-
{
25-
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a084",
26-
"title": "Match Expression for Replace",
27-
"snippet": "/replace.json",
28-
"description": "This trigger will only execute if the name field was \"Alice Smith\" before the Replace, and \"Alex Smith\" after the Replace."
29-
}
30-
]
2+
"category": "MatchTriggerExpressions",
3+
"categoryId": "ed4b3598-6800-413f-9158-d9b1b2a2a080",
4+
"viewType": "triggerMatch",
5+
"snippets": [
6+
{
7+
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a081",
8+
"title": "Update",
9+
"snippet": "/update.json",
10+
"description": "This trigger will only execute if the `storeLocation` array has changed to \"East Langley\" and the \"storeItems\" field has been removed."
11+
},
12+
{
13+
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a082",
14+
"title": "Delete",
15+
"snippet": "/delete.json",
16+
"description": "This trigger will only execute if the document (before deletion) has a userName of \"Alice Smith\""
17+
},
18+
{
19+
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a083",
20+
"title": "Insert",
21+
"snippet": "/insert.json",
22+
"description": "This trigger will only execute if the document has a userName of \"Alice Smith\" and a department of \"engineering\""
23+
},
24+
{
25+
"id": "ed4b3598-6800-413f-9158-d9b1b2a2a084",
26+
"title": "Replace",
27+
"snippet": "/replace.json",
28+
"description": "This trigger will only execute if the name field was \"Alice Smith\" before the Replace, and \"Alex Smith\" after the Replace."
29+
}
30+
]
3131
}
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
{
2-
"category": "ProjectTriggerExpressions",
3-
"categoryId": "ee1e471e-72c7-4dc9-b80a-9f745836a254",
4-
"viewType": "triggerProject",
5-
"snippets": [
6-
{
7-
"id": "7dd66555-8efb-4a76-8920-64c0874e0214",
8-
"title": "Project Expression for Updates",
9-
"snippet": "/update.json",
10-
"description": "This trigger will return the \"operationType\" and \"updateDescription.updateFields.storeLocation\" field values."
11-
},
12-
{
13-
"id": "7c9b14f4-30ec-4865-b153-8bd52c6566ad",
14-
"title": "Project Expression for Deletes",
15-
"snippet": "/delete.json",
16-
"description": "This trigger will return the \"documentKey._id\", \"operationType\", and \"wallTime\" field values."
17-
},
18-
{
19-
"id": "e040947b-08a4-4a8f-9f82-1bccd7826892",
20-
"title": "Project Expression for Inserts",
21-
"snippet": "/insert.json",
22-
"description": "This trigger will return the \"baseSalary\", \"department\", \"operationType\", and \"documentKey.userName\" field values."
23-
},
24-
{
25-
"id": "90768f43-3d5a-4746-8ccc-f709cda09eae",
26-
"title": "Project Expression for Replace",
27-
"snippet": "/replace.json",
28-
"description": "This trigger will return the \"operationType\", \"fullDocument.name\", and \"fullDocumentBeforeChange.name\" field values."
29-
}
30-
]
2+
"category": "ProjectTriggerExpressions",
3+
"categoryId": "ee1e471e-72c7-4dc9-b80a-9f745836a254",
4+
"viewType": "triggerProject",
5+
"snippets": [
6+
{
7+
"id": "7dd66555-8efb-4a76-8920-64c0874e0214",
8+
"title": "Update",
9+
"snippet": "/update.json",
10+
"description": "This trigger will return the \"operationType\" and \"updateDescription.updateFields.storeLocation\" field values."
11+
},
12+
{
13+
"id": "7c9b14f4-30ec-4865-b153-8bd52c6566ad",
14+
"title": "Delete",
15+
"snippet": "/delete.json",
16+
"description": "This trigger will return the \"documentKey._id\", \"operationType\", and \"wallTime\" field values."
17+
},
18+
{
19+
"id": "e040947b-08a4-4a8f-9f82-1bccd7826892",
20+
"title": "Insert",
21+
"snippet": "/insert.json",
22+
"description": "This trigger will return the \"baseSalary\", \"department\", \"operationType\", and \"documentKey.userName\" field values."
23+
},
24+
{
25+
"id": "90768f43-3d5a-4746-8ccc-f709cda09eae",
26+
"title": "Replace",
27+
"snippet": "/replace.json",
28+
"description": "This trigger will return the \"operationType\", \"fullDocument.name\", and \"fullDocumentBeforeChange.name\" field values."
29+
}
30+
]
3131
}

0 commit comments

Comments
 (0)