Files
certd/packages/ui/certd-server/src/modules/pipeline/service/storage-service.ts
T

77 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
2023-01-29 13:44:19 +08:00
import { InjectEntityModel } from '@midwayjs/typeorm';
2024-08-04 02:35:45 +08:00
import { In, Repository } from 'typeorm';
2024-10-03 22:03:49 +08:00
import { BaseService } from '@certd/lib-server';
2024-07-15 00:30:33 +08:00
import { StorageEntity } from '../entity/storage.js';
2023-01-29 13:44:19 +08:00
/**
*/
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
2023-01-29 13:44:19 +08:00
export class StorageService extends BaseService<StorageEntity> {
@InjectEntityModel(StorageEntity)
repository: Repository<StorageEntity>;
2024-10-09 02:34:28 +08:00
//@ts-ignore
2023-01-29 13:44:19 +08:00
getRepository() {
return this.repository;
}
2024-07-17 01:30:00 +08:00
async get(where: { scope: any; namespace: any; userId: number; version: string; key: string }) {
2023-01-29 13:44:19 +08:00
if (where.userId == null) {
throw new Error('userId 不能为空');
}
return await this.repository.findOne({
where,
});
}
2024-07-17 01:30:00 +08:00
async set(entity: { id?: any; scope: any; namespace: any; userId: number; version: string; value: string; key: string }) {
2023-01-29 13:44:19 +08:00
entity.id = null;
const query = { ...entity };
delete query.value;
const ret = await this.get(query);
if (ret != null) {
entity.id = ret.id;
if (ret.userId !== entity.userId) {
throw new Error('您没有权限修改此数据');
}
await this.repository.save(entity);
} else {
await this.repository.insert(entity);
}
return;
}
2024-08-04 02:35:45 +08:00
async findPipelineVars(pipelineIds: number[]) {
2024-10-15 12:59:40 +08:00
if (pipelineIds == null || pipelineIds.length === 0) {
return [];
2024-10-15 12:59:40 +08:00
}
2024-08-04 02:35:45 +08:00
return await this.repository.find({
where: {
scope: 'pipeline',
namespace: In(pipelineIds),
key: 'vars',
},
});
}
2024-10-15 12:59:40 +08:00
async getPipelinePrivateVars(pipelineId: number) {
if (pipelineId == null) {
return [];
2024-10-15 12:59:40 +08:00
}
2024-10-15 17:12:42 +08:00
const res = await this.repository.findOne({
2024-10-15 12:59:40 +08:00
where: {
scope: 'pipeline',
namespace: pipelineId + '',
key: 'privateVars',
},
});
2024-10-15 17:12:42 +08:00
if (!res) {
return {};
}
const value = JSON.parse(res.value);
return value.value;
2024-10-15 12:59:40 +08:00
}
2023-01-29 13:44:19 +08:00
}