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

relations are not consistently cached #547

Open
jasonbodily opened this issue Jan 16, 2020 · 1 comment
Open

relations are not consistently cached #547

jasonbodily opened this issue Jan 16, 2020 · 1 comment

Comments

@jasonbodily
Copy link

jasonbodily commented Jan 16, 2020

Description

Relations of resources are not predictably made available, even when using the with and bypassCache operators.

Environment

  • js-data version: 3.0.8
  • node: 10.15.3
  • operating system: Catalina

Steps to reproduce

I've included a full example that can be run. The idea is that a user is on one team. Sometimes team information is attached to the serialized object received by the client, and sometimes it isn't. But if it is, it should be available.

  1. Copy and run the server with the code below.
  2. Invoke the client code function as it is, and you'll find that the team will be logged to the console successfully as expected. This is because the team was included in the user serializer
  3. Uncomment out the first request, you'll find that the team is not logged on the first attempt (expected) since it was not on the vanilla serializer. The problem is that it is still not logged on the second request which is explicitly looking for it.

Client Code

import { HttpAdapter } from 'js-data-http'
import { DataStore } from 'js-data'

export default async () => {
  const http = new HttpAdapter({
    basePath: 'http://localhost:3000/api'
  })
  const store = new DataStore({ useFetch: true })
  store.registerAdapter('http', http, { 'default': true })
  store.defineMapper('Team')
  store.defineMapper('User', {
    endpoint: 'users',
    relations: {
      belongsTo: {
        Team: {
          foreignKey: 'team_id',
          localField: 'team'
        }
      }
    }
  })
  // await store.findAll('User', {}, { force: true }).then(users => {
  //   console.log('no_team_expected', users[0].team)
  // })
  store.findAll('User', { serializer: 'include_team' }, { force: true, bypassCache: true, with: ['Team'] }).then(users => {
    console.log('team_expected', users[0].team)
  })
}

Server Code
Download the attached server code, run npm install and node index.js from the root
dev-server.zip
(Contents shown below:)

const express = require('express')
const app = express()
const port = 3000

app.use(express.json())

app.get('/api/users', (request, response) => {
  if (request.query.serializer === 'include_team') {
    response.send([
      {
        "id": "762f9b2c-2af9-4dc0-ba23-768ef035e33e",
        "team": {
          "id": 722,
          "name": "Team A"
        },
        "team_id": 722
      },
      {
        "id": "4b2d480c-e0aa-44e8-93c7-5d7233ab2223",
        "team": {
          "id": 2061,
          "name": "Team B"
        },
        "team_id": 2061,
      }
    ])
  } else {
    response.send([
      {
        "id":"762f9b2c-2af9-4dc0-ba23-768ef035e33e",
        "team_id":722
      },
      {
        "id":"4b2d480c-e0aa-44e8-93c7-5d7233ab2223",
        "team_id":2061
      }
    ])
  }
})

app.listen(port, (err) => {
  if (err) {
    return console.log('something bad happened', err)
  }
  console.log(`server is listening on ${port}`)
})
@crobinson42
Copy link
Member

PR's are welcome on this :-)

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

2 participants