Sunday, 2 December 2018

Angular 6 with lowdb - not getting it to work

I'm trying to create an Electron app with Angular 6 that uses lowdb as a local database.

It's all very new to me and it's trial and error, but I don't seem to be able to figure out how to overcome the following error:

I've installed lowdb in my application by using the command

npm install --save @types/lowdb

I've created a service to communicate with this "local database".

import { Injectable } from '@angular/core';
import lowdb from 'lowdb';
import { default as FileAsync } from 'lowdb/adapters/FileAsync';
import { CurrentUserModel } from '../models/current-user';

@Injectable({
  providedIn: 'root'
})
export class LowdbService {

  private db: lowdb.LowdbAsync;

  constructor() {
    this.initDatabase();
  }

  set( field: string, value: any ) {
    this.db.set( field, value ).write();
  }

  private async initDatabase() {
    const adapter = new FileAsync( 'db.json' );
    this.db = await lowdb( adapter );

    this.db.defaults( { user: CurrentUserModel } );
  }
}

But when I include the service in the constructor of a component I get errors.

ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb' in '/Users/paul/Projects/application-name/src/app/services'
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb/adapters/FileAsync' in '/Users/paul/Projects/application-name/src/app/services'
ℹ 「wdm」: Failed to compile.
ERROR in src/app/services/lowdb.service.ts(2,12): error TS1192: Module '"/Users/paul/Projects/application-name/node_modules/@types/lowdb/index"' has no default export.
src/app/services/lowdb.service.ts(3,14): error TS2305: Module '"/Users/paul/Projects/application-name/node_modules/@types/lowdb/adapters/FileAsync"' has no exported member 'default'.

As far as I can see I'm doing the same as mentioned in this Github comment and this Stackoverflow comment. I can't find any more documentation though.

Can somebody help me out?



from Angular 6 with lowdb - not getting it to work

1 comment:

  1. I am actually having similar issues. It seems like the library works great for new/blank json files. I want to start will a json file prepopulated.

    ReplyDelete