typicaljoe / taffydb Goto Github PK
View Code? Open in Web Editor NEWTaffyDB - an open source JavaScript Database for your browser
Home Page: http://taffydb.com
License: MIT License
TaffyDB - an open source JavaScript Database for your browser
Home Page: http://taffydb.com
License: MIT License
It crashes when we try to use custom filters (left, right, has, hasAll, etc..), thats uses methods on not existing filter column.
Example:
var cities = TAFFY([
{name:"New York",state:"WA"},
{name:"Las Vegas",state:"NV"},
{name:"Boston",state:"MA"}
]);
cities({columnNotExists:{'left':'Hello'}})
.each(function (r) {
console.log(r)
}
);
Throws error in filter-related method.
Small fix to it:
// push a function onto the filter collection to do the matching
matchFunc = function () {
// get the value from the record
var
mvalue = this[i],
eqeq = '==',
bangeq = '!=',
eqeqeq = '===',
lt = '<',
gt = '>',
lteq = '<=',
gteq = '>=',
bangeqeq = '!==',
r
;
//<<<<<FIX THERE
if (undefined === mvalue) {
return false;
}
//<<<<<END FIX THERE
....
Caching is broken for some cases where the only difference is the content of a regular expression. Example:
{
name: 'James'
types: ['x','y','z']
}
The other thing is a broken caching key with regex:
taffyDB({name:{regex:/James/}}).count() === 1;
taffyDB({name:{regex:/Anything else at this point/}}).count() === 1;
taffyDB({name:{regex:/Will always work/}}).count() === 1;
Because:
JSON.stringify(/Anything/) === JSON.stringify(/Everything/) &&
JSON.stringify(/Everything/) === '{}'
As JSON.stringify is what's used for the cache key this won't work for regex queries.
I have a fix for it in my fork and submitted a pull request.
It would be great if there were a reduce method.
Hi, I was reading the document to check the API.
db.store() says it returns a boolean. But when I checked the source code it return the root where it should return "r".
I think the change might be for the chaining reason. But, is it possible to have a flag to say that localStorage is available?
Thanks.
Hello :)
I am searching for a solution to handling of around 100,000 objects in the browser, and it should operate on IE8 per customer requirements.
You mention robust browser support but no specific information about IE8. So I am posting this issue.
So how well and how fast does it work on IE8?
Thank you.
I wannt toJson string no has _id property ,can I ?
On the "working with data" page on the site, it looks like there is an error...
db().last("balance"); // returns the sum of the "balance" column.
should be be
db().sum("balance"); // returns the sum of the "balance" column.
https://gist.github.com/2601361
took the liberty of creating it myself.
it works, but i never used taffy before so I don't know if that is the best way to do this or not.
would be nice to have this in the lib.
my english is poor, so an example:
var db = TAFFY(
[{col1:"val1",col2:"val21",col3:"val31"},
{col1:"val1",col2:"val22",col3:"val32"},
{col1:"val1",col2:"val23",col3:"val31"},
{col1:"val1",col2:"val24",col3:"val32"}])
alpha = db({col1:"val1"})
//alert(alpha.select("col2"))
beta = alpha.filter({col3:"val31"}).select("col2")
alpha.get()
After alpha.get(), I see (I removed the __id and __s keys):
[{col1:"val1", col2:"val21", col3:"val31"}, {col1:"val1", col2:"val23", col3:"val31"}]
But if I uncommented the alert line, I see what I always expected:
[{col1:"val1", col2:"val21", col3:"val31"}, {col1:"val1", col2:"val22", col3:"val32"}, {col1:"val1", col2:"val23", col3:"val31"}, {col1:"val1", col2:"val24", col3:"val32"}]
Regards, idoric.
Like mentioned on the wiki: https://github.com/typicaljoe/taffydb/wiki/Suggestions-and-New-Feature-Ideas
We could use the .splice()
function or another solution for this?http://stackoverflow.com/a/4538364/753676
Hi. I've been experimenting with adding trigger.io's storage support as on mobiles, it can provide much greater storage capacity whilst falling back to localStorage if required. I've got everything working, however trigger.io's storage APIs are asynchronous, relying on callbacks for reads. What this means is that situations like this happen:
var a = TAFFY();
a.store( "test" );
a.insert( { "a" : 1 } );
var b = TAFFY();
b.store( "test" );
b().count() --> 0
Wrapping the second part in a setTimeout function with a 2000ms delay will make it work but is not ideal. The only real situation this happens is on the initial load so I was thinking what the best way would be to flag that the initial call to .store() had actually populated the internal memory.
Any thoughts?
Thanks.
ExtendScript is Adobe's take on Ecma3 which allows you to write application for CS programs.
TaffyDB runs without errors after implementing the JSON library and changing if ( ! TAFFY ){
into if ( typeof TAFFY !== 'function' ){
However, the implementation doesn't allow me to run queries and filters. Here are the results from the test case:
by_city:
===================
[
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}
]
by_id:
===================
[
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}
]
by_id_f:
===================
[
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}
]
by_name:
===================
[
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}
]
kelly_by_id:
===================
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true}
kelly_last_name:
===================
"Smith"
id_list:
===================
[1,2,3,4]
city_list:
===================
["Seattle, WA","Dallas, TX","Washington, D.C."]
filter_list:
===================
[
{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}
]
Example filter bug - rows changed without using .update() will filter by their original values.
We expect 0 rows, but get 2...
[object Object],[object Object],[object Object],[object Object]
...even though the city has changed in the collection.
[object Object],[object Object],[object Object],[object Object]
Example filter when .update() is used.
now we get the correct response (0 rows) ...
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
... that reflects the taffy collection.
Can anyone tell me what the possible culprit might be or make a suggestion on how I might try to solve this?
Tnx!
compTable().each(function(r){
console.log(r);
html+='
Object {compRelMainId: "2", compMainId: "1", compName: "部件1", compVolume: "28cm x 47cm x29", compWeight: ""…}
Hmm, from what I can tell this is just using local storage?
And no storage when running in node.js?
have you seen https://github.com/rvagg/node-levelup ?
it's a node.js binding to leveldb, and there is also an adapter for
indexedDb https://github.com/maxogden/level.js
So you can run the same db code on the server and on the browser!
maybe levelup could be used as a storage adapter for taffy db?
I don't believe the taffydb behaviour matches the following example from the documentation. onUpdate is always getting called.
db().update({column:"value"},false); // update but do not call onUpdate event
I believe line 670 of version 2.6 should be:
runEvent = arg1;
there are no more downloads https://github.com/typicaljoe/taffydb/downloads
the feature is also deprecated on Github
Where I can get the ID of the record generated by an insert operation?
The docs does not specify this.
Regards.
Currently taffydb doesn't order properly characters with accents:
var db = TAFFY([{name:"a"},{name:"â"},{name:"Â"},{name:"A"},{name:"Z"},{name:"z"}]);
db().order("name logical").select("name")
// ["a", "A", "Z", "z", "â", "Â"]
// correct order in most locales: ["a", "â", "Â", "A", "Z", "z"]
That result could be achieved by use of the String#localeCompare javascript function:
"A".localeCompare("â", "generic", {sensitivity:"base"})
// 0
The locale parameter could maybe be specified as an option to .order() and .sort().
Does TaffyDB have any abstractions for web worker support? Chrome currently allows async access to indexdb, but you would have to create a postmessage hack for everything else.
I don't think "turtoial" is spelled that way ;)
Hello,
I am currently planning on integrating TaffyDB into a framework I'm trying to build in ExtendScript, an extension of Ecma 262 edition 3 for the Adobe CS programs. In order to get TaffyDB to work with ExtendScript, I had to change the following line:
if ( ! TAFFY ){
into this:
if ( typeof TAFFY !== 'function' ){
because it kept throwing a reference error. (TAFFY is undefined. Line: 42).
Even though some libraries like the JSON library by Douglas Crockford for instance use this exact same technique, it says on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined that this technique is to be avoided:
"However, this kind of technique should be avoided. JavaScript is a statically scoped language, so knowing if a variable is defined can be read by seeing whether it is defined in an enclosing context. The only exception is the global scope, but the global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object (using the in operator, for instance)"
I'm not entirely sure why this technique is to be avoided, but I was hoping maybe some one could explain this to me why TAFFY uses if(! TAFFY) in stead of if( typeof TAFFY !== 'function').
Plus, I wanted to share this so that other developers facing the same issue would know where to look. I'm really glad I finally got it working, because TaffyDB is definitely going to save me a lot of dev-time ;)
Cheers,
p.
Ill initiate a pull when I resolve my github connection. Thanks.
//line 1138
settings = {
...
autoCommit : true,
forcePropertyCase : null,
cacheSize : 100,
name : ''
},
//line 1237
...
if ( settings.storageName && settings.autoCommit ){
setTimeout( function () {
localStorage.setItem( 'taffy_' + settings.storageName,
JSON.stringify( TOb ) );
});
}
//line 1654
...
if ( TOb.length > 0 ){
root.commit();
}
//below root.store
...
root.commit = function ( n, callback ) {
setTimeout( function () {
localStorage.setItem( 'taffy_' + settings.storageName,
JSON.stringify( TOb ) );
if (callback) callback();
});
};
ID array contains the following ids for example: T000004R000002, T000004R000003, T000004R000004...
When updating after a query, id passed to DBI.update is T000005R000002 ... which doesn't exists in the ID array.
This then raises an error:
Uncaught TypeError: Cannot read property 'xxx' of undefined
I believe the problem is not in the update but in the query which changes the ids at some point I guess.
I've got a lot of updates working correctly and I do not see what's different in this case.
https://github.com/DanielRuf/taffydb
Please test and let me know if there are any issues which are not resolved
It's posible that we can made subqueries like the next example?
var newsfeed = TAFFY([
{
"id":1,
"user":"kmart77",
"stars":3,
"telphones":[ { 'tel_id': 1,
"number": 000000},
{ 'tel_id': 2,
"number": 111111}]
])
newsfeed({ telphones: { "tel_id": 1 } })
Hi, I literally fell in love with your framework!!!
I have one suggestion though. when using .merge() is annoying to have to redeclare the 'key' arg to 'id' to set up the event handler to true. I think that it's more common in real cases to set up the event handler than to use a different key for merging than 'id'.
But of course this could be a personal point of view. So one more general solution could be to pass option via an object with defaults {key: 'id', event: 'false'} to be merged with the one passed by the user.
I usually appreciate this solution whenever there are more than 3 args in a function.
What do think about it?
Let us choose for ordering rows, for example by length
Hi, I'm trying to update a field in my db with an array of objects.
The field is updated without problems in the db but the onUpdate event doesn't trigger.
With strings / number data types the job is well done.
This is the command I run:
mydb("TR....").update({"prest_valor_cuota": rPrestValorCuota});
where rPrestValorCuota is :
[{valor: 0, prestatario: "", cuota: 0, total_cuotas: 0}]
Why the onUpdate event does not trigger?
Thanks
I wanted to change a column of strings ["1.2", "2.3",...] to floats so I used:
db().update(function(){ this.floatish = parseFloat(this.floatish); return this; });
But this fails, seemingly because the following check should use strict not equals (https://github.com/typicaljoe/taffydb/blob/master/taffy.js#L1084):
- if (TAFFY.isUndefined(or[i]) || or[i] != v) {
+ if (TAFFY.isUndefined(or[i]) || or[i] !== v) {
Workaround is to put the new value a new column (or patch of course).
Thanks for your work.
Peace,
David S.
line 1772 taffy.js, comare should be compare. just fyi
If data is something like
var friendList = [
{attributes:{is_trusted:"false", trusted_status: "false"},"gender":"M","first":"John","last":"Smith",
"city":"Seattle, WA","status":"Active"},
{"id":2,"gender":"F","first":"Kelly","last":"Ruth",
"city":"Dallas, TX","status":"Active"},
{"id":3,"gender":"M","first":"Jeff","last":"Stevenson",
"city":"Washington, D.C.","status":"Active"},
{"id":4,"gender":"F","first":"Jennifer","last":"Gill",
"city":"Seattle, WA","status":"Active"}
];
var friend_db = TAFFY(friendList);
console.log(friend_db({ attributes : {is_trusted:"false"}}).get()); // Not working
console.log(friend_db({ attributes : {trusted_status:"false"}}).get()); // Works
if you store db using db().store(),
the update method won't work (auto synchronize to localStorage).
The older version works well
How are has and hasAll supposed to work?
In case of
var db = TAFFY([ {"author": "name", "title": "anything", "keywords": ["a", "b"]}, {"author": "name", "title": "anything", "keywords": []} ]);
Both rows are returned for db({keywords: {has: 'a'}})
and for db({keywords: {hasAll: ['a', 'b']}})
. Is this normal? How can I query this array?
If you use sum() dataset and some objects are missing the column in question, NaN is returned. The following fixes the issue:
--- taffy.js (revision 131)
+++ taffy.js (working copy)
@@ -788,7 +788,7 @@
var that = this;
each( arguments, function ( c ) {
each( that.context().results, function ( r ) {
- total = total + r[c];
+ total = total + (r[c] || 0);
});
});
return total;
Hi Ian,
I am using TaffyDB with node.js and first of all: you did a great job with TaffyDB. Very fast and extremely cool.
I found a strange behaviour and I am not sure whether this is a bug or a feature in TaffyDB ;) Or maybe I have a missunderstanding of JavaScript features.
Here is some short code:
//create DB
var db = new TAFFY();
//db.settings({cacheSize:0});
//insert one record:
db.insert({name:'Test',category:'Test'});
//"search" for first record
var firstRecord = db().filter({'name':'Test'}).first();
console.log("Name: " +firstRecord.name); // returns "Name: Test" -> ok
//change the name property of the record to "side effect"
firstRecord.name = "side effect";
//this is immediately written to TaffyDB, why ? I did not say update.
//"search" for the record again:
var secondObject= db({'category':'Test'}).first();
console.log("Name: " +secondObject.name); // returns "Name: side effect" -> not ok?! Should be Test.
My workaround is to clone the returned record into a new javascript object to get rid of the direct connection to Taffy, so this code works fine, but thats kind of a hack.
Thanks in advance for any answer to this issue, greetings from Germany
Gunnar
B.T.W: I hope that TaffyDB is getting more attention by the time nodejs gets really production stable ;-)
Node integration is much easier with an npm package.
It looks like all you need to do is create the package.json
( npm init
), then publish to the repo (npm publish
).
I'm very much a node novice but here's a package.json start:
{
"author": "Ian Smith",
"name": "taffydb",
"description": "TaffyDB is an opensouce library that brings database features into your JavaScript applications.",
"version": "0.0.1",
"homepage": "http://taffydb.com/",
"repository": {
"type": "git",
"url": "git://github.com/typicaljoe/taffydb.git"
},
"engines": {
"node": "~0.4.12"
},
"dependencies": {},
"devDependencies": {},
"main": "taffy"
}
Hello there,
I wonder, is it possible to get the index
of the row object?
Something like this:
var db = TAFFY([.....]);
var row = db("T000008R000003");
var index = db.indexOf(row); // for example 3
thanks
var db = TAFFY([{id: 1, val: "1"}, {id: 2, val: "2"}]);
var results = db().get();
results[0].val = "3";
console.log(db().stringify());
I would have expected the above to print (I removed the ___id and ___s keys):
[{"id":1,"val":"1"},{"id":2,"val":"2"}]
But instead it prints:
[{"id":1,"val":"3"},{"id":2,"val":"2"}]
Should taffydb clone/duplicate the data that is returned to prevent this? Changing line 751 to the following resolves the issue (at what performance hit I don't know):
return $.extend(true, [], this.context().results);
I imagine the same issue will also occur with first(), last() and any other method that returns an object.
$.extend() is a jQuery method: http://api.jquery.com/jQuery.extend/
Hello,
I'd like to perform or
query, like this:
var db = TAFFY([...]);
db( {name: "my name 1"}, {name: "my name 2"});
In this case I'd like to have all records that have "my name 1" and all records that have "my name 2".
Thanks
How is the data persisted? Can this be integrated with other js storage libraries focusing localstorage or other DB options like websql and indexdb?
Hello,
Is there a way to pass another function for sorting data?
I mean: now you are using the standard native function Array.prototype.sort
, but this can cause browser loops (Chrome/Firefox) if there are a lot of data with the same value.
So, can we set another function?
Something like this
var db = TAFFY([.....])
db.settings({ sortFunction: function(data){} });
Thanks
Would like to see a taffy as a node npm module.
Hello, sorry for my english
I have a DB :
var articles = TAFFY([
{title:"Title 1",content:"Content 1",favorite:"yes"},
{title:"Title 2",content:"Content 2",favorite:"no"}
]);
I want to loop results to show like this :
<label> Title 1 </label>
<p> Content 1 </p>
<label> Title 2 </label>
<p> Content 2 </p>
<label> Title 3 </label>
<p> Content 3 </p>
My code :
articles().each(function (r) {document.write("<div><label>" + r.title + "</label><p >" + r.content+ "</p></div>"
)});
I had a need for an onPostUpdate method. I had to add a couple of extra DBI.dm(new Date()); calls because my onPostUpdate methods where calling methods on other taffydb instances (which might then call a method on the first instance) and I didn't want any stale data returned.
--- taffy.js (revision 40)
+++ taffy.js (working copy)
@@ -748,7 +748,7 @@
// * Returns: An array of all matching records
// ****************************************
run.call( this );
return $.extend(true, [], this.context().results);
return this.context().results;
});
API.extend( 'stringify', function () {
@@ -1120,6 +1120,7 @@
template : false,
onInsert : false,
onUpdate : false,
onPostUpdate : false,
onRemove : false,
onDBChange : false,
storageName : false,
@@ -1272,6 +1273,7 @@
TOb.push( v );
ID[v.___id] = TOb.length - 1;
DBI.dm( new Date() );
if ( settings.onInsert &&
(runEvent || TAFFY.isUndefined( runEvent )) )
{
@@ -1330,6 +1332,12 @@
}
TOb[ID[id]] = nr;
DBI.dm( new Date() );
if ( settings.onPostUpdate &&
(runEvent || TAFFY.isUndefined( runEvent )) )
{
settings.onPostUpdate.call( nr, tc );
}
DBI.dm( new Date() );
}
},
remove : function ( id ) {
It appears that when using Mozilla Rhino, the 'setTimeout' function (called at line 1225) does not exist.
It appears that this is a 'built' in function but is not available in Rhino. Therefore, it'd be useful (i'd think) to provide a implementation of setTimeout for use in such environments, or documentation that it does not work with Rhino.
So we have documentation and sourcecode in one place
Hi!
Great work with taffydb. I'm planing to use it for a single page app that will load the whole database on startup (it's not that big).
I'd love to see bower support since I'm using yeoman and with that bower. It seems fairly easy to add a github js-project to bower but it needs to conform to some standards. I think the userbase of taffydb could see an increase if taffy was added to the bower repository. Do a search on bower: http://sindresorhus.com/bower-components/ for database and not many hits will show.
If you think it's a good idea but don't have the time or interest to implement the changes and registering your library with bower please let me know, I'll do my best to produce a pull request or perhaps just register a fork of your repo (which will give me a world of hurt since I'll need to merge your updates).
Regards
Emil
the code : JSON.stringify
IE 7 not support this native method,how can I fix it?
How to remove record??
I do the following :
if(notes.store('notes')){
notes({note_id:$id}).remove();
}
this did not work
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.