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?
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".
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:
...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().This will update existing entry with same name and surname to change from "Wonderful" to "Samsung".