UNPKG

stitch-ui

Version:

74 lines (70 loc) 2.27 kB
/* globals window, document */ export const WEB_LINK = "https://s3.amazonaws.com/stitch-sdks/js/library/stable/stitch.min.js"; export const NPM_PACKAGE = "mongodb-stitch"; export const LANG_WEB = "web"; export const LANG_NODE = "node"; export const findMongoDBService = services => { let mongoSvc; if (services) { // pick the best mongodb service to use. const atlasServices = Object.entries(services).filter( svcEntry => svcEntry[1].type === "mongodb" ); const mongoServices = Object.entries(services).filter( svcEntry => svcEntry[1].type === "mongodb-atlas" ); if (atlasServices.length > 0) { mongoSvc = atlasServices[0][0]; } else if (mongoServices.length > 0) { mongoSvc = mongoServices[0][0]; } } return mongoSvc; }; export const codeSnippet = ( clientAppId, svc, db = "<DATABASE>", collection = "<COLLECTION>", language = LANG_WEB ) => { let serverString = ""; if (window.location.hostname !== "stitch.mongodb.com") { serverString = `, {baseUrl: "${document.location.origin}"}`; } if (language === LANG_WEB) { return ` <script src="${WEB_LINK}"></script> <script> const client = new stitch.StitchClient('${clientAppId}'${serverString}); const db = client.service('mongodb', '${svc}').db('${db}'); client.login().then(() => db.collection('${collection}').updateOne({owner_id: client.authedId()}, {$set:{number:42}}, {upsert:true}) ).then(()=> db.collection('${collection}').find({owner_id: client.authedId()}) ).then(docs => { console.log("Found docs", docs) console.log("[MongoDB Stitch] Connected to Stitch") }).catch(err => { console.error(err) }); </script> `.trim(); } return ` const stitch = require("mongodb-stitch") const client = new stitch.StitchClient('${clientAppId}'${serverString}); const db = client.service('mongodb', '${svc}').db('${db}'); client.login().then(() => db.collection('${collection}').updateOne({owner_id: client.authedId()}, {$set:{number:42}}, {upsert:true}) ).then(() => db.collection('${collection}').find({owner_id: client.authedId()}) ).then(docs => { console.log("Found docs", docs) console.log("[MongoDB Stitch] Connected to Stitch") }).catch(err => { console.error(err) }); `.trim(); };