Why does my sequelize model instance lose its id?

118
June 14, 2019, at 02:50 AM

I've got a node-based microservice built on top of postgres, using sequelize to perform queries. I've got a table of Pets, each with an id (uuid) and a name (string). And, I've got a function for fetching Pets from the database by name, which wraps the nasty-looking sequelize call:

async function getPetByName( petName ) {
    const sqlzPetInstance = Database.Pet.findOne({
        where: { name: { [Sequelize.Op.iLike]: petName } }
    })
    if(!sqlzPetInstance) return undefined
    return sqlzPetInstance
}

It works great.

Later, to improve performance, I added some very short-lived caching to that function, like so:

async function getPetByName( petName ) {
    if( ramCache.get(petName) ) return ramCache.get(petName)
    const sqlzPetInstance = await Database.Pet.findOne({ ... })
    if(!sqlzPetInstance) return undefined
    return ramCache.set(petName, sqlzPetInstance) // persists for 5 seconds
}

Now I've noticed that items served from the cache sometimes have their id prop removed! WTF?!

I've added logging, and discovered that the ramCache entry is still being located reliably, and the value is still an instance of the sqlz Pet model. All the other attributes on the model are still present, but dataValues.id is undefined. I also noticed that _previousDataValues.id has the correct value, which suggests to me this really is the model instance I want it to be, but modified for some reason.

What can explain this? Is this what I would see if callers who obtain the model mutate it by assigning to id? What can cause _previousDataValues and dataValues to diverge? Are there cool sqlz techniques I can use to catch the culprit (perhaps by defining custom setters that log or throw)?

EDIT: experimentation shows that I can't overwrite the id by assigning to it. That's cool, but now I'm pretty much out of ideas. If it's not some kind of irresponsible mutation (which I could protect against), then I can't think of any sqlz instance methods that would result in removing the id.

Rent Charter Buses Company
READ ALSO
Slack App (bot) send private messages to multiple users

Slack App (bot) send private messages to multiple users

i'm trying to create a chess bot to play with other users on the workspaceI can't figure out how to create a private conversation including 2 users (the 2 players) and the bot user

144
Async and recursive directory scan, for file listing in Nodejs and Expressjs

Async and recursive directory scan, for file listing in Nodejs and Expressjs

In this Expressjs route file I'm trying to get (recursively) all the JSON files inside a/data directory

113
Why is my object not populating data into the chart? (demo included)

Why is my object not populating data into the chart? (demo included)

Question: Why is my object not populating data into the chart?

127
BeautifulSoup - Modify contents of Tag

BeautifulSoup - Modify contents of Tag

Given the object soup with a value bs4BeautifulSoup("<tr><td>Hello!</td><td>World!</td></tr>"), how do I remove exclamation marks from all tr tags?

23