1.0.27-5.1 • Published 4 years ago

tw-cloud-common v1.0.27-5.1

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
4 years ago

tw-cloud-models

Model definitions for all our services.

Adding a model

// src/models/Example.js

const createModel = require('../helpers/createModel')

module.exports = createModel({
   // Model Name
   // Singular and UpperCamelCase (and match the file name/export in index.js)

  name: 'Example',

  // Schema
  // Follows the JSON schema (and ajv) structure
  // http://epoberezkin.github.io/ajv/keywords.html

  // Ids should be UUIDs
  // Naming to be snake_case (follows postgres conventions)
  // Foreign keys to be ${tableName}_id (follows pgModeler conventions)
  // Properties in meta (JSON object) to be defined here

  schema: {
    type: 'object',
    meta: ['meta_property'],
    required: ['required_property'],

    properties: {
      id: { type: 'string', format: 'uuid' },
      required_property: { type: 'string' },
      optional_number: { type: 'number' },
      foreigns_id: { type: 'string', format: 'uuid' },
      meta_property: { type: 'string' } // in db as meta.meta_property
    }
  },

  // Relationships
  // Closely follows the Objection.js relations defiitions.
  // http://vincit.github.io/objection.js/#relations

  // Name to be what the relation is called (will map to $eager)
  // Model to be the name of the foreign object model
  // Join is required if is manyToMany (to be the join model name)
  // To/from fields do not need to be defined (computed automatically)

  relationships: {
    foreign: {
      model: 'Foreign',
      type: 'belongsToOne'
    }
  }
}
// src/index.js

const Example = require('./models/Example')

module.exports = {
  ...,
  Example
}