Add ability to specify locationId on addOrUpdateEntity

This commit is contained in:
Fredrik Adelöw
2020-06-01 21:27:38 +02:00
parent 2f1256e2ad
commit 5094dc75a5
2 changed files with 11 additions and 5 deletions
@@ -49,13 +49,16 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog {
);
}
async addOrUpdateEntity(entity: Entity): Promise<Entity> {
async addOrUpdateEntity(
entity: Entity,
locationId?: string,
): Promise<Entity> {
await this.policy.enforce(entity);
return await this.database.transaction(async tx => {
let response: DbEntityResponse;
if (entity.metadata.uid) {
response = await this.database.updateEntity(tx, { entity });
response = await this.database.updateEntity(tx, { locationId, entity });
} else {
const existing = await this.entityByNameInternal(
tx,
@@ -64,9 +67,12 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog {
entity.metadata.namespace,
);
if (existing) {
response = await this.database.updateEntity(tx, { entity });
response = await this.database.updateEntity(tx, {
locationId,
entity,
});
} else {
response = await this.database.addEntity(tx, { entity });
response = await this.database.addEntity(tx, { locationId, entity });
}
}
+1 -1
View File
@@ -30,7 +30,7 @@ export type EntitiesCatalog = {
namespace: string | undefined,
name: string,
): Promise<Entity | undefined>;
addOrUpdateEntity(entity: Entity): Promise<Entity>;
addOrUpdateEntity(entity: Entity, locationId?: string): Promise<Entity>;
removeEntityByUid(uid: string): Promise<void>;
};