{"id":20135524,"url":"https://github.com/raisely/sequelize-to-json-schema","last_synced_at":"2025-04-09T17:32:50.554Z","repository":{"id":31867733,"uuid":"122807691","full_name":"raisely/sequelize-to-json-schema","owner":"raisely","description":"Flexible json-schema generator from sequelize models","archived":false,"fork":false,"pushed_at":"2022-06-14T04:13:51.000Z","size":31,"stargazers_count":4,"open_issues_count":3,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-23T19:39:12.605Z","etag":null,"topics":["json-schema","schema-generation","sequelize-models"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/raisely.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-25T05:21:35.000Z","updated_at":"2023-09-08T04:54:26.000Z","dependencies_parsed_at":"2022-09-16T05:22:02.467Z","dependency_job_id":null,"html_url":"https://github.com/raisely/sequelize-to-json-schema","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fsequelize-to-json-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fsequelize-to-json-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fsequelize-to-json-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fsequelize-to-json-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/raisely","download_url":"https://codeload.github.com/raisely/sequelize-to-json-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248077575,"owners_count":21043988,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-schema","schema-generation","sequelize-models"],"created_at":"2024-11-13T21:15:18.511Z","updated_at":"2025-04-09T17:32:50.277Z","avatar_url":"https://github.com/raisely.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sequelize-to-json-schema\nFlexible json-schema generator from sequelize models\n\n[![Build Status](https://travis-ci.org/raisely/sequelize-to-json-schema.svg?branch=master)](https://travis-ci.org/raisely/sequelize-to-json-schema)\n\nFeatures:\n* Rename attributes if they are different in your json schema (eg snake_case to camelCase)\n* Include associations using $ref or inline\n* Automatically generate examples for enum\n* Easily customise your schema with descriptions, examples, validation, etc\n* Built for Draft 06\n\n## Example\n\n```javascript\n\n// User model is defined as\n// userDefinition = {\n//   full_name: Sequelize.STRING,\n//   status: {\n//     type: Sequelize.ENUM,\n//     values: ['REAL', 'IMAGINED'],\n//   },\n// }\n// With associations for hasMany addresses and hasOne profile\n\nconst schemaFactory = require('sequelize-to-json-schema');\n\nconst factory = new SchemaFactory({\n  customSchema: {\n    // modelName: { attributeName: { \u003cschema\u003e } }\n    user: { status: { description: 'Was it all just a dream?' } },\n  }\n  hrefBase: 'http://schema.example',\n});\nconst schemaGenerator = factory.getSchemaGenerator(User);\nconst schema = schemaGenerator.getSchema();\n\n// Results in\nschema = {\n  {\n    title: 'User',\n    '$id': 'http://schema.example/user.json',\n    type: 'object',\n    '$schema': 'http://json-schema.org/draft-06/schema#',\n    properties: {\n      full_name: {\n        '$id': '/properties/full_name',\n        type: 'string',\n        examples: [],\n        title: 'Full name'\n      },\n      status: {\n        '$id': '/properties/status',\n        type: 'string',\n        examples: ['REAL', 'IMAGINED'],\n        enum: ['REAL', 'IMAGINED'],\n        title: 'Status',\n        description: 'Was it all just a dream?'\n      }\n    }\n  }\n}\n```\n\nYou can customise your factory by passing options\n\n### options.association\nAn object listing the associations to include in the schema *for all models*\nThe keys of the objects are the names of models, each key selects an object where\nthe keys of the object are the association names and the value should be either\n'INLINE' or 'REL'.\nUse 'INLINE' if you want the association to be presented within the schema instead\nof by reference ('REL')\n\n### options.customSchema\nAn object defineing custom information to be placed in the schema.\nTop level keys are model names, which contain a objects who's keys are\nattribute or association names, and then custom keys to place in your\nschema for that attribute.\n\n### options.hrefBase\nThe base to use for any references\n\n### options.jsonAssociationMapper\nA function for mapping association names, it is of the form `jsonAssociationMapper(model, attributeName)`\nIt must return an array of the form `[modelAttribute, jsonAttribute]`\nThis is useful if your associations are named differently in your models to how they\nare presented in JSON. (eg pluralization or snake_case to camelCase)\n\nIf it is unspecified, the schema generator will simply use association names unchanged\n\n**Note** This function must return [false, false] if the given property is not an association\n\n\n### options.jsonAttributeMapper\nA function for mapping attribute names in the model to json schema attributes\n\neg\n```javascript\njsonAttributeMapper = (modelAttr) =\u003e toCamelCase(modelAttr);\n```\n\n### options.selectAttributes\nSelects which attributes to describe in the schema for a given model\n`selectAttributes(model)`\nThis must return an array of strings, where each entry in the array is an attribute.\nUse this to prevent all attributes being described by the schema\n\n### options.virtualProperties\nAdd virtual properties - properties that are not present in the model, but are\ngenerated dynamically whenever a model is converted to JSON.\nThese take the form of customSchema, but *must* include the attribute type\nwhich contains a string representation of the Sequelize type that the\nproperty would be if it were \"real\"\n\n```javascript\nconst options = {\n  virtualProperties: {\n    users: {\n      postCount: { type: 'INTEGER', description: 'Number of posts by the user' },\n    },\n  },\n}\n```\n\n## Advanced example\n\n```javascript\nconst factory = new SchemaFactory({\n  customSchema,\n  jsonAttributeMapper = (attr) =\u003e _.camelCase(attr),\n  selectAttributes = () =\u003e ['full_name', 'status', 'address', 'profile'],\n  associations: { user: { address: 'inline' } },\n  hrefBase: 'http://schema.example',\n});\nconst schemaGenerator = factory.getSchemaGenerator(user);\nconst const schema = schemaGenerator.getSchema();\n\nschema = {\n  title: 'User',\n  '$id': 'schema.example/user.json',\n  type: 'object',\n  '$schema': 'http://json-schema.org/draft-06/schema#',\n  properties: {\n    address: { type: 'array', items: [Object], title: 'Address' },\n    fullName: {\n      '$id': '/properties/fullName',\n      type: 'string',\n      examples: [],\n      title: 'Full name'\n    },\n    profile: { '$ref': 'http://schema.example/profile.json', title: 'Profile' },\n    status: {\n      '$id': '/properties/status',\n      type: 'string',\n      examples: ['REAL', 'IMAGINED'],\n      enum: ['REAL', 'IMAGINED'],\n      title: 'Status',\n    }\n  }\n};\n```\n\n# Contributing\n\nContributions are welcome. Please submit a pull request and include tests.\n\nPlease follow the coding style in `.editorconfig` and `.eslintrc`.\n\nContributions should pass `npm run test:ci \u0026\u0026 npm run lint` (see below on testing)\n\n## Testing\n\nRun `npm test`\n\n# License\n\nThis software is licensed under the [Just World License](./LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraisely%2Fsequelize-to-json-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraisely%2Fsequelize-to-json-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraisely%2Fsequelize-to-json-schema/lists"}