Skip to content
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

Open
michael2043 opened this issue Jun 21, 2016 · 4 comments
Open

Problem with opening exsiting database #273

michael2043 opened this issue Jun 21, 2016 · 4 comments
Labels

Comments

@michael2043
Copy link

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.

@dfahlander
Copy link
Collaborator

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.

@dfahlander
Copy link
Collaborator

dfahlander commented Jun 21, 2016

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:

  1. Open with version and schema.
  2. Open without version or schema.

Open with version and schema

This 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 schema

Dexie 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.
});

@dfahlander dfahlander added the Q&A label Jun 21, 2016
@michael2043
Copy link
Author

"Open with version and schema" means every time the page reload ,the "new Dexie()","db.stores()","db.open()" will be fired.

@dfahlander
Copy link
Collaborator

dfahlander commented Jun 22, 2016

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 db.yourTable can be used as a shortcut for db.table('yourTable').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants