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

@brainhubeu/hadron-serialization

v1.0.0

Published

Hadron serialization module

Downloads

56

Readme

Installation

npm install @brainhubeu/hadron-serialization --save

More info about installation

Overview

Serializer allows You to quickly and easy shape and parse data way You want it. You just need to create schema (in json file, or as simple object) and You are ready to go!s

Initializing as Hadron package

Pass package as an argument for hadron bootstrapping function:

// ... importing and initializing other components

hadron(expressApp, [require('@brainhubeu/hadron-serialization')], config);

That way, You should be able to get it from Container like that:

const serializer = container.take('serializer');
serializer.addSchema({
  name: 'User',
  properties: [ ... ],
});

// ...

const data = { ... };
serializer.serialize(data, 'User');
// or
const data = new User();
serializer.serialize(data);

Initializing without Hadron

Just import serializerProvider function from package and pass there Your schemas and parsers.

const serializerProvider = require('@brainhubeu/hadron-serialization');

const serializer = serializerProvider({
  schemas: mySchemas,
  parsers: {
    superParser: (value) => `Super ${value}`,
  },
  // ...
});

Configuration

If You are using hadron application, You just need to add to it's config schemas and set of parsers:

const config = {
  // ...
  serializer: {
    schemas: [ ... ],
    parser: [ ... ],
  }
};

If You are using TypeScript, You can just implement exported interface ISerializerConfig

interface ISerializerConfig {
  parsers?: object;
  schemas?: ISerializationSchema[];
}

Usage

Serializer contain three methods.

serialize(data, groups, schemaName);
  • data - object we want to serialize
  • groups - optional array of access groups, on default []
  • schemaName - name of schema, on default name of passed object
addSchema(schemaObj);
  • schemaObj - schema object we want to add
addParser(parser, name);

Adds parser that can be used in schemas, where:

  • parser is a method
  • name is name under which parser will be available

Schema

Schema is basic structure, that allows You to easily define your desired object. You can provider them as json file. F.e.

{
  "name": "User",
  "properties": [
    { "name": "name", "type": "string" },
    { "name": "address", "type": "string", "groups": ["admin"] },
    {
      "name": "money",
      "type": "number",
      "parsers": ["currency"],
      "groups": ["admin"]
    },
    {
      "name": "friends",
      "type": "array",
      "properties": [
        { "name": "name", "type": "string" },
        { "name": "profession", "type": "string", "groups": ["admin"] },
        { "name": "salary", "type": "number", "parsers": ["currency"] }
      ]
    }
  ]
}

Each schema should contain name, which will be its identifier, and properties which should be an array of fields of defined schema.

All properties that are not defined in schema, will be excluded from the serialized data result.

If You are using TypeScript, You can just implement exported interface ISerializationSchema:

interface ISerializationSchema {
  name: string;
  properties: IProperty[];
}

Property

Each property should contain such fields:

  • name - (required) name of the field
  • type - (required) one of such types:
    • string
    • number
    • bool
    • array
    • object
  • groups - array of strings, that will define accessibility to this field (link). If empty, such field if public and will be returned always.
  • parsers - array of parsers name, that should be run on this field, before it's returned
  • properties - array of properties, that are required in case of type object and array
  • serializedName - name of field after serialization

If You are using TypeScript, You can just implement exported interface IProperty:

interface IProperty {
  name: string;
  type: string;
  serializedName?: string;
  groups?: string[];
  parsers?: string[];
  properties?: IProperty[];
}

Groups

While defining schema, You can add groups parameter to properties. That way, while serializing data, You can specify serialization group.

const schema = {
  name: 'User',
  properties: [
    // ...
    { name: 'firstname', type: 'string' },
    { name: 'lastname', type: 'string', groups: ['friends'] }
  ],
}
serializer.addSchema(schema);

// ...

const data = {
  firstname: 'John',
  lastname: 'Doe',
  id: 481,
};

console.log(serializer.serialize(data, [], 'User');
// { 'firstname': 'John' }

console.log(serializer.serialize(data, ['friends'], 'User');
// { 'firstname': 'John', 'lastname': 'Doe' }