Skip to main content

Indexing API

Create fast lookup indexes in a datastore. Combined with streaming queries, indexing can be a big improvement for your application performance.

API quick overview

Datastore.open()

Datastore.open() opens a connection to a datastore in the active project space.

import {Datastore} from 'codehooks-js'

async function myfunc() {
const conn = await Datastore.open();
// use conn to call API functions
...
}

Returns Promise / Datastore connection

createIndex(collection, indexes)

Create index(es) on a collection in the current Datastore

Parameters

  • collection: collection string
  • indexes: Array of field names to index

Returns Promise / Indexes

Code example

const conn = await Datastore.open();    
const idx = await conn.createIndex('stocks', ['Name', 'Volume']);
warning

Index creation can take a long time on big collections. It's better to create indexes before adding data.

removeIndex(collection, indexes)

Remove index(es) on a collection in the current Datastore

Parameters

  • collection: collection string
  • indexes: Array of field names to remove from index

Returns Promise / Indexes

Code example

const conn = await Datastore.open();    
const idx = await conn.removeIndex('stocks', ['Name', 'Volume']);
tip

You can also use the CLI to manage indexes.