CosmosDB + NestJS:系统中已存在指定id的实体
CosmosDB + NestJS: Entity with the specified id already exists in the system
我已经使用 @nestjs/azure-database 包将我的 NestJS 项目与 CosmosDB 数据库连接起来。我已经在我的数据库中成功创建了第一个项目,但是每当我尝试添加另一个项目(创建函数)时,它都会给我以下错误:
UnhandledPromiseRejectionWarning: Error: Entity with the specified id already exists in the system.,
RequestStartTime: 2020-10-22T11:54:32.5660620Z, RequestEndTime: 2020-10-22T11:54:32.5860730Z, Number of regions attempted:1
ResponseTime: 2020-10-22T11:54:32.5860730Z, StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westeurope1-fd37.documents.azure.com:14162/apps/6cfd606f-f95e-4068-9026-f2f1ea7a7521/services/9ef1da94-92b6-488d-afa0-7566d7382073/partitions/4336f3e2-783d-4132-a682-84d5afed64a1/replicas/132474264435072969p/, LSN: 32, GlobalCommittedLsn: 32, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 409, SubStatusCode: 0, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#32, UsingLocalLSN: False, TransportException: null, ResourceType: Document, OperationType: Create
我的实体 class 是这样定义的:
import { CosmosDateTime, CosmosPartitionKey, CosmosUniqueKey } from '@nestjs/azure-database';
@CosmosPartitionKey('id')
export class TeamsSupport {
constructor(botId: string, sessionId: string, version: string, supportId: string, teamsConversationRef: string) {
this.botId = botId;
this.version = version;
this.sessionId = sessionId;
this.supportId = supportId;
this.teamsConversationRef = teamsConversationRef;
}
id?: string;
botId: string;
teamsConversationRef: string;
version: string;
supportId: string;
@CosmosUniqueKey() sessionId: string;
@CosmosDateTime() createdAt: Date;
@CosmosDateTime() updatedAt?: Date;
}
保存用户凭据,如您所见,supportId 和 teamsConversationRef 在第一个实例中未定义:
public async saveUserCredentials(sessionId: any, botId: string, version: string) {
const teamsSupport: TeamsSupport = new TeamsSupport(botId, sessionId, version, undefined, undefined);
await this.teamsSupportService.create(teamsSupport);
}
存储库class(仅创建方法):
import { Container } from '@azure/cosmos';
import { InjectModel } from '@nestjs/azure-database';
import { Injectable } from '@nestjs/common';
import { HandoverLogger } from '../logger/handoverlogger.service';
import { TeamsSupport } from './teamssupport.entity';
@Injectable()
export class TeamsSupportRepository {
constructor(@InjectModel(TeamsSupport) private readonly container: Container, private handoverLogger: HandoverLogger) {
this.handoverLogger.setContext('ContactRepository');
}
async create(item: TeamsSupport): Promise<TeamsSupport> {
item.createdAt = new Date();
const response = await this.container.items.create(item);
this.handoverLogger.verbose(`Create RUs: ${response.requestCharge}`);
return response.resource;
}
一些注意事项:
- sessionId 在我的测试中始终是唯一的,所以这不是问题。
- 更新我已经创建的一项,更新插入功能没有问题
- 我已尝试删除实体中的 'id?' 参数但无济于事
我认为导致问题的原因是 CosmosDB 生成的 ID,但我真的不知道。有人能帮我一下吗?提前致谢!
我能够创建的第一个项目(所有标识都被 *** 缩短):
{
"sessionId": "testId",
"createdAt": "2020-10-22T10:01:40.852Z",
"id": "368c7693-f57f-4dba-84b2-cd3c0504f088",
"_rid": "i+NVANCshdgBAAAAAAAAAA==",
"_self": "dbs/i+NVAA==/colls/i+NVANCshdg=/docs/i+NVANCshdgBAAAAAAAAAA==/",
"_etag": "\"0800f5fc-0000-0d00-0000-5f915cdd0000\"",
"_attachments": "attachments/",
"teamsConversationRef": "{\"activityId\":\"1603362010475\",\"user\":{\"id\":\"29:12zv1Zc3R***\",\"name\":\"Ros***\",\"aadObjectId\":\"33070d33-866***\",\"email\":\"jasp***\",\"userPrincipalName\":\"jaspe***\",\"tenantId\":\"8c292c0d-05c***\",\"userRole\":\"user\"},\"bot\":{\"id\":\"28:6ce0c0e***\",\"name\":\"Human Handover\"},\"conversation\":{\"isGroup\":true,\"conversationType\":\"channel\",\"tenantId\":\"8c292***\",\"id\":\"19:02bd604cd4284d4***;messageid=1603360903103\"},\"channelId\":\"msteams\",\"locale\":\"en-GB\",\"serviceUrl\":\"https://smba.trafficman***\"}",
"supportId": "29:12zv1Zc3RHpHBDKDf3***",
"updatedAt": "2020-10-22T10:20:13.681Z",
"_ts": 1603362013
}
我通过删除 Cosmos DB 中的容器然后重新启动我的程序解决了这个问题,从而创建了一个具有正确 PartitionKey 的新容器。更新现有的显然不起作用。
我已经使用 @nestjs/azure-database 包将我的 NestJS 项目与 CosmosDB 数据库连接起来。我已经在我的数据库中成功创建了第一个项目,但是每当我尝试添加另一个项目(创建函数)时,它都会给我以下错误:
UnhandledPromiseRejectionWarning: Error: Entity with the specified id already exists in the system.,
RequestStartTime: 2020-10-22T11:54:32.5660620Z, RequestEndTime: 2020-10-22T11:54:32.5860730Z, Number of regions attempted:1
ResponseTime: 2020-10-22T11:54:32.5860730Z, StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westeurope1-fd37.documents.azure.com:14162/apps/6cfd606f-f95e-4068-9026-f2f1ea7a7521/services/9ef1da94-92b6-488d-afa0-7566d7382073/partitions/4336f3e2-783d-4132-a682-84d5afed64a1/replicas/132474264435072969p/, LSN: 32, GlobalCommittedLsn: 32, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 409, SubStatusCode: 0, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#32, UsingLocalLSN: False, TransportException: null, ResourceType: Document, OperationType: Create
我的实体 class 是这样定义的:
import { CosmosDateTime, CosmosPartitionKey, CosmosUniqueKey } from '@nestjs/azure-database';
@CosmosPartitionKey('id')
export class TeamsSupport {
constructor(botId: string, sessionId: string, version: string, supportId: string, teamsConversationRef: string) {
this.botId = botId;
this.version = version;
this.sessionId = sessionId;
this.supportId = supportId;
this.teamsConversationRef = teamsConversationRef;
}
id?: string;
botId: string;
teamsConversationRef: string;
version: string;
supportId: string;
@CosmosUniqueKey() sessionId: string;
@CosmosDateTime() createdAt: Date;
@CosmosDateTime() updatedAt?: Date;
}
保存用户凭据,如您所见,supportId 和 teamsConversationRef 在第一个实例中未定义:
public async saveUserCredentials(sessionId: any, botId: string, version: string) {
const teamsSupport: TeamsSupport = new TeamsSupport(botId, sessionId, version, undefined, undefined);
await this.teamsSupportService.create(teamsSupport);
}
存储库class(仅创建方法):
import { Container } from '@azure/cosmos';
import { InjectModel } from '@nestjs/azure-database';
import { Injectable } from '@nestjs/common';
import { HandoverLogger } from '../logger/handoverlogger.service';
import { TeamsSupport } from './teamssupport.entity';
@Injectable()
export class TeamsSupportRepository {
constructor(@InjectModel(TeamsSupport) private readonly container: Container, private handoverLogger: HandoverLogger) {
this.handoverLogger.setContext('ContactRepository');
}
async create(item: TeamsSupport): Promise<TeamsSupport> {
item.createdAt = new Date();
const response = await this.container.items.create(item);
this.handoverLogger.verbose(`Create RUs: ${response.requestCharge}`);
return response.resource;
}
一些注意事项:
- sessionId 在我的测试中始终是唯一的,所以这不是问题。
- 更新我已经创建的一项,更新插入功能没有问题
- 我已尝试删除实体中的 'id?' 参数但无济于事
我认为导致问题的原因是 CosmosDB 生成的 ID,但我真的不知道。有人能帮我一下吗?提前致谢!
我能够创建的第一个项目(所有标识都被 *** 缩短):
{
"sessionId": "testId",
"createdAt": "2020-10-22T10:01:40.852Z",
"id": "368c7693-f57f-4dba-84b2-cd3c0504f088",
"_rid": "i+NVANCshdgBAAAAAAAAAA==",
"_self": "dbs/i+NVAA==/colls/i+NVANCshdg=/docs/i+NVANCshdgBAAAAAAAAAA==/",
"_etag": "\"0800f5fc-0000-0d00-0000-5f915cdd0000\"",
"_attachments": "attachments/",
"teamsConversationRef": "{\"activityId\":\"1603362010475\",\"user\":{\"id\":\"29:12zv1Zc3R***\",\"name\":\"Ros***\",\"aadObjectId\":\"33070d33-866***\",\"email\":\"jasp***\",\"userPrincipalName\":\"jaspe***\",\"tenantId\":\"8c292c0d-05c***\",\"userRole\":\"user\"},\"bot\":{\"id\":\"28:6ce0c0e***\",\"name\":\"Human Handover\"},\"conversation\":{\"isGroup\":true,\"conversationType\":\"channel\",\"tenantId\":\"8c292***\",\"id\":\"19:02bd604cd4284d4***;messageid=1603360903103\"},\"channelId\":\"msteams\",\"locale\":\"en-GB\",\"serviceUrl\":\"https://smba.trafficman***\"}",
"supportId": "29:12zv1Zc3RHpHBDKDf3***",
"updatedAt": "2020-10-22T10:20:13.681Z",
"_ts": 1603362013
}
我通过删除 Cosmos DB 中的容器然后重新启动我的程序解决了这个问题,从而创建了一个具有正确 PartitionKey 的新容器。更新现有的显然不起作用。