Skip to content

Instantly share code, notes, and snippets.

@pfrazee
Created November 27, 2017 19:17
Show Gist options
  • Save pfrazee/8d99837aa3867bc2f4fe00a280f7a5c7 to your computer and use it in GitHub Desktop.
Save pfrazee/8d99837aa3867bc2f4fe00a280f7a5c7 to your computer and use it in GitHub Desktop.
Example usage of a hypothetical new version of Ingest, which is renamed to WebDB and tweaked a little
import WebDB from 'dat://modules.beakerbrowser.com/webdb.1.0.0.js'
// setup
// ==
// create the database instance
const webdb = new WebDB()
// define our data model
webdb.define('people', {
// the schema that objects must match
// (uses JSONSchema v6)
schema: {
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
age: {
description: 'Age in years',
type: 'integer',
minimum: 0
}
},
required: ['firstName', 'lastName']
},
// secondary indexes for fast queries (optional)
index: ['lastName', 'lastName+firstName', 'age']
})
// add sites to be indexed
webdb.people.addSource(alicesUrl, {
filePattern: '/people/*.json'
})
webdb.people.addSource(bobsUrl, {
filePattern: '/person.json'
})
webdb.people.addSource(carlasUrl, {
filePattern: [
'/person.json',
'/people/*.json'
]
})
// queries
// =
// get any person record where lastName === 'Roberts'
var mrRoberts = await webdb.people.get('lastName', 'Roberts')
// get any person record named Bob Roberts
var mrRoberts = await webdb.people.get('lastName+firstName', ['Roberts', 'Bob'])
// get all person records with the 'Roberts' lastname
var robertsFamily = await webdb.people
.where('lastName')
.equalsIgnoreCase('roberts')
.toArray()
// get all person records with the 'Roberts' lastname
// and a firstname that starts with 'B'
// - this uses a compound index
var robertsFamilyWithaBName = await webdb.broadcasts
.where('lastName+firstName')
.between(['Roberts', 'b'], ['Roberts', 'b\uffff'])
.toArray()
// get all person records on a given origin
// - origin is an auto-generated attribute
var personsOnBobsSite = await webdb.people
.where('origin')
.equals(bobsSiteUrl)
.toArray()
// get the 30 oldest people indexed
var oldestPeople = await webdb.people
.orderBy('age')
.reverse() // oldest first
.limit(30)
.toArray()
// count the # of young people
var oldestPeople = await webdb.people
.where('age')
.belowOrEqual(18)
.count()
// writing records
// =
// set the record
await webdb.people.put(bobsUrl + '/person.json', {
firstName: 'Bob',
lastName: 'Roberts',
age: 31
})
// update the record if it exists
await webdb.people.update(bobsUrl + '/person.json', {
age: 32
})
// update or create the record
await webdb.people.upsert(bobsUrl + '/person.json', {
age: 32
})
// delete the record
await webdb.people.delete(bobsUrl + '/person.json')
// update the spelling of all Roberts records
await webdb.people
.where('lastName')
.equals('Roberts')
.update({lastName: 'Robertos'})
// increment the age of all people under 18
var oldestPeople = await webdb.people
.where('age')
.belowOrEqual(18)
.update(record => {
record.age = record.age + 1
})
// delete the 30 oldest people
var oldestPeople = await webdb.people
.orderBy('age')
.reverse() // oldest first
.limit(30)
.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment