Forum: Rails Spinoffs (closed, excessive spam) building a many-to-many structure in javascript

D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-05-23 00:40
(Received via mailing list)
hi,

for some purpose, I need a "in-javascript database", maybe built with
some Prototype $H function calls for creating "tables"
but what about junction tables, I mean those which express many-to-
many relations between two entity tables ?

I though using concatenation of strings with a separator, for example
"key1 key2"
"1234-56"
or "ABC \n 789"
but is it a really good way to do this ?

could Prototype's Hash or Enumerable be useful for that ?

regards,

Hefeust
www.eozine.fr
F1fcf834ddad415f60d29c87cc10d4d4?d=identicon&s=25 kangax (Guest)
on 2008-05-23 03:52
(Received via mailing list)
I guess you could try implementing a table data structure with Hash
instances but its performance will most likely be weak.
It would make sense to try to use DOM built-in API for working with
tabular data - using table element. Such implementation could
encapsulate an actual table and provide a convenient interface on top
of that.

Another option would be to use google gears.
http://code.google.com/apis/gears/

- kangax
D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-05-24 07:44
(Received via mailing list)
than for the reply

ok I'll try Google Gears or others
but my needs are not playing with DOM table but rather store tags/
articles in a way that I could count :
1 - total number of articles per tag
2 - number of articles sharing each given pair of tag

so it can be build with 3 tables :

ARTICLE(id_article, title, summarize)
TAGS(id_tag, text)
and a crossing table :
ARTICLES_TAGS(id_article, id_tag)

as it can be easily writing in ordinary SQL

my implementation with be client-side javascript and have to be
simpliest (and simpler than a complete set of DAO / ActiveRecord
classes...)

have you any more idea ?
Af85e01e19b6caa679621b7b783a598f?d=identicon&s=25 T.J. Crowder (Guest)
on 2008-05-24 08:49
(Received via mailing list)
What kind of numbers are we talking about here?  E.g., numbers of
articles, and numbers of tags?  It's just, #1 is pretty
straightforward, but #2 will require rather a bit more work.

Meanwhile, I haven't looked at it much, but there are things like
TrimQuery:
http://code.google.com/p/trimpath/wiki/TrimQuery

It's an SQL engine subset written entirely in JavaScript and intended
for use in the browser.

A Google search for "+sql +javascript" should reveal other options as
well.

Hope this helps,
--
T.J. Crowder
tj / crowder software / com
D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-05-25 17:13
(Received via mailing list)
this is surely not optimal withit works pretty fine :

var HashCounter = Class.create(Hash, {
  initialize : function( $super ) {
    $super( {} );
  },
  store: function(data) {
    found = this.seek(data);
    if( !found) {
      newid = (this.keys().map(parseInt).max() || 0) + 1;
      this.set(newid, $H( {id : newid, data: data, count: 1} ));
    } else {
      var id = found.get('id');
      var newcount = 1 + found.get('count');
      this.set(id, $H( {id: id, count: newcount, data: data} ));
    }
  },
  seek: function(data) {
    var pair = this.detect( function(entry) {
      return data == entry[1].get('data');
    });
    return pair ? pair[1] : false;
  },
  toString : function() {
    var text = "toString\n";
    this.each( function(pair) {
      text += pair.key + ' => '
        + pair.value.get('data') + ' ('
        + pair.value.get('count')
        + ')\n';
    });
    return text;
  }
});

// -- USAGE
//  var Tags = new HashCounter();
//  Tags.store('abc');
//  Tags.store('abc');
//  Tags.store('def');
//  -- displayed with Firebug console
//  console.log(Tags.toString());

it is the first step for building javascript cloud tags parsing an
Ajax string like
ARTICLE1_TITLE
TAG1, TAG2, TAG3

ARTICLE2_TITLE
TAG4, TAG5,    TAG1

ARTICLE3_TITLE
TAG6, TAG7,    TAG2, TAG3

(a big list...)

the number I attemp to compute is the STRENGTH of the coupling between
all possible pair of tags :
e.g. given a pair of tags, find the number of articles  sharing this
pair
for example
  strength(TAG1, TAG7) = 0
  strength(TAG2, TAG3) = 2 because of ARTICLE1 and ARTICLE3
  strength(TAG1, TAG2) = 1 *
etc, for all pairs
D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-05-28 08:58
(Received via mailing list)
ooops !
it's not as fine as i saidd
i m refactorying the code at the moment
but the objective is the same !
Af85e01e19b6caa679621b7b783a598f?d=identicon&s=25 T.J. Crowder (Guest)
on 2008-05-28 13:34
(Received via mailing list)
Did you look a TrimQuery and rule it out?  If so, I'd be interested in
why (not a dig, seriously) -- was it too big (49k), too difficult to
integrate, not powerful/flexible enough, etc., etc.
--
T.J. Crowder
tj / crowder software / com
D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-06-01 03:34
(Received via mailing list)
Arghh !
this query engine is nice but I'm thinking is too generic and weighty
for my needs
(SQL speaking:)  INSERT, SELECt, JOIN, COUNT are enough
but kangax is right : prototype.Hash is weaker if used as a table

maybe writing a table object encapsulting a array of records (Hashes
or JSON)
a Statement Object and a ResultSet one ?

but keeping it simple ! a complete ORM is too much

a brief description of what I'm doing is available on my site
www.eozine.fr/trigradyn/
for the moment explanation are in french (a english summarize
sooner...) but I hope there are enough drawings/schemas for being
understanded by english readers !

Regards

Hefeust
D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-06-01 03:39
(Received via mailing list)
Note : TriGraDyn stands for 'Trieur Graphique Dynamique' which means
'Dynamic and Graphical Sorter ' (of data, of course)
D410f21a350aeb96a0fa9da39c957d12?d=identicon&s=25 hefeust (Guest)
on 2008-06-09 11:11
(Received via mailing list)
finally, I have made a fallback to ordinary arrays, storing objects
like :

0 => { tag : "lion", count : 10 }
1 => { tag : ''cat', count : 4 }

or for association table :

{ id_article : 0, id_tag : 0}
{ id_article : 0, id_tag : 1}

using array indexes as id's

Prototype is commun with Array#select method which is useful and can
be used as a customisable database SQL SELECT statement

Thanks for your views (advice)

demo is visible at
www.eozine.fr/trigradyn/
links at the bottom of the page
Ab66d85e52ec1b77e5cff85392f37486?d=identicon&s=25 Elias Baixas (kriptonet)
on 2008-06-10 17:10
I would definitely try to use XPath, it has quite a powerful SELECT-like
syntax, and its the fastest solution you can probably achieve (as its
implemented natively within the browser). I think every browser has
XPath support...
creating the XML structures might be slightly more difficult.

For Firefox, dealing with XML from within javascript can be done with
E4X, which eases very much the process.

elias
This topic is locked and can not be replied to.