Represents an object mapper for a specific model.

Members

ModelBatchMapper

batching

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

String

name

Gets the name identifier of the model.

Constructor

new

ModelMapper

()

Methods

find

(Object doc, [Object docInfo], [Object or String executionOptions])

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

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' }});
Parameters:
Name Type Description
doc Object

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

docInfo optional Object

An object containing the additional document information.

docInfo.fields optional Array<String>

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.

docInfo.orderBy optional Object<String, String>

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.

docInfo.limit optional Number

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

executionOptions optional Object or String

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

executionOptions.executionProfile optional String

The name of the execution profile.

executionOptions.fetchSize optional Number

The amount of rows to retrieve per page.

executionOptions.pageState optional Number

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

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

Returns:
Type Description
Promise<Result>

A Promise that resolves to a Result instance.

findAll

([Object docInfo], [Object or String executionOptions])

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 Description
docInfo optional Object

An object containing the additional document information.

docInfo.fields optional Array<String>

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.

docInfo.orderBy optional Object<String, String>

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.

docInfo.limit optional Number

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

executionOptions optional Object or String

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

executionOptions.executionProfile optional String

The name of the execution profile.

executionOptions.fetchSize optional Number

The mount of rows to retrieve per page.

executionOptions.pageState optional Number

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

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

Returns:
Type Description
Promise<Result>

A Promise that resolves to a Result instance.

get

(Object doc, [Object docInfo], [Object or String executionOptions])

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.

Examples:
Get a video by id
videoMapper.get({ id })
Get a video by id, selecting specific columns
videoMapper.get({ id }, fields: ['name', 'description'])
Parameters:
Name Type Description
doc Object

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

docInfo optional Object

An object containing the additional document information.

docInfo.fields optional Array<String>

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 optional Object or String

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

executionOptions.executionProfile optional String

The name of the execution profile.

Returns:
Type Description
Promise<Object>

insert

(Object doc, [Object docInfo], [Object or String executionOptions])

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.

Examples:
Insert a video
videoMapper.insert({ id, name });
Parameters:
Name Type Description
doc Object

An object containing the properties to insert.

docInfo optional Object

An object containing the additional document information.

docInfo.fields optional Array<String>

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.

docInfo.ttl optional Number

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

docInfo.ifNotExists optional Boolean

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 optional Object or String

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

executionOptions.executionProfile optional String

The name of the execution profile.

executionOptions.isIdempotent optional Boolean

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.

executionOptions.timestamp optional Number or Long

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.

Returns:
Type Description
Promise<Result>

A Promise that resolves to a Result instance.

mapWithQuery

(String query, function paramsHandler, [Object or String executionOptions])

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 Description
query String

The query to execute.

paramsHandler function

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

executionOptions optional Object or String

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.

executionOptions.executionProfile optional String

The name of the execution profile.

executionOptions.fetchSize optional Number

Amount of rows to retrieve per page.

executionOptions.isIdempotent optional Boolean

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

executionOptions.pageState optional Number

Buffer or string token representing the paging state.

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

executionOptions.timestamp optional Number or Long

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.

Returns:
Type Description
function

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

remove

(Object doc, [Object docInfo], [Object or String executionOptions])

Deletes a document.

Examples:
Delete a video
videoMapper.remove({ id });
Parameters:
Name Type Description
doc Object

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

docInfo optional Object

An object containing the additional doc information.

docInfo.when optional Object

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.

docInfo.ifExists optional Boolean

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.

docInfo.fields optional Array<String>

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.

docInfo.deleteOnlyColumns optional Boolean

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 optional Object or String

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

executionOptions.executionProfile optional String

The name of the execution profile.

executionOptions.isIdempotent optional Boolean

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.

executionOptions.timestamp optional Number or Long

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.

Returns:
Type Description
Promise<Result>

A Promise that resolves to a Result instance.

update

(Object doc, [Object docInfo], [Object or String executionOptions])

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.

Examples:
Update the name of a video
videoMapper.update({ id, name });
Parameters:
Name Type Description
doc Object

An object containing the properties to update.

docInfo optional Object

An object containing the additional document information.

docInfo.fields optional Array<String>

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.

docInfo.ttl optional Number

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

docInfo.ifExists optional Boolean

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.

docInfo.when optional Object

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 optional Object or String

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

executionOptions.executionProfile optional String

The name of the execution profile.

executionOptions.isIdempotent optional Boolean

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.

executionOptions.timestamp optional Number or Long

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.

Returns:
Type Description
Promise<Result>

A Promise that resolves to a Result instance.