This commit is contained in:
admin 2025-08-21 12:27:20 +02:00
parent 08fd76dcef
commit f94e266d4a
4 changed files with 69 additions and 8 deletions

61
README.md Normal file
View File

@ -0,0 +1,61 @@
# InMemory Repository
Implementation of the [@apihub24/repository](https://www.npmjs.com/package/@apihub24/repository) to store the Objects in Memory.
## Install
```bash
npm i --save @apihub24/in-memory-repository
```
## Usage
```typescript
import { InMemoryRepository } from '@apihub24/in-memory-repository';
class TestModel {
id: string;
name: string;
age: number;
}
async function run() {
const repo = new InMemoryRepository<TestModel>();
// writes {id:"1",name:"Test",age:28} into console
console.info(await repo.save({
id: '1',
name: 'Test'
age: 28,
}));
// writes [{id:"1",name:"Test",age:28}] into console
console.info(await repo.getBy(x => x.name === 'Test'));
// writes true into console
console.info(await repo.deleteBy(x => x.name === 'Test'));
}
run();
```
or use it in nestjs with dependency injection
```typescript
...
{
provide: '@apihub24/organization_repository',
useClass: InMemoryRepository<IOrganization>,
},
{
provide: '@apihub24/account_repository',
useClass: InMemoryRepository<IAccount>,
},
{
provide: '@apihub24/group_repository',
useClass: InMemoryRepository<IGroup>,
},
{
provide: '@apihub24/right_repository',
useClass: InMemoryRepository<IRight>,
},
```

8
package-lock.json generated
View File

@ -9,16 +9,16 @@
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@apihub24/repository": "^1.0.1" "@apihub24/repository": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^5.9.2" "typescript": "^5.9.2"
} }
}, },
"node_modules/@apihub24/repository": { "node_modules/@apihub24/repository": {
"version": "1.0.1", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/@apihub24/repository/-/repository-1.0.1.tgz", "resolved": "https://registry.npmjs.org/@apihub24/repository/-/repository-1.0.2.tgz",
"integrity": "sha512-ex3Z+lxsHtVKDTolJQqLHswq9SKfXzM/hWv17zsrhKqJwuGxO7CeBFM60aiuApZX9NqBhGAkPGGj9jt+F/Y9HQ==", "integrity": "sha512-brZkSENCpC1/MJYBSHHeO+8odrKdq/q4U2z7HN5mrAvPl8GNjq0egOCZ22axQKynWuP9yimIIwJxExwp12YBzQ==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/typescript": { "node_modules/typescript": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@apihub24/in-memory-repository", "name": "@apihub24/in-memory-repository",
"version": "1.0.0", "version": "1.0.1",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
@ -8,7 +8,7 @@
"build": "tsc" "build": "tsc"
}, },
"dependencies": { "dependencies": {
"@apihub24/repository": "^1.0.1" "@apihub24/repository": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^5.9.2" "typescript": "^5.9.2"

View File

@ -1,11 +1,11 @@
import { Repository } from '@apihub24/repository'; import { IRepository } from "@apihub24/repository";
interface IdModel { interface IdModel {
id: string; id: string;
} }
export class InMemoryRepository<TModel extends IdModel> export class InMemoryRepository<TModel extends IdModel>
implements Repository<TModel> implements IRepository<TModel>
{ {
private source: TModel[] = []; private source: TModel[] = [];