Doubt about Indexeddb

Asked

Viewed 224 times

1

I’m needing to do a many-to-many table on Indexeddb, where I have People Classes and People Classes, one can participate in several classes, and several classes can have several students, so I created this class table person, where I put people code, class code, a flag to identify who’s in that class. Follow the code of my table.

    var pessoa turma = db.createObjectStore("tbl_PESSOA_TURMA", {keyPath: "COD_IDENT_TURMA"});

pessoa_turma .createIndex("COD_IDENT_PESSO", "COD_IDENT_PESSO", {unique: false});
pessoa_turma .createIndex("COD_IDENT_CELUL", "COD_IDENT_TURMA", {unique: false});
pessoa_turma .createIndex("FLG_IDENT_PESSO", "FLG_IDENT_PESSO", {unique: false});
pessoa_turma .createIndex("COD_IDULT_ATUAL", "COD_IDULT_ATUAL", {unique: false});
pessoa_turma .createIndex("DAT_ULTIM_ATUAL", "DAT_ULTIM_ATUAL", {unique: false});

With this code I am only able to save some part of the class, because the class code is suffering a "group by", for example I have 16 people, and it appears only 3 classes, because these 16 people are in 3 different classes.

How do I accept the registration of more than one code ? I need the class code to look like a primary key, because in class research I need it to return to me all the codes of people who have class 'X code'.

  • If this table is turma_pessoas, PK can’t just be the class code. You said yourself that this table is from an N-M relationship, that is, the PK should be COD_IDENT_TURMA and COD_IDENT_PESSO.

  • However Indexeddb does not allow this.

2 answers

2


In your case, the problem is that with PK is the COD_IDENT_TURMA field, it does not accept two records with the same key.

An alternative would be to make the table have the composite PK (COD_IDENT_TURMA and COD_IDENT_PESSO). But in his commenting you say that the IndexedDB can’t stand it.

So, a possible solution for you to store the information the way you want, is to create a new column (COD_PESSOA_TURMA) to be your PK.

|tbl_PESSOA_TURMA|
+----------------+
|COD_PESSOA_TURMA| (PK)
|COD_IDENT_TURMA |
|COD_IDENT_PESSOA|

This way, you will be able to enter records of multiple students from multiple classes without PK violation, since it will be a sequential ID.


Related question in the English OS

When the use of composite primary key is recommended?

  • I didn’t do it this way, and succeed. But this way will also work.

  • Then post the way you did. It may be that in the future it will help more people who come to have the same doubt.

1

The js stayed this way:

    bd =    db.transaction("tbl_PESSOA_CELULA").objectStore("tbl_PESSOA_CELULA"),
    singleKeyRange = IDBKeyRange.only(w_codigo_turma);

index = bd.index("COD_IDENT_TURMA");
index.openCursor(singleKeyRange).onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {           
       console.log(cursor.value.COD_IDENT_PESSO);
       cursor.continue();
    }

And the database I structured like this:

var pessoa_turma= db.createObjectStore("tbl_PESSOA_TURMA", { autoIncrement : true });

pessoa_turma.createIndex("COD_IDENT_PESSO", "COD_IDENT_PESSO", {unique: false});
pessoa_turma.createIndex("COD_IDENT_CELUL", "COD_IDENT_TURMA", {unique: false});
pessoa_turma.createIndex("FLG_IDENT_PESSO", "FLG_IDENT_PESSO", {unique: false});
pessoa_turma.createIndex("COD_IDULT_ATUAL", "COD_IDULT_ATUAL", {unique: false});
pessoa_turma.createIndex("DAT_ULTIM_ATUAL", "DAT_ULTIM_ATUAL", {unique: false});

In my database structured in order to create a key, autoincrement, this generates code in sequence for each record of the class person table, above to get the answer of the code of the students of a certain class, I created an index, to inform which field I want from the table, created an Idbkeyrange that allows me to create something to be compared, is the synonym of where in Mysql, and last I created a cursor, its function is to go through all the result obtained in the specified query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.