Adding meta timestamps
In this example we can easily create both updatedAt
and createdAt
attributes on our model. createdAt
will use ElectroDB’s set
and readOnly
attribute properties, while updatedAt
will make use of readOnly
, and watch
with the “watchAll” syntax: {watch: "*"}
. By supplying an asterisk, instead of an array of attribute names, attributes can be defined to watch all changes to all attributes.
Using watch
in conjunction with readOnly
is another powerful modeling technique. This combination allows you to model attributes that can only be modified via the model and not via the user. This is useful for attributes that need to be locked down and/or strictly calculated.
Notable about this example is that both updatedAt
and createdAt
use the set
property without using its arguments. The readOnly
only prevents modification of an attributes on update
, and patch
. By disregarding the arguments passed to set
, the updatedAt
and createdAt
attributes are then effectively locked down from user influence/manipulation.
{
model: {
entity: "transaction",
service: "bank",
version: "1"
},
attributes: {
accountNumber: {
type: "string"
},
transactionId: {
type: "string"
},
description: {
type: "string",
},
createdAt: {
type: "number",
readOnly: true,
set: () => Date.now()
},
updatedAt: {
type: "number",
readOnly: true,
watch: "*",
set: () => Date.now()
}
},
indexes: {
transactions: {
pk: {
field: "pk",
facets: ["accountNumber"]
},
sk: {
field: "sk",
facets: ["transactionId"]
}
}
}
}