I want to use NeDB as a database for a very simple ExpressJS application. I would like to store collections of objects into a separate file, so one file for orders (orders.json). However, whenever I insert an order object into the datastore, it is just appended as an extra object instead of creating an array.
var Datastore = require('nedb')
, orders = new Datastore({ filename: __dirname + '/orders.json', autoload: true });
const order = {
chatId: 4324234,
url: 'https://google.com',
maxPrice: 200,
incrementPrice: 2,
expiryHours: 24,
timestamp: Date.now()
}
orders.insert(order);
orders.insert(order);
This stores the objects as:
{"chatId":1,"url":"https://google.com","maxPrice":200,"incrementPrice":2,"expiryHours":24,"timestamp":1631185683197,"_id":"mh9tdb06Tw29JXSW"}
{"chatId":1,"url":"https://google.com","maxPrice":200,"incrementPrice":2,"expiryHours":24,"timestamp":1631185691156,"_id":"8Dg6GXFlygYMPmRZ"}
As you might see already, this is invalid JSON. I expect them to be stored in an array (one array containing all orders).
How can I achieve this using NeDB?