Skip to content

Commit 446a3bc

Browse files
MongoCalebmongodb-atlas-app-services[bot]jwongmongodb
authored
Latest Snippets: Functions, Expressions, and manifests (#46)
* Development (#45) * update diagram and readme * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * update commit hash * update commit hash * remove function name prefix * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * update commit hash * remove function name prefix * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * update commit hash * remove function name prefix * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * update unit tests to use changeEvent object * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * update expression manifest * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * update commit hash * remove function name prefix * update commit hash * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash * update commit hash * update commit hash * Commit performed using Copy Push Files action * update commit hash * update project manifest * update commit hash * update action to add git commit hash to manifest rather than extra file * add debugging * combine steps * update commit hash * remove old file * update commit hash * BAAS-30361: add amazon third party snippets (#47) * update commit hash * Update trigger insert title correctly (#48) * update commit hash * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * Commit performed using Copy Push Files action * remove function name prefix * update commit hash --------- Co-authored-by: mongodb-atlas-app-services[bot] <mongodb-atlas-app-services[bot]@users.noreply.github.com> Co-authored-by: MongoCaleb <[email protected]> Co-authored-by: jwongmongodb <[email protected]>
1 parent 7ce9bfd commit 446a3bc

38 files changed

+867
-306
lines changed

.github/workflows/Remove-Function-Name-Prefix.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
justfile="${justfile#*_}"
1919
echo "was:" $file
2020
echo "saving as" $justpath/$justfile
21-
mv -n $file $justpath/$justfile
21+
mv -f $file $justpath/$justfile
2222
fi
2323
done
2424
if [ "$(git diff --name-only)" ]; then
@@ -27,6 +27,7 @@ jobs:
2727
git config --global user.name "ActionBot"
2828
git commit -m 'remove function name prefix'
2929
git push origin ${{ github.ref }}
30+
else
31+
echo "no changes to commit"
3032
fi
31-
32-
echo "done"
33+
echo "changes committed"

.github/workflows/Update-Commit-Hash.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v3
13-
- name: Update file with merge commit hash
13+
- name: 'Setup jq'
14+
uses: dcarbone/install-jq-action@v2
15+
with:
16+
version: '${{ inputs.version }}'
17+
force: '${{ inputs.force }}'
18+
- id: validate_manifest
1419
run: |
20+
version=$(git log -1 --format='%H')
21+
jq '.version' manifest.json
22+
jq --arg v "${version}" '.version = $v' manifest.json > tempManifest.json
23+
mv tempManifest.json manifest.json
24+
jq '.version' manifest.json
1525
git pull origin ${{ github.ref }}
16-
git log -1 --format='%H' > latestCommit.md
1726
git config --global user.email "[email protected]"
1827
git config --global user.name "ActionBot"
19-
git add latestCommit.md
28+
git add manifest.json
2029
git commit -m 'update commit hash'
2130
git push origin ${{ github.ref }}

Flow Diagram.graffle

18 KB
Binary file not shown.

Flow Diagram.png

-143 KB
Binary file not shown.

GitHubActionProcesses.png

154 KB
Loading

README.md

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,46 @@ and a push to `main` uploads to the `production` directory.
99

1010
For bucket access, consult the Developer Docs team.
1111

12-
## Add Function and Trigger Snippets
12+
## Adding Function and Trigger Snippets
1313

1414
The Developer Docs team maintains these code snippets as tested examples.
15-
Adding a new snippet involves:
16-
17-
1. Adding a Function or Trigger to the Atlas backend (ask Dev Docs for details
18-
and access).
19-
2. Adding a relevant test for the Function or Trigger to the test suite. Refer to
20-
the README in the `test/integration` directory for more details.
21-
3. Exporting or pulling the Atlas backend with your Function or Trigger, and adding
22-
the relevant file(s) to this repository's `backend`.
23-
4. Running the Bluehawk script in the `tools` directory to copy the files from the
24-
backend into the `snippets` files that get uploaded to S3.
25-
5. Adding a metadata file to the `metadata` directory with a title and other
26-
relevant details based on the type of snippet you're adding.
27-
28-
Details about filenames and metadata schema will be added to this repository
29-
after the spec is finalized.
30-
31-
If you are not a member of the Bushicorp org, create your own test app to test
32-
the Function or Trigger, and the Developer Docs team will import your Function
33-
or Trigger into the org test app as part of your PR. You can use the `backend`
34-
in this repository to create your own test app that should run the entire test
35-
suite successfully.
36-
37-
<!-- TODO: Add more details about the repo structure and its purpose -->
15+
When you add a new function, be sure to:
16+
17+
1. Add a Function or Trigger to the Atlas backend (ask Dev Docs for details
18+
and access). Follow these rules when creating a function or trigger:
19+
20+
- Function names **must** start with their type, which is one of `api_`,
21+
`crud_`, or `context_`.
22+
- Functions must be tested within the AAS app.
23+
- Triggers should use the "fake_function_for_expressions"
24+
function, since we're only creating triggers to extract the ``match`` and ``project`` stages and nothing function-related. Following this rule means we don't end up with any additional functions in the repo.
25+
26+
2. Deploy your changes. This will automatically push the changes to the *source* repo, where github actions will remove function name prefixes, extract the ``match`` and ``project`` json from the triggers, and then copy the functions and expressions to this repo.
27+
28+
3. Update the `manifest.json` file in the correct subfolder (snippets/functions/crud, for example) with your new function. Be sure to generate a new uuid for the `id` field.
29+
30+
4. Add a relevant test for the function or trigger to the test suite. Refer to the README in the `test/integration` directory for more details.
31+
32+
## Adding a New Function Category
33+
34+
If we decide we need a type of function beyond CRUD, function context, third-party integration, and API calls, do the following:
35+
36+
1. Coordinate with dev and product for naming the new `viewType` and subfolder.
37+
2. Create the new subfolder that will contain these functions.
38+
3. Copy an existing `manifest.json` file from one of the other function types and update it with the following:
39+
a. The new `viewType`
40+
b. The new snippets. Even if there is only 1 new snippet, be sure it is in an array.
41+
4. In the `atlas-functions-triggers-source` repo, you **must** update the `/.github/workflows/Copy-Functions-to-Examples-Repo.yml` action to copy the new category functions. Each function category has a section in the github action that looks like this:
42+
```
43+
- name: Copy API Functions
44+
uses: MongoCaleb/copy-push-files@main
45+
env:
46+
API_TOKEN_GITHUB: "${{ secrets.API_TOKEN_GITHUB }}"
47+
with:
48+
source_files: ./functions/api_*
49+
remote_repository: https://github.com/mongodb/atlas-functions-triggers-examples
50+
target_dir: "snippets/functions/api-functions/"
51+
target_branch: "development"
52+
access_token: "${{ secrets.API_TOKEN_GITHUB }}"
53+
```
54+
Copy one of these sections and change the `source_files`, `target_dir` values.

latestCommit.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

manifest.json

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
{
2-
"snippetCategories": [
3-
{
4-
"metadata": "/snippets/functions/third-party/manifest.json"
5-
},
6-
{
7-
"metadata": "/snippets/functions/mongodb-crud/manifest.json"
8-
},
9-
{
10-
"metadata": "/snippets/functions/api-functions/manifest.json"
11-
},
12-
{
13-
"metadata": "/snippets/functions/function-context/manifest.json"
14-
},
15-
{
16-
"metadata": "/snippets/triggers/match/manifest.json"
17-
},
18-
{
19-
"metadata": "/snippets/triggers/project/manifest.json"
20-
}
21-
]
2+
"snippetCategories": [
3+
{
4+
"metadata": "/snippets/functions/third-party/manifest.json"
5+
},
6+
{
7+
"metadata": "/snippets/functions/mongodb-crud/manifest.json"
8+
},
9+
{
10+
"metadata": "/snippets/functions/api-functions/manifest.json"
11+
},
12+
{
13+
"metadata": "/snippets/functions/function-context/manifest.json"
14+
},
15+
{
16+
"metadata": "/snippets/triggers/match/manifest.json"
17+
},
18+
{
19+
"metadata": "/snippets/triggers/project/manifest.json"
20+
}
21+
],
22+
"version": "d2de28d9e0967f028b5e02fd70359c7d69b87ece"
2223
}

snippets/functions/func.js

Whitespace-only changes.
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
1-
exports = async function(args){
2-
console.log(JSON.stringify(args));
3-
1+
exports = async function(changeEvent){
2+
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
43
var serviceName = "mongodb-atlas";
4+
5+
// Update these to reflect your db/collection
56
var dbName = "sample_supplies";
67
var collName = "sales";
78

9+
// Get a collection from the context
810
var collection = context.services.get(serviceName).db(dbName).collection(collName);
11+
12+
const deleteFilter = { "storeLocation": changeEvent.fullDocument.storeLocation };
913

1014
try {
11-
deleteResult = await collection.deleteMany(args);
15+
deleteResult = await collection.deleteMany(deleteFilter);
1216
return deleteResult["deletedCount"];
1317
} catch(err) {
1418
console.log("Failed to delete item: ", err.message);
1519
return { error: err.message };
1620
}
1721
};
22+
23+
// In the Testing Console tab, paste the code below and click Run:
24+
/*
25+
exports({
26+
_id: {_data: '62548f79e7f11292792497cc' },
27+
operationType: 'insert',
28+
clusterTime: {
29+
"$timestamp": {
30+
t: 1649712420,
31+
i:6
32+
}
33+
},
34+
ns: {
35+
db: 'engineering',
36+
coll: 'users'
37+
},
38+
documentKey: {
39+
storeLocation: 'East Appleton',
40+
_id: "62548f79e7f11292792497cc"
41+
},
42+
fullDocument: {
43+
_id: "599af247bb69cd89961c986d",
44+
storeLocation: 'East Appleton',
45+
couponUsed: false
46+
}
47+
})
48+
*/

0 commit comments

Comments
 (0)