import { Injectable , Optional } from '@nestjs/common' ; import { InMemoryDBV1Config , InMemoryDBV1Entity } from '../interfaces' ; import { Observable , of } from 'rxjs' ; deprecated @ Injectable ( ) export class InMemoryDBV1Service < T extends InMemoryDBV1Entity > { private recordMap : { [ id ] : T ; } = { } ; constructor ( @ Optional ( ) private readonly config ) { } param records set records ( records ) { if ( ! records || records . length === 0 ) { this . recordMap = { } ; } this . recordMap = records . reduce ( ( previous , current ) => { return { ... previous , [ current . id ] : current , } ; } , this . recordMap ) ; } get records ( ) : T [ ] { return Object . keys ( this . recordMap ) . map ( ( key ) => this . recordMap [ key ] ) ; } param record public create ( record ) { const id = record . id || this . getNextId ( ) ; const newRecord = { ... record , id } as T ; this . recordMap = { ... this . recordMap , [ id ] : newRecord , } ; return newRecord ; } param record public createAsync ( record ) { const result$ = of ( this . create ( record ) ) ; return result$ ; } param records public createMany ( records ) { return records . map ( ( record ) => this . create ( record ) ) ; } param records public createManyAsync ( records ) { const result$ = of ( this . createMany ( records ) ) ; return result$ ; } param record public update ( record ) { this . recordMap = { ... this . recordMap , [ record . id ] : { ... record } , } ; } param record public updateAsync ( record ) { this . update ( record ) ; const result$ = of < void > ( ) ; return result$ ; } param records public updateMany ( records ) { for ( const record of records ) { this . update ( record ) ; } } param records public updateManyAsync ( records ) { this . updateMany ( records ) ; const result$ = of < void > ( ) ; return result$ ; } param id public delete ( id ) { const { [ id ] : removed , ... remainder } = this . recordMap ; this . recordMap = { ... remainder , } ; } param id public deleteAsync ( id ) { this . delete ( id ) ; const result$ = of < void > ( ) ; return result$ ; } param ids public deleteMany ( ids ) { for ( const id of ids ) { this . delete ( id ) ; } } param ids public deleteManyAsync ( ids ) { this . deleteMany ( ids ) ; const result$ = of < void > ( ) ; return result$ ; } param id public get ( id ) { return this . recordMap [ id ] ; } param id public getAsync ( id ) { const result$ = of ( this . get ( id ) ) ; return result$ ; } param ids public getMany ( ids ) { const records = ids . filter ( ( id ) => this . recordMap [ id ] ) . map ( ( id ) => { return this . recordMap [ id ] ; } ) ; return records ; } param ids public getManyAsync ( ids ) { const result$ = of ( this . getMany ( ids ) ) ; return result$ ; } /**
     * Return all of the records of type `T`.
     */ public getAll ( ) { return this . records || [ ] ; } /**
     * Return all the records of type `T` as an Observable.
     */ public getAllAsync ( ) { const result$ = of ( this . getAll ( ) ) ; return result$ ; } param predicate public query ( predicate ) { return this . records . filter ( predicate ) ; } param predicate public queryAsync ( predicate ) { const result$ = of ( this . query ( predicate ) ) ; return result$ ; } param recordFactory param amount public seed ( recordFactory , amount = 10 ) { amount = amount === null ? 10 : amount ; const recordsToCreate = [ ... Array ( amount ) . keys ( ) ] . map ( ( i ) => recordFactory ( i ) ) ; this . createMany ( recordsToCreate ) ; } /**
     * get the next id by finding the max id in the current records array and adding 1 to that value.
     * Example:
     * - current `recordMap`
     * ```json5
     * {
     *    1: { "id": 1, "prop": "test1" },
     *    4: { "id": 4, "prop": "test2" }
     * }
     * ```
     * - next next id would be: `5` as the current highest id is `4` + `1` = `5`.
     */ private getNextId ( ) { if ( this . records && this . records . length > 0 ) { return Math . max ( ... this . records . map ( ( r ) => r . id ) ) + 1 ; } return 1 ; } }