0

我写了一个简单的nodejs访问者程序。在这里我想存储这个程序的执行日期。执行是指使用node index.js命令运行这个程序。

第一次运行程序时,它会将数据({execution-time: date})存储到 db.json 文件中。但下次我运行程序时,旧数据将被删除并插入新数据。

但我想保留所有以前的数据和新数据。

index.js

const COLLECTION = 'build';
const DB_NAME = "db.json";
const UPLOAD_PATH= "database";
const Loki = require("lokijs");

const db = new Loki(`${UPLOAD_PATH}/${DB_NAME}`);

const col = db.getCollection(COLLECTION) || db.addCollection(COLLECTION );

col.insert({'execution-date':new Date()})

db.saveDatabase();


console.log(`Total Number of execution of this program : ${col.chain().data().length} `);

在这里我得到以下结果

Total Number of execution of this program : 1          #first run
Total Number of execution of this program : 1          #second run
Total Number of execution of this program : 1          #third run

预期产出

Total Number of execution of this program :1   #first run
Total Number of execution of this program :2   #second run
Total Number of execution of this program :3   #third run
4

0 回答 0