UNPKG

tedb

Version:

TypeScript Embedded Database

100 lines (99 loc) 3.12 kB
/** * Created by tsturzl on 4/11/17. */ import Datastore from "./datastore"; import { IRange, IindexOptions } from "./types"; import { ASNDBS, SNDBSA } from "binary-type-tree"; /** * Index interface used for the datastore indices * Inherits types from binary-type-tree * * ~~~ * Array String Number, Date, Boolean, -> symbol was redacted. * ASNDBS = Array<any[]|string|number|Date|boolean|null>|string|number|Date|boolean|null * -> redacted symbol, Number, Date, Boolean, String, Array * SNDBSA = Array<{}|any[]|string|number|Date|boolean|null>; * ~~~ */ export interface IIndex { insert(doc: any): Promise<any>; insertMany(key: ASNDBS, indices: any[]): Promise<null>; updateKey(key: ASNDBS, newKey: ASNDBS): Promise<any>; remove(doc: any): Promise<any>; removeByPair(key: any, value: string): Promise<any>; toJSON(): Promise<string>; search(key: ASNDBS): Promise<SNDBSA>; traverse(fn: any): Promise<any>; searchRange(range: IRange): Promise<SNDBSA>; } export default class Index implements IIndex { /** Field Name for Index */ protected fieldName: string; /** ALV Tree for indexing */ private avl; /** Reference to Datastore */ private datastore; /** Is the index holding an array */ private isArray; /** * Constructor * @param datastore - reference to Datastore * @param options - Options for Index, `{fieldName: string}` */ constructor(datastore: Datastore, options: IindexOptions); traverse(fn: any): Promise<any>; /** * Insert document into Index * @param doc - document to insert into Index * @returns {Promise<any>} */ insert(doc: any): Promise<any>; /** * Inserts many documents and updates the indices * @param key * @param indices * @returns {Promise<null>} */ insertMany(key: ASNDBS, indices: any[]): Promise<null>; /** * Update a key of a tree * - keys are actually the value, in the tree the keys are values * of the to be updated document while the value in the tree is the * _id of the to be updated document. * @param key * @param newKey * @returns {Promise<null>} */ updateKey(key: ASNDBS, newKey: ASNDBS): Promise<null>; /** * Remove document from Index * @param doc * @returns {Promise<any>} */ remove(doc: any): Promise<any>; /** * Made to remove an indexed item just by the key value pair itself instead * of the full object. * @param key * @param {string} value * @returns {Promise<any>} */ removeByPair(key: any, value: string): Promise<any>; /** * Return the tree as JSON [{ key, value }, ...] pairs. * @returns {Promise<string>} */ toJSON(): Promise<string>; /** * Search Index for key * @param key * @returns {Promise<SNDBSA>} */ search(key: ASNDBS): Promise<SNDBSA>; /** * Search Index within bounds * @param range An IRange to search within bounds * @returns {Promise<SNDBSA>} */ searchRange(range: IRange): Promise<SNDBSA>; }