Skip to main content

Number

Introduction

Number is DynamoDB's Number data type (marshalled as "N") for DynamoQL.

Define a Number

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
order: Number,
score: {
type: Number,
}
} as const);

Options

- primaryIndex

top-level only

boolean which markes attribute as HASH key and makes attribute as required, default is false.
A Schema can have only one primaryIndex.

import { Schema } from "dynamoql";

const userSchema = new Schema({
id: {
type: Number,
primaryIndex: true,
},
} as const);

- sortKey

top-level only

boolean which markes attribute as RANGE key and makes attribute as required, default is false.
A Schema can have only one sortKey.

import { Schema } from "dynamoql";

const userSchema = new Schema({
countryCode: {
type: Number,
primaryIndex: true,
},
cityCode: {
type: Number,
sortKey: true
}
} as const);

- LSI

top-level only

defines a Local Secondary Index.

LSI option is an object where you must provide:

  • indexName which should be unique across the Schema.
  • project which may be ALL | KEYS or string[] where strings are attriubute names defined in the Schema.
import { Schema } from "dynamoql";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
},
age: {
type: Number,
LSI: {
indexName: "age-index",
project: "ALL"
}
}
} as const);

- GSI

top-level only

defines a Global Secondary Index.

GSI option is an object where you must provide:

  • indexName which should be unique across the Schema.
  • project which may be ALL | KEYS or string[] where strings are attriubute names defined in the Schema.
import { Schema } from "dynamoql";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
},
age: {
type: Number,
GSI: {
indexName: "age-index",
project: "ALL"
}
}
} as const);

for composite table (HASH and RANGE) you must provide another attribute with GSI:

  • indexName which must be one of defined GSI indexName.
  • sortKey true.
import { Schema } from "dynamoql";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
},
age: {
type: Number,
GSI: {
indexName: "age-index",
project: "ALL"
}
},
order: {
type: Number,
GSI: {
indexName: "age-index",
sortKey: true
}
}
} as const);

- required

boolean which makes attribute as required or optionnal, default is false when type is defined with an Object.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
score: {
type: Number,
required: true
}
} as const);

- default

To set a default value for an attribute use default option.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
score: {
type: Number,
default: 5
}
} as const);

With this configuration when you put an Item into your table, your Item will contain score attribute with 5 as value.

default must be a Number.
Otherwise it will throw an error during dev time and runtime.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
score: {
type: Number,
default: "some-score" // DynamoQLInvalidTypeException: "score" expected to be "N" received "S".
}
} as const);

default can also be a (async) function which accepts one argument (put Item value) and must return a number.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
default: (item: Record<string, any>)=> {
if(item.moderator) {
return 5
}
return []
}
}
} as const);

- validate

validate option allows you to manually validate provided value in put and update commands.
To return an error you should return a string which explains value invalidity. Any other returned value is considered as valid.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
validate: (self: number)=> {
if(self > 10) {
return "Can not be greater than 10."
}
}
}
} as const);

- min

define minimum accepted value.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
min: 1
}
} as const);

- max

define maximum accepted value.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
max: 89
}
} as const);

- enum

define accepted values.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
enum: [1234, 789,]
}
} as const);

- set

To modify a value before storing it use set option.
set (async) function accepts 3 arguments:

  1. self provided value.
  2. item entier put Item object.
  3. setterInfo an optionnal value provided inside in put, batchPut, batchWrite, transactWrite command's options.

set will not be called if attribute doesn't exists in put Item object.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
set: (self: number, item: Record<string, any>, setterInfo?: any)=> {

if(item.moderator && setterInfo?.someCondition) {
return 12345
}

return self
}
}
} as const);

- get

When reteving an Item we can transform field's value with get option.

get (async) function accepts 3 arguments:

  1. self retrieved value.
  2. item entier retrieved Item object.
  3. getterInfo an optionnal value provided inside get, batchGet, transactGet, query, scan command's options.

get can return anything.
get will not be called if attribute doesn't exists in stored Item.

import { Schema } from "dynamoql";
import { randomUUID } from "crypto";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
score: {
type: Number,
get: (self: number, item: Record<string, any>, getterInfo?: any)=> {
if(getterInfo.someCondition) {
return {
level: 4,
label: "Good!"
}
}
return self
}
}
} as const);
info

get returned value's type affects Item type when retriving Item(s) from DynamoDB.

- description

add any information to the Schema for your personal usage.

Condition expression

Condition expression are not part of Schema but they are based on your defined Schema.
They are used in various DynamoDB operations to check for some condition(s).
DynamoQL supports all DynamoDB Condition expression.

- equals

{
score: 78
}

or

{
score: {
$eq: 78
}
}

- not equals

{
score: {
$neq: 78
}
}

- attribute exists

$exists can be true or false.

{
score: {
$exists: true
}
}

- attribute type

Checks for stored attribute type.

{
score: {
$type: Number
}
}

$type can be String, Number, Boolean, Null, Buffer, Object, Array, or {type: Set, items: String | Number | Buffer }

- in

Checks if provided array includes stored number.

{
score: {
$in: ["Steve", 214, "Sara", false, null, "John", true, "Mike",]
}
}

- greather than

$gt compares if stored number is greather than provided value.

{
score: {
$gt: 5
}
}

- greather than or equal

$gte compares if stored number is greather than or equal provided value.

{
score: {
$gte: 5
}
}

- little than

$lt compares if stored number is little than provided value.

{
score: {
$lt: 5
}
}

- little than or equal

$lte compares if stored number is little than or equal provided value.

{
score: {
$lte: 5
}
}

- between

$between compares if stored number is greather than or equal value1 AND is little than or equal value2.

{
score: {
$between: [value1, value2]
}
}

- AND

Checks for multiple conditions. Condition is valid if ALL specified conditions are satisfied.

{
score: {
$and: [
{
$lt: 100
},
{
$gt: 87
}
]
}
}

shorthand style

{
score: {
$lt: 100,
$gt: 87
}
}

- OR

Checks for multiple conditions. Condition is valid if at least one of specified conditions is satisfied.

{
score: {
$or: [
{
$eq: 6
},
{
$lt: 4
}
]
}
}

- NOT

Condition is valid if specified condition is NOT satisfied.

{
score: {
$not: {
$gt: 3
}
}
}

$not accepts any Condition expression.

When multiple conditions are provided inside $not: {} they are considered as $and condition.

{
score: {
$not: {
$eq: 6,
$gt: 8
}
}
}

longhand equivalent is:

{
score: {
$not: {
$and: [
{
$eq: 6
},
{
$gt: 8
}
]
}
}
}

Update expressions

Like Condition expression, Update expressions are not part of Schema, but they are based on defined Schema.

Update expressions are used in update, transactUpdate and transactWrite operations.

DynamoQL supports all DynamoDB update operations.

- set

$set replaces stored number by provided number.

{
score: {
$set: 78
}
}

shorthand version is

{
score: 78
}

- if not exists

$ifNotExists sets provided number if attribute do not exists in stored item.
$ifNotExists dont affects Condition expression and is attribute specific.
If attribute exists, stored value stays unchanged.

{
score: {
$ifNotExists: 78
}
}

- increase

$incr increases stored number by provided number.

{
score: {
$incr: 10
}
}