Created
November 30, 2017 01:23
-
-
Save jimpick/40ffbd708e5d8a0a17f52a70460aa4d0 to your computer and use it in GitHub Desktop.
WebDB simple demo - watching for changes on a Dat archive
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
const DatArchive = require('node-dat-archive') | |
const WebDB = require('@beaker/webdb') | |
var webdb = new WebDB('./webdb', { DatArchive }) | |
webdb.define('people', { | |
// 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' ], | |
// files to index | |
filePattern: [ | |
'/person.json', | |
'/people/*.json' | |
] | |
}) | |
const sources = { | |
jim: 'dat://4f5cdf5b524c9b1d26a0c9f87594488fc3cda298eb1f1cf9f1e89cf1dd95cda8/' | |
} | |
async function listPeople () { | |
const people = await webdb.people.toArray() | |
people.forEach(({ firstName, lastName, age }) => { | |
console.log({ firstName, lastName, age }) | |
}) | |
} | |
async function run () { | |
await webdb.open() | |
await webdb.addSource(sources.jim) | |
await listPeople() | |
webdb.people.on('index-updated', async ({ url }, version) => { | |
console.log('Table was updated for', url, 'at version', version) | |
await listPeople() | |
}) | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment