Boolean
Boolean
is DynamoDB's Number data type (marshalled as "BOOL") for DynamoQL.
- TypeScript
- JSDoc
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
isActive: Boolean,
deleted: {
type: Boolean
}
} as const);
// @ts-check
const { Schema } = require("dynamoql");
const { randomUUID } = require("crypto");
const userSchema = new Schema(/** @type {const} */({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
isActive: Boolean,
deleted: {
type: Boolean
}
}));
Options
- 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
},
isActive: {
type: Boolean,
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
},
isActive: {
type: Boolean,
default: true
}
} as const);
With this configuration when you put
an Item into your table, your Item will contain isActive attribute with true as value.
default
must be a Boolean.
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
},
isActive: {
type: Boolean,
default: "some-value" // DynamoQLInvalidTypeException: "isActive" expected to be "BOOL" received "S".
}
} as const);
default
can also be a (async) function which accepts one argument (put Item value) and must return a Boolean.
import { Schema } from "dynamoql";
import { randomUUID } from "crypto";
const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
default: randomUUID
},
moderator: Boolean,
isActive: {
type: Boolean,
default: (item: Record<string, any>)=> {
if(item.moderator) {
return false
}
return false
}
}
} 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,
score: {
type: Boolean,
set: (self: boolean, item: Record<string, any>, setterInfo?: any)=> {
if(item.moderator && setterInfo?.someCondition) {
return true
}
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,
score: {
type: Boolean,
get: (self: boolean, item: Record<string, any>, getterInfo?: any)=> {
if(getterInfo.someCondition) {
return null
}
return self
}
}
} 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
{
isActive: true
}
or
{
isActive: {
$eq: true
}
}
- not equals
{
isActive: {
$neq: true
}
}
- attribute exists
$exists
can be true
or false
.
{
isActive: {
$exists: true
}
}
- attribute type
Checks for stored attribute type.
{
isActive: {
$type: Boolean
}
}
$type
can be String, Number, Boolean, Null, Buffer, Object, Array, or {type: Set, items: String | Number | Buffer }
- in
Checks if provided array includes stored boolean.
{
isActive: {
$in: ["Steve", 214, "Sara", false, null, "John", true, "Mike",]
}
}
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 boolean by provided boolean.
{
isActive: {
$set: true
}
}
shorthand version is
{
isActive: true
}
- if not exists
$ifNotExists
sets provided boolean if attribute do not exists in stored item.
$ifNotExists
dont affects Condition expression and is attribute specific.
If attribute exists, stored value stays unchanged.
{
isActive: {
$ifNotExists: true
}
}