simple-knex-fixtures

knex fixture loader

Usage no npm install needed!

<script type="module">
  import simpleKnexFixtures from 'https://cdn.skypack.dev/simple-knex-fixtures';
</script>

README

Build Status codecov

simple-knex-fixtures

A simple library to help you load data to a database to facilitate testing.

Caveats:

  • does not protect against duplicate records

Install

npm install --save simple-knex-fixtures

Test

npm test

Usage

const connection = require("knex")({
  client: "mysql",
  connection: {
    host : "127.0.0.1",
    user : "your_database_user",
    password : "your_database_password",
    database : "myapp_test"
  }
});

const fixtures = require("simple-knex-fixtures");

await fixtures.loadFile("fixtures/file.json", connection)

await fixtures.loadFiles("fixtures/*.json", connection)

await fixtures.loadFiles([
    "fixtures/file1.json",
    "fixtures/file2.json",
], connection);

File formats

javascript

module.exports = [
  {
    "table": "users",
    "data": {
      "id": 1,
      "first": "john",
      "last": "doe"
    }
  }
];

json

[
  {
    "table": "users",
    "data": {
      "id": 1,
      "first": "john",
      "last": "doe"
    }
  }
]

yaml

fixtures:
  -
    table: users
    data:
      id: 1
      first: john
      last: doe