npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-db

v1.0.1-1

Published

Entity manager for IndexedDB written entirely in TypeScript

Downloads

1

Readme

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 auf 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 :)