Home > Enterprise >  I want to change my dexie values which are null to empty strings
I want to change my dexie values which are null to empty strings

Time:10-28

I want to change my data-tables values which are null to empty strings.

Sometimes the department value will be empty, in which case it will put null in my data tables.

db = db_open();

db.fuel.toArray().then(fuel => {
  fuel.forEach(function(fuel) { 
    $('#table_bod').append('<tr> <td>' fuel.department '</td> </tr> ');
  })
});

I tried:

const fuels = db.fuel.where({department: null});

But that doesn't seem to work.

CodePudding user response:

IndexedDB cannot match against null so in order to find those you must do a full table scan.

const fuelsWithoutDepartment = await db.fuel
  .filter(x => x.department === null)
  .toArray();

Create an upgrader that modifies all null departments to empty string in case you want to have that instead of null.

db.version(2).stores({
  fuel: 'id, department'
}).upgrade (tx => {
  return tx.fuel.filter(
    x => x.department === null
  ).modify({department: ""});
});

Once you've declared such an upgrader (please adjust it to the appropriate version, id and indexes for your case), then you won't need a full table scan to find fuels without departments anymore but can use the department index (if you have one):

const fuelsWithoutDepartment = await db.fuel
  .where({department: ""})
  .toArray();
  • Related