Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/3966 database access layer refactoring impruving requests db microservises #4143

85 changes: 67 additions & 18 deletions common/src/database-modules/database-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export class DatabaseServer extends AbstractDatabaseServer {
async updateMany<T extends BaseEntity>(
entityClass: new () => T,
entities: T[],
filter: FilterQuery<T>,
filter?: FilterQuery<T>,
): Promise<DryRun[] | T[]> {
if (this.dryRun) {
this.addDryRunId(entityClass, entities);
Expand Down Expand Up @@ -1893,12 +1893,20 @@ export class DatabaseServer extends AbstractDatabaseServer {
return await this.remove(Tag, tag);
}

/**
* Update tag
* @param tag
*/
public async updateTag(tag: Tag): Promise<Tag> {
return await this.update(Tag, tag.id, tag);
}

/**
* Update tags
* @param row
* @param tags
*/
public async updateTag(row: Tag): Promise<Tag> {
return await this.update(Tag, row.id, row);
public async updateTags(tags: Tag[]): Promise<DryRun[] | Tag[]> {
return await this.updateMany(Tag, tags)
}

/**
Expand Down Expand Up @@ -2359,7 +2367,7 @@ export class DatabaseServer extends AbstractDatabaseServer {
* @param items Schemas
*/
public static async updateSchemas(items: SchemaCollection[]): Promise<void> {
await new DataBaseHelper(SchemaCollection).update(items);
await new DataBaseHelper(SchemaCollection).updateMany(items);
}

/**
Expand All @@ -2379,25 +2387,33 @@ export class DatabaseServer extends AbstractDatabaseServer {
}

/**
* Get schema
* Save schema
* @param item
*/
public static async saveSchema(item: SchemaCollection): Promise<SchemaCollection> {
return await new DataBaseHelper(SchemaCollection).save(item);
}

/**
* Get schema
* @param item
* Save schemas
* @param items
*/
public static async saveSchemas(item: SchemaCollection[]): Promise<SchemaCollection[]> {
const result = [];
for await (const schema of item) {
result.push(await new DataBaseHelper(SchemaCollection).save(schema));
}
return result;
public static async saveSchemas(items: SchemaCollection[]): Promise<SchemaCollection[]> {
return await new DataBaseHelper(SchemaCollection).saveMany(items);
}

// /**
// * Get schema
// * @param item
// */
// public static async saveSchemas(item: SchemaCollection[]): Promise<SchemaCollection[]> {
// const result = [];
// for await (const schema of item) {
// result.push(await new DataBaseHelper(SchemaCollection).save(schema));
// }
// return result;
// }

/**
* Get schema
* @param item
Expand Down Expand Up @@ -2520,13 +2536,21 @@ export class DatabaseServer extends AbstractDatabaseServer {
}

/**
* Get policy
* Update policy
* @param model
*/
public static async updatePolicy(model: Policy): Promise<Policy> {
return await new DataBaseHelper(Policy).save(model);
}

/**
* Update policies
* @param models
*/
public static async savePolicies(models: Policy[]): Promise<Policy[]> {
return await new DataBaseHelper(Policy).saveMany(models);
}

/**
* Get policies and count
* @param filters
Expand Down Expand Up @@ -2975,6 +2999,15 @@ export class DatabaseServer extends AbstractDatabaseServer {
return await new DataBaseHelper(ArtifactCollection).save(artifact);
}

/**
* Save Artifacts
* @param artifacts Artifacts
* @returns Saved Artifacts
*/
public static async saveArtifacts(artifacts: ArtifactCollection[]): Promise<ArtifactCollection[]> {
return await new DataBaseHelper(ArtifactCollection).saveMany(artifacts);
}

/**
* Get Artifact
* @param filters Filters
Expand Down Expand Up @@ -3334,12 +3367,20 @@ export class DatabaseServer extends AbstractDatabaseServer {
return await new DataBaseHelper(Tag).find(filters, options);
}

/**
* Update tag
* @param tag
*/
public static async updateTag(tag: Tag): Promise<Tag> {
return await new DataBaseHelper(Tag).update(tag);
}

/**
* Update tags
* @param row
* @param tags
*/
public static async updateTag(row: Tag): Promise<Tag> {
return await new DataBaseHelper(Tag).update(row);
public static async updateTags(tags: Tag[]): Promise<Tag[]> {
return await new DataBaseHelper(Tag).updateMany(tags);
}

/**
Expand Down Expand Up @@ -3368,6 +3409,14 @@ export class DatabaseServer extends AbstractDatabaseServer {
return await new DataBaseHelper(TagCache).update(row);
}

/**
* Update tags cache
* @param rows
*/
public static async updateTagsCache(rows: TagCache[]): Promise<TagCache[]> {
return await new DataBaseHelper(TagCache).updateMany(rows);
}

/**
* Create Theme
* @param theme
Expand Down
51 changes: 45 additions & 6 deletions common/src/interfaces/database-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,14 @@ export abstract class AbstractDatabaseServer {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.updatePolicy.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Update policies
* @param models
*/
public static async savePolicies(models: Policy[]): Promise<Policy[]> {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.savePolicies.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Get policies and count
* @param filters
Expand Down Expand Up @@ -968,9 +976,9 @@ export abstract class AbstractDatabaseServer {

/**
* Get schema
* @param item
* @param items
*/
public static async saveSchema(item: SchemaCollection): Promise<SchemaCollection> {
public static async saveSchema(items: SchemaCollection): Promise<SchemaCollection> {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.saveSchema.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

Expand Down Expand Up @@ -1355,11 +1363,17 @@ export abstract class AbstractDatabaseServer {
*/
public abstract removeTag(tag: Tag): Promise<void>

/**
* Update tag
* @param tag
*/
public abstract updateTag(tag: Tag): Promise<Tag>

/**
* Update tags
* @param row
* @param tags
*/
public abstract updateTag(row: Tag): Promise<Tag>
public abstract updateTags(tags: Tag[]): Promise<DryRun[] | Tag[]>

/**
* Get tag By UUID
Expand Down Expand Up @@ -1766,11 +1780,19 @@ export abstract class AbstractDatabaseServer {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.getTags.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Update tag
* @param tag
*/
public static async updateTag(tag: Tag): Promise<Tag> {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.updateTag.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Update tags
* @param row
* @param tags
*/
public static async updateTag(row: Tag): Promise<Tag> {
public static async updateTags(tags: Tag[]): Promise<Tag[]> {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.updateTag.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

Expand Down Expand Up @@ -1799,6 +1821,14 @@ export abstract class AbstractDatabaseServer {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.updateTagCache.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Update tags cache
* @param rows
*/
public static async updateTagsCache(rows: TagCache[]): Promise<TagCache[]> {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.updateTagsCache.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Create Theme
* @param theme
Expand Down Expand Up @@ -1875,6 +1905,15 @@ export abstract class AbstractDatabaseServer {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.saveArtifact.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Save Artifacts
* @param artifacts Artifacts
* @returns Saved Artifacts
*/
public static async saveArtifacts(artifacts: ArtifactCollection[]): Promise<ArtifactCollection[]> {
throw new Error(`${AbstractDatabaseServer.name}.${AbstractDatabaseServer.saveArtifact.name}: ${STATUS_IMPLEMENTATION.METHOD_IS_NOT_IMPLEMENTED}`);
}

/**
* Get Artifact
* @param filters Filters
Expand Down
45 changes: 45 additions & 0 deletions common/tests/unit-tests/database-helper/database-helper.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,49 @@ describe('DataBaseHelper Tests', () => {
assert.equal(finalCount, initialCount + 10);
});
});

describe('saveMany Method', () => {
it('should save multiple new entities correctly', async () => {
const existingEntities = await dbHelper.findAll();
await dbHelper.remove(existingEntities);

const entitiesData = [
{ name: 'Entity 1' },
{ name: 'Entity 2' }
];

const savedEntities = await dbHelper.saveMany(entitiesData);

assert.isArray(savedEntities);
assert.lengthOf(savedEntities, 2);
assert.equal(savedEntities[0].name, 'Entity 1');
assert.equal(savedEntities[1].name, 'Entity 2');

assert.equal(db[TestEntity.name].length, 2);
assert.equal(db[TestEntity.name][0].name, 'Entity 1');
assert.equal(db[TestEntity.name][1].name, 'Entity 2');
});
});

describe('updateMany Method', () => {
it('should update multiple entities correctly', async () => {
const savedEntities = await dbHelper.findAll();

const entitiesToUpdate = [
{ name: 'Updated Entity 1', id: savedEntities[0].id },
{ name: 'Updated Entity 2', id: savedEntities[1].id }
];

const updatedEntities = await dbHelper.updateMany(entitiesToUpdate);

assert.isArray(updatedEntities);
assert.lengthOf(updatedEntities, 2);
assert.equal(updatedEntities[0].name, 'Updated Entity 1');
assert.equal(updatedEntities[1].name, 'Updated Entity 2');

assert.equal(db[TestEntity.name].length, 2);
assert.equal(db[TestEntity.name][0].name, 'Updated Entity 1');
assert.equal(db[TestEntity.name][1].name, 'Updated Entity 2');
});
});
});
33 changes: 30 additions & 3 deletions common/tests/unit-tests/database-helper/mocks-database-helper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ const entityManagerMock = {
return collection.filter(entity => entity._id.toString() === query.toString());
}

if (query.id && query.id.$in) {
return collection.filter(entity => query.id.$in.includes(entity.id));
}

return collection.filter(entity => {
return Object.keys(query).every(key => entity[key] === query[key]);
});
Expand All @@ -161,6 +165,26 @@ const entityManagerMock = {
findAll: sandbox.stub().callsFake(() => {
return inMemoryStore[TestEntity.name] || [];
}),
getMongoManager: sandbox.stub().returns({
bulkWrite: sandbox.stub().callsFake((bulkOps) => {
// console.log('bulkOps', bulkOps);
const collection = inMemoryStore[TestEntity.name] || [];

bulkOps.forEach(op => {
const { filter, update } = op.updateOne;
// console.log('filter, update', filter, update);
const entityToUpdate = collection.find(e => e.id.toString() === filter.id?.toString());

// console.log('entityToUpdate', entityToUpdate);

if (entityToUpdate) {
Object.assign(entityToUpdate, update.$set);
}
});

inMemoryStore[TestEntity.name] = collection;
}),
}),
}),
getDriver: sandbox.stub().returns({
getConnection: sandbox.stub().returns({
Expand Down Expand Up @@ -198,9 +222,12 @@ export const {DataBaseHelper} = await esmock('../../../dist/helpers/db-helper.js
CreateRequestContext: () => {
return (target, propertyKey, descriptor) => descriptor;
},
wrap: () => ({
assign: sandbox.stub().returns(new TestEntity()),
})
wrap: (entity) => ({
assign: (data) => {
Object.assign(entity, data);
return entity;
},
}),
},
'mongodb': {
GridFSBucket: function () {
Expand Down
Loading