/seedlings

🌱 Create some seed data, via json in a flat format that can be batch loaded

Primary LanguageJavaScriptMIT LicenseMIT

🌱 seedlings

Create some seed data, via json in a flat format that can be batch loaded

stability-unstable Build Status Dependency Status Dev Dependency Status

Usage

Seedlings will modify a json structure replacing any keys of the format {%type:id%}

Where

  • id is any string and the same string will produce the same resulting id in the final output
  • type groups the id for example in incremental mode the first of each type will reset to zero

Now lets create a new instance of seedlings to show what that all means. Here we'll be using the incremental id generator

Notice in the below the change of type will reset the index to zero ({%posts:1%} and {%users:1%})

var seedlings = require("seedlings");
var seeder = seedlings(require("seedlings/generators/incremental"));

var out1 = seeder([
  {
    "id": "{%users:1%}",
    "name": "Bob"
  },
  {
    "id": "{%users:2%}",
    "name": "Jane"
  }
]);
assert.deepEqual(out1, [
  {
    "id": 1,
    "name": "Bob"
  },
  {
    "id": 2,
    "name": "Jane"
  }
]);

var out2 = seeder([
  {
    "id": "{%posts:1%}",
    "owner": "{%users:1%}",
    "title": "Hello World",
    "content": "Hi everyone!"
  }
]);
assert.deepEqual(out2, [
  {
    "id": 1,
    "owner": 1,
    "title": "Hello World",
    "content": "Hi everyone!"
  }
]);

API

Create a new instance with

var seeder = seedlings(/* [id_generator_function] */);

Where id_generator_function is one of

  • seedlings/generator/incremental (default)
  • seedlings/generator/uuid

Both incremental and uuid will product the same output given the same input. Below is an example using the uuid generator

var seeder = seedlings(require("seedlings/generators/uuid"))
var outUUID = seeder([
  {id: "{%test:uuid%}"}
])
assert.deepEqual(outUUID, [
  {id: "01010101-0101-4101-8101-010101010101"}
]);

License

MIT