Skip to content

Commit

Permalink
Updates according to issue #317 (#318)
Browse files Browse the repository at this point in the history
* Removed samples for ES7 and Typescript
* Update according to issue #317
  • Loading branch information
dfahlander authored Sep 21, 2016
1 parent 5f2fd85 commit 0a86b48
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 81 deletions.
80 changes: 2 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,84 +146,8 @@ db.transaction('rw', db.friends, function*() {
```
*NOTE: db.transaction() will treat generator functions (function * ) so that it is possible to use `yield` for consuming promises. [Yield can be used outside transactions as well](https://github.com/dfahlander/Dexie.js/wiki/Simplify-with-yield).*
#### Hello World (ES2016 / ES7)
```js
import Dexie from 'dexie';
let Promise = Dexie.Promise; // KEEP! (*1)

//
// Declare Database
//
var db = new Dexie("FriendDatabase");
db.version(1).stores({ friends: "++id,name,age" });

db.transaction('rw', db.friends, async() => {

// Make sure we have something in DB:
if ((await db.friends.where('name').equals('Josephine').count()) === 0) {
let id = await db.friends.add({name: "Josephine", age: 21});
alert (`Addded friend with id ${id}`);
}

// Query:
let youngFriends = await db.friends.where("age").below(25).toArray();

// Show result:
alert ("My young friends: " + JSON.stringify(youngFriends));

}).catch(e => {
alert(e.stack || e);
});

```
_*1: Makes it safe to use async / await within transactions. ES7 async keyword will take the Promise implementation of the current scope. Dexie.Promise can track transaction scopes, which is not possible with the standard Promise. This declaration needs only to be local to the scope where your async functions reside. If working with different promise implementations in the same module, declare your async functions in a block and put the declaration there `{ let Promise = Dexie.Promise; async function (){...} }` ._
#### Hello World (Typescript)
```js
import Dexie from 'dexie';
let Promise = Dexie.Promise; // KEEP! (See *1 above)

interface IFriend {
id?: number;
name?: string;
age?: number;
}

//
// Declare Database
//
class FriendDatabase extends Dexie {
friends: Dexie.Table<IFriend,number>;

constructor() {
super("FriendDatabase");
this.version(1).stores({
friends: "++id,name,age"
});
}
}

var db = new FriendDatabase();

db.transaction('rw', db.friends, async() => {

// Make sure we have something in DB:
if ((await db.friends.where('name').equals('Josephine').count()) === 0) {
let id = await db.friends.add({name: "Josephine", age: 21});
alert (`Addded friend with id ${id}`);
}

// Query:
let youngFriends = await db.friends.where("age").below(25).toArray();

// Show result:
alert ("My young friends: " + JSON.stringify(youngFriends));

}).catch(e => {
alert(e.stack || e);
});
```
#### async await
ES7 async await can be used with Dexie promises, but not within transactions. The reason is that indexedDB and native Promise does not play in any browser other than chromium based. The TC39 group has tied the new async/await algorithms hard to native Promises. Together this makes it incompatible with IndexedDB as of September 2016. See [Issue 317](https://github.com/dfahlander/Dexie.js/issues/317).
Samples
-------
Expand Down
2 changes: 1 addition & 1 deletion samples/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a sample on how to use Dexie.js with Typescript and babel. The following

* How to subclass Dexie and define tables in a type-safe manner.
* How to create an entity with Dexie.
* How to use async / await with Dexie.
* How to use async / await with Dexie **NOTE! Discouraged since Typescript 2.0. See [this issue](https://github.com/dfahlander/Dexie.js/issues/317)**
* How to create something similar to navigation properties on entities.

## Install
Expand Down
5 changes: 4 additions & 1 deletion samples/typescript/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

// DISCLAIMBER: This sample won't work with Typescript 2.0. Async / await is not encouraged any longer when using
// indexedDB in any library due to the incompability between IndexedDB and native Promise in Firefox, Safari and
// Edge browsers. See https://github.com/dfahlander/Dexie.js/issues/315

import Dexie from 'dexie';
import Console from './console';
import {db,Contact} from './appdb';
Expand Down
6 changes: 5 additions & 1 deletion samples/typescript/src/appdb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import Dexie from 'dexie';
// DISCLAIMBER: This sample won't work with Typescript 2.0. Async / await is not encouraged any longer when using
// indexedDB in any library due to the incompability between IndexedDB and native Promise in Firefox, Safari and
// Edge browsers. See https://github.com/dfahlander/Dexie.js/issues/315

import Dexie from 'dexie';

const Promise = Dexie.Promise; // KEEP! (or loose transaction safety in await calls!)
const all = Promise.all;
Expand Down

0 comments on commit 0a86b48

Please sign in to comment.