SQL Fixtures

1.0.4 · maintenance · verified Wed Apr 22

sql-fixtures is a JavaScript library designed to populate SQL databases with structured test data, commonly referred to as 'fixtures'. It automatically handles foreign key dependencies, making it suitable for integration testing and generating dummy data for development environments. The current stable version is 1.0.4, last published approximately five years ago. The package maintainer has stated it is 'dormant but stable,' indicating a maintenance-only release cadence where new features are not expected, but critical issues will be addressed. Internally, it leverages the `knex` SQL query builder, supporting PostgreSQL, MySQL, MariaDB, and SQLite. Its key differentiator is the ability to define data specifications in a simple JavaScript object format and have the library intelligently insert rows, resolving dependencies to ensure data integrity during population.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `sql-fixtures` to define and insert data into a PostgreSQL database, including handling foreign key relationships. It first sets up a basic schema using `knex` and then populates it with users and posts.

const sqlFixtures = require('sql-fixtures');
const Knex = require('knex');

// Ensure you have a running PostgreSQL database and 'pg' driver installed (npm install pg)
// For a real application, use environment variables for sensitive data.
const dbConfig = {
  client: 'pg',
  connection: {
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'testuser',
    password: process.env.DB_PASSWORD || 'testpassword',
    database: process.env.DB_NAME || 'testdb',
    port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432
  }
};

const knex = Knex(dbConfig);

const dataSpec = {
  users: [
    { id: 1, username: 'alice', email: 'alice@example.com' },
    { id: 2, username: 'bob', email: 'bob@example.com' }
  ],
  posts: [
    { id: 101, title: 'First Post', content: 'Lorem ipsum...', author_id: '@user->id (id=1)' },
    { id: 102, title: 'Second Post', content: 'Dolor sit amet...', author_id: '@user->id (id=2)' }
  ]
};

async function runFixtures() {
  try {
    // Ensure tables exist before inserting data
    await knex.schema.dropTableIfExists('posts');
    await knex.schema.dropTableIfExists('users');
    await knex.schema.createTable('users', table => {
      table.integer('id').primary();
      table.string('username').notNullable().unique();
      table.string('email').notNullable().unique();
    });
    await knex.schema.createTable('posts', table => {
      table.integer('id').primary();
      table.string('title').notNullable();
      table.text('content');
      table.integer('author_id').unsigned().notNullable();
      table.foreign('author_id').references('id').inTable('users');
    });

    console.log('Database schema created/reset.');

    const result = await sqlFixtures.create(dbConfig, dataSpec);
    console.log('Fixtures inserted successfully:');
    console.log(JSON.stringify(result, null, 2));
    console.log('Users inserted:', result.users.length);
    console.log('Posts inserted:', result.posts.length);

  } catch (err) {
    console.error('Error running fixtures:', err);
  } finally {
    await knex.destroy();
  }
}

runFixtures();

view raw JSON →