I am developing a REST API using ArangoDB and Foxx. A pattern that keeps coming back in multiple models is the following:
const NewAccount = Foxx.Model.extend({
schema: {
name: Joi.string().required(),
... multiple properties
}});
When I store the model in the database, I want to add properties like the creation timestamp, the status of the account, ... .
const Account = Foxx.Model.extend({
schema: {
name: Joi.string().required(),
... multiple properties,
created: Joi.number().integer().required().default(Date.now, 'Current date'),
status: Joi.number().integer().required()
}});
question: Is there a way to let the Account model inherit all properties from the NewAccount model, so I only have to define the created and status properties?
Secondly, is there an efficient and easy way to copy all properties from a NewAccount instance into an Account instance ?
There's no direct support for extending schemas via inheritance. However schemas are just objects, so you can extend them like any other object, for example using _.extend
:
var _ = require('underscore');
var baseAccountSchema = {
// ...
};
var extendedAccountSchema = _.extend({}, baseAccountSchema, {
// ...
});
var NewAccount = Foxx.Model.extend({
schema: baseAccountSchema
});
var Account = Foxx.Model.extend({
schema: extendedAccountSchema
});