DynamoDB Seeder

0.5.0 · abandoned · verified Wed Apr 22

dynamo-seeder is a Node.js library designed to simplify the process of populating DynamoDB tables with initial data, often referred to as 'seeding.' It works by accepting JSON data, which can include references to external schema definitions for table creation and item data. The library supports basic data insertion and more advanced features like inline JavaScript expressions for dynamic data generation (e.g., dates, concatenated strings) and dependency injection for external libraries like `moment.js` within these expressions. Currently at version 0.5.0, it is not actively maintained, with its last known activity several years ago. It relies on `dynongo` for DynamoDB interactions, which itself appears to be in maintenance mode. This tool differentiates itself by allowing highly structured JSON data, including schema definitions and computed fields, directly within the seeding configuration, rather than solely relying on code-driven seeding.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `dynamo-seeder` to connect to DynamoDB (local or remote), define table schemas and data in JSON, and seed the database. It includes an example of dynamic data generation using JavaScript expressions and the `dropTables` option for fresh seeding.

const seeder = require('dynamo-seeder');
const path = require('path');
const fs = require('fs');

// Mock schema data for UserTable.json
const userSchema = {
    "TableName": "User",
    "AttributeDefinitions": [
        { "AttributeName": "email", "AttributeType": "S" }
    ],
    "KeySchema": [
        { "AttributeName": "email", "KeyType": "HASH" }
    ],
    "ProvisionedThroughput": {
        "ReadCapacityUnits": 1,
        "WriteCapacityUnits": 1
    }
};

// Create a dummy schema file for the seeder to find
const schemaPath = path.join(__dirname, 'UserSchema.json');
fs.writeFileSync(schemaPath, JSON.stringify(userSchema, null, 2));

// Mock data for seeding
const seedData = {
    "users": {
        "_schema": "./UserSchema.json",
        "foo": {
            "firstName": "Foo",
            "name": "Bar",
            "fullName": "=this.firstName + ' ' + this.name",
            "email": "foo@bar.com",
            "birthday": "=new Date(1988, 8, 16).toISOString()"
        },
        "bar": {
            "firstName": "Baz",
            "name": "Qux",
            "email": "baz@qux.com"
        }
    }
};

async function runSeeder() {
    try {
        // Connect to DynamoDB (defaults to local DynamoDB if not configured explicitly)
        // For AWS, ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION are set as env vars
        seeder.connect({
            prefix: 'mytestapp.', // Prefix for table names
            local: process.env.DYNAMODB_LOCAL === 'true', // Connect to DynamoDB Local
            // For local, you might also need host/port:
            // host: 'localhost',
            // localPort: 8000
        });

        console.log('Seeding DynamoDB tables...');
        await seeder.seed(seedData, { dropTables: true }); // dropTables: true will recreate tables
        console.log('Database successfully seeded!');
    } catch (err) {
        console.error('Error seeding database:', err);
    } finally {
        // Clean up dummy schema file
        fs.unlinkSync(schemaPath);
        // In a real application, you might disconnect or simply exit
        process.exit(0);
    }
}

// Ensure DYNAMODB_LOCAL is set to 'true' to run against local DynamoDB for testing
// Otherwise, ensure AWS credentials are configured for remote DynamoDB
process.env.DYNAMODB_LOCAL = process.env.DYNAMODB_LOCAL ?? 'true'; 

runSeeder();

view raw JSON →