Set
Introduction
Set
represents DynamoDB's "SS", "NS", "BS" data types.
With DynamoQL Every Set
type must be defined with its items
type.
items
can be String, Number, Date or Buffer.
Define an Set
- DynamoQL
- produced type
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
friendId: {
type: Set,
items: String
}
} as const);
interface IUserSchema {
id: string,
friendId?: Set<string>
}
items
can not be a union type.
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
friendId: {
type: Set,
items: [String, Number]
}
} as const);
However you can use union type for the entier Set type
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
friendId: [
{
type: Set,
items: String
},
{
type: Set,
items: Number
},
]
} as const);
Options
- required
boolean which makes attribute as required or optionnal, default is false
.
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
friendId: {
type: Set,
items: String,
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
},
friendId: {
type: Set,
items: String,
default: new Set(["steve", "john"])
}
} as const);
default
value's type must match defined Set's type.
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
},
friendId: {
type: Set,
items: String,
default: new Set([1, 2, 9]) // DynamoQLInvalidTypeException: "friendId[0]" expected to be "SS" received "NS".
}
} as const);
default
can also be a (async) function which accepts one argument (put Item value) and must return a Set of defined type.
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
friendId: {
type: Set,
items: String,
default: (item: Record<string, any>)=> {
if(item.moderator) {
return ["admin-id"]
}
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,
friendId: {
type: Set,
items: String,
validate: (self: Set<string>)=> {
if(self.length > 10) {
return "Error: Can not have more than 10 friends."
}
}
}
} as const);
- set
To modify a value before storing it use set
option.
set (async) function accepts 3 arguments:
self
provided value.item
entier put Item object.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,
friendId: {
type: Set,
items: String,
set: (self: Set<string>, item: Record<string, any>, setterInfo?: any)=> {
if(item.moderator && setterInfo?.someCondition) {
return new Set(["admin"])
}
return self
}
}
} as const);
- get
When reteving an Item we can transform field's value with get option.
get (async) function accepts 3 arguments:
self
retrieved value.item
entier retrieved Item object.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,
friendId: {
type: Set,
items: String,
get: (self: Set<string>, item: Record<string, any>, getterInfo?: any)=> {
return Array.from(self.values()).map(x=> doSomething(x))
}
}
} as const);
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
{
friendId: new Set(["user-1"])
}
or
{
friendId: {
$eq: new Set(["user-1"])
}
}
- not equals
{
friendId: {
$neq: new Set(["user-1"])
}
}
- attribute exists
$exists
can be true
or false
.
{
friendId: {
$exists: true
}
}
- includes
Checks if stored Set contains provided value.
{
friendId: {
$includes: "user-1"
}
}
- Set length
Checks for stored Set's length.
{
friendId: {
$size: 1
}
}
$size
can also be any valid numeric comparison operator.
{
friendId: {
$size: {
$gt: 0
}
}
}
Possible operators are $eq, $neq, $gt, $gte, $lt, $lte, $in, $between.
- attribute type
Checks for stored attribute type.
{
friendId: {
$type: {
type: Set,
items: String
}
}
}
$type
can be String, Number, Boolean, Null, Buffer, Object, Array, or {type: Set, items: String | Number | Buffer }
- in
Checks if provided array includes stored Set.
{
friendId: {
$in: ["Steve", 214, "Sara", false, "John", "Mike", null, new Set(["user-1"])]
}
}
- AND
Checks for multiple conditions. Condition is valid if ALL specified conditions are satisfied.
{
friendId: {
$and: [
{
$size: 6
},
{
$includes: "user-1"
}
]
}
}
shorthand style
{
friendId: {
$size: 6,
$includes: "user-1"
}
}
- OR
Checks for multiple conditions. Condition is valid if at least one of specified conditions is satisfied.
{
friendId: {
$or: [
{
$size: 6
},
{
$includes: "user-1"
}
]
}
}
- NOT
Condition is valid if specified condition is NOT satisfied.
{
friendId: {
$not: {
$includes: "user-1"
}
}
}
$not
accepts any Condition expression.
When multiple conditions are provided inside $not: {} they are considered as $and condition.
{
friendId: {
$not: {
$size: 6,
$includes: "user-1"
}
}
}
longhand equivalent is:
{
friendId: {
$not: {
$and: [
{
$size: 6
},
{
$includes: "user-1"
}
]
}
}
}
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 Set by provided Set.
{
friendId: {
$set: new Set(["user-1"])
}
}
shorthand version is
{
friendId: new Set(["user-1"])
}
- if not exists
$ifNotExists
sets provided Set if attribute do not exists in stored item.
$ifNotExists
dont affects Condition expression and is attribute specific.
If attribute exists, stored value stays unchanged.
{
friendId: {
$ifNotExists: new Set(["user-1"])
}
}
- add
Adds value(s) to stored Set.
{
friendId: {
$add: "user-1"
}
}
Add multiple values.
{
friendId: {
$add: ["user-1", "user-2"]
}
}
- delete
Deletes value(s) from stored Set.
{
friendId: {
$delete: "user-1"
}
}
Delete multiple values.
{
friendId: {
$delete: ["user-1", "user-2"]
}
}