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

grimlock

v2.0.0

Published

Grimlock is a relational data transformer for JavaScript

Downloads

1

Readme

Grimlock

Grimlock is a relational data transformer for JavaScript/TypeScript. It can be used to format your data according to your collection schemas. Grimlock is mostly for back-end usage and provides consistency with the response data of the API.

Installing

Using npm:

$ npm install grimlock

Using yarn:

$ yarn add grimlock

Using pnpm:

$ pnpm add grimlock

Basic Example

import Grimlock from 'grimlock'

const simpleCollection = {
  schema: data => ({
    id: data.id,
    userId: data.userId,
    title: data.title,
    body: data.body
  }),
}

// Object transformation
const single = new Grimlock(post, simpleCollection).toObject()

// Array<Object> transformation
const multiple = new Grimlock(posts, simpleCollection).toArray()

Features

Key Value Manipulation

The output value and the input value do not have to be the same. In this example input data has the name and surname but output data has the full_name.

const userCollection = {
  schema: data => ({
    id: data.id,
    full_name: `${data.name} ${data.surname}`,
    age: data.age,
  }),
}

const data = new Grimlock(user, userCollection).toObject()

| Input Data | Output Data | |----------------------------------------------------|--------------------------------| | - id- name- surname- age | - id- full_name- age |

Optional Values and With

If you need optional properties in your collection, you should add your keys of optional properties to optionals variable in your extended Grimlock class. Now, Optional properties will not be included in transformed response data.

If you want to include this optional data later, you can use with method. with method can be use with single or multiple property.

const postCollection = {
  optionals: ['title', 'body'],
  schema: data => ({
    id: data.id,
    userId: data.userId,
    title: data.title,
    body: data.body,
  }),
}

const data = new Grimlock(post, postCollection).with('title').toObject()

| Input Data | Output Data | |-------------------------------------------------------|------------------------------------------------| | - id- userId- title- body- rating | - id- userId- title |

In that case, title and body are optional but title has been added into the with method. So only title will be in output data.

Note: An array can be sent to with method for add more than one optional value.

const data = new Grimlock(post, postCollection).with(['title', 'body']).toObject()

Without

Sometimes you may not need some properties. without method excludes some properties from output data during translation. These properties do not have to be optional, available for all properties.

const postCollection = {
  optionals: ['title', 'body'],
  schema: data => ({
    id: data.id,
    userId: data.userId,
    title: data.title,
    body: data.body,
  }),
}

const data = new Grimlock(post, postCollection).without('userId').toObject()

| Input Data | Output Data | |-------------------------------------------------------|-----------------| | - id- userId- title- body- rating | - id |

Note: An array can be sent to without method for add more than one optional value like with.

const data = new Grimlock(post, postCollection).without(['id', 'userId']).toObject()

Note: You can use with and without at the same time.

const data = new Grimlock(post, postCollection).with(['title', 'body']).without(['id', 'userId']).toObject()

Function Values

If a value is a function in the collection schema, Grimlock run this function and use returned value. Async functions and promises aren't support yet.

const userCollection = {
  schema: data => ({
    id: data.id,
    name: data.name,
    surname: data.surname,
    age: function () {
      return new Date().getFullYear() - data.birthday
    },
  }),
}

Relations

In the real world, we have a lot of resource/model and these models are related to each other. Grimlock supports the related collections.

import Grimlock from "grimlock";

const userCollection = {
  schema: data => ({
    id: data.id,
    name: data.name,
    username: data.username,
  }),
}

const postCollection = {
  schema: data => ({
    id: data.id,
    title: data.title,
    body: data.body,
    user: new Grimlock(data.user, userCollection).toObject(),
  }),
}

Note: Not only toObject but also toArray can be used in the collection.

API

Properties of Collection object:

| Property | Type | Description | Required | Default Value | |-----------|-----------------|--------------------------------------------------------------------------------------|----------|---------------------| | optionals | Array<String> | Values that are not included in the output by default but can be optionally included | False | [] | | schema | Function | Is the schema that will generate the output. It must return an object. | True | (data) => {} |

Methods of Grimlock class:

| Property | Params | Description | |----------|---------------------------|-------------------------------------------------------------------------| | with | String or Array<String> | It allows to include properties in the output from optional properties. | | without | String or Array<String> | Deletes the values you don't want in the output. | | toObject | - | Returns a single result for a collection. | | toArray | - | Returns multiple results in an array for a collection. |

License

Apache-2.0 license