typicaljoe / taffydb

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

is it possible to insert new data in a specific row? #133

Open jorgemejia opened 7 years ago

jorgemejia commented 7 years ago

I want to insert new data in a specific row

Something like this:

db({id:key}).get()[0].insert(newInfo)

or

db({id:key}).insert(newInfo)

csterritt commented 7 years ago

You can always add an 'order' field, and use it for sorting when you want data in order.

You might start with assigning 1, 2, 3, 4... and then realize that if you want to do an insert somewhere, you have to renumber everything above where you're inserting.

JavaScript numbers have enough precision, however, that you can step by something large (say, 10,000) in assigning ordering, and then just pick a number halfway between the two you want to insert between, so for example you'd insert order 25,000 between 20,000 and 30,000, and later you could insert 22,500 between 20,000 and 25,000, and so on.

Of course, you'll have to watch out for the (hopefully unusual!) case of having every integer between 'n' and 'm' assigned, and then recognize that and perhaps renumber everything. But it'd be a rare case.

Remember that JavaScript only has Number, which is a 64-bit floating point value. It's up to you if you want to go into non-integer values. Math.floor is your friend :-)