certificate.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. const db = require('../db');
  4. const Model = require('objection').Model;
  5. const User = require('./user');
  6. Model.knex(db);
  7. class Certificate extends Model {
  8. $beforeInsert () {
  9. this.created_on = Model.raw('NOW()');
  10. this.modified_on = Model.raw('NOW()');
  11. // Default for expires_on
  12. if (typeof this.expires_on === 'undefined') {
  13. this.expires_on = Model.raw('NOW()');
  14. }
  15. // Default for domain_names
  16. if (typeof this.domain_names === 'undefined') {
  17. this.domain_names = [];
  18. }
  19. // Default for meta
  20. if (typeof this.meta === 'undefined') {
  21. this.meta = {};
  22. }
  23. this.domain_names.sort();
  24. }
  25. $beforeUpdate () {
  26. this.modified_on = Model.raw('NOW()');
  27. // Sort domain_names
  28. if (typeof this.domain_names !== 'undefined') {
  29. this.domain_names.sort();
  30. }
  31. }
  32. static get name () {
  33. return 'Certificate';
  34. }
  35. static get tableName () {
  36. return 'certificate';
  37. }
  38. static get jsonAttributes () {
  39. return ['domain_names', 'meta'];
  40. }
  41. static get relationMappings () {
  42. return {
  43. owner: {
  44. relation: Model.HasOneRelation,
  45. modelClass: User,
  46. join: {
  47. from: 'certificate.owner_user_id',
  48. to: 'user.id'
  49. },
  50. modify: function (qb) {
  51. qb.where('user.is_deleted', 0);
  52. qb.omit(['id', 'created_on', 'modified_on', 'is_deleted', 'email', 'roles']);
  53. }
  54. }
  55. };
  56. }
  57. }
  58. module.exports = Certificate;