/ src / server / db / Build.js
Build.js
 1  const shortid = require('shortid')
 2  const { ref } = require('objection')
 3  const { Model } = require('objection')
 4  
 5  class Build extends Model {
 6    static get tableName() {
 7      return 'build'
 8    }
 9  
10    static get jsonSchema () {
11      return {
12        type: 'object',
13        required: ['type', 'fileUrl'],
14        properties: {
15          id:        {type: 'integer'},
16          created:   {type: 'string', minLength: 8, maxLength: 30},
17          /* Mandatory */
18          name:      {type: 'string', minLength: 3, maxLength: 50},
19          type:      {type: 'string', minLength: 3, maxLength: 50},
20          fileUrl:   {type: 'string', minLength: 3, maxLength: 255},
21          filename:  {type: 'string', minLength: 3, maxLength: 255},
22          /* Optional */
23          commit:    {type: 'string', minLength: 5, maxLength: 40},
24          buildUrl:  {type: 'string', minLength: 3, maxLength: 255},
25        }
26      }
27    }
28  
29    static get relationMappings() {
30      const Diff = require('./Diff')
31      return {
32        diffs: {
33          relation: Model.ManyToManyRelation,
34          modelClass: Diff,
35          join: {
36            from: 'build.id',
37            through: {
38              from: 'build_diffs.buildId',
39              to: 'build_diffs.diffId'
40            },
41            to: 'diff.id',
42          },
43        },
44      }
45    }
46  
47    $beforeInsert () {
48      this.created = new Date().toISOString()
49      this.filename = this.fileUrl.split('/').pop()
50      if (this.name === undefined) {
51        this.name = shortid.generate()
52      }
53    }
54  }
55  
56  module.exports = Build