Published
Edited
Aug 8, 2022
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
example_usage = endpoint("data-api-usage", async (request, response, ctx) => {
// The secrets are a pain--use the secrets manager above and make sure to bind the secret after creating it
const {endpoint, api_key, cluster_name} = JSON.parse(ctx.secrets["DB_CONNECTION_INFO"])
const db = get_data_api({endpoint, api_key, cluster_name, database: 'test_database'})
const foo_collection = db({collection: 'test_collection'})
response.send(await foo_collection('findOne', {filter: {foo: "bar"}}))
})
Insert cell
hello_world = endpoint("info", async (request, response) => {
response.send("hello world!");
})
Insert cell
Insert cell
import {fetchp} from '@tomlarkworthy/fetchp'
Insert cell
get_data_api =
// usage (from an endpoint): users = get_data_api({see args})({collection: 'users'}); users('findOne', {filter: { "key": "value" }})
({
api_key,
endpoint,
cluster_name,
database
}) => ({collection}) => async (action="findOne", action_parameters={filter: { "key": "value" }}) => {
const response = await fetchp(`${endpoint}/action/${action}`, {method: 'POST', headers: {'api-key': api_key, 'Content-Type': 'application/json'}, body: JSON.stringify({dataSource: cluster_name, database, collection, ...action_parameters})})
if (response.status == 200) {
return response.json()
} else {
return response.text()
}
}
Insert cell
examples = endpoint("under-the-hood", async (request, response, ctx) => {
throw Error('You can try running queries like these on your own database.')
const {endpoint, api_key, cluster_name} = JSON.parse(ctx.secrets["DB_CONNECTION_INFO"])
const insertOne = async (document, database="sample_airbnb", collection="listingsAndReviews") => await fetchp(`${endpoint}/action/insertOne`, {method: 'POST', headers: {'api-key': api_key, 'Content-Type': 'application/json'}, body: JSON.stringify({dataSource: cluster_name, database, collection, document})})
const findOne = async (filter, database="sample_airbnb", collection="listingsAndReviews") => await fetchp(`${endpoint}/action/findOne`, {method: 'POST', headers: {'api-key': api_key, 'Content-Type': 'application/json'}, body: JSON.stringify({dataSource: cluster_name, database, collection, filter})})
const deleteOne = async (filter, database="sample_airbnb", collection="listingsAndReviews") => await fetchp(`${endpoint}/action/deleteOne`, {method: 'POST', headers: {'api-key': api_key, 'Content-Type': 'application/json'}, body: JSON.stringify({dataSource: cluster_name, database, collection, filter})})
//const result = await insertOne({name: "John Sample2", age:84})
const result = await findOne({name: "John Sample"})
response.send(JSON.stringify(await result.json()));
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more