typeorm-mimic
typeorm-mimic is a simple (but powerful) Node.js library to generate test data using only the TypeORM schema.
Features
- Generate random values depending on primitive data types (String, Number, Boolean, Date...)
- Generate custom values for specific fields
- Generate custom values that match non-primitive data types (email, phone, address...)
- Ignore fields
- Generate dates as object or string
Installation
npm install @genially/typeorm-mimic
Usage
mimic(model, opts)
Generates a mimetic document from schemaDefinition
schemaDefinition
: TypeORM schema definitionopts
: Generation options, where the options are in the following format:
{
ignore: Array,
datesAsString: Boolean,
custom: {
field: {
value: Any,
type: String
}
}
}
Option | Type | Usage |
---|
ignore | Array | It can contains string paths or RegExp of fields to ignore during generation |
datesAsString | Boolean | Return dates as Date or String |
custom | Object | Special generator for specific fields |
custom.field.value | Any | Predefined value to the given field |
custom.field.type | String | Data type to generate to the given field, in the format: "type.subtype". Examples: "internet.email" or "address.city". See Faker.js methods to know all supported data types |
Usage Example
const { EntitySchema } = require('typeorm');
const mimic = require('@genially/typeorm-mimic');
const schemaDefinition = new EntitySchema({
name: 'Test',
columns: {
name: {
type: String,
default: 'Mimic'
},
address: {
type: String,
nullable: true
},
email: {
type: String
},
phones: {
type: [String]
},
birth_date: {
type: Date
},
role: {
enum: [0, 1, 2]
},
is_student: {
type: Boolean
},
created_at: {
type: Date,
default: Date.now
}
}
});
const randomObject = mimic(schemaDefinition, {
ignore: ['address'],
datesAsString: false,
custom: {
email: {
type: 'internet.email'
},
phones: {
type: 'phone.phoneNumber'
},
birth_date: {
value: () => new Date('December 25, 1995 23:15:30')
}
}
});
console.log(randomObject);
Testing
To run the test cases use npm test
Related packages
typeorm-mimic is strongly inspired by mongoose-mimic, which provides similar features to Mongoose ODM.
License
Licensed under MIT