aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request/request-to-pod.ts
blob: 67331be1d2321c8f373339ed5849496b36542e62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import * as Sequelize from 'sequelize'

import { addMethodsToModel } from '../utils'
import {
  RequestToPodClass,
  RequestToPodInstance,
  RequestToPodAttributes,

  RequestToPodMethods
} from './request-to-pod-interface'

let RequestToPod: Sequelize.Model<RequestToPodInstance, RequestToPodAttributes>
let removeByRequestIdsAndPod: RequestToPodMethods.RemoveByRequestIdsAndPod

export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
  RequestToPod = sequelize.define<RequestToPodInstance, RequestToPodAttributes>('RequestToPod', {}, {
    indexes: [
      {
        fields: [ 'requestId' ]
      },
      {
        fields: [ 'podId' ]
      },
      {
        fields: [ 'requestId', 'podId' ],
        unique: true
      }
    ]
  })

  const classMethods = [
    removeByRequestIdsAndPod
  ]
  addMethodsToModel(RequestToPod, classMethods)

  return RequestToPod
}

// ---------------------------------------------------------------------------

removeByRequestIdsAndPod = function (requestsIds: number[], podId: number, callback?: RequestToPodMethods.RemoveByRequestIdsAndPodCallback) {
  if (!callback) callback = function () { /* empty */ }

  const query = {
    where: {
      requestId: {
        $in: requestsIds
      },
      podId: podId
    }
  }

  RequestToPod.destroy(query).asCallback(callback)
}