typed-db
typed-db
is a small promise-based entity manager for IndexedDB written entirely in TypeScript.
In general it exposes almost the same API as the native IndexedDB factory, but all available methods are wrapped in promises.
Additionally it allows you to define your entities in a more declarative way.
;db.useEmployee; ; getAllEmployees.then
For more query examples see tests/Db.ts
:)
Install
Your environment needs support for the Reflect Metadata API, so install a proper polyfill alongside typed-db
if you haven't already one :)
$ npm install typed-db reflect-metadata --save
or
$ yarn add typed-db reflect-metadata
Additionally you will need a Promise polyfill, Map polyfill and some ES2015 array functions (e.g. find).
babel-polyfill
satisfies all this requirements :)
API
Db
Creates the database object, opens the connection if needed and let you register your entities.
// pass in database name and version;db.useFoo;
If you increase the version number, you have to add migrations for you entites. See the migration guide for an example. (TODO)
Create a transaction:
// pass in an array of entities which are in the same transaction scope// set the mode of the transaction, supported: readwrite | readonly// callback with transaction (tx), return value auf this function will be the result of the transaction, can be asyncdb.transaction, 'readwrite', ;
@Entity
;
Sets up a class to be used as a collection.
@Key
;
Indentifies a property as a primary key of the entity.
You can create compound keys by applying @Key
multiple times.
In this case the optional options
object is omitted.
@Index
Creates an index for the property its applied on.
;;;;
This decorator has multiple overloads, for examle you can create a named
index by applying @Index('myIndex')
or set some options @Index({ unique: true })
.
You can easily create an compound index by using the same name for multiple properties:
Contribution
File an issue or place a PR if you want to :)