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

谁都会走 提交于 2019-12-11 04:23:30

问题


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

creating lowdb using Js

const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
var customerpath = process.env.APPDATA+'/VEGFRUIT/Customer.json';
const cusadapter1 = new FileSync(customerpath)
const db = low(cusadapter1)
db.defaults({ customer: [{}]}).write();

how to convert above set of coding into Tyepscript?


回答1:


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<any>;

    constructor(){
        this.initDatabase();
    }

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

    private async SetSomeonesName(fullname: string): Promise<any> {
        await this.db.set("user.fullname", fullname).write();
    }
}


来源:https://stackoverflow.com/questions/52063654/how-to-create-lowdb-in-typescriptmain-ts-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!