new TrustDB()

Description

Creates a TrustDB instance.

Details

EventEmitter

Members


connected

Description

Get the database connection state.

Returns

Wether the database connection is established.

Details

filepath

Description

Get the filepath of the database.

Returns

Filepath of the database.

Details

created

Description

Get the date and time of creation of the database.

Returns

Date and time of creation.

Details

collections

Description

Get all collections of the database.

Returns

All collections of the database.

Details

size

Description

Get the amount of collections of the database.

Returns

Amount of collections of the database.

Details

totalSize

Description

Get the amount of documents of all collections.

Returns

Amount of documents of all collections.

Details

settings

Description

Get all database related settings.

Returns

Settings related to the database.

Details

autosave

Description

Get the current state of autosave.

Returns

Whether autosave is enabled.

Details

autosaveInterval

Description

Get the current autosave interval.

Returns

Autosave interval in milliseconds.

Details

autosave

Description

Enable or disable autosave.

Parameters
Name Type Description
value boolean

Whether to enable or disable autosave.

Details

autosaveInterval

Description

Set a new autosave interval.

Parameters
Name Type Description
value number

New interval in milliseconds.

Details

Methods


<async> connect( [ settings ] ) → {Promise.<void>}

Description

Create a connection to a new database. If the file already exists, it will be loaded and the collections (and optionally the settings, too) will be applied to this instance, unless the setting overwriteExisting is set to true.

Parameters
Name Type Attributes Default Description
settings object <optional>
{}

Optional settings related to the database.

Name Type Attributes Description
filepath string <optional>

Filepath for the database.

autosave boolean <optional>

Whether the database should be automatically saved.

autosaveInterval number <optional>

Interval for the autosave in milliseconds.

restoreSettings boolean <optional>

Whether it should restore settings from the loaded database, if it exists.

overwriteExisting boolean <optional>

Whether the existing database (same filepath) should be overwritten.

Returns

A Promise that resolves after everything is configured and set up.

Details

<async> disconnect( [ save ] )

Description

Close the database connection.

Disables autosave, removes the event listeners for the database and its collections, clears all documents and collections.

Parameters
Name Type Attributes Default Description
save boolean <optional>
true

Whether to save the database before disconnecting.

Details

createCollection( name ) → {Collection}

Description

Create a collection with the given name. Returns an existing collection in case a collection with the same name already eixists in this database.

Use TrustDB#deleteCollection before creating or adding a collection with the same name.

Parameters
Name Type Description
name string

Name of the collection.

Returns

Newly created collection or exisiting collection with the same name.

Details

addCollection( collection ) → {Collection}

Description

IMPORTANT: This function does not create a new collection. Use TrustDB#createCollection to create a new collection.

Add a collection to the database. Returns an existing collection in case a collection with the same name already eixists in this database.

Use TrustDB#deleteCollection before creating or adding a collection with the same name.

Parameters
Name Type Description
collection Collection

Collection instance to add.

Returns

Added collection or exisiting collection with the same name.

Details

deleteCollection( collection ) → {Collection}

Description

Delete a collection with the given name or using a Collection instance.

Parameters
Name Type Description
collection Collection | string

Name of the collection or a Collection instance.

Returns

Deleted collection.

Details

getCollection( name ) → {Collection}

Description

Retrieve a collection by name.

Parameters
Name Type Description
name string

Name of the collection.

Returns

Matching collection.

Details

<async> saveDatabase() → {Promise.<void>}

Description

Save the database with its collections, settings and metadata to the specified filepath.

Returns

A Promise which resolves after saving the database.

Details

<async> save() → {Promise.<void>}

Description

Save the database with its collections, settings and metadata to the specified filepath.

Returns

A Promise which resolves after saving the database.

Details

<async> save() → {Promise.<void>}

Description

Save the database with its collections, settings and metadata to the specified filepath.

Returns

A Promise which resolves after saving the database.

Details

<async> deleteDatabase() → {Promise.<void>}

Description

Delete the database from the filesystem, if it exists.

Returns

A Promise which resolves after deleting the database.

Details

<async> moveDatabase( filepath ) → {Promise.<void>}

Description

Move the database to the new path on the filesystem.

Parameters
Name Type Description
filepath string

New path for the database file.

Returns

A Promise which resolves after moving the database.

Details

configureSettings( [ settings ] )

Description

(Re-)configure the settings.

Parameters
Name Type Attributes Default Description
settings object <optional>
{}

Optional settings related to the database.

Name Type Attributes Description
filepath string <optional>

Filepath for the database. Only applies if there is no filepath set.

autosave boolean <optional>

Whether the database should be automatically saved.

autosaveInterval number <optional>

Interval for the autosave in milliseconds.

Details

clear() → {Array.<Collection>}

Description

Clear the database by deleting all of its collections.

Returns

All of the removed collections.

Details