Model
The Model object represents a table in DynamoDB. It takes in both a name and a schema and has methods to retrieve, and save documents in the database.
dynamoose.model[<Document>](name, [schema][, config])
This method is the basic entry point for creating a model in Dynamoose. When you call this method a new model is created, and it returns a Document initializer that you can use to create instances of the given model.
The name
parameter is a string representing the table name that will be used to store documents created by this model. Prefixes and suffixes may be added to this name using the config
options.
The schema
parameter can either be an object OR a Schema instance. If you pass in an object for the schema
parameter it will create a Schema instance for you automatically.
An optional TypeScript class which extends Document
can be provided right before the function bracket. This provides type checking when using operations like Model.create()
.
You can also pass in an array of Schema instances or schema objects into the schema
paremeter. This is useful for cases of single table design where you want one model to have multiple options for a schema. Behind the scenes Dynamoose will automatically pick the closest schema to match to your document, and use that schema for all operations pertaining to that document. If no matching schema can be found, it will default to the first schema in the array.
note
If you use multiple schemas in one model, the hash & range keys must match for all schemas.
If you don't pass the schema
parameter it is required that you have an existing model already registed with that name. This will use the existing model already registered.
The config
parameter is an object used to customize settings for the model.
Name | Description | Type | Default |
---|---|---|---|
create | If Dynamoose should attempt to create the table on DynamoDB. This function will run a describeTable call first to ensure the table doesn't already exist. For production environments we recommend setting this value to false . | Boolean | true |
throughput | An object with settings for what the throughput for the table should be on creation, or a number which will use the same throughput for both read and write. If this is set to ON_DEMAND the table will use the PAY_PER_REQUEST billing mode. If the table is not created by Dynamoose, this object has no effect. | Object | Number | String | |
throughput.read | What the read throughput should be set to. Only valid if throughput is an object. | Number | 1 |
throughput.write | What the write throughput should be set to. Only valid if throughput is an object. | Number | 1 |
prefix | A string that should be prepended to every model name. | String | "" |
suffix | A string that should be appended to every model name. | String | "" |
waitForActive | Settings for how DynamoDB should handle waiting for the table to be active before enabling actions to be run on the table. This property can also be set to false to easily disable the behavior of waiting for the table to be active. For production environments we recommend setting this value to false . | Object | Boolean | |
waitForActive.enabled | If Dynamoose should wait for the table to be active before running actions on it. | Boolean | true |
waitForActive.check | Settings for how Dynamoose should check if the table is active | Object | |
waitForActive.check.timeout | How many milliseconds before Dynamoose should timeout and stop checking if the table is active. | Number | 180000 |
waitForActive.check.frequency | How many milliseconds Dynamoose should delay between checks to see if the table is active. If this number is set to 0 it will use setImmediate() to run the check again. | Number | 1000 |
update | If Dynamoose should update the capacity of the existing table to match the model throughput. If this is a boolean of true all update actions will be run. If this is an array of strings, only the actions in the array will be run. The array can include the following settings to update, ttl , indexes , throughput . | Boolean | [String] | false |
populate | If Dynamoose should automatically run document.populate on retrieved items. | Boolean | false |
expires | The setting to describe the time to live for documents created. If you pass in a number it will be used for the expires.ttl setting, with default values for everything else. If this is undefined , no time to live will be active on the model. | Number | Object | undefined |
expires.ttl | The default amount of time the document should stay alive from creation time in milliseconds. | Number | undefined |
expires.attribute | The attribute name for where the document time to live attribute. | String | ttl (if expires is set to a number) |
expires.items | The options for documents with ttl. | Object | |
expires.items.returnExpired | If Dynamoose should include expired documents when returning retrieved documents. | Boolean | true |
The default object is listed below.
dynamoose.model.defaults.set(defaults)
This function is used to set default values for the config object for new models that are created. Ensure that you set this before initializing your models to ensure the defaults are applied to your models.
The priority of how the configuration gets set for new models is:
- Configuration object passed into model creation
- Custom defaults provided by
dynamoose.model.defaults.set(defaults)
- Dynamoose internal defaults
In the event that properties are not passed into the configuration object or custom defaults, the Dynamoose internal defaults will be used.
You can set the defaults by setting the property to a custom object:
In order to revert to the default and remove custom defaults you can set it to an empty object:
Model.table.create.request()
This function will return the object used to create the table with AWS. You can use this to create the table manually, for things like the Serverless deployment toolkit, or just to peak behind the scenes and see what Dynamoose is doing to create the table.
This function is an async function so you must wait for the promise to resolve before you are able to access the result.
Model.get(key[, settings][, callback])
You can use Model.get to retrieve a document from DynamoDB. This method uses the getItem
DynamoDB API call to retrieve the object.
This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the callback
parameter to have it be used in a callback format as opposed to a promise format. A Document instance will be the result of the promise or callback response. In the event no item can be found in DynamoDB this method will return undefined.
You can also pass in an object for the optional settings
parameter that is an object. The table below represents the options for the settings
object.
Name | Description | Type | Default |
---|---|---|---|
return | What the function should return. Can be document , or request . In the event this is set to request the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made. If this is request , the function will not be async anymore. | String | document |
attributes | What document attributes should be retrieved & returned. This will use the underlying ProjectionExpression DynamoDB option to ensure only the attributes you request will be sent over the wire. If this value is undefined , then all attributes will be returned. | [String] | undefined |
consistent | Whether to perform a strongly consistent read or not. If this value is undefined , then no ConsistentRead parameter will be included in the request, and DynamoDB will default to an eventually consistent read. | boolean | undefined |
In the event you have a rangeKey for your model, you can pass in an object for the key
parameter which includes the hashKey & rangeKey.
Model.batchGet(keys[, settings][, callback])
You can use Model.batchGet to retrieve multiple documents from DynamoDB. This method uses the batchGetItem
DynamoDB API call to retrieve the object.
This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the callback
parameter to have it be used in a callback format as opposed to a promise format. An array of Document instances will be the result of the promise or callback response. In the event no documents can be found in DynamoDB this method will return an empty array.
The array you receive back is a standard JavaScript array of objects. However, the array has some special properties with extra information about your scan operation that you can access. This does not prevent the ability do running loops or accessing the objects within the array.
The extra properties attached to the array are:
unprocessedKeys
- In the event there are more documents to get in DynamoDB this property will be equal to an array of unprocessed keys. You can take this property and callbatchGet
again to retrieve those documents. Normally DynamoDB returns this property as a DynamoDB object, but Dynamoose returns it and handles it as a standard JS object without the DynamoDB types.populate
- A function that is an alias todocument.populate
and will populate all documents in the array.
You can also pass in an object for the optional settings
parameter that is an object. The table below represents the options for the settings
object.
Name | Description | Type | Default |
---|---|---|---|
return | What the function should return. Can be documents , or request . In the event this is set to request the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made. If this is request , the function will not be async anymore. | String | documents |
attributes | What document attributes should be retrieved & returned. This will use the underlying AttributesToGet DynamoDB option to ensure only the attributes you request will be sent over the wire. If this value is undefined , then all attributes will be returned. | [String] | undefined |
In the event you have a rangeKey for your model, you can pass in an object for the key
parameter which includes the rangeKey & hashKey.
Model.create(document, [settings], [callback])
This function lets you create a new document for a given model. This function is almost identical to creating a new document and calling document.save
, with one key difference, this function will default to setting overwrite
to false.
If you do not pass in a callback
parameter a promise will be returned.
Model.batchPut(documents, [settings], [callback])
This saves documents to DynamoDB. This method uses the batchWriteItem
DynamoDB API call to store your objects in the given table associated with the model. This method is overwriting, and will overwrite the data you currently have in place for the existing key for your table.
This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the callback
parameter to have it be used in a callback format as opposed to a promise format. Nothing will be passed into the result for the promise or callback.
You can also pass a settings object in as the second parameter. The following options are available for settings are:
Name | Description | Type | Default |
---|---|---|---|
return | If the function should return the response or request . If you set this to request the request that would be made to DynamoDB will be returned, but no requests will be made to DynamoDB. | String | response |
Both settings
and callback
parameters are optional. You can pass in a callback
without settings
, just by passing in your array of objects as the first parameter, and the second argument as the callback
function. You are not required to pass in settings
if you just want to pass in a callback
.
Model.update(key[, updateObj[, settings]],[ callback])
This function lets you update an existing item in the database. You can either pass in one object combining both the hashKey you wish to update along with the update object, or keep them separate by passing in two objects.
key
can be a string representing the hashKey or an object containing the hashKey & rangeKey.
If you do not pass in a callback
parameter a promise will be returned.
You can also pass in a settings
object parameter to define extra settings for the update call. If you pass in a settings
parameter, the updateObj
parameter is required. The table below represents the options for the settings
object.
Name | Description | Type | Default |
---|---|---|---|
return | What the function should return. Can be document , or request . In the event this is set to request the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made. | String | document |
condition | This is an optional instance of a Condition for the update. | dynamoose.Condition | null |
returnValues | Set which documents to return after the update. This setting will be passed into the DynamoDB ReturnValues parameter. | String | ALL_NEW |
There are two different methods for specifying what you'd like to edit in the document. The first is you can just pass in the attribute name as the key, and the new value as the value. This will set the given attribute to the new value.
The other method you can use is by using specific update types. These update types are as follows.
$SET
- This method will set the attribute to the new value (as shown above)$ADD
- This method will add the value to the attribute. If the attribute is a number it will add the value to the existing number. If the attribute is a list, it will add the value to the list. Although this method only works for sets in DynamoDB, Dynamoose will automatically update this method to work for lists/arrays as well according to your schema. This update type does not work for any other attribute type.$REMOVE
- This method will remove the attribute from the document. Since this method doesn't require values you can pass in an array of attribute names.$DELETE
- This method will delete one or more elements from a Set.
You are allowed to combine these two methods into one update object.
The validate
Schema attribute property will only be run on $SET
values. This is due to the fact that Dynamoose is unaware of what the existing value is in the database for $ADD
properties.
Model.delete(key[, settings][, callback])
You can use Model.delete to delete a item from DynamoDB. This method uses the deleteItem
DynamoDB API call to delete the object.
key
can be a string representing the hashKey or an object containing the hashKey & rangeKey.
This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the callback
parameter to have it be used in a callback format as opposed to a promise format. In the event the operation was successful, noting will be returned to you. Otherwise an error will be thrown.
You can also pass in an object for the optional settings
parameter that is an object. The table below represents the options for the settings
object.
Name | Description | Type | Default |
---|---|---|---|
return | What the function should return. Can be null, or request . In the event this is set to request the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made. If this is request , the function will not be async anymore. | String | null | null |
condition | This is an optional instance of a Condition for the delete. | dynamoose.Condition | null |
In the event you have a rangeKey for your model, you can pass in an object for the key
parameter which includes the rangeKey & hashKey.
Model.batchDelete(keys[, settings][, callback])
You can use Model.batchDelete to delete documents from DynamoDB. This method uses the batchWriteItem
DynamoDB API call to delete the objects.
keys
can be an array of strings representing the hashKey and/or an array of objects containing the hashKey & rangeKey.
This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the callback
parameter to have it be used in a callback format as opposed to a promise format. In the event the operation was successful, an object with the unprocessedItems
will be returned to you. Otherwise an error will be thrown.
You can also pass in an object for the optional settings
parameter that is an object. The table below represents the options for the settings
object.
Name | Description | Type | Default |
---|---|---|---|
return | What the function should return. Can be response , or request . In the event this is set to request the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made. If this is request , the function will not be async anymore. | String | response |
In the event you have a rangeKey for your model, you can pass in an object for the key
parameter which includes the rangeKey & hashKey.
Model.transaction
This object has the following methods that you can call.
- Model.transaction.get
- Model.transaction.create
- Model.transaction.delete
- Model.transaction.update
- Model.transaction.condition
You can pass in the same parameters into each method that you do for the normal (non-transaction) methods, except for the callback parameter.
These methods are meant to only be called to instantiate the dynamoose.transaction
array.
Model.transaction.create
Note that this method corresponds more closely to Model.put
, as it will overwrite an item if it already exists in the database. For Model.create
-like functionality you have to add an extra Model.transaction.condition
call that ensures the item does not exist.
Model.transaction.condition(key, condition)
This method allows you to run a conditionCheck
when running a DynamoDB transaction.
The condition
parameter is a dynamoose.Condition
instance that represents the conditional you want to run.
Model.methods.set(name, function)
This function allows you to add a method to the given model that you can call later. When Dynamoose calls your function
parameter, this
will be set to the underlying model. If an existing method exists with the given name, it will be overwritten, except if you are trying to replace an internal method, then this function will fail silently.
You can also pass parameters into your custom method. It is important to note that if you decide to pass custom parameters into your custom method, the callback
parameter will always be passed in as the last parameter. This means it's highly recommended that you always pass in the same number of parameters every time to your custom method. In the event you are unable to do this (dynamic/custom parameter length), you can use the JavaScript arguments
variable to retrieve the last argument that was passed into the function.
Model.methods.delete(name)
This allows you to delete an existing method from the model. If no existing method is assigned for that name, the function will do nothing and no error will be thrown.
Model.methods.document.set(name, function)
This function allows you to add a method to the model documents that you can call later. When Dynamoose calls your function
parameter, this
will be set to the underlying document. If an existing method exists with the given name, it will be overwritten, except if you are trying to replace an internal method, then this function will fail silently.
You can also pass parameters into your custom method. It is important to note that if you decide to pass custom parameters into your custom method, the callback
parameter will always be passed in as the last parameter. This means it's highly recommended that you always pass in the same number of parameters every time to your custom method. In the event you are unable to do this (dynamic/custom parameter length), you can use the JavaScript arguments
variable to retrieve the last argument that was passed into the function.
Model.methods.document.delete(name)
This allows you to delete an existing method from the document. If no existing method is assigned for that name, the function will do nothing and no error will be thrown.
Model.serializeMany(items[, serializer])
This function takes in an array of items
and serializes all of them. This function is very similar to document.serialize
except it takes in an array of documents to serialize and returns an array of those documents.
Model.serializer.add(name, serializer)
This function adds a serializer to the model.
The serializer
parameter can be an object containing the following properties.
Name | Type | Description |
---|---|---|
include | [string] | The properties you wish to include when serializing. |
exclude | [string] | The properties you wish to exclude when serializing. |
modify | (serialized: Object, original: Object) => Object | A function you want to use to modify the object in the serializer. The serialized parameter is the new object (after include & exclude have been applied). The original parameter is the original document (before include & exclude have been applied). |
You can also pass an array into the serializer
parameter, which acts as a shorthand for the include
property.
Model.serializer.delete(name)
This function will delete the serializer from the list of serializer on the model. If no existing serializer is assigned for that name, the function will do nothing and no error will be thrown.
Model.serializer.default.set([name])
This function sets the default serializer for the given model. By default the default serializer has the same behavior as document.toJSON
. The default serializer will be used for Model.serializeMany
and document.serialize
if you don't pass anything into the serializer
parameter.
You can revert back to the default serializer by calling this method with no arguments.