orbit-db-tablestore
An indexed and remoted loaded datastore for orbit-db. Indexed fields are searchable and sortable.
An orbit-db datastore that can be indexed and searched without downloading the entire dataset. Allows the creation of SQL-like tables. Access using ipfs-http-client.
The goal is to be able to load and search large datasets quickly in a browser.
Used in orbit-db.
Table of Contents
Install
npm install orbit-dbnpm install orbit-db-tablestorenpm install ipfs-http-client
Usage
First, create an instance of OrbitDB:
const ipfsClient = const OrbitDB = const TableStore = const ipfs = OrbitDB const orbitdb = await OrbitDB
Creating a table
Each table starts with a schema definition. Creating a schema defines the fields and indexes that will be created in the table. Multiple columns can be indexed and searched. Each index is implemented as a B-tree.
The properties of an index are:
- primary - This designates a column as the primary key. There should only be one.
- type - The data type the column will hold. Supported values are 'string', 'number', and 'boolean'
- unique - Whether the column has unique values.
To create a table we'll start by creating a JS class that has a static getter named 'constraints'. It should also contain matching properties for every contraint defined.
static { return id: primary: true unique:true type: 'number' name: unique: false type: 'string' currentTeam: unique: false type: 'string' battingHand: unique: false type: 'string' throwingHand: unique: false type: 'string' } { thisid = null thisname = null thiscurrentTeam = null thisbattingHand = null thisthrowingHand = null }
/** Put in an async function **/ let table = await orbitdb //Create the schema. Only needs to be done when creating the table initially. await table
Loading an existing table
let store = await orbitdb await store
Insert JSON objects into the table.
await table
- Note: The key and the primary key values should match.
Query the table by the primary key
let player = await table console //Prints // {// id: 9,// name: "Doug Drabek",// currentTeam: "BAL",// battingHand: "L",// throwingHand: "R",// someOtherData: "day"// }
Query the full list. Takes an offset and a limit as parameters
let list = await table //offset 0, limit 10
Query the table by the indexed fields. Returns an array with all matching values.
let teamPIT = await table //100 is the limit and 0 is the offsetlet teamBAL = await table let battingR = await tablelet battingL = await table let throwingR = await table
Count the records
let count = await table
Commit your changes.
Your changes are persisted to disk when you call commit(). Before commit() the store will respond with the old data when queried. Multiple calls to "put" can be made inside the same transaction.
Todo: There should be an auto-commit option somewhere.
await table