Skip to content

Commit

Permalink
Merge pull request #175 from tnramalho/feature/improvements-cache
Browse files Browse the repository at this point in the history
Feature/improvements cache
  • Loading branch information
MrMaz authored Jun 27, 2024
2 parents d24e626 + e4c2165 commit af4d7db
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, ManyToOne } from 'typeorm';
import { Entity, ManyToOne, Unique } from 'typeorm';
import { ReferenceIdInterface } from '@concepta/ts-core';
import { UserEntityFixture } from './user-entity.fixture';
import { CacheSqliteEntity } from '../../entities/cache-sqlite.entity';
Expand All @@ -7,7 +7,10 @@ import { CacheSqliteEntity } from '../../entities/cache-sqlite.entity';
* Cache Entity Fixture
*/
@Entity()
@Unique(['key', 'type', 'assignee.id'])
export class UserCacheEntityFixture extends CacheSqliteEntity {
@ManyToOne(() => UserEntityFixture, (user) => user.userCaches)
@ManyToOne(() => UserEntityFixture, (user) => user.userCaches, {
nullable: false,
})
assignee!: ReferenceIdInterface;
}
9 changes: 8 additions & 1 deletion packages/nestjs-cache/src/cache.seeder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Seeder } from '@concepta/typeorm-seeding';
import { CacheFactory } from './cache.factory';
import { UserFactoryFixture } from './__fixtures__/factories/user.factory.fixture';

/**
* Cache seeder
Expand All @@ -16,8 +17,14 @@ export class CacheSeeder extends Seeder {

// the factory
const cacheFactory = this.factory(CacheFactory);
const userFactory = this.factory(UserFactoryFixture);
const user = await userFactory.create();

// create a bunch
await cacheFactory.createMany(createAmount);
await cacheFactory.createMany(createAmount, {
assignee: {
id: user.id,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ describe('CacheAssignmentController (e2e)', () => {
});
});

it('POST /cache/user assignee id null', async () => {
const payload = {
key: 'dashboard-1',
type: 'filter',
data: '{}',
expiresIn: '1d',
assignee: { id: null },
};

await supertest(app.getHttpServer())
.post('/cache/user')
.send(payload)
.expect(500);
});

it('POST /cache/user wrong assignee id', async () => {
const payload = {
key: 'dashboard-1',
type: 'filter',
data: '{}',
expiresIn: '1d',
assignee: { id: 'test' },
};

await supertest(app.getHttpServer())
.post('/cache/user')
.send(payload)
.expect(500);
});

it('POST /cache/user Duplicated', async () => {
const payload: CacheCreatableInterface = {
key: 'dashboard-1',
Expand All @@ -148,15 +178,45 @@ describe('CacheAssignmentController (e2e)', () => {
expect(res.body.assignee.id).toBe(user.id);
});

payload.data = '{ "name": "John Doe" }';
payload.expiresIn = null;
await supertest(app.getHttpServer())
.post('/cache/user')
.send(payload)
.expect(201)
.then((res) => {
// check error message
expect(res.body.message).toBe(
'userCache already exists with the given key, type, and assignee ID.',
);
expect(res.status).toBe(400);
expect(res.body.key).toBe(payload.key);
expect(res.body.data).toBe(payload.data);
expect(res.body.assignee.id).toBe(user.id);
});
});

it('POST /cache/user Update', async () => {
const payload: CacheCreatableInterface = {
key: 'dashboard-1',
type: 'filter',
data: '{}',
expiresIn: '1d',
assignee: { id: user.id },
};
await supertest(app.getHttpServer())
.post('/cache/user')
.send(payload)
.expect(201)
.then((res) => {
expect(res.body.key).toBe(payload.key);
expect(res.body.assignee.id).toBe(user.id);
});
payload.data = '{ "name": "John Doe" }';
payload.expiresIn = null;
await supertest(app.getHttpServer())
.post('/cache/user/')
.send(payload)
.expect(201)
.then((res) => {
expect(res.body.key).toBe(payload.key);
expect(res.body.data).toBe(payload.data);
expect(res.body.assignee.id).toBe(user.id);
});
});

Expand Down
33 changes: 23 additions & 10 deletions packages/nestjs-cache/src/controllers/cache-crud.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { CacheSettingsInterface } from '../interfaces/cache-settings.interface';
import { CacheCrudService } from '../services/cache-crud.service';
import getExpirationDate from '../utils/get-expiration-date.util';
import { CacheService } from '../services/cache.service';
import { CacheEntityAlreadyExistsException } from '../exceptions/cache-entity-already-exists.exception';
import { CacheCreateDto } from '../dto/cache-create.dto';
/**
* Cache assignment controller.
*/
Expand Down Expand Up @@ -121,7 +121,7 @@ export class CacheCrudController
@AccessControlCreateOne(CacheResource.One)
async createOne(
@CrudRequest() crudRequest: CrudRequestInterface,
@CrudBody() cacheCreateDto: CacheCreatableInterface,
@CrudBody() cacheCreateDto: CacheCreateDto,
@Param('assignment') assignment: ReferenceAssignment,
) {
const expirationDate = getExpirationDate(
Expand All @@ -133,17 +133,30 @@ export class CacheCrudController
cacheCreateDto,
);

// update or create
if (existingCache) {
throw new CacheEntityAlreadyExistsException(
this.getEntityKey(assignment),
crudRequest.parsed.search.$and?.push({
id: {
$eq: existingCache.id,
},
});
// call crud service to create
const response = await this.getCrudService(assignment).updateOne(
crudRequest,
{
id: existingCache.id,
...cacheCreateDto,
expirationDate,
},
);
return response;
} else {
// call crud service to create
return this.getCrudService(assignment).createOne(crudRequest, {
...cacheCreateDto,
expirationDate,
});
}

// call crud service to create
return this.getCrudService(assignment).createOne(crudRequest, {
...cacheCreateDto,
expirationDate,
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/nestjs-cache/src/entities/cache-postgres.entity.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Column, Index } from 'typeorm';
import { Column, Unique } from 'typeorm';
import { ReferenceIdInterface } from '@concepta/ts-core';
import { CacheInterface } from '@concepta/ts-common';
import { CommonPostgresEntity } from '@concepta/typeorm-common';

/**
* Cache Postgres Entity
*/
@Index('key_unique_index', ['key', 'type', 'assignee.id'], { unique: true })
@Unique(['key', 'type', 'assignee.id'])
export abstract class CachePostgresEntity
extends CommonPostgresEntity
implements CacheInterface
Expand Down
4 changes: 2 additions & 2 deletions packages/nestjs-cache/src/entities/cache-sqlite.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Index } from 'typeorm';
import { Column, Unique } from 'typeorm';
import { CommonSqliteEntity } from '@concepta/typeorm-common';
import { ReferenceIdInterface } from '@concepta/ts-core';
import { CacheInterface } from '@concepta/ts-common';
Expand All @@ -7,7 +7,7 @@ import { CacheInterface } from '@concepta/ts-common';
* Cache Sqlite Entity
*/

@Index('key_unique_index', ['key', 'type', 'assignee.id'], { unique: true })
@Unique(['key', 'type', 'assignee.id'])
export abstract class CacheSqliteEntity
extends CommonSqliteEntity
implements CacheInterface
Expand Down
8 changes: 2 additions & 6 deletions packages/nestjs-cache/src/services/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { DeepPartial, Repository } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import {
CacheCreatableInterface,
CacheInterface,
CacheUpdatableInterface,
} from '@concepta/ts-common';
import { CacheInterface, CacheUpdatableInterface } from '@concepta/ts-common';
import { ReferenceAssignment, ReferenceId, Type } from '@concepta/ts-core';
import {
QueryOptionsInterface,
Expand Down Expand Up @@ -44,7 +40,7 @@ export class CacheService implements CacheServiceInterface {
*/
async create(
assignment: ReferenceAssignment,
cache: CacheCreatableInterface,
cache: CacheCreateDto,
queryOptions?: QueryOptionsInterface,
): Promise<CacheInterface> {
// get the assignment repo
Expand Down

0 comments on commit af4d7db

Please sign in to comment.