How to create lowdb in TypeScript(main.ts) Angular?

后端 未结 1 1524
长发绾君心
长发绾君心 2021-01-25 02:12

Creating Lowdb in Javascript working fine, I have searched in net, couldn\'t find proper(as per beginner\'s understanding) solution.

creating lowdb

1条回答
  •  北海茫月
    2021-01-25 02:34

    1) perform this command

    npm install --save @types/lowdb
    

    2) this brief example class shows how to use lowdb in a typescript way

    import lowdb from "lowdb";
    import { default as FileAsync } from "lowdb/adapters/FileAsync";
    
    export class DbService {
        private db: lowdb.LowdbAsync;
    
        constructor(){
            this.initDatabase();
        }
    
        private async initDatabase() {
            const adapter = new FileAsync("db.json");
            this.db = await lowdb(adapter);
        }
    
        private async SetSomeonesName(fullname: string): Promise {
            await this.db.set("user.fullname", fullname).write();
        }
    }
    

    0 讨论(0)
提交回复
热议问题