Skip to content

Duplicated entries when initializing multiple resources #5

@ghost

Description

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:

  1. 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));
  1. Create new element for new resource artists:
POST http://localhost:1947/slam/data/artists { "name" : "foo" }
  1. Database content is as expected:
{
  "artists": {
    "data": [
      {
        "name": "foo",
        "id": 1
      }
    ],
    "metadata": {
      "lastId": 1
    }
  }
}
  1. Create another new element for a different new resource rounds:
POST http://localhost:1947/slam/data/rounds { "name": "bar" }
  1. 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
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions