Skip to content
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

[ACTIONS] MongoDb - Aggregation, Update Many Documents, Find a Document #11893

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import app from "../../mongodb.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "mongodb-execute-aggregation",
name: "Execute Aggregation",
description: "Execute an aggregation pipeline on a MongoDB collection. [See the documentation](https://www.mongodb.com/docs/drivers/node/current/fundamentals/aggregation/)",
version: "0.0.1",
type: "action",
props: {
app,
database: {
propDefinition: [
app,
"database",
],
},
collectionName: {
propDefinition: [
app,
"collection",
({ database }) => ({
database,
}),
],
},
pipeline: {
type: "string[]",
label: "Pipeline",
description: "The aggregation pipeline to execute where each row represents a stage as a JSON string. Eg. `[ { \"$match\": { \"categories\": \"Bakery\" } }, { \"$group\": { \"_id\": \"$stars\", \"count\": { \"$sum\": 1 } } } ]`",
},
},
methods: {
async excecuteAggregation({
database, collectionName, pipeline,
} = {}) {
const { app } = this;
const client = await app.getClient();
const collection = app.getCollection(client, database, collectionName);
const cursor = collection.aggregate(pipeline);
const result = await utils.iterate(cursor);
await client.close();
return result;
},
},
async run({ $ }) {
const {
excecuteAggregation,
database,
collectionName,
pipeline,
} = this;

const response = await excecuteAggregation({
database,
collectionName,
pipeline: utils.parseArray(pipeline),
});

$.export("$summary", `Successfully executed aggregation pipeline on collection with \`${response.length}\` document(s).`);

return response;
},
};
75 changes: 75 additions & 0 deletions components/mongodb/actions/find-document/find-document.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import app from "../../mongodb.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "mongodb-find-document",
name: "Find Document",
description: "Finds a document by a query filter. [See the documentation](https://docs.mongodb.com/manual/reference/method/db.collection.find/)",
version: "0.0.1",
type: "action",
props: {
app,
database: {
propDefinition: [
app,
"database",
],
},
collectionName: {
propDefinition: [
app,
"collection",
({ database }) => ({
database,
}),
],
},
filter: {
type: "string",
label: "Filter",
description: "JSON string to use as a filter. Eg. `{ \"name\": \"Twitter\" }`",
},
options: {
type: "string",
label: "Options",
description: "JSON string to use as options. Eg. `{ \"projection\": { \"_id\": 0, \"title\": 1 } }`",
optional: true,
},
},
methods: {
async findDocument({
database, collectionName, filter, options,
} = {}) {
const { app } = this;
const client = await app.getClient();
const collection = app.getCollection(client, database, collectionName);
const result = await collection.findOne(filter, options);
await client.close();
return result;
},
},
async run({ $ }) {
const {
findDocument,
database,
collectionName,
filter,
options,
} = this;

const response = await findDocument({
database,
collectionName,
filter: utils.valueToObject(filter),
options: utils.valueToObject(options),
});

if (!response) {
$.export("$summary", "Document not found in the collection.");
return;
}

$.export("$summary", "Successfully found a document in the collection.");
return response;
},
};
69 changes: 69 additions & 0 deletions components/mongodb/actions/update-documents/update-documents.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import app from "../../mongodb.app.mjs";
import utils from "../../common/utils.mjs";

export default {
key: "mongodb-update-documents",
name: "Update Documents",
description: "Updates many documents by query filter. [See the documentation](https://www.mongodb.com/docs/drivers/node/current/usage-examples/updateMany/)",
version: "0.0.1",
type: "action",
props: {
app,
database: {
propDefinition: [
app,
"database",
],
},
collectionName: {
propDefinition: [
app,
"collection",
({ database }) => ({
database,
}),
],
},
filter: {
type: "string",
label: "Filter",
description: "JSON string to use as a filter. Eg. `{ \"rated\": \"G\" }`",
},
data: {
label: "Data To Update",
type: "string",
description: "JSON data to use as the update. Eg. `{ \"$set\": { \"rating\": \"Everyone\" } }`",
},
},
methods: {
async updateDocuments({
database, collectionName, filter, data,
} = {}) {
const { app } = this;
const client = await app.getClient();
const collection = app.getCollection(client, database, collectionName);
const result = await collection.updateMany(filter, data);
await client.close();
return result;
},
},
async run({ $ }) {
const {
updateDocuments,
database,
collectionName,
filter,
data,
} = this;

const response = await updateDocuments({
database,
collectionName,
filter: utils.valueToObject(filter),
data: utils.valueToObject(data),
});

$.export("$summary", `Successfully updated \`${response.modifiedCount}\` document(s) in the collection.`);
return response;
},
};
63 changes: 63 additions & 0 deletions components/mongodb/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ConfigurationError } from "@pipedream/platform";

function isJson(value) {
if (typeof(value) !== "string") {
return false;
}

try {
JSON.parse(value);
} catch (e) {
return false;
}

return true;
}

function valueToObject(value) {
if (value === undefined || typeof(value) === "object") {
return value;
}

if (!isJson(value)) {
throw new ConfigurationError(`Make sure the value contains a valid JSON string: ${value}`);
}
return JSON.parse(value);
}

function parseArray(value) {
try {
if (!value) {
return [];
}

if (Array.isArray(value)) {
return value;
}

const parsedValue = JSON.parse(value);

if (!Array.isArray(parsedValue)) {
throw new Error("Not an array");
}

return parsedValue;

} catch (e) {
throw new ConfigurationError("Make sure the value contains a valid JSON array");
}
}

async function iterate(iterations) {
const items = [];
for await (const item of iterations) {
items.push(item);
}
return items;
}

export default {
valueToObject,
parseArray: (value) => parseArray(value).map(valueToObject),
iterate,
};
4 changes: 2 additions & 2 deletions components/mongodb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/mongodb",
"version": "0.0.8",
"version": "0.1.0",
"description": "Pipedream MongoDB Components",
"main": "mongodb.app.mjs",
"keywords": [
Expand All @@ -14,7 +14,7 @@
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.2.0",
"@pipedream/platform": "^1.6.5",
"mongodb": "^4.6.0"
}
}
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading