Interface: ServerlessDbLiteNode

ServerlessDbLite.ServerlessDbLiteNode

Represents a data node in a database managed by a ServerlessDbLite.

This interface provides methods to read or write data from the database, and subscribe to event notifications via webhooks (node changes, child added,...).

Instances of this interface are first provided by the ServerlessDbLite#rootNode property, and further by the various browsing-related members of the got ServerlessDbLite.ServerlessDbLiteNode instance.

Since:
  • 3.4

Members

(nullable) key :string

Returns the key of the data node represented by this ServerlessDbLite.ServerlessDbLiteNode or null if this instance represents the root node of the database.

Type:
  • string

(nullable) parent :ServerlessDbLite.ServerlessDbLiteNode

Returns the parent node of this ServerlessDbLite.ServerlessDbLiteNode in the database or null if this instance represents the root node.

Type:

path :string

Returns the path of the data node represented by this ServerlessDbLite.ServerlessDbLiteNode as a slash-separated string.

Type:
  • string

Methods

clear() → {Promise}

Clears data at this ServerlessDbLite.ServerlessDbLiteNode. This method is equivalent to the ServerlessDbLite.ServerlessDbLiteNode#set one passing null as value.

Returns:
Type
Promise

get() → {Promise.<?(number|string|boolean|Object)>}

Retrieves the value associated with this ServerlessDbLite.ServerlessDbLiteNode as a JSON value. Subtrees whose keys are integers are mapped to JSON arrays.

This operation returns null if the node does not exist, and fails if there is no network connectivity.

See:
  • getRawValue
Returns:

A literal or data object.

Type
Promise.<?(number|string|boolean|Object)>
Example
const app = Webcom.App("contacts");
const node = app.ServerlessDbLite.rootNode.relativeNode("friends");
ref.get().then(returnedData => {
    console.log(returnedData);
    // [{"firstname": "John", "name": "Doe"}, {"firstname": "David", "name": "Smith"}]
});

getChildCount() → {Promise.<number>}

Returns the number of children of this ServerlessDbLite.ServerlessDbLiteNode when it represents a JSON object. If the node has a literal value (string, number or boolean), and not a JSON object, value -1 is returned.

Returns:
Type
Promise.<number>

getChildren() → {Promise.<?(Object)>}

Returns a simplified (and lightweight) value of the data at this ServerlessDbLite.ServerlessDbLiteNode.

This operation returns null if the node does not exist, and fails if there is no network connectivity.

Children of this ServerlessDbLite.ServerlessDbLiteNode node are returned within a JSON object, with the following values:

  • A literal value number/string/boolean if this child node is a leaf.
  • An empty JSON object ({}) otherwise, that is, if the child node is a JSON object.

Note: If the ServerlessDbLite.ServerlessDbLiteNode is itself a leaf (so without children) an empty JSON object ({}) is returned.

Returns:

A JSON object with a key for each child of this ServerlessDbLite.ServerlessDbLiteNode.

Type
Promise.<?(Object)>

getKeys() → {Promise.<Array.<string>>}

Returns an array with the child keys of this ServerlessDbLite.ServerlessDbLiteNode, or a literal value number/string/boolean if this child node is a leaf.

Returns:
Type
Promise.<Array.<string>>

getRawValue() → {Promise.<?(number|string|boolean|Object)>}

Retrieves the raw value associated with this ServerlessDbLite.ServerlessDbLiteNode. The raw value is either a literal or a treelike value involving exclusively JSON objects (with no JSON array at any level).

This operation returns 'null' if the node does not exist, and fails if there is no network connectivity.

See:
  • get
Returns:

A literal or data object.

Type
Promise.<?(number|string|boolean|Object)>
Example
const app = Webcom.App("contacts");
const node = app.serverlessDbLite.rootNode.relativeNode("friends");
node.getRawValue().then(returnedData => {
    console.log(returnedData);
    // {"0": {"firstname": "John", "name": "Doe"},
    //  "1": {"firstname": "David", "name": "Smith"}}
});

increment(step, startValueopt) → {Promise}

Increments the value at this ServerlessDbLite.ServerlessDbLiteNode.

If the data node has a non-numeric value, then the increment fails.

If the data node has no value, then the value of the startValue parameter or '0' is used as initial value before executing the increment operation.

Parameters:
Name Type Attributes Description
step number

The amount to add to the current data node value.

startValue number <optional>

The initial value to be used in case the node does not exist.

Returns:
Type
Promise

merge(value) → {Promise}

Merges some data with the data at this ServerlessDbLite.ServerlessDbLiteNode.

This operation will fail if there is no network connectivity, the request is not queued to wait for future connectivity.

Parameters:
Name Type Description
value Object

The value to merge with the current data node. It must be serializable to a regular JSON object. Its 1st-level keys are merged with the existing ones at the current data node. Keys from the value to merge overwrite keys with the same name in the current data. Keys from the current data that don't occur in the value to merge are kept as is.

Returns:
Type
Promise

push(value) → {Promise.<string>}

Creates a child node of this ServerlessDbLite.ServerlessDbLiteNode with a unique key and writes a given value at it.

Parameters:
Name Type Description
value any

The value to write at the created child node.

Returns:

The returned Promise is resolved with the key of the created child node.

Type
Promise.<string>

relativeNode(relativePath) → {ServerlessDbLite.ServerlessDbLiteNode}

Returns the data node specified by a path relatively to this ServerlessDbLite.ServerlessDbLiteNode.

Parameters:
Name Type Description
relativePath string

The path of the targeted node relatively to this instance, expressed with a slash-separated string.
It may include:

  • .. or . path segments, which respectively represent (as usual) the parent and the current node of the previous segment.
  • #-prefixed path segments, which are automatically expanded in special hashed path segments - especially useful to handle very large number of child nodes.
  • .list.-prefixed path-segments, which force the Webcom back end to shard the management of each of their children over independent processes of the cluster, in order to optimize their massive and concurrent access (see List of independent items). As a consequence, the data of such nodes cannot be read one shot as a whole.
  • .item.-prefixed path-segments, which prevent the Webcom back end from sharding the management of their children over independent processes, and so force to always keep their data as a whole (see Non fragmentable node). As a consequence, the size of such nodes cannot exceed the maximum size of a data node (see Weight limitations).
Returns:
Type
ServerlessDbLite.ServerlessDbLiteNode

set(value, comparedValueopt) → {Promise}

Writes some data at this ServerlessDbLite.ServerlessDbLiteNode.

This operation will fail if there is no network connectivity, the request is not queued to wait for future connectivity.

Parameters:
Name Type Attributes Description
value any

The value to set at the current data node. It must be serializable to a regular JSON value (e.g. null, a string, a number, a boolean or a JSON object). If null or undefined, the method is equivalent to ServerlessDbLite.ServerlessDbLiteNode#clear.

comparedValue any <optional>

When specified, the write operation is aborted by the Webcom back end if the actual value of the current data node is different from the specified value.
Using this parameter makes the write operation a "compare-and-set" operation, which guarantees that the data is really written only if the data node has not been modified by another client in the meanwhile.

Returns:
Type
Promise

subscribe(event, webhook) → {Promise.<ServerlessDb.Subscription>}

Subscribes to a data event to watch on the current ServerlessDbLite.ServerlessDbLiteNode through a ServerlessDb.Webhook.

The resulting subscription may be further unsubscribed either by calling the ServerlessDb.Subscription#cancel method on the ServerlessDb.Subscription instance resolved by the returned Promise, or by calling the ServerlessDbLite.ServerlessDbLiteNode#unsubscribe method (which doesn't need further a reference to the ServerlessDb.Subscription instance).

Parameters:
Name Type Description
event ServerlessDb.EventDescriptor

The data event to watch.

webhook ServerlessDb.Webhook

The webhook to use for notifying when subscribed events are raised.
Note: Using a webhook requires to be authenticated. Read permissions required to watch specified data events will be evaluated in real-time by the Webcom back end according to the current authentication state at the time this method is called.

Returns:
Type
Promise.<ServerlessDb.Subscription>

unsubscribe(webhook, notifyCancelationopt) → {Promise}

Unsubscribes a given ServerlessDb.Webhook from all data event previously subscribed to using the ServerlessDbLite.ServerlessDbLiteNode#subscribe method on the current ServerlessDbLite.ServerlessDbLiteNode.

Parameters:
Name Type Attributes Default Description
webhook ServerlessDb.Webhook

The webhook that unsubscribes events.
Note: Using a webhook requires to be authenticated with the same user as the one used with the ServerlessDbLite.ServerlessDbLiteNode#subscribe method to create the subscription to cancel.

notifyCancelation boolean <optional>
false

Indicates whether to notify this subscription cancelation to the webhook.

Returns:

This method will asynchronously resolve the returned Promise when performed.

Type
Promise
show
deprecated