Database <T>
Hierarchy
- Target<[typeof WarnEventName, typeof DatabaseDocumentLoadEventName, typeof DatabaseCloseEventName]>
- Database
Index
Constructors
Properties
Methods
- addCollection
- addEventListener
- autosaveClearFlags
- autosaveDirty
- autosaveDisable
- autosaveEnable
- clearChanges
- close
- configureOptions
- copy
- deleteDatabase
- deserializeCollection
- deserializeDestructured
- dispatchEvent
- generateChangesNotification
- getCollection
- listCollections
- loadCollection
- loadDatabase
- loadDatabaseInternal
- loadJSON
- loadJSONObject
- removeCollection
- removeEventListener
- renameCollection
- saveDatabase
- saveDatabaseInternal
- serialize
- serializeChanges
- serializeCollection
- serializeDestructured
- throttledSaveDrain
- isDatabaseObject
Constructors
constructor
Type parameters
- T: PersistenceAdapterMode
Parameters
fileName: string = 'database.db'
options: Partial<IDatabaseOptions<T>> = {}
Returns Database<T>
Properties
autosave
autosaveHandle
autosaveInterval
readonlycollections
readonlydatabaseVersion
readonlyengineVersion
readonlyfileName
fire
Type declaration
Type parameters
- T: EventName<void> | EventName<IErrorEventDetail> | EventName<IDatabaseDocumentLoadEventDetail>
Parameters
event: Event<EventDetail<T>>
Returns boolean
ignoreAutosave
isIncremental
off
Type declaration
Type parameters
- T: EventName<void> | EventName<IErrorEventDetail> | EventName<IDatabaseDocumentLoadEventDetail>
Parameters
type: EventName<T>
callback: Listener<T>
optionaloptions: boolean | EventListenerOptions
Returns void
on
Type declaration
Type parameters
- T: EventName<void> | EventName<IErrorEventDetail> | EventName<IDatabaseDocumentLoadEventDetail>
Parameters
type: T
listener: Listener<T>
optionaloptions: boolean | AddEventListenerOptions
Returns void
options
persistenceAdapter
save
Type declaration
Handles manually saving to file system, local storage, or adapter (such as indexed-db) This method utilizes database configuration options (if provided) to determine which persistence method to use, or environment detection (if configuration was not provided).
If you are configured with autosave, you do not need to call this method yourself.
Returns Promise<boolean>
throttledSaves
toJson
Type declaration
Serialize database to a string which can be loaded via loadJSON
Parameters
optionaloptions: { serializationMethod: Normal }
Returns string
Stringified representation of the database database.
Parameters
optionaloptions: { serializationMethod: Pretty }
Returns string
Parameters
optionaloptions: { serializationMethod: Destructured }
Returns string[]
readonlyverbose
Methods
addCollection
Adds a collection to the database.
Type parameters
- P: object
Parameters
name: string
name of collection to add
optionaloptions: Partial<ICollectionOptions<P>>
options to configure collection with.
Returns Collection<P>
addEventListener
Type parameters
- T: EventName<any, T>
Parameters
type: T
listener: Listener<T>
optionaloptions: boolean | AddEventListenerOptions
Returns void
autosaveClearFlags
autosaveClearFlags - resets dirty flags on all collections. Called from saveDatabase() after db is saved.
Returns void
autosaveDirty
autosaveDirty - check whether any collections are ‘dirty’ meaning we need to save (entire) database
Returns boolean
true if database has changed since last autosave, false if not.
autosaveDisable
autosaveDisable - stop the autosave interval timer.
Returns void
autosaveEnable
autosaveEnable - begin a javascript interval to periodically save the database.
Returns Promise<void>
clearChanges
(Changes API) : clears all the changes in all collections.
Returns void
close
Emits the close event. In autosave scenarios, if the database is dirty, this will save and disable timer. Does not actually destroy the db.
Returns Promise<unknown>
configureOptions
Allows reconfiguring database options
Parameters
options: Partial<IDatabaseOptions<T>>
configuration options to apply to db object
initialConfig: boolean = false
(internal) true is passed when ctor is invoking
Returns void
copy
Copies ‘this’ database into a new Database instance. Object references are shared to make it lightweight enough.
Parameters
optionaloptions: ICopyDatabaseOptions
apply or override collection level settings
Returns Database<Default>
deleteDatabase
Handles deleting a database from file system, localStorage, or adapter (indexed-db) This method utilizes database configuration options (if provided) to determine which persistence method to use, or environment detection (if configuration was not provided).
Returns Promise<void>
deserializeCollection
Collection level utility function to deserializes a destructured collection.
Type parameters
- P: object
Parameters
destructuredSource: string | string[] | Database<any>
destructured representation of collection to inflate
optionaloptions: Partial<IDeserializeCollectionOptions>
used to describe format of destructuredSource input
Returns (P & ICollectionDocument)[]
an array of documents to attach to collection.data.
deserializeDestructured
Database level destructured JSON deserialization routine to minimize memory overhead. Internally, Database supports destructuring via database “serializationMethod’ option and the optional LokiPartitioningAdapter class. It is also available if you wish to do your own structured persistence or data exchange.
Parameters
destructuredSource: string | string[]
destructured json or array to deserialize from
optionaloptions: Partial<IDeserializeDestructuredOptions>
source format options
Returns any
An object representation of the deserialized database, not yet applied to ‘this’ db or document array
dispatchEvent
Type parameters
- T: EventName<any, T>
Parameters
event: Event<EventDetail<T>>
Returns boolean
generateChangesNotification
(Changes API) : takes all the changes stored in each collection and creates a single array for the entire database. If an array of names of collections is passed then only the included collections will be tracked.
Parameters
optionalcollectionNamesArray: string[]
array of collection names. No arg means all collections are processed.
Returns ICollectionChange<any>[]
array of changes
getCollection
Retrieves reference to a collection by name.
Type parameters
- U: object
Parameters
collectionName: string
name of collection to look up
Returns Collection<U>
listCollections
Returns a list of collections in the database.
Returns ICollectionSummary[]
array of objects containing ‘name’, ‘type’, and ‘count’ properties.
loadCollection
Type parameters
- P: object
Parameters
collection: Collection<P>
Returns void
loadDatabase
Handles manually loading from file system, local storage, or adapter (such as indexed-db). This method utilizes database configuration options (if provided) to determine which persistence method to use, or environment detection (if configuration was not provided). To avoid contention with any throttledSaves, we will drain the save queue first.
If you are configured with autosave, you do not need to call this method yourself.
Parameters
optionaloptions: Partial<IDatabaseOptions<T> & ILoadJSONOptions<any> & IThrottledSaveDrainOptions>
if throttling saves and loads, this controls how we drain save queue before loading
Returns Promise<any>
loadDatabaseInternal
Internal load logic, decoupled from throttling/contention logic
Parameters
optionaloptions: Partial<ILoadJSONOptions<any>>
not currently used (remove or allow overrides?)
Returns Promise<any>
loadJSON
Inflates a database database from a serialized JSON string
Type parameters
- P: object
Parameters
serializedDb: string
a serialized database database string
optionaloptions: Partial<ILoadJSONOptions<P>>
apply or override collection level settings
Returns void
loadJSONObject
Inflates a database database from a JS object
Type parameters
- P: object
Parameters
databaseObject: unknown
a serialized database database string
optionaloptions: Partial<ILoadJSONOptions<P>>
apply or override collection level settings
Returns void
removeCollection
Removes a collection from the database.
Parameters
collectionName: string
name of collection to remove
Returns void
removeEventListener
Type parameters
- T: EventName<any, T>
Parameters
type: EventName<T>
callback: Listener<T>
optionaloptions: boolean | EventListenerOptions
Returns void
renameCollection
Renames an existing database collection
Parameters
oldName: string
name of collection to rename
newName: string
new name of collection
Returns Collection<object>
saveDatabase
Handles manually saving to file system, local storage, or adapter (such as indexed-db) This method utilizes database configuration options (if provided) to determine which persistence method to use, or environment detection (if configuration was not provided).
If you are configured with autosave, you do not need to call this method yourself.
Returns Promise<boolean>
saveDatabaseInternal
Internal save logic, decoupled from save throttling logic
Returns Promise<void>
serialize
Serialize database to a string which can be loaded via loadJSON
Parameters
optionaloptions: { serializationMethod: Normal }
Returns string
Stringified representation of the database database.
serializeChanges
(Changes API) - stringify changes for network transmission
Parameters
optionalcollectionNamesArray: string[]
array of collection names. No arg means all collections are processed.
Returns string
string representation of the changes
serializeCollection
Collection level utility method to serialize a collection in a ‘destructured’ format
Parameters
optionaloptions: Partial<ISerializeCollectionOptions>
Returns string | string[]
A custom, restructured aggregation of independent serializations for a single collection.
serializeDestructured
Database level destructured JSON serialization routine to allow alternate serialization methods. Internally, Database supports destructuring via database “serializationMethod’ option and the optional PartitioningAdapter class. It is also available if you wish to do your own structured persistence or data exchange.
Parameters
optionaloptions: Partial<ISerializeDestructedOptions>
Returns string | string[]
throttledSaveDrain
Wait for throttledSaves to complete and invoke your callback when drained or duration is met.
Parameters
optionaloptions: Partial<IThrottledSaveDrainOptions>
configuration options
Returns OpenPromise<boolean> | Promise<boolean>
staticisDatabaseObject
Parameters
x: unknown
Returns x is Database<any>
The main database class.