0.2.0 • Published 6 years ago

publicio v0.2.0

Weekly downloads
5
License
ISC
Repository
github
Last release
6 years ago

publicio

Publicio is the interface to database with subscriptions.

NPM version Build Status Dependency Status Coverage percentage experimental

Publicio supports any database adapters, which provide these methods: find, insert, update, remove.

For subscriptions Publicio uses the special type of query. It contains a restricted set of expressions, like equal, contains or greaterThanOrEqual.

Every subscription has a special parameter - variables, which allow to change the subscription query without to create the new subscription.

How it works

Publicio has classic methods for working with rows of databases: insert, remove, update, find.

For example, when you call some insert, like insert("table1", { field1: "value1" }), Publicio looks for all subscriptions, checking query filter to match with this inserted row data and broadcasts the updates to all matched subscribers.

Install

npm install publicio --save

or

yarn add publicio

Usage

import { DB } from "dabby";
import { MysqlAdapter, Publicio } from "publicio";

const orm = new Publicio({
    db: new MysqlAdapter({
        db: new DB({
            charset: "utf8",
            user: "root",
            password: "123123",
            database: "test",
        }),
    }),
});

(async ()=>{

const subscription = await orm.subscribe({
    query: ({ field1 }) => ({
        modelName: "table1",
        params: {
            filter: {
                type: "startsWith",
                fieldName: "field1",
                value: field1,
            }
        },
    }),
    variables: { field1: "val" },
});
let rows = subscription.get();

subscription.on((value)=>{
    rows = value;
});

await orm.insert("table1", { field1: "value1" });

console.log(rows);

subscription.dispose();

})();

API

Publicio

subscribe(subscriptionInfo: ISubscriptionInfo): Promise<Subscription<any>>;

insert: (modelName: string, row: any) => Promise<IModelItem>;
update: (modelName: string, row: IModelItem) => Promise<void>;
remove: (modelName: string, id: ModelId) => Promise<void>;
find: (modelName: string, filter?: FilterExpression) => Promise<IModelItem[]>;

registerHook: (hook: HookType, modelName: string, cb: (row: any) => void) => void;
unregisterHook: (hook: HookType, modelName: string, cb: (row: any) => void) => void;    

interface ISubscriptionInfo {
    query: (vars: any) => IQuerySet;
    variables: any;
}    

Subscription

Subscription extends Onemitter
    dispose(): void;
    setVariables(vars: any): void;

Types

enum HookType {
    AfterInsert = "AfterInsert",
    AfterUpdate = "AfterUpdate",
    AfterRemove = "AfterRemove",
}

export interface IQuerySet {
    modelName: string;
    count?: number;
    params?: IFindParams;
    fields?: Array<IQuerySet | string>;
}
export interface IFindParams {
    filter?: FilterExpression;
    sort?: IFindParamsSort[];
    offset?: number;
    limit?: number;
}
export interface IFindParamsSort {
    type: "asc" | "desc";
    fieldName: string;
}
export type FilterExpression = IAndFilterExpression |
    IOrFilterExpression | IEqualFilterExpression | IContainsFilterExpression
    | IStartsWithFilterExpression | IEndsWithFilterExpression |
    IGreaterThanFilterExpression | IGreaterThanOrEqualFilterExpression |
    ILessThanFilterExpression | ILessThanOrEqualFilterExpression
    ;

export interface IAndFilterExpression {
    type: "and";
    expressions: FilterExpression[];
}
export interface IOrFilterExpression {
    type: "or";
    expressions: FilterExpression[];
}
export interface IEqualFilterExpression {
    type: "equal";
    fieldName: string;
    value: any;
}
export interface IContainsFilterExpression {
    type: "contains";
    fieldName: string;
    value: string;
}
export interface IStartsWithFilterExpression {
    type: "startsWith";
    fieldName: string;
    value: string;
}
export interface IEndsWithFilterExpression {
    type: "endsWith";
    fieldName: string;
    value: string;
}
export interface IGreaterThanFilterExpression {
    type: "greaterThan";
    fieldName: string;
    value: number;
}
export interface IGreaterThanOrEqualFilterExpression {
    type: "greaterThanOrEqual";
    fieldName: string;
    value: number;
}
export interface ILessThanFilterExpression {
    type: "lessThan";
    fieldName: string;
    value: number;
}
export interface ILessThanOrEqualFilterExpression {
    type: "lessThanOrEqual";
    fieldName: string;
    value: number;
}
export type ModelId = string | number;
export interface IModelItem {
    id: ModelId;
    [index: string]: any;
}
export interface IDBAdapter {
    insert: (modelName: string, row: any) => Promise<IModelItem>;
    update: (modelName: string, row: IModelItem) => Promise<void>;
    remove: (modelName: string, id: ModelId) => Promise<void>;
    find: (modelName: string, params?: IFindParams) => Promise<IModelItem[]>;
}

Test

npm install
npm test
0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.1-init

6 years ago