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

pine-ql

v0.5.4

Published

PineQL is lightweight GraphQL-like library. It's based on [typed-props](https://npmjs/packages/typed-props) and [response-ql](https://npmjs/packages/response-ql) libraries.

Downloads

37

Readme

PineQL

PineQL is lightweight GraphQL-like library to populate data. It's framework or protocol agnostic and could be used anywhere.

It's based on typed-props and response-ql libraries.

Install

Install via npm:

npm i pine-ql

Usage

Usage example:

const _ = require('lodash');
const mongoist = require('mongoist');

const PineQL = require('pine-ql');
const {Types} = PineQL;

const db = mongoist();

// Create Schema
const types = {
    user: Types.shape({
        name: Type.string.isRequired,
        score: Type.number,
        friends: Type.ref('user', {
            // Get from DB
            get(ctx, users) {
                return db.users.find({
                    friends: {
                        $in: users.map(users, _.property('id')),
                    },
                });
            },
            // Return result. Append friends to users
            set(ctx, users, friends) {
                return users.map((user) => ({
                    ...user,
                    friends: friends.filter(
                        ({id}) => user.friends.includes(id)
                    ),
                }));
            },
        }),
    }),
};

const pql = new PineQL({
    types,
});

// Get some user from DB
db.users.findOne({id: 1})
.then((user) => {
    // Populate data
    return pql.grow(user, 'user', 'name,friends{score}', {});
})
.catch((error) => console.error(error));

Also context could be concrete:

rql.context({userId: 1}).grow(user, 'user', 'name,friends{score}');

API

PineQL

constructor()

({ schema:<type,Shape> }) -> PineQL
  • schema - object. Where key is shape name and shape is a shape.

Shape{}

{ <type,any> }
  • <type,any> - object. Object where key is PineTypes property or method and value is check method arguments converted to internal PineQL's representation. Example:
{
    ref: {type: 'user', get(){/*...*/}, set(){/*...*/}},
    object: true,
    isRequired: true,
}

Shape object represents PineTypes converted to object with internal PineQL rules.

context()

(context:object) -> PineContext

Returns contextified PineQL. Which mean that you should not to pass context each time you call populate(). This is helpful when PineQl is using with WebSockets.

  • context - object. Object with context params.

returns PineContext.

grow()

(target:object, type:string, rql:string, context:object) -> Promise<object,Error>

Populate related data according to schema, put it in target object, filtrate and returns result.

  • target - object. Object which props should be populated.
  • type - string. Target object type name in schema.
  • rql - string. ResponseQL string.
  • context - object. Context data.

returns Promise with updated target as result or raised error.

PineQL.Context | PineContext

constructor()

({ pine:PineQL, context:object }) -> PineContext
  • pine - PineQL. PineQL instance.
  • context - object. Context data.

grow()

(target:object, type:string, rql:string) -> Promise<object,Error>

Populate related data according to schema, put it in target object, filtrate and returns result. Use context from the Context.

  • target - object. Object which props should be populated.
  • type - string. Target object type name in schema.
  • rql - string. ResponseQL string.

returns Promise with updated target as result or raised error.

PineQL.Types | PineTypes

shape()

(shape:object<name,PineTypes>) -> PineTypes

Creates new TypedProps shape.

  • shape - object<name,PineTypes>. PineTypes instance describing shape.

Returns PineTypes.

use()

(type:string) -> PineTypes
  • type - string. Type name from schema.

Returns PineTypes.

ref()

(type:string, opts:RefOptions) -> PineTypes

Creates population reference.

  • type - string. Type name from schema.
  • opts - RefOptions. Type which describes how to get linked data and how to insert it back into objects.

Returns PineTypes.

RefOptions{}

get: (context:object, targets:object[], prop:string) -> Promise,
set: (context:object, targets:object[], values[]) -> object[]

RefOptions contains functions get and set. get is using to retrieve data. and set to append retrieved data to dataset.

  • get - function. Populates data for specified field.
  • set - function. Puts data into objects. Returns updated objects.

default()

(value:*) -> PineTypes

Sets default property value.

  • value - any. Property default value.

Returns PineTypes.

value()

(fn:(context:object, data:object) -> object) -> PineTypes

Defines value converter.

  • fn - function(context:object, data:object). Function which extracts value from populated object accoring to context value.

License

MIT.