Dexie.js: How can I update the existing data if two or three keys in my new data are same as the existing one?

Created on 5 Mar 2019  路  1Comment  路  Source: dfahlander/Dexie.js

Let's suppose I have this piece of code.
data = [{ name: 'Rahul', surname: 'Punase', mobile: 'Redmi' }, { name: 'Prakhar', surname: 'Kumar', mobile: 'Wonderful' }, { name: 'Sanjeev', surname: 'Gour', mobile: 'Omni' } ];

And I already have this in db.
Now I am inserting another row.
{ name: 'Prakhar', surname: 'Kumar', mobile: 'Samsung' }
Since name: Prakhar and surname: Kumar already exist, the mobile key's value must get update with the new value 'Samsung'.

How can I create something like this?

question

Most helpful comment

You need to decide which property is the primary key. A primary key is typically separate from the other properties but could also be a compound value of some or all properties.

If you define your schema like this:

const db = new Dexie('yourDB');
db.version(1).stores({
  people: `
    [name+surname],
    mobile,
    ...other indexed props
  `
});

...then your primary key will be the compound value of the three props name and surname.
In that case, you can update an entry using put().

await db.people.put({ name: 'Prakhar', surname: 'Kumar', mobile: 'Samsung' });

This will update existing entry with same name and surname to change from "Wonderful" to "Samsung".

>All comments

You need to decide which property is the primary key. A primary key is typically separate from the other properties but could also be a compound value of some or all properties.

If you define your schema like this:

const db = new Dexie('yourDB');
db.version(1).stores({
  people: `
    [name+surname],
    mobile,
    ...other indexed props
  `
});

...then your primary key will be the compound value of the three props name and surname.
In that case, you can update an entry using put().

await db.people.put({ name: 'Prakhar', surname: 'Kumar', mobile: 'Samsung' });

This will update existing entry with same name and surname to change from "Wonderful" to "Samsung".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SlIdE42 picture SlIdE42  路  11Comments

bennycode picture bennycode  路  24Comments

abieri picture abieri  路  15Comments

ValeriyPogosyan picture ValeriyPogosyan  路  21Comments

nicolashenry picture nicolashenry  路  29Comments