0.8.2 • Published 12 months ago

@chehsunliu/seeder-dynamodb v0.8.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Seeder.js / DynamoDB

NPM Version

The DynamoDB implementation for Seeder.js.

Getting Started

Install Seeder.js:

npm install -D @chehsunliu/seeder @chehsunliu/seeder-dynamodb

Assume the tables in the local DynamoDB have been managed by Terraform:

services:
  dynamodb:
    image: amazon/dynamodb-local:latest
    ports:
      - "127.0.0.1:8000:8000"

  dynamodb-init:
    image: hashicorp/terraform:1.9
    depends_on:
      - dynamodb
    restart: on-failure
    volumes:
      - type: bind
        source: ./db-schemas/dynamodb
        target: /infra
    working_dir: /infra
    entrypoint: "/bin/sh"
    command: ["-c", "terraform init && terraform apply -auto-approve"]
    environment:
      TF_VAR_endpoint: http://dynamodb:8000
      TF_VAR_region: us-west-2
      TF_VAR_access_key_id: xxx
      TF_VAR_secret_access_key: xxx

Configure the seeders in setup.ts, which should be loaded in Jest setupFilesAfterEnv or in Vitest setupFiles:

import { seederManager } from "@chehsunliu/seeder";
import { DynamoDbSeeder } from "@chehsunliu/seeder-dynamodb";

seederManager.configure([
  new DynamoDbSeeder({
    connection: {
      endpoint: "http://127.0.0.1:8000",
      region: "us-west-2",
      accessKeyId: "xxx",
      secretAccessKey: "xxx",
    },
    dataSelectors: [
      // The order matters. A table will only pick the first matched selector.
      { type: "dynamodb-json", getFilename: (tableName: string) => `${tableName}.ddb.json` },
      { type: "json", getFilename: (tableName: string) => `${tableName}.json` },
    ],
  }),
]);

afterAll(async () => {
  await seederManager.release();
});

Put some test data in data/demo.json:

[
  { "pk": "user#alice", "sk": "info", "email": "alice@example.com" },
  { "pk": "user#bob", "sk": "info", "email": "bob@example.com" }
]

Invoke the seeders in tests:

import { seederManager } from "@chehsunliu/seeder";

beforeEach(async () => {
  await seederManager.truncate();
  await seederManager.seed(path.join(__dirname, "data"));
});

test("blah blah blah", () => {
  // Data should be available here.
});
0.8.2

12 months ago

0.8.1

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.7.0-alpha.3

1 year ago

0.7.0-alpha.2

1 year ago

0.7.0-alpha.1

1 year ago

0.7.0-alpha

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.1.0

1 year ago