Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 23, 2020 10:22
Show Gist options
  • Save velotiotech/df9c6900d73fb0340fd6acd42d67dbfb to your computer and use it in GitHub Desktop.
Save velotiotech/df9c6900d73fb0340fd6acd42d67dbfb to your computer and use it in GitHub Desktop.
const { Model } = require('objection');
const Animal = require('./Animal');
class Person extends Model {
// Table name is the only required property.
static get tableName() {
return 'persons';
}
// Optional JSON schema. This is not the database schema. Nothing is generated
// based on this. This is only used for validation. Whenever a model instance
// is created it is checked against this schema. http://json-schema.org/.
static get jsonSchema() {
return {
type: 'object',
required: ['firstName', 'lastName'],
properties: {
id: { type: 'integer' },
parentId: { type: ['integer', 'null'] },
firstName: { type: 'string', minLength: 1, maxLength: 255 },
lastName: { type: 'string', minLength: 1, maxLength: 255 },
age: { type: 'number' },
address: {
type: 'object',
properties: {
street: { type: 'string' },
city: { type: 'string' },
zipCode: { type: 'string' }
}
}
}
};
}
// This object defines the relations to other models.
static get relationMappings() {
return {
pets: {
relation: Model.HasManyRelation,
// The related model. This can be either a Model subclass constructor or an
// absolute file path to a module that exports one.
modelClass: Animal,
join: {
from: 'persons.id',
to: 'animals.ownerId'
}
}
};
}
}
module.exports = Person;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment