dynotable-orm API: queries, scans and conditions

How do the query, scan and condition chains work?

Model.query(field) starts a query chain and Model.scan() a scan. Each call adds a condition or a setting, and nothing is sent until exec(). The result is an array of items with count and lastKey: pass lastKey to startAt() to read the next page. A Condition uses the same chain for the condition of a write.

import {model} from 'dynotable-orm';
import {z} from 'zod';

const Order = model(
  'Order',
  z.object({customerId: z.string(), orderId: z.string(), status: z.enum(['open', 'paid'])}),
  {hashKey: 'customerId', rangeKey: 'orderId'}
);

const page = await Order.query('customerId').eq('c1').where('status').eq('open').limit(25).exec();
const next = await Order.query('customerId').eq('c1').startAt(page.lastKey).exec();

await Order.update(
  {customerId: 'c1', orderId: 'o1'},
  {status: 'paid'},
  {condition: Order.condition().where('status').eq('open')}
);
console.log(page.count, next.length);

The query picks its index from the conditions unless using() names one. The update input types and ifNotExists are here too, because an update and a condition share their paths.

Query type

type Query<M> = Retriever<'query', TypesOf<M>, ItemOf<M>>;

Type Parameters

Type Parameter
M extends AnyModel

Scan type

type Scan<M> = Retriever<'scan', TypesOf<M>, ItemOf<M>>;

Type Parameters

Type Parameter
M extends AnyModel

QueryResponse type

Extends

  • RetrieverResponse<T>

Type Parameters

Type Parameter
T

Indexable

[n: number]: T

Properties

PropertyTypeDescriptionInherited from
countnumber-RetrieverResponse.count
lastKey?Record<string, unknown>The key to pass to startAt() for the next page.-
queriedCountnumber--
timesQueriednumber--

ScanResponse type

Extends

  • RetrieverResponse<T>

Type Parameters

Type Parameter
T

Indexable

[n: number]: T

Properties

PropertyTypeDescriptionInherited from
countnumber-RetrieverResponse.count
lastKey?Record<string, unknown>The key to pass to startAt() for the next page.-
scannedCountnumber--
timesScannednumber--

ParallelScanResponse type

A parallel() scan's result: several unfinished segments give one key per segment.

Extends

Type Parameters

Type Parameter
T

Indexable

[key: number]: T

Properties

PropertyTypeInherited from
countnumberQueryResponse.count
lastKey?Record<string, unknown> | Record<string, unknown>[]-
scannedCountnumberOmit.scannedCount
timesScannednumberOmit.timesScanned

Condition

A condition, built with the dynamoose chain. Calls change the instance and return it, as in dynamoose. Keys and values are checked and converted against a model when the condition is rendered.

Type Parameters

Type ParameterDefault type
Tany
K extends stringnever

Constructors

Constructor

new Condition<T, K>(key?): Condition<T, K>;

Parameters

ParameterType
key?string

Returns

Condition<T, K>

Constructor

new Condition<T, K>(init): Condition<T, K>;

Parameters

ParameterType
initInit<T>

Returns

Condition<T, K>

Methods

and()

and(): this;

Readability only: conditions are joined with AND by default.

Returns

this


attribute()

attribute<P>(key): Condition<T, P>;

Type Parameters

Type Parameter
P extends string

Parameters

ParameterType
keyP

Returns

Condition<T, P>


attributeExists()

attributeExists(): Condition<T>;

Returns

Condition<T>


attributeNotExists()

attributeNotExists(): Condition<T>;

Returns

Condition<T>


beginsWith()

beginsWith(value): Condition<T>;

Parameters

ParameterType
valueExtract<ValueAt<T, K>, string>

Returns

Condition<T>


between()

between(low, high): Condition<T>;

Parameters

ParameterType
lowValueAt<T, K>
highValueAt<T, K>

Returns

Condition<T>


contains()

contains(value): Condition<T>;

A substring of a string, or an element of a Set or a list.

Parameters

ParameterType
valueElementAt<T, K>

Returns

Condition<T>


eq()

eq(value): Condition<T>;

Parameters

ParameterType
valueValueAt<T, K>

Returns

Condition<T>


exists()

exists(): Condition<T>;

Returns

Condition<T>


filter()

filter<P>(key): Condition<T, P>;

Type Parameters

Type Parameter
P extends string

Parameters

ParameterType
keyP

Returns

Condition<T, P>


ge()

ge(value): Condition<T>;

Parameters

ParameterType
valueValueAt<T, K>

Returns

Condition<T>


group()

group(condition): Condition<T>;

As in dynamoose, group calls parenthesis, so a spy on parenthesis sees both.

Parameters

ParameterType
conditionCondition<T, any> | ((condition) => Condition<T, any>)

Returns

Condition<T>


gt()

gt(value): Condition<T>;

Parameters

ParameterType
valueValueAt<T, K>

Returns

Condition<T>


in()

in(values): Condition<T>;

Parameters

ParameterType
valuesreadonly ValueAt<T, K>[]

Returns

Condition<T>


le()

le(value): Condition<T>;

Parameters

ParameterType
valueValueAt<T, K>

Returns

Condition<T>


lt()

lt(value): Condition<T>;

Parameters

ParameterType
valueValueAt<T, K>

Returns

Condition<T>


ne()

ne(value): Condition<T>;

Parameters

ParameterType
valueValueAt<T, K>

Returns

Condition<T>


not()

not(): this;

Negates the next comparison. beginsWith, in and between cannot be negated.

Returns

this


or()

or(): Condition<T>;

Returns

Condition<T>


parenthesis()

parenthesis(condition): Condition<T>;

Parameters

ParameterType
conditionCondition<T, any> | ((condition) => Condition<T, any>)

Returns

Condition<T>


toNode()

toNode(plan): ConditionNode | undefined;

Internal

The condition tree, or undefined when nothing was added. Throws on a key the model does not declare.

Parameters

ParameterType
planPlan

Returns

ConditionNode | undefined


where()

where<P>(key): Condition<T, P>;

Type Parameters

Type Parameter
P extends string

Parameters

ParameterType
keyP

Returns

Condition<T, P>

ConditionInput type

type ConditionInput = Condition<any, any> | RawCondition;

A dynamoose condition, checked against the model when the request is built, or a raw condition object.

RawCondition type

A raw condition object, passed through as is.

Properties

PropertyType
ConditionExpressionstring
ExpressionAttributeNames?Record<string, string>
ExpressionAttributeValues?Record<string, AttributeValue>

UpdateInput type

type UpdateInput<T> = IsAny<T> extends true ? Record<string, any> : SetInput<T> & object;

Plain fields are an implicit $SET.

Type Parameters

Type Parameter
T

UpdateInputOf type

type UpdateInputOf<M> = UpdateInput<TypesOf<M>['input']>;

What update takes: the schema's input, which it parses.

Type Parameters

Type Parameter
M extends AnyModel

UpdateOptions type

'ALL_OLD': when the condition fails, DynamoDB returns the stored item, and the thrown SDK error (its class unchanged) carries it read back as get reads it, in item.

Extends

Properties

PropertyTypeDescriptionInherited from
abortSignal?AbortSignal-CallOptions.abortSignal
allowUndeclared?booleanWrites a top-level attribute the schema does not declare as given (no check, Dates in the default storage), where the default, as in dynamoose, drops it. A condition on it takes a raw condition object.-
client?DynamoDBClient-CallOptions.client
condition?ConditionInput--
rawKey?boolean--
return?"item" | "request"--
returnValues?ReturnValue--
returnValuesOnConditionCheckFailure?ReturnValuesOnConditionCheckFailure-ConditionFailureOptions.returnValuesOnConditionCheckFailure

ifNotExists()

function ifNotExists<V>(value): IfNotExists<V>;

Type Parameters

Type Parameter
V

Parameters

ParameterType
valueV

Returns

IfNotExists<V>

IfNotExists type

A $SET value written only when the attribute is not stored yet: SET p = if_not_exists(p, v).

Type Parameters

Type ParameterDefault type
Vunknown

Properties

PropertyModifierType
valuereadonlyV

Updated