Giter Club home page Giter Club logo

typed-db's Introduction

typed-db

Build Status

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.

@Entity()
class Employee {
  @Key({ autoIncrement: true })
  id?: number;

  name: string;

  @Index()
  age: number;

  @Index()
  salary: number;
}

const db = new Db('myDb', 1);
db.use(Employee);

const getAllEmployees = () => 
  db.transaction([Employee], 'readonly', 
    tx => tx.for(Employee).openCursor().asList()
  );

getAllEmployees().then(employees => {
  console.log('Transaction has finished successfully.');
  console.log(employees);
})

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
const db = new Db('dbname', 1);
db.use(Foo);

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 of this function will be the result of the transaction, can be async
db.transaction([Foo], 'readwrite', tx => {...});

@Entity

function Entity(): ClassDecorator;

Sets up a class to be used as a collection.

@Key

function Key(options?: KeyOptions): PropertyDecorator;

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.

function Index(): PropertyDecorator;
function Index(options: IDBIndexParameters): PropertyDecorator;
function Index(key: string): PropertyDecorator;
function Index(key: string, options: IDBIndexParameters): PropertyDecorator;

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:

@Entity()
class Foo {
  @Index('compoundIndex')
  age: number;

  @Index('compoundIndex')
  salary: number;
}

Contribution

File an issue or place a PR if you want to :)

typed-db's People

Stargazers

 avatar Kevin avatar Kay Gosho avatar David Gonzalez avatar Jeff West avatar Kris Haamer avatar Donald Pipowitch avatar Alain Galvan avatar Uwe Schaefer avatar

Watchers

James Cloos avatar Benjamin Otto avatar  avatar

Forkers

yu0410aries

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.