Skip to content

Commit

Permalink
fix: delete update many[3966]
Browse files Browse the repository at this point in the history
  • Loading branch information
IharTsykala committed Sep 20, 2024
1 parent 866b4e1 commit ebfa5f5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
6 changes: 3 additions & 3 deletions common/src/database-modules/database-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ export class DatabaseServer extends AbstractDatabaseServer {
criteria: FilterQuery<T>,
row: unknown | unknown[]
): Promise<T> {
if(Array.isArray(criteria)) {
return await this.updateMany(entityClass, row as unknown as T[], criteria) as any
}
// if(Array.isArray(criteria)) {
// return await this.updateMany(entityClass, row as unknown as T[], criteria) as any
// }

if (this.dryRun) {
this.addDryRunId(entityClass, row);
Expand Down
10 changes: 9 additions & 1 deletion common/src/helpers/db-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,16 @@ export class DataBaseHelper<T extends BaseEntity> extends AbstractDataBaseHelper
entity: T | T[],
filter?: FilterQuery<T>
): Promise<T | T[]> {
// if (Array.isArray(entity)) {
// return await Promise.all(entity.map(item => this.update(item)));
// }

if (Array.isArray(entity)) {
return await Promise.all(entity.map(item => this.update(item)));
const result = [];
for (const item of entity) {
result.push(await this.update(item));
}
return result;
}

if (!entity.id && !entity._id && !filter) {
Expand Down
2 changes: 1 addition & 1 deletion guardian-service/src/api/contract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ export async function contractAPI(
) < 0;
}

await dataBaseServer.updateMany(RetirePool, pools);
await dataBaseServer.update(RetirePool, null, pools);

const syncDate = new Date();

Expand Down
52 changes: 29 additions & 23 deletions guardian-service/src/api/tag.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ export async function publishSchemaTags(
const messageServer = new MessageServer(root.hederaAccountId, root.hederaAccountKey, root.signOptions)
.setTopicObject(topicConfig);

const tagObjects = []
// const tagObjects = []

for (const tag of tags) {
tag.target = schema.messageId;
await publishTag(tag, messageServer, owner);

tagObjects.push(tag);
await DatabaseServer.updateTag(tag);
//
// tagObjects.push(tag);
}

await new DatabaseServer().updateTags(tagObjects);
// await new DatabaseServer().updateTags(tagObjects);
}

/**
Expand All @@ -60,16 +61,17 @@ export async function publishPolicyTags(
const messageServer = new MessageServer(root.hederaAccountId, root.hederaAccountKey, root.signOptions)
.setTopicObject(topicConfig);

const tagObjects = []
// const tagObjects = []

for (const tag of tags) {
tag.target = policy.messageId;
await publishTag(tag, messageServer, owner);
await DatabaseServer.updateTag(tag);

tagObjects.push(tag);
// tagObjects.push(tag);
}

await new DatabaseServer().updateTags(tagObjects);
// await new DatabaseServer().updateTags(tagObjects);
}

/**
Expand All @@ -95,16 +97,17 @@ export async function publishTokenTags(
const messageServer = new MessageServer(root.hederaAccountId, root.hederaAccountKey, root.signOptions)
.setTopicObject(topicConfig);

const tagObjects = []
// const tagObjects = []

for (const tag of tags) {
tag.target = token.tokenId;
await publishTag(tag, messageServer, owner);
await DatabaseServer.updateTag(tag);

tagObjects.push(tag);
// tagObjects.push(tag);
}

await new DatabaseServer().updateTags(tagObjects);
// await new DatabaseServer().updateTags(tagObjects);
}

/**
Expand All @@ -129,16 +132,17 @@ export async function publishToolTags(
const messageServer = new MessageServer(root.hederaAccountId, root.hederaAccountKey, root.signOptions)
.setTopicObject(topicConfig);

const tagObjects = []
// const tagObjects = []

for (const tag of tags) {
tag.target = tool.tagsTopicId;
await publishTag(tag, messageServer, owner);
await DatabaseServer.updateTag(tag);

tagObjects.push(tag);
// tagObjects.push(tag);
}

await new DatabaseServer().updateTags(tagObjects);
// await new DatabaseServer().updateTags(tagObjects);
}

/**
Expand All @@ -164,16 +168,17 @@ export async function publishModuleTags(
const messageServer = new MessageServer(root.hederaAccountId, root.hederaAccountKey, root.signOptions)
.setTopicObject(topicConfig);

const tagObjects = []
// const tagObjects = []

for (const tag of tags) {
tag.target = module.messageId;
await publishTag(tag, messageServer, owner);
await DatabaseServer.updateTag(tag);

tagObjects.push(tag);
// tagObjects.push(tag);
}

await new DatabaseServer().updateTags(tagObjects);
// await new DatabaseServer().updateTags(tagObjects);
}

/**
Expand Down Expand Up @@ -485,7 +490,7 @@ export async function tagsAPI(logger: PinoLogger): Promise<void> {
map.set(tag.messageId, { message: null, local: tag });
}
}
const tagObjects = []
// const tagObjects = []

for (const item of map.values()) {
if (item.message) {
Expand All @@ -506,14 +511,15 @@ export async function tagsAPI(logger: PinoLogger): Promise<void> {
tag.date = tag.date || (new Date()).toISOString();

if (tag.id) {
tagObjects.push(tag);
// tagObjects.push(tag);
await DatabaseServer.updateTag(tag);
} else {
await DatabaseServer.createTag(tag);
}
}
}

await new DatabaseServer().updateTags(tagObjects)
// await new DatabaseServer().updateTags(tagObjects)
}
} else {
throw new Error('Invalid target');
Expand All @@ -523,15 +529,15 @@ export async function tagsAPI(logger: PinoLogger): Promise<void> {
const cache = await DatabaseServer.getTagCache(filter);
if (cache.length) {

const tagCacheObjects = []
// const tagCacheObjects = []

for (const item of cache) {
item.date = date;

tagCacheObjects.push(item);
await DatabaseServer.updateTagCache(item);
// tagCacheObjects.push(item);
}

await DatabaseServer.updateTagsCache(tagCacheObjects)
// await DatabaseServer.updateTagsCache(tagCacheObjects)
} else {
await DatabaseServer.createTagCache({ localTarget, entity, date });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class PolicyImport {

for (const addedArtifact of this.artifactsResult.artifacts) {
addedArtifact.policyId = policy.id;
await DatabaseServer.saveArtifact(addedArtifact);
// await DatabaseServer.saveArtifact(addedArtifact);

artifactObjects.push(addedArtifact);
}
Expand Down
7 changes: 4 additions & 3 deletions guardian-service/src/policy-engine/policy-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ export class PolicyEngine extends NatsService {
const tokenIds = findAllEntities(model.config, ['tokenId']);
const tokens = await DatabaseServer.getTokens({ tokenId: { $in: tokenIds }, owner: model.owner });

const policyObjects = []
// const policyObjects = []

for (const token of tokens) {
let _token = token;
Expand All @@ -769,10 +769,11 @@ export class PolicyEngine extends NatsService {
replaceAllEntities(model.config, ['tokenId'], oldId, newToken.tokenId);
replaceAllVariables(model.config, 'Token', oldId, newToken.tokenId);

policyObjects.push(model);
model = await DatabaseServer.updatePolicy(model);
// policyObjects.push(model);
}

await DatabaseServer.savePolicies(policyObjects);
// await DatabaseServer.savePolicies(policyObjects);

const tokenMessage = new TokenMessage(MessageAction.UseToken);
tokenMessage.setDocument(_token);
Expand Down
7 changes: 4 additions & 3 deletions policy-service/src/policy-engine/blocks/tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export class TagsManagerBlock {
}
}

const tagObjects = []
// const tagObjects = []

for (const item of map.values()) {
if (item.message) {
Expand All @@ -390,14 +390,15 @@ export class TagsManagerBlock {
tag.date = tag.date || (new Date()).toISOString();

if (tag.id) {
tagObjects.push(tag);
await ref.databaseServer.updateTag(tag);
// tagObjects.push(tag);
} else {
await ref.databaseServer.createTag(tag);
}
}
}

await ref.databaseServer.updateTags(tagObjects)
// await ref.databaseServer.updateTags(tagObjects)
}

}

0 comments on commit ebfa5f5

Please sign in to comment.