Git Product home page Git Product logo

data-repositories's Introduction

@subshell/data-repositories

CircleCI npm version

This is a wrapper around Dexie, which itself is already a wrapper around IndexedDB. This wrapper allows to create repository classes, similar as you might be used to from Java and Spring Data.

Short example of a repository:

/*********************************************************
* Define a class you want to store and a repository class:
*********************************************************/


class Person {    
    @Id() name: string;
    @Unique() identificationNumber: number;
    @Indexed() email: string;
    
    constructor(name: string, identificationNumber: number, email: string) {
        this.name = name;
        this.identificationNumber = identificationNumber;
        this.email = email;
    }
}

class PersonRepository extends AbstractRepository<Person, string> {
    constructor() {
        super(new DatabaseAccess(PersonRepository.name), Person);
    }
}

/*****************************
* Somewhere else in your code:
*****************************/

const personRepository = new PersonRepository();
const gandalf = new Person("Gandalf", 42, "[email protected]");

// Save the object
personRepository.save(gandalf).subscribe();

// Find the object
personRepository.findById("Gandalf").subscribe(foundGandalf => {
    // do something with it here
});

You can define a field to be the primary key of the repository by adding @Id() to it. If you want a number that is incremental you can use @IncrementalId(). If you want multiple properties to be a compound primary key you can add multiple @CompoundId() decorators to those fields. If you are using @IncrementalId() or @CompoundId() you need to add an additional generic parameter to the repository, e.g.:

In any case you must use one of the @Id(), @CompoundId() or @IncrementalId() decorators in your repository.

/*****************************
* Compound ID:
*****************************/

class Person {
    @CompoundId() firstName: string;
    @CompoundId() lastName: string;
    // ...
}
class PersonRepository extends AbstractRepository<Person, [string, string]> {
    // ...
}

/*****************************
* Incremental ID:
*****************************/
class Person {
    @IncrementalId() identificationNumber: number;
    name: string;
    // ...
}
class PersonRepository extends AbstractRepository<Person, number> {
    // ...
}

To make keys unique you can use @Unique() and to have indexed fields (you can only use those in search queries) you need to use @Indexed(). @CompoundId() fields are not automatically indexed separately, so if you want to be able to query these you need to additionally add the @Indexed() decorator to them.

If you should at a later point upgrade your data model and e.g. add a unique or indexed field, you need to specify the respective database version in the decorator, e.g. @Unique({sinceVersion: 2}). Versions start with 1 so if you update your model it is definitely at least version 2.

Methods you can use on repositories are: save, saveAll, findById, findAll, delete, clear, count.

Additionally, the AbstractRepository and AbstractMappingRepository allow searching using simple property equality checks and predicates with the .search() method. This functionality is very basic and far from what Dexie actually offers. This is currently a low priority TODO but feel free to contribute.

It is recommended to have separate Dexie databases for every repository (due to some issues with Dexie and the versioning across multiple repositories). Use the DatabaseAccess class with care and try not to create those classes with duplicate names unless you know what you do.

The repositories naturally work with RxJS and Observables. If you want to use JavaScript Promises instead you can just call .toPromise() on every returned Observable. Due to the nature of IndexedDB there is no synchronous way to read or write any data.

data-repositories's People

Contributors

dependabot[bot] avatar philmtd avatar

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.