Databridge

@maynoothuniversity/caching-json-databridge~ Databridge

A class representing a data bridge with an associated cache.

Constructor

Fcnew Databridge(optionsopt)

Parameters:
Name Type Attributes Description
options PlainObject <optional>

a plain object defining configuration settings.

Name Type Attributes Default Description
cacheDir string <optional>
'./databridgeJsonCache'

the path to the folder to use for caching without a trailing separator.

defaultCacheTTL number <optional>
60

the default time to live for cached data in minutes.

Throws:

a validation error is thrown if parameter validation fails.

Type
external:validateParams.ValidationError

Methods

Fdatasource(datasourceName) → {Datasource}

A read-only accessor for all registered datasources.

Parameters:
Name Type Description
datasourceName DatabridgeName

the name of the datasource to get.

Throws:

a validation error is thrown if parameter validation fails.

Type
external:validateParams.ValidationError
Returns:
Type:
Datasource

a reference to the requested datasource object, or undefined if there is no datasource registered with the given name.

Ffetch()

An alias for .fetchResponse().

See:
  • Databridge.fetchResponse

FfetchDataPromise(datasourcePath, optionsopt, fetcherArgsopt) → {Promise}

A function to fetch a promise of data from a data source.

Note that this is simply a wrapper around .fetch().

See:
  • Databridge#fetch
Parameters:
Name Type Attributes Default Description
datasourcePath DatabridgeNamePath

for datasources with single data fetchers, the name of the data source as a string, for data sources with multiple data fetchers, an array of strings with the name of the datasource as the first element, and the path to the required fetcher in the remaining elements.

options PlainObject <optional>
{}

a plain object with options

Name Type Attributes Default Description
streamName string <optional>
'main'

for datasources that access multiple streams of data, this option can be used to maintain multiple caches, one for each stream.

fetcherArgs Array <optional>
[]

an array of arguments to pass to the fetcher function.

Throws:
  • a validation error is thrown if parameter validation fails.

    Type
    external:validateParams.ValidationError
  • An error is thrown when an unregistered datasource name is specified, or if there is a problem fetching data from the datasource.

    Type
    Error
Returns:
Type:
Promise

a promise that resolves to the data returned from the datasource.

FfetchResponse(datasourcePath, optionsopt, fetcherArgsopt) → {FetchResponse}

A function to fetch data from a data source. The function returns an object that contains a promise for the data.

Parameters:
Name Type Attributes Default Description
datasourcePath DatabridgeNamePath

for datasources with single data fetchers, the name of the data source as a string, for data sources with multiple data fetchers, an array of strings with the name of the datasource as the first element, and the path to the required fetcher in the remaining elements.

options PlainObject <optional>
{}

a plain object with options

Name Type Attributes Default Description
streamName DatabridgeName <optional>
'main'

a custom stream name to use when reading and writting the cache.

bypassCache boolean <optional>
false

a truthy value will bypass the cache, regardless of the data source's caching settings and TTL.

fetcherArgs Array <optional>
[]

an array of arguments to pass to the fetcher function.

Throws:
  • a validation error is thrown if parameter validation fails.

    Type
    external:validateParams.ValidationError
  • An error is thrown when an unregistered datasource name is specified, there's a problem generating the stream name, or, there's a problem fetching data from the datasource.

    Type
    Error
Returns:
Type:
FetchResponse

a fetch response object containing a promise that resolves to the data returned from the datasource.

Foption(optionName) → {*}

A read-only accessor for all the options.

Parameters:
Name Type Description
optionName string

the name of the option to get the value for

Throws:

a validation error is thrown if parameter validation fails.

Type
external:validateParams.ValidationError
Returns:
Type:
*

the option's value, or undefined if the option is not defined.

Fregister()

An alias for .registerDatasource().

See:
  • Databridge.registerDatasource

FregisterDatasource(datasourceName, datasource) → {Databridge}

A function for adding a data source to the bridge.

As well as storing the datasource so it can be accessed by name via the .datasource() function, and invoked via the .fetchDataPromise() function, a shortcut function is also created to allow the data to be fetched via a function created on the bridge object with the same name as the datasource. I.e. the data can be fetched from a datasource with the name myDataSource on a databridge object named db with db.myDataSource().

See:
  • Databridge#fetchDataPromise
Parameters:
Name Type Description
datasourceName DatabridgeName

the name to register the datasource with.

datasource Datasource
Throws:

a validation error is thrown if parameter validation fails.

Type
external:validateParams.ValidationError
Returns:
Type:
Databridge

a reference to self to facilitate function chaining.

Fsource()

An alias for .datasource().

See:
  • Databridge.datasource