typicaljoe / taffydb

TaffyDB - an open source JavaScript Database for your browser
http://taffydb.com
MIT License
2.21k stars 285 forks source link

Taffy restore rewrites "___id" field #131

Closed csterritt closed 3 years ago

csterritt commented 7 years ago

Hello,

Found a pretty substantial bug, IMHO.

My situation is that I'm using Taffy to store some state in a Node application.

I discovered that re-reading the database state from the saved file rewrites the "___id" field, based on (seemingly) the order that the databases are read in.

Here's the code that can demonstrate the bug:

const Taffy = require('taffy')

// Cities then states
const cities = Taffy()
const states = Taffy()

cities.insert([
  {name:'New York',state:'WA'},
  {name:'Las Vegas',state:'NV'},
  {name:'Boston',state:'MA'}
])

states.insert([
  {name: 'New York', capital: 'Albany'},
  {name: 'Nevada', capital: 'Carson City'},
  {name: 'Massachusetts', capital: 'Boston'}
])

const cityStr = cities().stringify()
const stateStr = states().stringify()

// Restore: States then cities
const restoredStates = Taffy(stateStr)
const restoredCities = Taffy(cityStr)

const newYorkCity = cities({name: 'New York'}).first()
const newYorkCityRestored = restoredCities({name: 'New York'}).first()
console.log("New York city original ID", newYorkCity.___id)
console.log("New York city restored ID", newYorkCityRestored.___id)

const newYorkState = states({name: 'New York'}).first()
const newYorkStateRestored = restoredStates({name: 'New York'}).first()
console.log("New York state original ID", newYorkState.___id)
console.log("New York state restored ID", newYorkStateRestored.___id)

Here's the output:

$ node show_bug_taffy.js
New York city original ID T000003R000002
New York city restored ID T000005R000002
New York state original ID T000003R000002
New York state restored ID T000004R000002

So I guess the question is, why would Taffy create new "___id"s when there are values already in the saved file? I was using them for table-to-table references, in effect a join.

Since Taffy supports join semantics, I figure it would want to support them on that field.

My intent is to have a unique ID for each record, and figured I'd found it there. If it's not supposed to be used by application code, (A) what is it for, and (B) what's a reasonable workaround? I'd just as soon not search my records for the maximum ID, add one, and set that for a new record.

Thanks!

acontia commented 4 years ago

Is there any solution or workaround for this? I'm having the same issue for the same use case.

I'm thinking about creating and maintaining my own id attributes and base my logic and joins based on that... unless there is a better solution?

Dakine135 commented 3 years ago

I also have this issue, but when trying to maintain a sync across browser and node backend. When updating, i have to use my own id, as the browser creates new ones and doesn't care that i gave it some already to match backend.

typicaljoe commented 3 years ago

This is intended behavior. TaffyDB assumes you are working working with records likely flowing from another data layer than have attached IDs (or you are creating unique identifiers as you push data into it). The ___id value is internal and used to speed up lookups, manage updates to records, etc.