ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

124 lines
2.6 KiB

  1. import { FindOptions } from 'sequelize'
  2. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
  3. import { MRunner } from '@server/types/models/runners/index.js'
  4. import { Runner } from '@peertube/peertube-models'
  5. import { SequelizeModel, getSort } from '../shared/index.js'
  6. import { RunnerRegistrationTokenModel } from './runner-registration-token.js'
  7. import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
  8. @Table({
  9. tableName: 'runner',
  10. indexes: [
  11. {
  12. fields: [ 'runnerToken' ],
  13. unique: true
  14. },
  15. {
  16. fields: [ 'runnerRegistrationTokenId' ]
  17. },
  18. {
  19. fields: [ 'name' ],
  20. unique: true
  21. }
  22. ]
  23. })
  24. export class RunnerModel extends SequelizeModel<RunnerModel> {
  25. // Used to identify the appropriate runner when it uses the runner REST API
  26. @AllowNull(false)
  27. @Column
  28. runnerToken: string
  29. @AllowNull(false)
  30. @Column
  31. name: string
  32. @AllowNull(true)
  33. @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
  34. description: string
  35. @AllowNull(false)
  36. @Column
  37. lastContact: Date
  38. @AllowNull(false)
  39. @Column
  40. ip: string
  41. @CreatedAt
  42. createdAt: Date
  43. @UpdatedAt
  44. updatedAt: Date
  45. @ForeignKey(() => RunnerRegistrationTokenModel)
  46. @Column
  47. runnerRegistrationTokenId: number
  48. @BelongsTo(() => RunnerRegistrationTokenModel, {
  49. foreignKey: {
  50. allowNull: false
  51. },
  52. onDelete: 'cascade'
  53. })
  54. RunnerRegistrationToken: Awaited<RunnerRegistrationTokenModel>
  55. // ---------------------------------------------------------------------------
  56. static load (id: number) {
  57. return RunnerModel.findByPk(id)
  58. }
  59. static loadByToken (runnerToken: string) {
  60. const query = {
  61. where: { runnerToken }
  62. }
  63. return RunnerModel.findOne(query)
  64. }
  65. static loadByName (name: string) {
  66. const query = {
  67. where: { name }
  68. }
  69. return RunnerModel.findOne(query)
  70. }
  71. static listForApi (options: {
  72. start: number
  73. count: number
  74. sort: string
  75. }) {
  76. const { start, count, sort } = options
  77. const query: FindOptions = {
  78. offset: start,
  79. limit: count,
  80. order: getSort(sort)
  81. }
  82. return Promise.all([
  83. RunnerModel.count(query),
  84. RunnerModel.findAll<MRunner>(query)
  85. ]).then(([ total, data ]) => ({ total, data }))
  86. }
  87. // ---------------------------------------------------------------------------
  88. toFormattedJSON (this: MRunner): Runner {
  89. return {
  90. id: this.id,
  91. name: this.name,
  92. description: this.description,
  93. ip: this.ip,
  94. lastContact: this.lastContact,
  95. createdAt: this.createdAt,
  96. updatedAt: this.updatedAt
  97. }
  98. }
  99. }