Created
July 2, 2019 21:24
-
-
Save e-lin/ab82fa5a39c441ea5a9d0d5b5397e4fd to your computer and use it in GitHub Desktop.
Mongo Stitch Service
This file contains 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
exports = async function (payload) { | |
const mongodb = context.services.get("mongodb-atlas"); | |
const exampledb = mongodb.db("exampledb"); | |
const examplecoll = exampledb.collection("examplecoll"); | |
const args = payload.query.text.split(" "); | |
switch (args[0]) { | |
case "stash": | |
const result = await examplecoll.insertOne({ | |
user_id: payload.query.user_id, | |
when: Date.now(), | |
url: args[1] | |
}); | |
if (result) { | |
return { | |
text: `Stashed ${args[1]}` | |
}; | |
} | |
return { | |
text: `Error stashing` | |
}; | |
case "list": | |
const findresult = await examplecoll.find({}).toArray(); | |
const strres = findresult.map(x => `<${x.url}|${x.url}> by <@${x.user_id}> at ${new Date(x.when).toLocaleString()}`).join("\n"); | |
return { | |
text: `Stash as of ${new Date().toLocaleString()}\n${strres}` | |
}; | |
case "remove": | |
const delresult = await examplecoll.deleteOne({ | |
user_id: { | |
$eq: payload.query.user_id | |
}, | |
url: { | |
$eq: args[1] | |
} | |
}); | |
return { | |
text: `Deleted ${delresult.deletedCount} stashed items` | |
}; | |
default: | |
return { | |
text: "Unrecognized" | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment