1

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 ?

4

1 回答 1

1

没有直接支持通过继承扩展模式。但是模式只是对象,因此您可以像任何其他对象一样扩展它们,例如使用_.extend

var _ = require('underscore');
var baseAccountSchema = {
  // ...
};
var extendedAccountSchema = _.extend({}, baseAccountSchema, {
  // ...
});
var NewAccount = Foxx.Model.extend({
  schema: baseAccountSchema
});
var Account = Foxx.Model.extend({
  schema: extendedAccountSchema
});
于 2016-03-29T14:24:11.620 回答