0

簡単なnodejsビジター プログラムを作成しました。ここに、このプログラムの実行日を格納します。実行とは、node index.jsコマンドを使用してこのプログラムを実行することです。

プログラムを初めて実行すると、データ ({実行時間: 日付}) が 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