Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

dbfixtures-mysql-driver

1.0.2 • Public • Published

Build Status

Fixtures Manager MySQL Driver

An abstraction layer for the mysql package to facilitate handling database fixtures for testing purposes, in a MySQL database. This package is ment to be used in conjunction with the dbfixtures package, but can also be used by itself.

Installation

npm install dbfixtures-mysql-driver

Usage

This package exposes the create(connectionOptions: string | ConnectionConfig): Promise<IDriver> function that returns a Promise that resolves with an instance of the driver. Note: For detailed information about the connectionOptions argument, please consult these connection options.

An instance of the driver exposes the following interface

// truncates the tables with the supplied names, ignoring foreign key constraints
truncate: (tableNames: string[]) => Promise<void>
 
// inserts the supplied rows into the specified table, respecting foreign key constraints
insertFixtures: (tableName: string, fixtures: [{}]) => Promise<void>
 
// terminates the connection to the database
close: () => Promise<void>

Example

This example uses Mocha as the potential test runner.

const dbfixtures = require('dbfixtures');
const fixturesMysqlDriver = require('dbfixtures-mysql-driver');
 
const mysqlConnectionInfo = {
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: '',
  database: 'fixtures_test',
};
const fixtures = {
  'roles': [
    { id: 1, name: 'role 1' },
    { id: 2, name: 'role 2' },
  ],
  'users': [
    { id: 1, email: 'myemail@test.net', role_id: 2 },
    { id: 2, email: 'test@gmail.com', role_id: 1 },
    { id: 3, email: 'another@email.org', role_id: 1 },
  ],
};
 
describe('fixtures example', function () {
  before(async function () {
    const mysqlDriver = await fixturesMysqlDriver.create(mysqlConnectionInfo);
    dbfixtures.setDrivers(mysqlDriver);
  });
 
  beforeEach(async function () {
    await dbfixtures.insertFixtures(fixtures);
  });
 
  it('should have the database seeded with the fixtures', function () {
    // ...
  });
});

Testing This Package

  • cd into the package's directory

  • run npm install

  • for unit tests run npm test -- test\unit\**\*.test.js

  • for integration tests run npm test -- test\integration\**\*.test.js NOTE: requires an active MySQL server and a database to be setup, using the following MySQL commands

CREATE DATABASE fixtures_test;
CREATE TABLE fixtures_test.roles (id INT AUTO_INCREMENT, name VARCHAR(100) NULL, PRIMARY KEY(id));
CREATE TABLE fixtures_test.users (id INT AUTO_INCREMENT, email VARCHAR(150) NOT NULL, role_id INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY (role_id) REFERENCES fixtures_test.roles(id));

install

npm i dbfixtures-mysql-driver

Downloadsweekly downloads

0

version

1.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability