Skip to content

Instantly share code, notes, and snippets.

@bravo-kernel
Last active July 31, 2019 10:47
Show Gist options
  • Select an option

  • Save bravo-kernel/96bf1648eb5840629074031c012371db to your computer and use it in GitHub Desktop.

Select an option

Save bravo-kernel/96bf1648eb5840629074031c012371db to your computer and use it in GitHub Desktop.
Feathers sequelize-model-to-json-schema
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const folders = sequelizeClient.define('folders', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
// eslint-disable-next-line no-unused-vars
folders.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
};
// generate JSON-schema
const schemaFactory = new SchemaFactory({
customSchema: {
folders: {
// override feathers-swagger default values
// NOTE: it would be nice if these could be set directly within the sequelize properties above
name: {
type: 'date', // dumb and invalid example but possible
title: 'This title was added from within the sequelize model',
description: 'This description was added from within the sequelize model',
examples: [
'entry1', 'entry2'
],
// for ajv validation simply add more validation related fields to the schema
"maxLength": 20
},
},
},
// add virtual fields
virtualProperties: {
folders: {
virtualField: {
type: 'STRING', // MUST be a sequelize data type or will go undefined
title: 'virtualField',
description: 'This description for a virtual field was added from within the sequelize model',
}
}
},
hrefBase: 'http://localhost:3003/docs/schema',
associations: folders.associate // must be set or the lib crashes on missing property (should PR)
});
// set `schema` property so it can be used for e.g. swagger and validation
folders.schema = schemaFactory.getSchemaGenerator(folders).getSchema();
return folders;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment