@marxlnfcs/nest-loki
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

LokiJS-ORM Module for NestJS

npm NPM Snyk Vulnerabilities for npm package Website

Warning This library is for experimentation and may contain some bugs that I will remove from time to time. With this library I'm learning how dependency injection works and how to build such libraries according to "best practice".

So please use this library with caution.

Installation

npm i @marxlnfcs/nest-loki

Usage

AppModule

@Module({
    imports: [
        LokiJSModule.forRoot({
            ...
        }),
        LokiJSModule.forRootAsync({
            useFactory: (configService: ConfigService) => ({
                ...
            }),
            inject: [ConfigService]
        })
    ]
})
export class AppModule {}

Patterns

Entity

The entity is a model definition, that will be stored in the database.

// ------------------------------
// entities/test.entity.ts
import {LokiEntity} from '@marxlnfcs/nest-loki';
import {LokiColumnBoolean, LokiColumnCreated, LokiColumnId, LokiColumnText, LokiColumnUpdated} from "../src/lib/decorators/column.decorator";

@LokiEntity('<collection-name>')
export class TestEntity {
    @LokiColumnId()
    id: string;

    @LokiColumnText()
    name: string;

    @LokiColumnText({ nullable: true })
    source: string|null;

    @LokiColumnBoolean({ default: false })
    enabled: boolean;

    @LokiColumnUpdated()
    updated: Date;

    @LokiColumnCreated()
    created: Date;
}

// ------------------------------
// app.module.ts
import {LokiJSModule} from '@marxlnfcs/nest-loki';

@Module({
    imports: [
        LokiJSModule.forFeature(TestEntity)
    ]
})
export class AppModule {}

Repositories

The repository helps you to fetch the data from the database. This class works like a service and you can use the dependency injection.

// ------------------------------
// repositories/test.repository.ts
import {LokiRepository, LokiJSRepository} from '@marxlnfcs/nest-loki';
import {TestEntity} from '../entities/test.entity.ts';

@LokiRepository(TestEntity)
export class TestRepository extends LokiJSRepository<TestEntity> {}

// ------------------------------
// app.module.ts
import {LokiJSModule} from '@marxlnfcs/nest-loki';

@Module({
  imports: [
    LokiJSModule.forFeature(TestRepository)
  ]
})
export class AppModule {}

Subscribers

The subscriber can be assigned to any entity. The subscriber's function is to manipulate the entities before they are returned from the repository or written to the database.

// ------------------------------
// subscribers/test.subscriber.ts
import {LokiSubscriber, ILokiJSSubscriber} from '@marxlnfcs/nest-loki';
import {TestEntity} from '../entities/test.entity.ts';

@LokiSubscriber(TestEntity)
export class TestSubscriber extends ILokiJSSubscriber<TestEntity> {
    afterLoad(entity: TestEntity): Promise<TestEntity> | Promise<void> | void | TestEntity {}
    beforeInsert(entity: TestEntity): Promise<TestEntity> | Promise<void> | void | TestEntity {}
    afterInsert(entity: TestEntity): Promise<TestEntity> | Promise<void> | void | TestEntity {}
    beforeUpdate(entity: TestEntity, databaseEntity: TestEntity): Promise<TestEntity> | Promise<void> | void | TestEntity {}
    afterUpdate(entity: TestEntity): Promise<TestEntity> | Promise<void> | void | TestEntity {}
    afterDelete(entity: TestEntity): Promise<TestEntity> | Promise<void> | void | TestEntity {}
}

// ------------------------------
// app.module.ts
import {LokiJSModule} from '@marxlnfcs/nest-loki';

@Module({
    imports: [
        LokiJSModule.forFeature(TestSubscriber)
    ]
})
export class AppModule {}

Readme

Keywords

none

Package Sidebar

Install

npm i @marxlnfcs/nest-loki

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

259 kB

Total Files

88

Last publish

Collaborators

  • marxlnfcs