Modules

Knex based fixtures seeding utility


Modules

Loader
SchemaInfo

Classes

Fixture

An instance of a single fixture which represents a single database record.

Model

An instance of a single model which represents an instance of a Fixture, which in turn represents a record in the database.

Resolver

Fixture resolver for resolving table columns and configured relationships. This supports polymorphism which is configurable via [ZeroOptions].

Table

An instance of an individual database table.

Zero

Entry class instance for Factory Zero.

Functions

seed(knex, options)Promise

Convenience method that runs Zero.seed() without having to instantiate the Zero module.

toJson(instance)object

Converts a Model instance to a plain object.

clone(object)object

Clones an object, including getters/setters and class prototypes.

filterKeys(object, predicate)object

Filters an object via a predicate over the object's keys.

filterValues(object, predicate)object

Filters an object via a predicate over the object's values.

Typedefs

ZeroOptions : object

Factory Zero options schema.

Loader

Loader.loadFixtures(options) ⇒ object

Loads all fixture files into memory based on configuration defined in Zero’s options.

Kind: static method of Loader
Returns: object - - All fixtures loaded from disk.

Param Type Description
options object Zero options.

Loader.getFixtureFiles(options) ⇒ Array.<string>

Retrieves the fixture file names from the configured directory.

Kind: static method of Loader
Returns: Array.<string> - - Collection of fixture file names.

Param Type Description
options object Zero options.

Loader.mapFixtures(options, files) ⇒ object

Maps fixture files into an object.

Kind: static method of Loader
Returns: object - - Mapped fixtures into an object.

Param Type Description
options object Zero options.
files Array.<object> Retrieved fixture files.

Loader.createFixtureMapping(options, file) ⇒ Array.<(string\|object)>

Imports an individual fixture file.

Kind: static method of Loader
Returns: Array.<(string\|object)> - - Imported fixture file.

Param Type Description
options object Zero options.
file object Individual file to import and name,

Loader.importFixture(path, ext) ⇒ Promise | object

Imports the fixture file via import or require, depending on the type.

Kind: static method of Loader
Returns: Promise | object - - Imported/required fixture file.

Param Type Description
path string Directory path to the fixtures directory.
ext string Fixture file type extension.

SchemaInfo

SchemaInfo.getTablesInfo(db, fixtures) ⇒ Array.<object>

Fetches all information schema objects related to all fixture files.

Kind: static method of SchemaInfo
Returns: Array.<object> - - Information schemas related to all fixture files.

Param Type Description
db Knex Knex DB instance.
fixtures Array.<object> All fixtures loaded from files.

SchemaInfo.queryInfoSchema(db, name) ⇒ Array.<(string\|object)>

Fetches a single information schema table object.

Kind: static method of SchemaInfo
Returns: Array.<(string\|object)> - - Key/value of the fetched information schema.

Param Type Description
db Knex Knex DB instance.
name stirng Information schema table name to fetch.

SchemaInfo.mapColumns(columns) ⇒ Array.<(string\|object)>

Mapping method for converting information schema query results to an array.

Kind: static method of SchemaInfo
Returns: Array.<(string\|object)> - - Key/value of the fetched information schema.

Param Type Description
columns object Information schema table columns.

Fixture

An instance of a single fixture which represents a single database record.

Kind: global class

new Fixture(name, data)

Param Type Description
name string Name of the fixture.
data object Fixture data.

fixture.resolve(relationMap, insertMap) ⇒ void

Resolves all the relations on the fixture.

Kind: instance method of Fixture

Param Type Description
relationMap Map.<string, object> Collection of all unresolved relations.
insertMap Map.<string, object> Collection of all insertable fixtures.

fixture.set(model, relations, insertMap) ⇒ void

Sets data onto the related columns.

Kind: instance method of Fixture

Param Type Description
model Model Instance of the current model.
relations object Collection of relations to resolve.
insertMap Map.<string, object> Collection of all insertable fixtures.

Model

An instance of a single model which represents an instance of a Fixture, which in turn represents a record in the database.

Kind: global class

new Model(options, table, data)

Param Type Description
options ZeroOptions Configuration for Factory Zero.
table Table Table instance defined for this Model.
data object Data object to bind to this instance.

Resolver

Fixture resolver for resolving table columns and configured relationships. This supports polymorphism which is configurable via [ZeroOptions].

Kind: global class

new Resolver(options)

Param Type Description
options ZeroOptions Configuration for Factory Zero.

resolver.fixtures() ⇒ Map.<string, object>

Resolves relations and returns a map of all insertable relations.

Kind: instance method of Resolver
Returns: Map.<string, object> - - All fixtures with relations resolved.

resolver.fixtureTables() ⇒ Array.<Fixture>

Fetches a new instance of Table for each fixture file.

Kind: instance method of Resolver
Returns: Array.<Fixture> - - Unresolved instances of Fixture.

resolver.fixtureModels(table) ⇒ Array.<Fixture>

Fetches a new instance of Model for each instance of Table.

Kind: instance method of Resolver
Returns: Array.<Fixture> - - Unresolved instances of Fixture.

Param Type Description
table Table Table instance to resolve relations against.

resolver.relations(model) ⇒ object

Searches the given fixture model for possible relatable columns.

Kind: instance method of Resolver
Returns: object - - Collection of relations to resolve later.

Param Type Description
model Model Model instance to search for relations on.

resolver.applyPolymorphism(model, relations) ⇒ void

Applies polymorphism to columns that can be polymorphically related.

Kind: instance method of Resolver

Param Type Description
model Model Model instance to search for polymorphic relations on.
relations object Collection of relations to check for polymorphism on.

Table

An instance of an individual database table.

Kind: global class

new Table(options, tableName, fixture)

Param Type Description
options ZeroOptions Configuration for Factory Zero.
tableName string Name of the table on this instance.
fixture Fixture Fixture instance to bind onto this table instance.

table.pk ⇒ object

Primary key configuration for this Table.

Kind: instance property of Table
Returns: object - - Primary key options for this Table.

table.columns ⇒ object

All columns defined on the database table this instance refers to.

Kind: instance property of Table
Returns: object - - Database columns.

table.fixtures ⇒ Array.<Fixture>

A collection of Fixture instances relating to this table instance.

Kind: instance property of Table
Returns: Array.<Fixture> - - Collection of Fixture instances.

table.configure(model, options) ⇒ object

Creates an options object with default values.

Kind: instance method of Table
Returns: object - - Defaulted options.

Param Type Description
model Model Model instance to build the options against.
options ZeroOptions Configuration for Factory Zero.

Zero

Entry class instance for Factory Zero.

Kind: global class

new Zero(connection, options)

Param Type Description
connection object | Knex Database connection options for this Knex instance.
options ZeroOptions Configuration for Factory Zero.

zero.seed() ⇒ Promise

Seeding method that runs all the necessary queries to seed all available fixtures into the configured database.

Kind: instance method of Zero
Returns: Promise - - A chained Promise instance of all queries on this instance.

zero.setup() ⇒ void

Sets up Factory Zero for seeding by loading fixtures and fetching schema information via information_schema.

Kind: instance method of Zero

zero.clear() ⇒ Promise

Clears data from all tables found in the fixtures directory.

Kind: instance method of Zero
Returns: Promise - - A collection of all DELETE queries.

zero.insert(fixtures) ⇒ Promise

Insert all fixtures into the configured database with all fixtures found in the fixtures directory.

Kind: instance method of Zero
Returns: Promise - - A collection of all INSERT queries.

Param Type Description
fixtures Map.<string, object> All fixtures to insert into the database.

zero.fixtures() ⇒ Map.<string, object>

Fetches all fixtures with all relations resolved via lib/Resolver.

Kind: instance method of Zero
Returns: Map.<string, object> - - A collection of all fixtures ready to insert.

zero.createKnex(kx) ⇒ Knex

Creates an individual Knex database instance.

Kind: instance method of Zero
Returns: Knex - - Knex database instance.

Param Type Description
kx object | Knex Knex connection instance.

zero.isKnex(kx) ⇒ boolean

Checks if [kx] is an instance of Knex.

Kind: instance method of Zero
Returns: boolean - - Is [kx] an instance of Knex?

Param Type Description
kx any Any value that might be an instance of Knex.

seed(knex, options) ⇒ Promise

Convenience method that runs Zero.seed() without having to instantiate the Zero module.

Kind: global function
Returns: Promise - - Resolved query.

Param Type Description
knex Knex | object Knex instance or a knex connection object.
options ZeroOptions Factory Zero options.

toJson(instance) ⇒ object

Converts a Model instance to a plain object.

Kind: global function
Returns: object - - Plain object of the given Model instance.

Param Type Description
instance Model Instance of a Model.

clone(object) ⇒ object

Clones an object, including getters/setters and class prototypes.

Kind: global function
Returns: object - - Cloned object.

Param Type Description
object object Data object to clone.

filterKeys(object, predicate) ⇒ object

Filters an object via a predicate over the object’s keys.

Kind: global function
Returns: object - - Filtered object.

Param Type Description
object object Object data to filter over.
predicate function Test function.

filterValues(object, predicate) ⇒ object

Filters an object via a predicate over the object’s values.

Kind: global function
Returns: object - - Filtered object.

Param Type Description
object object Object data to filter over.
predicate function Test function.

ZeroOptions : object

Factory Zero options schema.

Kind: global typedef
Properties

Name Type Description
extensions Array.<string> Fixture file extensions to search for.
snaked boolean Should file names be serialised in snake case?
pk string | object Default primary key for all models.
[col] string Primary key column name.
[type] string Primary key column type.
directory string Path to fixture files.
keys object Configurable keys for storing options relating to this program.
keys.model string Key to use for model options within the fixtures.
suffixes object Polymorphic column suffixes.
suffixes.type string Suffix used for the polymorphic type column.
suffixes.id string Suffix used for the polymorphic id column.