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

Updated samples to reflect #317 + bower.json ignore (#319) #321

Merged
merged 4 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 7 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
"**/.*",
"**/tmp/",
"addons/*/bower.json",
"addons/*/package.json",
"addons/*/test/",
"addons/*/tools/",
"samples/",
"*.njsproj"
"*.njsproj",
"src",
"test",
"tools"
],
"version": "1.4.2"
}
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
5 changes: 4 additions & 1 deletion src/Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ props (Promise, {
},

resolve: value => {
if (value && typeof value.then === 'function') return value;
if (value instanceof Promise) return value;
if (value && typeof value.then === 'function') return new Promise((resolve, reject)=>{
value.then(resolve, reject);
});
return new Promise(INTERNAL, true, value);
},

Expand Down