0

My question is focusing on the Dexie library, but I guess it could be valid for every json-like structurein javascript, so here it goes:

I have multiple tables created on the local memory by an IndexedDB library (Dexie), which uses the following structure each time I add a record:

    db.transaction('rw', db.table1, db.table2, ... , function() {
      db.table1.add({
        field101: value101,
        field102: value102,
        field103: value103,
        field104: value104,
        field105: value105,
        field106: value106,
        field107: value107,
        field108: value108,
        field109: value109,
        field110: value110,
        field111: value111
      });
      db.table2.add({
        field201: value201,
        field202: value202,
        field203: value203,
        field204: value204,
        field205: value205
      });
      .
      .
      .
    });

When I want to modify the data, the code looks like that:

    db.transaction('rw', db.table1, db.table2, ... , function() {
      bbdd.table1.where("value101").equals(x).modify({
        field102: value102,
        field103: value103,
        field104: value104,
        field105: value105,
        field106: value106,
        field107: value107,
        field108: value108,
        field109: value109,
        field110: value110,
        field111: value111
      });
      bbdd.table2.where("value201").equals(x).modify({
        field202: value202,
        field203: value203,
        field204: value204,
        field205: value205
      });
      .
      .
      .
    });

I would like to reduce the code to use only once each of these structures. If there were only 2 tables it would be ok, but there are around a dozen and it's taking a lot of space (and having to change twice when something changes).

Since the codes are more or less the same, I thought of adding a variable which will include all these fields inside the add or modify brackets, but I'm not sure how to do that. Also, in the modify code the first value of each table is not included (it is used in the modify condition).

I also though about a javascript function, but again - I'm not sure how to do it in this case.

Any suggestions?

Thanks a lot

G. Weisz
  • 37
  • 1
  • 7

0 Answers0