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

ngrx-coloquent

v1.2.28

Published

Angular library mapping objects and their relations to [JSON:API](http://jsonapi.org), with a clean synthax for creating, retrieving, updating and deleting model objects. With objects polymorphism and lazy relation loading. Object Loading are done so that

Downloads

5

Readme

NGRX-COLOQUENT

Angular library mapping objects and their relations to JSON:API, with a clean synthax for creating, retrieving, updating and deleting model objects. With objects polymorphism and lazy relation loading. Object Loading are done so that you will use a NGRX state for entities.

We also added a simple way to manage very simple NGRX State without creating a specific Reducer (Do not over use it also)

Install

$ npm install --save ngrx-coloquent @Herlinus/Coloquent @ngrx/store @ngrx/effects @ngrx/entity

Usage

Importing package

You need to import the module NgrxColoquentGlobalModule. Don not forget to import and initiate a NGRX Store and effects root.

@NgModule({
    imports: [
        StoreModule.forRoot(
            {},
            {
            runtimeChecks: {
                    strictStateImmutability: false,
                    strictActionImmutability: false,
                    strictStateSerializability: false,
                    strictActionSerializability: false
                }
            }
        ),
        EffectsModule.forRoot([]),
        NgrxColoquentGlobalModule
    ]
})
class AppModule {}

Creating An Entity

A simple entity is a class you create that describe your model. It extends the class Model from the library Ngrx-Coloquent.

For example:

import { Model, Entities } from 'ngrx-coloquent';

@Entities.hasReducer()
class Person extends Model {
    protected jsonApiType = 'Person';

    @Attribut('id') __id__: number;
    @Attribut() client_id: number;

    @toManyRelation(() => Address) addresses;
    @toOneRelation(() => Client) client;

    readOnlyAttributes = ['version'];
}

Retrieving objects

To retrive Model, you can use a query builder like the one Coloquent uses.

For example:

Identity.query$().where('client_id', 8009785).get().start();

If you need to change something when from your component or service after the loading is done, you can add some callback for success or failure

For example:

Identity.query$().where('client_id', 88456257).get().onSuccess(() =>  console.log('done')).onError(() => console.error('not done'));

To load One object by id, you can directly start the loading from the model

Identity.find$(id).start();

Saving objects

To save Object you just need to call the method Model.save$.

For example:

const identity = new Identity();
identity.save$().start();

Polymorphism

For polymorphism, you just need to manage some inheritance for all model. And you also need to add some decorators.

@Entities.hasReducer()
class Identity extends Model {
    protected jsonApiType = 'Identity';

    @Attribut('id') __id__: number;
    @Attribut('client') client: string;
}

@Identity.appendPolymorph('Person')
class Person extends Person {
    protected jsonApiType = 'Person';

    @Attribut('name') name: string;
}

Selector to fetch data from the store state

To select objects from the NGRX Store, you need to call the method Model.selectEntity$ and give as an argument a function that filter elements. It returns an observable of all the filtered elements in the store. The state you get on the callback function is a id: entity mapping value.

For example:

Person.selectEntity$(
    (state) => {
        return Object.values(state).filter(
            (entity) => entity.name !== 'Donald'
        );
    }
);

You cann add specific filter from any observable (and other state too) to filter it. You just need to add a list of the observable of the data you need for the filter as second argument.

For example:

const minValueSubject = new BehaviorSubject<number>(0);

Bill.selectEntity$(
    (state, minValue) => {
        return Object.values(state).filter(
            bill => bill.total >= minValue
        );
    },
    [
        minValueSubject.asObservable()
    ]
);

No need to specify that all change from the Observable will change the selector result automatically.

You need to wisely choose which entity model to use to select entities from polymorphic models because a type filter is automatically done so you only get Model that match the actual type.

Lets consider these three models.

@Entities.hasReducer()
class Identity extends Model {
    protected jsonApiType = 'Identity';

    @Attribut() department: string;
}

@Identity.appendPolymorph('Person')
class Person extends Identity {
    protected jsonApiType = 'Person';

    @Attribut() name: string;

}

@Identity.appendPolymorph('Company')
class Company extends Identity {
    protected jsonApiType = 'Company';

    @Attribut() title: string;
}

As you can see Person and Company are both Identity.

If you create a selector from the class Person, you will only get all entities of type Person on your selector, no Company or Identity will be there. It's the same for a selector from the class Company, only Company entities will be there. But if you make a selector from the class Identity, you will get Entities from both Identity type, Person type and Company type.

For example:

// you will get all entities
Identity.selectEntity$(
    (state) => Object.values(state)
);

// you will get all Person
Person.selectEntity$(
    (state) => Object.values(state)
);

NgrxColoquent has also a "loadNext" method that allow you to do a continious load of data and not managing it with pagination. The load next is identified by a key so that when you change key, you get a new loadNext.

For example:

Person.loadNext(
    'test',
    [
        {
            key: 'where',
            value: ['client_id', 500201]
        }
    ]
);