Fabricate and delete your data on integration testing.
npm i db-fabricate
yarn add db-fabricate
Structure
What's it?
db-fabricator
is a simple node.js module that can create, update and delete data and also manipulate created and updated data during sessions.
Simple Example
const fab = ;const MySQLAdapter = fabMySQLAdapter;const Fabricator = database: 'TestDatabase'; // CHANGE YOUR DB CREDENTIALS HERE Fabricator; Fabricator;
NOTE: Defaults for database connection are:
{
host: 'localhost',
user: 'root',
password: ''
}
So you can easily create integration tests with on-the-go data and simply remove it after creation.
Documentation for methods:
Fabricator.create()
Will create new entity in database with passed parameters:
Fabricator;
Fabricator.startSession()
Starts new session for fabricator. You can nest sessions. Data will be recoreded to the latest opened session. On close session all data created or modified in this session will be restored. NOTE: This works with insert, create and remove.
Fabricator; // Session 1Fabricator; // Session 2 Fabricator;
Fabricator.stopSession()
Stops latest opened session. On close session all data created or modified in this session will be restored.
Fabricator;Fabricator;
Fabricator.update(table, data, constraints)
Updates entity/enitities in table
with fields and values specified in data
. Entities will be found by passed constraints.
Fabricator;Fabricator;
Fabricator.select(table, filter)
Selects data by given constraints
Fabricator;
Fabricator.remove(table, data)
Removes data from table
by passed list of ids (or single id):
return Fabricator;
NOTE: Remove works with sessions. Added at 1.0.5
Fabricator; Fabricator
Fabricator.closeConnection()
Stops all active sessions and closes connection to current database.
Fabricator;console;
Fabricator.createTemplate(table, data)
Simple template that returns a function that can be called with some particular data and it will be written to database.
const template = Fabricator; Fabricator; ;
You can insert functions that returns some value based on object or function that returns other templates as a fields to templates: implemented at 1.0.14:
const organizationTemplate = Fabricator; const userTemplate = Fabricator; Fabricator; ;
obj
in function argument is an object with all pure data you passed into tempmate. In this case it will be
firstName: 'John' lastName: 'Smith'
Constraints for Fabricator.update
You can create loopback-like constraints or your own by simply passing an SQL WHERE string. List of avaliable filters:
equals
- {id: 1} coverts to`id` = 1
$gt
- {id: {$gt: 1}} converts to`id` > 1
$gte
- {id: {$gte: 1}} converts to`id` >= 1
$lt
- {id: {$lt: 1}} converts to`id` < 1
$lte
- {id: {$gt: 1}} converts to`id` <= 1
$ne
- {id: {$gt: 1}} converts to`id` <> 1
$like
- {id: {$like:%12%5
}} converts to`id` like '%12%5'
$in
- {id: {$in: [1,2,3,4,5]}} converts to`id` IN (1,2,3,4,5)
. {id: {$in:[1]}} coverts to`id` = 1
$json
- {address: {$json: {key: 'address.city', value: 'Pkway st'}}} converts to`address`->'$.address.city' = 'Pkway st'
. Can use all filters except $or, $and or $json.$exists
- {name: {$exists: true}} converts to`name` is not null
. {name: {$exists: false}} converts to`name` is null
$and
- can be nested. {$and: [{id: 1}, {name: '123'}]} converts to(`id` = 1 AND `name` = '123')
$or
- can be nested. {$or: [{id: 1}, {name: '123'}]} converts to(`id` = 1 OR `name` = '123')
Constraints nesting
$or: name: $like: 'yegor' lastName: $like: 't%st' $and: jobPosition: 5 langs: $in: 'ru' 'en' id: $in: 12345
Will be converted to:
((`name` like 'yegor') OR (`lastName` like 't%st' AND ((`jobPosition` = 5 AND `langs` IN ('ru','en'))))) AND `id` IN (1,2,3,4,5)