# base-repo

Latest version **3.1.13** (published 2024-06-04) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install base-repo
pnpm add base-repo
yarn add base-repo
bun add base-repo
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.13 |
| Published | 2024-06-04 |
| First published | 2020-10-28 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 191.7 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Fauzi Fadhillah |
| Maintainers | fauzifadhi |

## Links

- npm: https://www.npmjs.com/package/base-repo
- Repository: https://github.com/FauziFadhi/base-repo
- Homepage: https://github.com/FauziFadhi/base-repo#readme
- Issues: https://github.com/FauziFadhi/base-repo/issues
- npm.io page: https://npm.io/package/base-repo

## Dependencies (7)

- [rxjs](https://npm.io/package/rxjs.md) ^7.5.6
- [rimraf](https://npm.io/package/rimraf.md) ^3.0.2
- [xxhashjs](https://npm.io/package/xxhashjs.md) ^0.2.2
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^9.0.3
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^9.0.3
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@nestjs/platform-express](https://npm.io/package/@nestjs/platform-express.md) ^9.0.3

## Recent versions

- 3.1.13 (latest) — 2024-06-04
- 3.1.7 (release) — 2023-01-25
- 3.1.7-rc.1 (rc) — 2023-01-25
- 3.1.5-beta.2 (beta) — 2023-01-15
- 2.2.4-0 (v2.bugfix) — 2022-11-20
- 2.3.3 (support) — 2022-09-19
- 3.1.12 — 2024-06-04
- 3.1.11 — 2024-06-04
- 3.1.10 — 2023-08-08
- 3.1.9 — 2023-08-08
- 3.1.8 — 2023-01-25
- 3.1.7-rc.0 — 2023-01-25
- 3.1.6 — 2023-01-15
- 3.1.5-beta.1 — 2023-01-15
- 3.1.5-beta.0 — 2023-01-15
- … 75 more at https://npm.io/package/base-repo/versions

## README

## Description

Cache Invalidation at model level extended features for [sequelize-typescript](https://github.com/RobinBuschmann/sequelize-typescript) (v2.1.0 or later)

## Installation

```bash
$ npm install base-repo sequelize@^6.x.x sequelize-typescript@^2.x.x
```

Your `tsconfig.json` needs the following flags:

```json
"target": "es6", // or a more recent ecmascript version
"experimentalDecorators": true,
"emitDecoratorMetadata": true
```

## Module Definition

make sure defined all model of sequelize at this level
and

```typescript
@Module({
  ...
  imports: [
    RedisModule.register(cacheConfig() as RedisModuleOptions),

    SequelizeCacheModule.register({
      defaultTTL: 5, // DEFINE TTL FOR ALL PROJECT seconds
      // DEFINE HOW TO GET CACHE FROM GIVEN KEY
      callbackGet: async ({ key }) => CacheConfigModule.store.get(key),
      // DEFINE HOW TO INVALIDATE CACHE FROM GIVEN KEY
      callbackInvalidate: ({ key }) => (CacheConfigModule?.store?.del?.(key) || null),
      // DEFINE HOW TO SET CACHE FROM GIVEN KEY VALUE AND TTL
      callbackSet: async ({ key, value, ttl }) => CacheConfigModule.store.set(key, value, { ttl }),
      callbackGetKey: async ({ keyPattern }) => CacheConfigModule.store.keys?.(`${process.env.CACHE_PREFIX}${keyPattern}`) || [],
    }),

    SequelizeModule.forRoot({
      ...DBOptions,
    }),
   ...
  ],
})

export class CacheConfigModule {
  static store: Store;

  constructor(@Inject(CACHE_MANAGER) private store: Store) {
    CacheConfigModule.store = this.store;
  }
}
```

## Model Definition

### `@Cache(options)`

the @Cache is used for defined ttl and cache for findOne and automatically invalidate findOneCache/findByPkCache

#### `@Cache` API Options

| Options       | Description                                                         |
| ------------- | ------------------------------------------------------------------- |
| `options.ttl` | set TTL for this model, this will override ttl at module (Optional) |

```typescript
@Cache({
  ttl: 100,
})
@Table()
export class DmCourse extends BaseModel {}
```

### Extend `BaseModel`

```ts
@Cache({
  ttl: 100,
})
@Table()
export class DmCourse extends BaseModel {
  // default `{modelName} data not found'
  static notFoundMessage = 'your model not found message';

  /**
   *  @default `updatedAt`
   * @description `this is for checking newest updated timestamp for cached list.`
  */
  static onUpdateAttribute = 'modifiedAt'

   @UpdatedAt
  @Column({ field: 'UpdatedAt' })
    modifiedAt: Date;
}
```

the Model need to extends `BaseModel`

### More Strict

```ts
interface DmCourseAtt {
  id: number
  name: string;
  type: number;
}

interface DmCreateAtt extends DmCourseAtt Omit<DmCourseAtt, 'id'>

@Cache({
  ttl: 100,
})
@Table()
export class DmCourse extends BaseModel<DmCourseAtt, DmCreateAtt> {}
```

for strict type that can used at default function from sequelize-typescript can be planted at generic type 2 and 3 [sequelize-typescript strict](https://github.com/RobinBuschmann/sequelize-typescript#more-strict)

## How to use

### `Model.findOneCache(cacheOptions)`

- `cacheOptions : FindOptions` limited findOptions from sequelize-typescript

```ts
// file: DmCourse.ts

@Cache() //default ttl module
@Table()
export class DmCourse extends BaseModel<DmCourseAtt, DmCreateAtt> {}

...

// file: Course.controller.ts

class CourseController {
  async getCourse() {
    const course = await DmCourse.findOneCache({
      where: {
        isDeleted: false,
        name: 'Math',
      },
      rejectOnEmpty: true, // use model default throw, or use use throw Exception
      // rejectOnEmpty: new BadRequestException('message')
    })
  }
}
```

- you have to use All defined cache attributes that has name `byIsDeletedAndName`
- every query that outside of defined cache will not executed

#### `findOneCache` API Options

| Options                      | Description                                                                                                              |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `cacheOptions`               | some function from `Sequelize FindOptions`                                                                               |
| `cacheOptions.ttl`           | set TTL for this cache key, this will override ttl at module and model `(Optional)`, `(Required)` when has include Query |
| `cacheOptions.rejectOnEmpty` | will throw error when set `true` (Optional)                                                                              |

### `Model.findByPkCache(id, options)`

```ts
class CourseController {
  async getCourse() {
    const course = await DmCourse.findByPkCache(1, {
      ttl: 100,
    });
  }
}
```

- find By Primary Cache will invalidate when any destroy or update

#### `findByPkCache` API Options

| Options                      | Description                                                                                                              |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `id`                         | value of id                                                                                                              |
| `cacheOptions`               | some function from `Sequelize FindOptions`                                                                               |
| `cacheOptions.ttl`           | set TTL for this cache key, this will override ttl at module and model `(Optional)`, `(Required)` when has include Query |
| `cacheOptions.rejectOnEmpty` | will throw error when set `true` (Optional)                                                                              |

### `Model.findAllCache(cacheOptions)`

```ts
class CourseController {
  async getCourse() {
    const course = await DmCourse.findAllCache({
     ttl: 100,
     attributes: ['id','name','type']
     where: {
       isDeleted: false,
     },
     order: [
       ['id','desc']
     ],
     include: [
       {
         // any association
       }
     ],
     limit: 10,
    })
  }
}
```
- find all data and Cache it when has value. will invalidate and update cache when ttl reach 0 or when get max updates or count is different from before

#### `findAllCache` API Options

| Options             | Description                                                                                                            |
| ------------------- | ---------------------------------------------------------------------------------                                      |
| `cacheOptions.ttl`  | set TTL for this cache key, this will override ttl at module and model (Optional), `(Required)` when has include Query |
| `{...cacheOptions}` | is same with FindOptions from sequelize-typescript                                                                     |

## Stay in touch

- Author - [Fauzi Fadhillah](https://github.com/FauziFadhi)
- [Email](fauzifadhi@gmail.com)

---
_Source: https://npm.io/package/base-repo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
