Using Dexie I'm trying to add a new entry but I can only get it to add if I specify the id number. I want the id to be auto incremented as its the primary key, how do I achieve this?
This works
var db = new Dexie("user-database");
db.version(1).stores({
users: "id++"
});
db.open();
var insert_data = {id:5,name: "Paul", email: "test@test.com", user_group: 2};
db.users.add(insert_data);
But if I take out the id from the insert object it doesn't work. See below
This doesn't work
var db = new Dexie("user-database");
db.version(1).stores({
users: "id++"
});
db.open();
var insert_data = {name: "Paul", email: "test@test.com", user_group: 2};
db.users.add(insert_data);</code>
Any ideas?
id
will be appended check it.
html:
Name:<input type="text" name="name" id="name"/><br/>
Email<input type="text" name="email" id="email"/></br>
<button>add new</button>
javascript:
// Declare db instance
var db = new Dexie("test-database");
// Define Database Schema
db.version(1).stores({
users: "++id, name, email"
});
// Open Database
db.open();
function add_new(name,email){
// Interact With Database
db.transaction('rw', db.users, function () {
// Let's add some data to db:
insert_object = {name:name,email:email};
db.users.add(insert_object);
}).catch(function(err) {
console.error(err.stack || err);
});
}
add_new("susheel singh","susheel61@gmail.com");//default content
document.querySelector("button").addEventListener("click",function(e){
add_new(document.getElementById("name").value,document.getElementById("email").value);
});