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

dynamo-seeder

v0.5.0

Published

Seed your DynamoDB database easily

Downloads

3,633

Readme

dynamo-seeder

Seed your DynamoDB database easily

Installation

$ npm install --save dynamo-seeder

How to use

const seeder = require('dynamo-seeder');
const data = require('./fixtures/data.json');

seeder.connect({
	prefix: 'pridiktiv.test'
});

seeder.seed(data)
    .then(() => {
        // The database is successfully seeded
    })
    .catch(err => {
        // handle error
    });

First of all, we should make sure the database is connected with the correct tables. It uses dynongo in the back.

After the database is connected, we can start seeding the data in the correct tables.

Behaviour

You can also provide extra options that will indicate the drop strategy. By default, tables are not dropped and the data that is seeded in that table is just added as new data. If you want to drop and create the table every time the seeder runs, you can add an extra option in the seed function.

Drop table

By setting this option to true, it will drop the tables that are being seeded. If you have two tables for example, but only one table is seeded, only that table will be dropped and recreated.

seeder.seed(data, {dropTables: true})
    .then(() => {
        // The database is successfully seeded
    })
    .catch(err => {
        // handle error
    });

JSON file

Simple data

How does a json file looks like? Take a look at this simple example.

{
    "users": {
        "_schema": "path/to/UserSchema.json",
        "foo": {
            "firstName": "Foo",
            "name": "Bar",
            "email": "[email protected]"
        }
    }
}

Where the schema defines the DynamoDB createTable definition. An example of the UserSchema.json file could look like this.

{
    "TableName": "User",
    "AttributeDefinitions": [
        {
            "AttributeName": "email",
            "AttributeType": "S"
        }
    ],
    "KeySchema": [
        {
            "AttributeName": "email",
            "KeyType": "HASH"
        }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
    }
}

The path to the schema is relative from the path where the seeder calls the seed() method. It will throw an error if the schema could not be found.

Expressions

Sometimes you will need something as an expression, for instance to set the birthday of the user.

{
    "users": {
        "_schema": "path/to/UserSchema.json",
        "foo": {
            "firstName": "Foo",
            "name": "Bar",
            "email": "[email protected]",
            "birthday": "=new Date(1988, 08, 16).toISOString()"
        }
    }
}

Every statement that is preceded by an =-sign will be parsed.

We can also bring it a step further and reference to the object itself. For instance, if we want to store the full name of the user aswell, instead of adding it manually, you can do something like this.

{
    "users": {
        "_schema": "path/to/UserSchema.json",
        "foo": {
            "firstName": "Foo",
            "name": "Bar",
            "fullName": "=this.firstName + ' ' + this.name",
            "email": "[email protected]",
            "birthday": "=new Date(1988, 08, 16).toISOString()"
        }
    }
}

The result of the fullName expression will be Foo Bar. So every evaluation is evaluated in it's own context.

Dependencies

What if we don't want to make use of the plain old Date object, but instead use something like moment. This is possible by adding a list of dependencies.

{
    "_dependencies": {
        "moment": "moment"
    },
    "users": {
        "_schema": "path/to/UserSchema.json",
        "foo": {
            "firstName": "Foo",
            "name": "Bar",
            "email": "[email protected]",
            "birthday": "=moment('1988-08-16').format()"
        }
    }
}

If you are using a dependency in your json file, be sure to install it as dependency in your project. If not, it will stop the execution and return a MODULE_NOT_FOUND error.

API

connect(options)

See dynongo.

seed(data, [options])

data

Type: object

The JSON seeding data.

options
dropTables

Type: boolean Default: false

Drop every table and recreate before it is reseeded again.

cwd

Type: string Default: process.cwd()

Working directory which is used as base path for the _schema property.

Related

License

MIT © Sam Verschueren