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/cache module #156

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/nestjs-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Rockets NestJS Cache

Check notice on line 1 in packages/nestjs-cache/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-cache/README.md#L1

Expected: [None]; Actual: # Rockets NestJS Cache

A module for managing a basic Cache entity, including controller with full CRUD, DTOs, sample data factory and seeder.

Check notice on line 3 in packages/nestjs-cache/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-cache/README.md#L3

Expected: 80; Actual: 118

## Project

[![NPM Latest](https://img.shields.io/npm/v/@concepta/nestjs-user)](https://www.npmjs.com/package/@concepta/nestjs-user)
[![NPM Downloads](https://img.shields.io/npm/dw/@conceptadev/nestjs-user)](https://www.npmjs.com/package/@concepta/nestjs-user)
[![GH Last Commit](https://img.shields.io/github/last-commit/conceptadev/rockets?logo=github)](https://github.com/conceptadev/rockets)
[![GH Contrib](https://img.shields.io/github/contributors/conceptadev/rockets?logo=github)](https://github.com/conceptadev/rockets/graphs/contributors)
[![NestJS Dep](https://img.shields.io/github/package-json/dependency-version/conceptadev/rockets/@nestjs/common?label=NestJS&logo=nestjs&filename=packages%2Fnestjs-core%2Fpackage.json)](https://www.npmjs.com/package/@nestjs/common)

## Installation

`yarn add @concepta/nestjs-cache`

## Usage

```ts
// ...
import { TypeOrmExtModule } from '@concepta/nestjs-typeorm-ext';
import { CacheModule } from '@concepta/nestjs-user';
import { CrudModule } from '@concepta/nestjs-crud';

@Module({
imports: [
TypeOrmExtModule.forRoot({
type: 'postgres',
url: 'postgres://user:pass@localhost:5432/postgres',
}),
CrudModule.forRoot({}),
CacheModule.forRoot({}),
],
})
export class AppModule {}
```

## Configuration

- [Seeding](#seeding)
- [ENV](#env)

### Seeding

Configurations specific to (optional) database seeding.

#### ENV

Configurations available via environment.

| Variable | Type | Default | |

Check notice on line 51 in packages/nestjs-cache/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-cache/README.md#L51

Expected: 80; Actual: 92
| -------------------------- | ---------- | ------- | ------------------------------------ |

Check notice on line 52 in packages/nestjs-cache/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-cache/README.md#L52

Expected: 80; Actual: 92
| `CACHE_MODULE_SEEDER_AMOUNT` | `<number>` | `50` | number of additional users to create |

Check notice on line 53 in packages/nestjs-cache/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-cache/README.md#L53

Expected: 80; Actual: 94
| `CACHE_EXPIRE_IN` | `<string>` | `1d` | string for the amount of time to expire the cache |

Check notice on line 54 in packages/nestjs-cache/README.md

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/nestjs-cache/README.md#L54

Expected: 80; Actual: 96
43 changes: 43 additions & 0 deletions packages/nestjs-cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@concepta/nestjs-cache",
"version": "4.0.0-alpha.42",
"description": "Rockets NestJS User",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "BSD-3-Clause",
"publishConfig": {
"access": "public"
},
"files": [
"dist/**/!(*.spec|*.e2e-spec|*.fixture).{js,d.ts}"
],
"dependencies": {
"@concepta/nestjs-access-control": "^4.0.0-alpha.42",
"@concepta/nestjs-common": "^4.0.0-alpha.42",
"@concepta/nestjs-core": "^4.0.0-alpha.42",
"@concepta/nestjs-crud": "^4.0.0-alpha.42",
"@concepta/nestjs-typeorm-ext": "^4.0.0-alpha.42",
"@concepta/ts-common": "^4.0.0-alpha.42",
"@concepta/ts-core": "^4.0.0-alpha.42",
"@concepta/typeorm-common": "^4.0.0-alpha.42",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/swagger": "^6.0.0",
"ms": "^2.1.3"
},
"devDependencies": {
"@concepta/typeorm-seeding": "^4.0.0-beta.0",
"@faker-js/faker": "^6.0.0-alpha.6",
"@nestjs/testing": "^9.0.0",
"@nestjs/typeorm": "^9.0.0",
"@types/ms": "^0.7.31",
"@types/supertest": "^2.0.11",
"jest-mock-extended": "^2.0.4",
"supertest": "^6.1.3"
},
"peerDependencies": {
"class-transformer": "*",
"class-validator": "*",
"typeorm": "^0.3.0"
}
}
33 changes: 33 additions & 0 deletions packages/nestjs-cache/src/__fixtures__/app.module.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TypeOrmExtModule } from '@concepta/nestjs-typeorm-ext';
import { Module } from '@nestjs/common';

import { CacheModule } from '../cache.module';
import { UserEntityFixture } from './entities/user-entity.fixture';
import { UserCacheEntityFixture } from './entities/user-cache-entity.fixture';
import { CrudModule } from '@concepta/nestjs-crud';

@Module({
imports: [
TypeOrmExtModule.forRoot({
type: 'sqlite',
database: ':memory:',
synchronize: true,
entities: [UserEntityFixture, UserCacheEntityFixture],
}),
CacheModule.register({
settings: {
assignments: {
user: { entityKey: 'userCache' },
},
},
entities: {
userCache: {
entity: UserCacheEntityFixture,
},
},
}),
CrudModule.forRoot({}),
],
})
export class AppModuleFixture { }

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Entity, ManyToOne } from 'typeorm';
import { ReferenceIdInterface } from '@concepta/ts-core';
import { UserEntityFixture } from './user-entity.fixture';
import { CacheSqliteEntity } from '../../entities/cache-sqlite.entity';

/**
* Cache Entity Fixture
*/
@Entity()
export class UserCacheEntityFixture extends CacheSqliteEntity {
@ManyToOne(() => UserEntityFixture, (user) => user.userCaches)
assignee!: ReferenceIdInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { ReferenceIdInterface } from '@concepta/ts-core';
import { UserCacheEntityFixture } from './user-cache-entity.fixture';

/**
* User Entity Fixture
*/
@Entity()
export class UserEntityFixture implements ReferenceIdInterface {
@PrimaryGeneratedColumn('uuid')
id!: string;

@Column({ default: false })
isActive!: boolean;

@OneToMany(() => UserCacheEntityFixture, (userCache) => userCache.assignee)
userCaches!: UserCacheEntityFixture[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { Factory } from '@concepta/typeorm-seeding';
import { UserCacheEntityFixture } from '../entities/user-cache-entity.fixture';
import Faker from '@faker-js/faker';

export class UserCacheFactoryFixture extends Factory<UserCacheEntityFixture> {
protected options = {
entity: UserCacheEntityFixture,
};

protected async entity(
userCache: UserCacheEntityFixture,
): Promise<UserCacheEntityFixture> {
userCache.key = Faker.name.jobArea();
userCache.type = 'filter';
userCache.data = JSON.stringify({ sortBy: 'name' });
userCache.expirationDate = new Date();

return userCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Factory } from '@concepta/typeorm-seeding';
import { UserEntityFixture } from '../entities/user-entity.fixture';

export class UserFactoryFixture extends Factory<UserEntityFixture> {
options = {
entity: UserEntityFixture,
};
}
8 changes: 8 additions & 0 deletions packages/nestjs-cache/src/cache.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const CACHE_MODULE_SETTINGS_TOKEN = 'CACHE_MODULE_SETTINGS_TOKEN';
export const CACHE_MODULE_REPOSITORIES_TOKEN =
'CACHE_MODULE_REPOSITORIES_TOKEN';
export const CACHE_MODULE_CRUD_SERVICES_TOKEN =
'CACHE_MODULE_CRUD_SERVICES_TOKEN';
export const CACHE_MODULE_DEFAULT_SETTINGS_TOKEN =
'CACHE_MODULE_DEFAULT_SETTINGS_TOKEN';
export const CACHE_MODULE_CACHE_ENTITY_KEY = 'cache';
43 changes: 43 additions & 0 deletions packages/nestjs-cache/src/cache.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { randomUUID } from 'crypto';
import Faker from '@faker-js/faker';
import { Factory } from '@concepta/typeorm-seeding';
import { CacheInterface } from '@concepta/ts-common';
/**
* Cache factory
*/
export class CacheFactory extends Factory<CacheInterface> {
/**
* List of used names.
*/
keys: string[] = ['filter', 'sort', 'list'];

/**
* Factory callback function.
*/
protected async entity(cache: CacheInterface): Promise<CacheInterface> {
const fakeFilter = {
name: Faker.name.firstName,
orderBy: 'name',
};

// set the name
cache.key = randomUUID();
cache.type = this.randomKey();
cache.data = JSON.stringify(fakeFilter);
cache.expirationDate = new Date();

// return the new cache
return cache;
}

/**
* Get a random category.
*/
protected randomKey(): string {
// random index
const randomIdx = Math.floor(Math.random() * this.keys.length);

// return it
return this.keys[randomIdx];
}
}
Loading
Loading