Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated entries when initializing multiple resources #5

Open
ghost opened this issue Oct 11, 2019 · 0 comments
Open

Duplicated entries when initializing multiple resources #5

ghost opened this issue Oct 11, 2019 · 0 comments

Comments

@ghost
Copy link

ghost commented Oct 11, 2019

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
    }
  }
}
ghost pushed a commit to fschrempf/lowdb-api that referenced this issue Oct 11, 2019
ghost pushed a commit to fschrempf/slam.js that referenced this issue Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants