-
-
Notifications
You must be signed in to change notification settings - Fork 650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with opening exsiting database #273
Comments
The best thing is of you define your DB in a common file, for example db.js and then include (or require) that js file from both a.html and b.html. |
Since I get this question quite often, I think it deserves an explanation to use somewhere in the wiki as well. There are two modes that you can open a database in with Dexie:
Open with version and schemaThis is the most common use case, as well as the preferred one. You have a JS module where you define your database schema. You include that common js file from all your apps / html pages that should use that database. The first HTML page that run will create the database. The second time db is run, it will be used and the schema will serve as an API populator and set the direct table methods onto your db. db.js var Dexie = require('dexie'); // (if using modules. Otherwise skip this line)
var db = new Dexie('dbname');
db.version(1).stores({
// schema goes here
});
export = db; // If using modules. Otherwise skip this line. app1: var db = require('./db'); // (if using modules)
// Use db here app2: var db = require('./db'); // (if using modules)
// Use db here too. Open without version or schemaDexie will read the installed database and provide an API to use it, but you wont get the implicit table properties directly at your db instance. To use a table, you must go via db.table('tableName').method(). The downside with this way is that you make your code dependent on something that you cannot control. What happens if the user clears it's database? Your code will stop working wherever you expect a table to exists. You would need to add lots of error checkings if so. new Dexie('dbname').open().then(function(db) {
var tbl03 = db.table('tbl03');
tbl03.doSomething()...
}).catch ('InvalidTable', function (err) {
// This error may happen if your user has cleared out the database.
}); |
"Open with version and schema" means every time the page reload ,the "new Dexie()","db.stores()","db.open()" will be fired. |
Yes, but the database will just be created if needed, upgraded if needed or just opened as is, if the database was already installed in the browser with the declared version. The stores() method also serves as as API declaration - it makes sure that |
I worked with the code below:
a.html
var db = new Dexie("idb01");
db.version(1).stores({ tbl03: "[id1+id2],name,age" });
db.open();
.......
other operation...
........
when I use 'document.location.href="b.html"' and in b.html:
var db = new Dexie("idb01");
db.open();
db.tbl03.xxxx(other operations...)
"db.tbl03" is undefined
how can I use exsiting database in another page.
The text was updated successfully, but these errors were encountered: