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