Question about IndexedDB

1

I need to make a many table for many in IndexedDB, where I have People Turmas and Turma_Pessoas, the person can participate in several groups, and several groups can have several students, there I created this table class person, where I put people code , class code, a flag to identify who the person in that class is. Here is the code for 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 able to save only some part of the class, because the code of the group 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 be a primary key, because in the class search I need it to return all the codes of people who have the class code 'X'.

    
asked by anonymous 11.09.2015 / 13:22

2 answers

2

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

One alternative would be to have the table have the composite PK (COD_IDENT_TURMA and COD_IDENT_PESSO). But in your comment you say that IndexedDB does not support this.

So, one possible solution for you to store information the way you want it 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|

In this way, you will be able to insert multiple student records from multiple classes without any PK violation, since it will be a sequential ID.

Related question in English OS

When is the recommended use of composite primary key?

    
11.09.2015 / 14:29
1

The js looks like this:

    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 I structured to create a key, autoincrement, this generates code in sequence for each record of the class person table, above to get the response of the code of the students of a given class, I created an index, for I created an IDBKeyRange that allows me to create something to be compared, is synonymous with where in MYSql, and lastly I created a cursor, its function is to go through all the result obtained in the specified query.

    
11.09.2015 / 18:23