Schema definition
Schema is a mixin from which Records inherits. A schema is defined once
and must no change. We dont support schema migration at the moment. The Records
class inherit all the properties and method of the shema.
ron Reference to the Ron instance
options Schema definition. Options include:
nameName of the schema.propertiesProperties definition, an object or an array.
Record properties may be defined by the following keys:
typeUse to cast the value inside Redis, one ofstring,int,dateoremail.identifierMark this property as the identifier, only one property may be an identifier.indexCreate an index on the property.uniqueCreate a unique index on the property.temporalAdd creation and modification date transparently.
Define a schema from a configuration object:
1 2 3 4 | |
Define a schema with a declarative approach:
1 2 3 4 | |
Whichever your style, you can then manipulate your records:
1 2 | |
hash(key)
Utility function used when a redis key is created out of uncontrolled character (like a string instead of an int).
identifier(property)
Define a property as an identifier or return the record identifier if called without any arguments. An identifier is a property which uniquely define a record. Inside Redis, identifier values are stored in set.
index([property])
Define a property as indexable or return all index properties. An
indexed property allow records access by its property value. For example,
when using the list function, the search can be filtered such as returned
records match one or multiple values.
Calling this function without any argument will return an array with all the indexed properties.
Example:
1 2 3 4 5 | |
property(property, [schema])
Define a new property or overwrite the definition of an existing property. If no schema is provide, return the property information.
Calling this function with only the property argument will return the schema information associated with the property.
It is possible to define a new property without any schema information by providing an empty object.
Example:
1 2 3 4 | |
name()
Return the schema name of the current instance.
Using the function :
1 2 | |
serialize(records)
Cast record values before their insertion into Redis.
Take a record or an array of records and update values with correct property types.
temporal([options])
Define or retrieve temporal definition. Marking a schema as temporal will transparently add two new date properties, the date when the record was created (by default “cdate”), and the date when the record was last updated (by default “mdate”).
unique([property])
Define a property as unique or retrieve all the unique properties if no argument is provided. An unique property is similar to a index property but the index is stored inside a Redis hash. In addition to being filterable, it could also be used as an identifer to access a record.
Example:
1 2 3 | |
unserialize(records, [options])
Cast record values to their correct type.
Take a record or an array of records and update values with correct property types.
options Options include:
identifiersReturn an array of identifiers instead of the record objects.propertiesArray of properties to be returned.millisecondsConvert date value to milliseconds timestamps instead ofDateobjects.secondsConvert date value to seconds timestamps instead ofDateobjects.
validate(records, [options])
Validate the properties of one or more records. Return a validation object or an array of validation objects depending on the provided records arguments. Keys of a validation object are the name of the invalid properties and their value is a string indicating the type of error.
records Record object or array of record objects.
options Options include:
throwThrow errors on first invalid property instead of returning a validation object.skip_requiredDoesn’t validate missing properties defined asrequired, usefull for partial update.