Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Datastore<T>

The Datastore object is used to call methods on LevelDB instance.

Usage

const store = new Datastore<Human>('human', './database', () => Human)

Type parameters

  • T: Model<T>

    The model for this Datastore. T must extend from Model

Hierarchy

  • Datastore

Index

Constructors

constructor

  • new Datastore(name: string, directory: string, type: function): Datastore
  • Parameters

    • name: string

      The name of the database. Usually the name of the type in lower case.

    • directory: string

      The directory where the database will be created.

    • type: function

      A function that returns the type. For example () => Human

        • (): any
        • Returns any

    Returns Datastore

Properties

methods

LevelDB method wrappers. See DatastoreOperations for more info.

Private name

name: string

Name of the database.

Private store

store: LevelUp<EncodingDown<string, any>>

The LevelDB storage.

Private type

type: function

A function that returns the type of the model.

Type declaration

    • (): any
    • Returns any

Methods

create

  • create(data: T): T
  • This method is used to create a new object with this model. Without calling this method, you will not be able to use .save() or .delete() methods on the object.

    Usage

    const store = new Datastore<Human>(...)
    const human = store.create(new Human(...))
    await human.save() //Without using .create() this will throw.

    Parameters

    • data: T

      T object to initialize.

    Returns T

    T object with its Model.store field set.

delete

  • This method deletes one single object from database.

    Usage

    const store = new Datastore<Human>(...)
    const humanId = '...'
    const humanEdited = await store.delete().id(humanId).run()
    console.log(humanEdited)

    Returns DeleteOperation<T>

    A new instance of DeleteOperation<T>. More info on this is in DeleteOperation.

deleteBatched

edit

  • This method edits one single object from database.

    Usage

    const store = new Datastore<Human>(...)
    const humanId = '...'
    const humanEdited = await store.edit().id(humanId).with({...}).run()
    console.log(humanEdited)

    Returns EditOperation<T>

    A new instance of EditOperation<T>. More info on this is in EditOperation.

get

  • This method gets objects from the database.

    Usage

    const store = new Datastore<Human>(...)
    const humans = await store.get().take(5).run()
    console.log(humans)

    Returns GetOperation<T>

    A new instance of GetOperation<T>. More info on this is in GetOperation.

push

  • This method is used to push a new object to the database.

    Usage

    const store = new Datastore<Human>(...)
    const human = await store.push().item(new Human(...)).run()
    console.log(human)

    Returns PushOperation<T>

    A new instance of PushOperation<T>. More info on this is in PushOperation.

pushBatched

  • This method is used to push a bunch of objects to the database.

    Usage

    const store = new Datastore<Human>(...)
    const humansData: Human[] = [...]
    const humans = await store.pushBatched().items(humansData).run()
    console.log(humans)

    Returns BatchedPushOperation<T>

    A new instance of BatchedPushOperation<T>. More info on this is in BatchedPushOperation.

Generated using TypeDoc