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

publicio

v0.2.0

Published

SQL ORM with subscriptions

Downloads

19

Readme

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