Class: ModelMapper

mapping~ModelMapper()

Represents an object mapper for a specific model.

Constructor

new ModelMapper()

Source:

Members

batching :ModelBatchMapper

Gets a ModelBatchMapper instance containing utility methods to group multiple doc mutations in a single batch.

Type:
  • ModelBatchMapper
Source:

name :String

Gets the name identifier of the model.

Type:
  • String
Source:

Methods

find(doc, docInfoopt, executionOptionsopt) → {Promise.<Result>}

Executes a SELECT query based on the filter and returns the result as an iterable of documents.

Parameters:
Name Type Attributes Description
doc Object

An object containing the properties that map to the primary keys to filter.

docInfo Object <optional>

An object containing the additional document information.

Properties
Name Type Attributes Description
fields Array.<String> <optional>

An Array containing the name of the properties that will be used in the SELECT cql statement generated, in order to restrict the amount of columns retrieved.

orderBy Object.<String, String> <optional>

An associative array containing the column names as key and the order string (asc or desc) as value used to set the order of the results server-side.

limit Number <optional>

Restricts the result of the query to a maximum number of rows on the server.

executionOptions Object | String <optional>

An object containing the options to be used for the requests execution or a string representing the name of the execution profile.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

fetchSize Number <optional>

The amount of rows to retrieve per page.

pageState Number <optional>

A Buffer instance or a string token representing the paging state.

When provided, the query will be executed starting from a given paging state.

Source:
Returns:

A Promise that resolves to a Result instance.

Type
Promise.<Result>
Examples

Get user's videos

const result = await videoMapper.find({ userId });
for (let video of result) {
  console.log(video.name);
}

Get user's videos from a certain date

videoMapper.find({ userId, addedDate: q.gte(date)});

Get user's videos in reverse order

videoMapper.find({ userId }, { orderBy: { addedDate: 'desc' }});

findAll(docInfoopt, executionOptionsopt) → {Promise.<Result>}

Executes a SELECT query without a filter and returns the result as an iterable of documents.

This is only recommended to be used for tables with a limited amount of results. Otherwise, breaking up the token ranges on the client side should be used.

Parameters:
Name Type Attributes Description
docInfo Object <optional>

An object containing the additional document information.

Properties
Name Type Attributes Description
fields Array.<String> <optional>

An Array containing the name of the properties that will be used in the SELECT cql statement generated, in order to restrict the amount of columns retrieved.

orderBy Object.<String, String> <optional>

An associative array containing the column names as key and the order string (asc or desc) as value used to set the order of the results server-side.

limit Number <optional>

Restricts the result of the query to a maximum number of rows on the server.

executionOptions Object | String <optional>

An object containing the options to be used for the requests execution or a string representing the name of the execution profile.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

fetchSize Number <optional>

The mount of rows to retrieve per page.

pageState Number <optional>

A Buffer instance or a string token representing the paging state.

When provided, the query will be executed starting from a given paging state.

Source:
Returns:

A Promise that resolves to a Result instance.

Type
Promise.<Result>

get(doc, docInfoopt, executionOptionsopt) → {Promise.<Object>}

Gets the first document matching the provided filter or null when not found.

Note that all partition and clustering keys must be defined in order to use this method.

Parameters:
Name Type Attributes Description
doc Object

The object containing the properties that map to the primary keys.

docInfo Object <optional>

An object containing the additional document information.

Properties
Name Type Attributes Description
fields Array.<String> <optional>

An Array containing the name of the properties that will be used in the SELECT cql statement generated, in order to restrict the amount of columns retrieved.

executionOptions Object | String <optional>

An object containing the options to be used for the requests execution or a string representing the name of the execution profile.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

Source:
Returns:
Type
Promise.<Object>
Examples

Get a video by id

videoMapper.get({ id })

Get a video by id, selecting specific columns

videoMapper.get({ id }, fields: ['name', 'description'])

insert(doc, docInfoopt, executionOptionsopt) → {Promise.<Result>}

Inserts a document.

When the model is mapped to multiple tables, it will insert a row in each table when all the primary keys are specified.

Parameters:
Name Type Attributes Description
doc Object

An object containing the properties to insert.

docInfo Object <optional>

An object containing the additional document information.

Properties
Name Type Attributes Description
fields Array.<String> <optional>

An Array containing the name of the properties that will be used in the INSERT cql statements generated. If specified, it must include the columns to insert and the primary keys.

ttl Number <optional>

Specifies an optional Time To Live (in seconds) for the inserted values.

ifNotExists Boolean <optional>

When set, it only inserts if the row does not exist prior to the insertion.

Please note that using IF NOT EXISTS will incur a non negligible performance cost so this should be used sparingly.

executionOptions Object | String <optional>

An object containing the options to be used for the requests execution or a string representing the name of the execution profile.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

isIdempotent Boolean <optional>

Defines whether the query can be applied multiple times without changing the result beyond the initial application.

By default all generated INSERT statements are considered idempotent, except in the case of lightweight transactions. Lightweight transactions at client level with transparent retries can break linearizability. If that is not an issue for your application, you can manually set this field to true.

timestamp Number | Long <optional>

The default timestamp for the query in microseconds from the unix epoch (00:00:00, January 1st, 1970).

When provided, this will replace the client generated and the server side assigned timestamp.

Source:
Returns:

A Promise that resolves to a Result instance.

Type
Promise.<Result>
Example

Insert a video

videoMapper.insert({ id, name });

mapWithQuery(query, paramsHandler, executionOptionsopt) → {function}

Uses the provided query and param getter function to execute a query and map the results. Gets a function that takes the document, executes the query and returns the mapped results.

Parameters:
Name Type Attributes Description
query String

The query to execute.

paramsHandler function

The function to execute to extract the parameters of a document.

executionOptions Object | String <optional>

When provided, the options for all executions generated with this method will use the provided options and it will not consider the executionOptions per call.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

fetchSize Number <optional>

Amount of rows to retrieve per page.

isIdempotent Boolean <optional>

Defines whether the query can be applied multiple times without changing the result beyond the initial application.

pageState Number <optional>

Buffer or string token representing the paging state.

When provided, the query will be executed starting from a given paging state.

timestamp Number | Long <optional>

The default timestamp for the query in microseconds from the unix epoch (00:00:00, January 1st, 1970).

When provided, this will replace the client generated and the server side assigned timestamp.

Source:
Returns:

Returns a function that takes the document and execution options as parameters and returns a Promise the resolves to a Result instance.

Type
function

remove(doc, docInfoopt, executionOptionsopt) → {Promise.<Result>}

Deletes a document.

Parameters:
Name Type Attributes Description
doc Object

A document containing the primary keys values of the document to delete.

docInfo Object <optional>

An object containing the additional doc information.

Properties
Name Type Attributes Description
when Object <optional>

A document that act as the condition that has to be met for the DELETE to occur. Use this property only in the case you want to specify a conditional clause for lightweight transactions (CAS). When the CQL query is generated, this would be used to generate the IF clause.

Please note that using IF conditions will incur a non negligible performance cost on the server-side so this should be used sparingly.

ifExists Boolean <optional>

When set, it only issues the DELETE command if the row already exists on the server.

Please note that using IF conditions will incur a non negligible performance cost on the server-side so this should be used sparingly.

fields Array.<String> <optional>

An Array containing the name of the properties that will be used in the DELETE cql statement generated. If specified, it must include the columns to delete and the primary keys.

deleteOnlyColumns Boolean <optional>

Determines that, when more document properties are specified besides the primary keys, the generated DELETE statement should be used to delete some column values but leave the row. When this is enabled and more properties are specified, a DELETE statement will have the following form: "DELETE col1, col2 FROM table1 WHERE pk1 = ? AND pk2 = ?"

executionOptions Object | String <optional>

An object containing the options to be used for the requests execution or a string representing the name of the execution profile.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

isIdempotent Boolean <optional>

Defines whether the query can be applied multiple times without changing the result beyond the initial application.

By default all generated DELETE statements are considered idempotent, except in the case of lightweight transactions. Lightweight transactions at client level with transparent retries can break linearizability. If that is not an issue for your application, you can manually set this field to true.

timestamp Number | Long <optional>

The default timestamp for the query in microseconds from the unix epoch (00:00:00, January 1st, 1970).

When provided, this will replace the client generated and the server side assigned timestamp.

Source:
Returns:

A Promise that resolves to a Result instance.

Type
Promise.<Result>
Example

Delete a video

videoMapper.remove({ id });

update(doc, docInfoopt, executionOptionsopt) → {Promise.<Result>}

Updates a document.

When the model is mapped to multiple tables, it will update a row in each table when all the primary keys are specified.

Parameters:
Name Type Attributes Description
doc Object

An object containing the properties to update.

docInfo Object <optional>

An object containing the additional document information.

Properties
Name Type Attributes Description
fields Array.<String> <optional>

An Array containing the name of the properties that will be used in the UPDATE cql statements generated. If specified, it must include the columns to update and the primary keys.

ttl Number <optional>

Specifies an optional Time To Live (in seconds) for the inserted values.

ifExists Boolean <optional>

When set, it only updates if the row already exists on the server.

Please note that using IF conditions will incur a non negligible performance cost on the server-side so this should be used sparingly.

when Object <optional>

A document that act as the condition that has to be met for the UPDATE to occur. Use this property only in the case you want to specify a conditional clause for lightweight transactions (CAS).

Please note that using IF conditions will incur a non negligible performance cost on the server-side so this should be used sparingly.

executionOptions Object | String <optional>

An object containing the options to be used for the requests execution or a string representing the name of the execution profile.

Properties
Name Type Attributes Description
executionProfile String <optional>

The name of the execution profile.

isIdempotent Boolean <optional>

Defines whether the query can be applied multiple times without changing the result beyond the initial application.

The mapper uses the generated queries to determine the default value. When an UPDATE is generated with a counter column or appending/prepending to a list column, the execution is marked as not idempotent.

Additionally, the mapper uses the safest approach for queries with lightweight transactions (Compare and Set) by considering them as non-idempotent. Lightweight transactions at client level with transparent retries can break linearizability. If that is not an issue for your application, you can manually set this field to true.

timestamp Number | Long <optional>

The default timestamp for the query in microseconds from the unix epoch (00:00:00, January 1st, 1970).

When provided, this will replace the client generated and the server side assigned timestamp.

Source:
Returns:

A Promise that resolves to a Result instance.

Type
Promise.<Result>
Example

Update the name of a video

videoMapper.update({ id, name });