Schema
new dynamoose.Schema(schema[, options])
You can use this method to create a schema. The schema
parameter is an object defining your schema, each value should be a type or object defining the type with additional settings (listed below).
The options
parameter is an optional object with the following options:
Name | Type | Default | Information |
---|---|---|---|
saveUnknown | array | boolean | false | This setting lets you specify if the schema should allow properties not defined in the schema. If you pass true in for this option all unknown properties will be allowed. If you pass in an array of strings, only properties that are included in that array will be allowed. If you pass in an array of strings, you can use * to indicate a wildcard nested property one level deep, or ** to indicate a wildcard nested property infinite levels deep (ex. ["person.*", "friend.**"] will allow you store a property person with 1 level of unknown properties and friend with infinitely nested level unknown properties). If you retrieve documents from DynamoDB with saveUnknown enabled, all custom Dynamoose types will be returned as the underlying DynamoDB type (ex. Dates will be returned as a Number representing number of milliseconds since Jan 1 1970). |
timestamps | boolean | object | false | This setting lets you indicate to Dynamoose that you would like it to handle storing timestamps in your documents for both creation and most recent update times. If you pass in an object for this setting you must specify two keys createdAt & updatedAt , each with a value of a string or array of strings being the name of the attribute(s) for each timestamp. If you pass in null for either of those keys that specific timestamp won't be added to the schema. If you set this option to true it will use the default attribute names of createdAt & updatedAt . |
Attribute Types
Type | Set Allowed | DynamoDB Type | Custom Dynamoose Type | Nested Type | Settings | Notes |
---|---|---|---|---|---|---|
String | True | S | False | False | ||
Boolean | False | BOOL | False | False | ||
Number | True | N | False | False | ||
Buffer | True | B | False | False | ||
Date | True | N | True | False | storage - miliseconds | seconds (default: miliseconds) | Will be stored in DynamoDB as milliseconds since Jan 1 1970, and converted to/from a Date instance. |
Object | False | M | False | True | ||
Array | False | L | False | True | ||
dynamoose.NULL | False | NULL | False | False | ||
Schema | False | M | True | True | This will be converted to an Object type. | |
Model | Only if no rangeKey for model's schema | S | N | B | M | True | If rangeKey in model's schema | Model Types are setup a bit differently. Read below for more information. | |
Combine | False | S | True | False | attributes - [string] - The attributes to store in the combine attribute. seperator - string (default: , ) - The string used to seperate the attributes in the combine attribute. | When running Model.update you must update all the attributes in the combine attributes array, or none of them. This is to ensure your combine method remains in sync with your overall document. |
Constant | False | S | N | BOOL | True | False | value - string | number | boolean - The value this attribute should always match. |
Set's are different from Array's since they require each item in the Set be unique. If you use a Set, it will use the underlying JavaScript Set instance as opposed to an Array. If you use a set you will define the type surrounded by brackets in the schema
setting. For example to define a string set you would do something like:
When using saveUnknown
with a set, the type recognized by Dynamoose will be the underlying JavaScript Set constructor. If you have a set type defined in your schema the underlying type will be an Array.
Custom Dynamoose Types are not supported with the saveUnknown
property. For example, if you wish you retrieve a document with a Date type, Dynamoose will return it as a number if that property does not exist in the schema and saveUnknown
is enabled for that given property.
For types that are Nested Types
, you must define a schema
setting that includes the nested schema for that given attribute.
You can also define an array of types to allow your attribute to match any one of multiple types you set. For example in the following code example, the data
attribute can either be of type String or Number.
In the event you have multiple types that match (Date & Number, Set & Array, multple Objects with different Schemas), Dynamoose will attempt to pick the closest matching type. However, if all types are valid, Dynamoose will default to the first type in the array.
You are also not allowed to have multiple types on any hashKey
or rangeKey
attributes. DynamoDB requires that these key attributes only have one type.
Model Types
For Model types, you must pass in another model or dynamoose.THIS
(to reference your own model).
You can then set documents to be a Document instance of that Model, a value of the hashKey
(if you don't have a rangeKey
), or an object representing the hashKey
& rangeKey
(if you have a rangeKey
).
You can then call document.populate
to populate the instances with the documents.
Attribute Settings
type: type | object
The type attribute can either be a type (ex. Object
, Number
, etc.) or an object that has additional information for the type. In the event you set it as an object you must pass in a value
for the type, and can optionally pass in a settings
object.
schema: object | array
This property is only used for the Object
or Array
attribute types. It is used to define the schema for the underlying nested type. For Array
attribute types, this value must be an Array
with one element defining the schema. This element for Array
attribute types can either be another raw Dynamoose type (ex. String
), or an object defining a more detailed schema for the Array
elements. For Object
attribute types this value must be an object defining the schema. Some examples of this property in action can be found below.
You can also define an array attribute that accepts more than one data type. The following example will allow the friends
attribute to be an array of strings, or an array of numbers, but the elements in the array must all be strings or must all be numbers.
default: value | function | async function
You can set a default value for an attribute that will be applied upon save if the given attribute value is null
or undefined
. The value for the default property can either be a value or a function that will be executed when needed that should return the default value. By default there is no default value for attributes.
You can also pass in async functions or a function that returns a promise to the default property and Dynamoose will take care of waiting for the promise to resolve before saving the object.
forceDefault: boolean
You can set this property to always use the default
value, even if a value is already set. This can be used for data that will be used as sort or secondary indexes. The default for this property is false.
validate: value | RegExp | function | async function
You can set a validation on an attribute to ensure the value passes a given validation before saving the document. In the event you set this to be a function or async function, Dynamoose will pass in the value for you to validate as the parameter to your function. Validation will only be run if the item exists in the document. If you'd like to force validation to be run every time (even if the attribute doesn't exist in the document) you can enable required
.
required: boolean
You can set an attribute to be required when saving documents to DynamoDB. By default this setting is false
.
In the event the parent object is undefined and required
is set to false
on that parent attribute, the required check will not be run on child attributes.
enum: array
You can set an attribute to have an enum array, which means it must match one of the values specified in the enum array. By default this setting is undefined and not set to anything.
This property is not a replacement for required
. If the value is undefined or null, the enum will not be checked. If you want to require the property and also have an enum
you must use both enum
& required
.
get: function | async function
You can use a get function on an attribute to be run whenever retrieving a document from DynamoDB. This function will only be run if the item exists in the document. Dynamoose will pass the DynamoDB value into this function and you must return the new value that you want Dynamoose to return to the application.
set: function | async function
You can use a set function on an attribute to be run whenever saving a document to DynamoDB. This function will only be run if the attribute exists in the document. Dynamoose will pass the value you provide into this function and you must return the new value that you want Dynamoose to save to DynamoDB.
Unlike get
this method will additionally pass in the original value as the second parameter (if avaiable). Internally Dynamoose uses the document.original()
method to access the original value. This means that using Model.batchPut
, Model.update
or any other document save method that does not have access to document.original()
this second parameter will be undefined
.
index: boolean | object | array
Indexes on your DynamoDB tables must be defined in your Dynamoose schema. If you have the update option set to true on your model settings, and a Dynamoose schema index does not already exist on the DynamoDB table, it will be created on model initialization. Similarily, indexes on your DynamoDB table that do not exist in your Dynamoose schema will be deleted.
If you pass in an array for the value of this setting it must be an array of index objects. By default no indexes are specified on the attribute.
Your index object can contain the following properties:
Name | Type | Default | Notes |
---|---|---|---|
name | string | ${attribute}${global ? "GlobalIndex" : "LocalIndex"} | Name of index |
global | boolean | false | If the index should be a global secondary index or not. Attribute will be the hashKey for the index. |
rangeKey | string | undefined | The range key attribute name for a global secondary index. |
project | boolean | [string] | true | Sets the attributes to be projected for the index. true projects all attributes, false projects only the key attributes, and an array of strings projects the attributes listed. |
throughput | number | {read: number, write: number} | undefined | Sets the throughput for the global secondary index. |
If you set index
to true
, it will create an index with all of the default settings.
hashKey: boolean
You can set this to true to overwrite what the hashKey
for the Model will be. By default the hashKey
will be the first key in the Schema object.
hashKey
is commonly called a partition key
in the AWS documentation.
rangeKey: boolean
You can set this to true to overwrite what the rangeKey
for the Model will be. By default the rangeKey
won't exist.
rangeKey
is commonly called a sort key
in the AWS documentation.