import { nanoid } from 'nanoid' ; import { ICrudable , IModel } from './crudable' ; export class LocalCrud < T extends IModel > implements ICrudable < T > { store : Map < T [ 'id' ] , T > ; constructor ( map ) { this . store = map ; } async create ( data ) { const id = data . id || nanoid ( ) ; this . store . set ( id , { id , ... data } as T ) ; return this . store . get ( id ) ; } async read ( id ) { if ( ! this . store . has ( id ) ) throw new Error ( `Item with id = ${ id } not found` ) ; return this . store . get ( id ) ; } async update ( id , data ) { if ( ! this . store . has ( id ) ) throw new Error ( `Item with id = ${ id } not found` ) ; this . store . set ( id , { ... this . store . get ( id ) , ... data } ) ; return this . store . get ( id ) ; } async delete ( id ) { if ( ! this . store . has ( id ) ) throw new Error ( `Item with id = ${ id } not found` ) ; this . store . delete ( id ) ; return id ; } }