我在网上搜索过,在Javascript
中创建Lowdb可以正常工作,但找不到合适的(根据初学者的理解)解决方案。
使用lowdb
创建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();
如何将上述编码集转换为
Tyepscript
? 最佳答案
1)执行此命令
npm install --save @types/lowdb
2)这个简短的示例类展示了如何以打字稿方式使用lowdb
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();
}
}