I don't fully understand the reasons for this problem, but to me it looks like a bug in lowdb-api, lowdb or lodash. The steps to reproduce the issue are shown below.
The source of the problem seems to be here. The call to db.defaults() works fine the first time. But on the second run the defaultResource is not the default anymore, but contains the data for the first resource. The first call seems to have altered defaultResource, though I don't know why and how.
A workaround is to inline the defaultResource into the call like this:
db.defaults({ [key]: { data: [], metadata: {} } }).write()
Steps to reproduce:
- Config
var app = express();
var db_file = path.join(opts.baseDir, '/db.json');
var db_options = { prefix: "/slam/data" };
app.use('/slam/data', bodyParser.json());
app.use('/slam/data', lowdbApi(db_file, db_options));
- Create new element for new resource
artists:
POST http://localhost:1947/slam/data/artists { "name" : "foo" }
- Database content is as expected:
{
"artists": {
"data": [
{
"name": "foo",
"id": 1
}
],
"metadata": {
"lastId": 1
}
}
}
- Create another new element for a different new resource
rounds:
POST http://localhost:1947/slam/data/rounds { "name": "bar" }
- The database content shows duplicated entries for both resources:
{
"artists": {
"data": [
{
"name": "foo",
"id": 1
},
{
"name": "bar",
"rate_by_score": false,
"id": 2
}
],
"metadata": {
"lastId": 2
}
},
"rounds": {
"data": [
{
"name": "foo",
"id": 1
},
{
"name": "bar",
"rate_by_score": false,
"id": 2
}
],
"metadata": {
"lastId": 2
}
}
}
I don't fully understand the reasons for this problem, but to me it looks like a bug in lowdb-api, lowdb or lodash. The steps to reproduce the issue are shown below.
The source of the problem seems to be here. The call to
db.defaults()works fine the first time. But on the second run thedefaultResourceis not the default anymore, but contains the data for the first resource. The first call seems to have altereddefaultResource, though I don't know why and how.A workaround is to inline the
defaultResourceinto the call like this:Steps to reproduce:
artists:rounds: