Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

gracenode-staticdata

0.2.3 • Public • Published

Staticdata Module

Staticdata module for gracenode framework.

This is designed to function within gracenode framework.

How to include it in my project

To add this package as your gracenode module, add the following to your package.json:

"dependencies": {
        "gracenode": "",
        "gracenode-staticdata": ""
}

To use this module in your application, add the following to your gracenode bootstrap code:

var gracenode = require('gracenode');
// this tells gracenode to load the module
gracenode.use('gracenode-staticdata');

To access the module:

// the prefix gracenode- will be removed automatically
gracenode.staticdata

Access

gracenode.staticdata

Configurations

// staticdata module supports CSV and JSON format
{
    "modules": {
        "staticdata": {
            "path": "directory path to the static files",
            "delimiter": optional and defaults to ',', // for parsing CSV files
            "index": { // optional // for getOneByIndex and getManyByIndex
                "staticFileName": ["indexName", [...]]
            },
            "maxOpenFiles": <int> // optional. the number of opened and read files at once on set up. default is 100
        }
    }
}

CSV

Supported CSV Delimiters

gracenode-staticdata module supports the following delimiters in CSV file format:

  • Comma (,)
  • Tab (\t)
  • Semicolon (;)
  • Caret (^)
  • Vertical bar (|)

Escaping Characters

If your CSV data contains the same characters as the deimiter, the characters must be escaped with a backslash ().

Example (delimiter is ,):

"id","name","age"
100,"Marley, Bob",33
101,"Harper, Ben",45

API: csvToObj

Parses CSV data into an object.

Object csvToObj(String csvData)
API: create
StaticData create(String dataName)

Returns and instance of StaticData class

Example:

/* 
In order to create a static data object from a static data file called "example.csv",
do the following:
*/
var example = gracenode.staticdata.create('example');
StaticData class
.inflate()
void inflate(Object staticdata, String parentKey, String childKey)

Combines 2 staticdata objects on parentKey and childKey.

parentKey is the column from source staticdata and childKey is the column from the staticdata given to the function.

NOTE: The child staticdata MUST be indexed by childKey.

Example:

// CSV data of men.csv
name,wife
Bob,1
Kevin,2
Nathan,3
/*
[
    { "name": "Bob", "wife": 1 },
    { "name": "Kevin", "wife": 2 },
    { "name": "Nathan", "wife": 3 }
]
*/
// CSV data of women.csv
id,name
1,Sandy
2,Olivia
3,Jess
/*
[
    { "id": 1, "name": "Sandy" }
    { "id": 2, "name": "Olivia" }
    { "id": 3, "name": "Jess" }
]
*/
// inflate the 2 files
var men = gracenode.staticdata.create('men');
var women = gracenode.staticdata.create('women');
men.inflate(women, 'wife', 'id');
/*
Resulting data
[
    {
        "name": "Bob",
        "wife": {
            "id": 1,
            "Sandy"
        }
    },
    {
        "name": "Kevin",
        "wife": {
            "id": 2,
            "Olivia"
        }
    },
    {
        "name": "Nathan",
        "wife": {
            "id": 3,
            "Jess"
        }
    }
]
*/
.getOneByIndex()
mixed getOneByIndex(String indexName, String indexKey, Array props)
.getManyByIndex()
mixed getManyByIndex(String indexName, Array indexKeyList, Array props)
.getOne()
mixed getOne(mixed key, Array props)
.getMany()
mixed getMany(Array keyList, Array props)
.getAll()
mixed getAll(Array props)
.getAllByIndexName()
mixed getAllByIndexName(String indexName, Array props)

All get methods accepts an array of property names as an option to retrieve ONLY the given properties

Install

npm i gracenode-staticdata

DownloadsWeekly Downloads

32

Version

0.2.3

License

MIT

Last publish

Collaborators

  • avatar