Repository navigation
Expand file tree
/
Copy pathdeployments.ts
More file actions
58 lines (55 loc) · 1.66 KB
/
Copy pathdeployments.ts
File metadata and controls
58 lines (55 loc) · 1.66 KB
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
55
56
57
58
import _ from 'lodash';
import { DataTypes, Model } from 'sequelize';
import { AppError } from '../core/app-error';
import { sequelize } from '../core/utils/connections';
interface DeploymentsInterface extends Model {
id: number;
appid: number;
name: string;
description: string;
deployment_key: string;
last_deployment_version_id: number;
label_id: number;
created_at: Date;
updated_at: Date;
}
export const Deployments = sequelize.define<DeploymentsInterface>(
'Deployments',
{
id: {
type: DataTypes.INTEGER({ length: 10 }),
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
appid: DataTypes.INTEGER({ length: 10 }),
name: DataTypes.STRING,
description: DataTypes.STRING,
deployment_key: DataTypes.STRING,
last_deployment_version_id: DataTypes.INTEGER({ length: 10 }),
label_id: DataTypes.INTEGER({ length: 10 }),
created_at: DataTypes.DATE,
updated_at: DataTypes.DATE,
},
{
tableName: 'deployments',
underscored: true,
paranoid: true,
},
);
export function generateDeploymentsLabelId(deploymentId: number) {
return sequelize.transaction((t) => {
return Deployments.findByPk(deploymentId, {
transaction: t,
lock: t.LOCK.UPDATE,
}).then((data) => {
if (_.isEmpty(data)) {
throw new AppError('does not find deployment');
}
data.label_id += 1;
return data.save({ transaction: t }).then((d) => {
return d.label_id;
});
});
});
}