Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Datastore

Datastore class

Example:

const UserStorage = new yourStorageClass("users");
const Users = new Datastore({storage: UserStorage});

Creates a new Datastore using a specified storageDriver

Hierarchy

  • Datastore

Implements

Index

Constructors

constructor

Properties

Private generateId

generateId: boolean

whether or not to generate IDs automatically

Private indices

indices: Map<string, Index>

A HashMap of all the indices keyed by the fieldName.

Private storage

StorageDriver that is used for this Datastore

Methods

count

Private createId

  • createId(): string
  • Create Unique ID that contains timestamp

    Returns string

Private createIdsArray

  • createIdsArray(promises: Array<Promise<any>>, ids: string[]): void
  • Return fill provided promise array with promises from the storage driver

    Parameters

    • promises: Array<Promise<any>>
    • ids: string[]

    Returns void

ensureIndex

find

  • Find documents

    Examples:

    Users.find()
     .sort({age: -1})
     .skip(1)
     .limit(10)
     .exec()
     .then((users) => console.log(users))
     .catch((e) => console.log(e));
    
    Users.find({$or: [{name: "a"}, {name: "b"}]})
     .then((docs) => console.log(docs.length)) // 2 if unique
     .catch((e) => console.log(e));
    
    return Users.find({age: {$gt: 0, $lte: 27, $ne: 5}});
    

    Parameters

    • Default value query: any = {}

    Returns Cursor

getDocs

  • getDocs(options: Ioptions, ids: string | string[]): Promise<any>
  • Get Document by ID/s Used internally

    Parameters

    • options: Ioptions

      sort limit skip options

    • ids: string | string[]

      ID or Array of IDs

    Returns Promise<any>

getIdDate

  • getIdDate(id: string): Date
  • Get Date from ID ... do we need this on the dataStore?

    Example:

    let id = "UE9UQVJWd0JBQUE9cmZ4Y2MxVzNlOFk9TXV4dmJ0WU5JUFk9d0FkMW1oSHY2SWs9"; // an ID
    Users.getIdDate(id); // date object -> 2017-05-26T17:14:48.252Z
    

    Parameters

    • id: string

      the _id of the document to get date of

    Returns Date

getIndices

  • getIndices(): Promise<any>
  • Retrieve the indices of this datastore.

    Example:

    Users.getIndices()
     .then((indices) => {
         let usernameIndex = indices.get("username");
         if(usernameIndex) {
             return usernameIndex.toJSON(); // a method on index bTree
         }
     }); // no reject, will always resolve
    

    Returns Promise<any>

insert

  • insert(doc: any): Promise<any>
  • Insert a single document and insert any indices of document into its respective binary tree.

    Users.insert({name: "xyz", age: 30})
         .then((doc) => console.log(doc)) // {_id: "...", name: "xyz", age: 30}
         .catch((e) => console.log(e));
    

    Parameters

    • doc: any

      document to insert

    Returns Promise<any>

insertIndex

  • insertIndex(key: string, index: any[]): Promise<null>
  • Insert a stored index into the index of this datastore

    Parameters

    • key: string

      the index fieldName

    • index: any[]

      the key value pair obj

    Returns Promise<null>

remove

  • remove(query?: any): Promise<number>
  • Removes document/s by query - uses find method to retrieve ids. multi always

    Parameters

    • Default value query: any = {}

    Returns Promise<number>

removeIndex

  • removeIndex(fieldName: string): Promise<null>
  • Remove index will delete the index from the Map which also holds the Btree of the indices. If you need to remove the index from the persisted version in from the storage driver, call the removeIndex from the storage driver from a different source. This method should not assume you saved the index.

    Parameters

    • fieldName: string

      Field that needs index removed

    Returns Promise<null>

saveIndex

  • saveIndex(fieldName: string): Promise<null>
  • Save the index currently in memory to the persisted version if need be through the storage driver.

    Parameters

    • fieldName: string

    Returns Promise<null>

search

  • search(fieldName?: undefined | string, value?: any): Promise<any>
  • Search for IDs, chooses best strategy. Handles logical operators($or, $and) Returns array of IDs Used Internally

    Parameters

    • Optional fieldName: undefined | string

      element name or query start $or/$and

    • Optional value: any

      string,number,date,null - or [{ field: value }, { field: value }]

    Returns Promise<any>

Private searchCollection

  • searchCollection(fieldName?: undefined | string, value?: IRange): Promise<string[]>
  • Search collection by field, essentially a collection scan Returns array of IDs

    Parameters

    • Optional fieldName: undefined | string

      field to search

    • Optional value: IRange

      value to search by

    Returns Promise<string[]>

Private searchField

  • searchField(fieldName?: undefined | string, value?: any): Promise<BTT.SNDBSA>
  • Search for IDs, chooses best strategy, preferring to search indices if they exist for the given field. Returns array of IDs

    Parameters

    • Optional fieldName: undefined | string
    • Optional value: any

    Returns Promise<BTT.SNDBSA>

Private searchIndices

  • searchIndices(fieldName: string, value: IRange): Promise<BTT.SNDBSA>
  • Search indices by field Example 1: dbName.searchIndices("fieldName", "1234"); will return the value id of that key as an array of one element. Example 2: dbName.searchIndices("fieldName", { $gte: 1, $lte 10, $ne: 3 } will return an array of ids from the given range. Returns array of IDs

    Parameters

    • fieldName: string

      field to search

    • value: IRange

      value to search by

    Returns Promise<BTT.SNDBSA>

update

  • update(query: any, operation: any, options?: IupdateOptions): Promise<any>
  • Update document/s

    Examples: -> lets say two users {name: "bob", age: 1}, {name: "slydel", age: 45, companies: {name: "Initech", alternate: "Intertrode"}}

    Users.update({name: "bob"},{$rename: {name: "first"}, $set: {job: "consultant"}},
     {returnUpdatedDocs: true})
     .then((docs) => console.log(docs[0]) // {_id: "...", first: "bob", age: 1, job: "consultant"}
     .catch();
    
    Users.update({first: "bob"},{$unset: {"companies.alternate": ""}, $inc: {age: -44},
     $mul: {age: 5}}, {returnUpdatedDocs: true})
     .then((docs) => console.log(docs[0]) // {_id: "...", name: "bob", age: 5, companies: {name: "Initech"}}
     .catch();
    
    Users.update({name: "Charles", age: 22}, {$inc: {age: 4}}, {upser: true, returnUpdatedDocs: true}})
     .then((docs) => console.log(docs[0]) // {_id: "...", name: "Charles", age: 26}
     .catch();
    
    Users.update({age: {$gt: 0}},{$unset: {age: "", name: ""}},{ multi: true, returnUpdatedDocs: true})
     .then((docs) => console.log(docs)) // {_id: ".."}, {_id: ".."}, {_id: "..", companies: {name: "Initech"}}
     .catch();
    

    Parameters

    • query: any

      query document/s to update

    • operation: any

      update operation, either a new doc or modifies portions of a document(eg. $set)

    • Default value options: IupdateOptions = {}

      { fieldName, unique?, compareKeys?, checkKeyEquality? }

    Returns Promise<any>

Private updateDocsIndices

  • updateDocsIndices(docs: any[], promises: Array<Promise<any[]>>, indexPromises: Array<Promise<null>>, operation: any, operators: string[], operationKeys: string[], reject: any): any
  • Actual method to update the documents and associated indices

    Parameters

    • docs: any[]

      an array of documents to be updated

    • promises: Array<Promise<any[]>>

      reference to promise array to be resolved later

    • indexPromises: Array<Promise<null>>

      reference to promise array to be resolved later

    • operation: any

      operation query from update method

    • operators: string[]

      array of operators passed by reference from the update method

    • operationKeys: string[]

      Each key from the query. could be less than operators array length

    • reject: any

      passed reference to reject from update method.

    Returns any

Generated using TypeDoc