wawasdat
v0.0.4
Published
wawasdat -> what was that a simple DSL library to generate data records
Downloads
4
Maintainers
Readme
wawasdat -> what was that
a simple DSL library to generate data records
wawasdat
a simple DSL library to generate datasets
Installation
npm i wawasdat
Usage
wawasdat contains two DSL function
- field: describe the field name and the data-generate function for this field
- trait: describe the other variant of dataset
Examples
const { defineFactory } = require('wawasdat')
const carFactory = defineFactory(() => {
// set the field name and the generation function
field('color', () => 'blue')
field('weight', () => 2500)
})
console.log(carFactory.create())
// this generate
{ color: 'blue', weight: 2500 }
const { defineFactory } = require('wawasdat')
const { faker } = require('@faker-js/faker')
const userFactory = defineFactory(() => {
field('name', () => faker.name.fullName())
field('sex', () => faker.name.sex())
})
// use "times" option to generate array of data
console.log(userFactory.create({ times: 3 }))
// this generate
[
{ name: 'Darrell Lehner', sex: 'female' },
{ name: 'Evelyn Kohler Jr.', sex: 'female' },
{ name: 'Sammy Hudson', sex: 'female' }
]
const { defineFactory } = require('wawasdat')
const { faker } = require('@faker-js/faker')
const eventFactory = defineFactory(() => {
field('title', () => faker.lorem.sentence(2))
field('location', () => faker.address.cityName())
const startAt = faker.datatype.datetime()
field('startAt', () => startAt)
// set the event duration between 1 hour to 1 day
const duration = faker.datatype.number({ min: 3600_000, max: 86400_000 })
field('endAt', () => new Date(+startAt + duration))
// define a trait to describe events that already started
trait('started', () => {
field('startAt', () => +(new Date) - faker.datatype.number({ min: 30_000, max: 1800_000 }) )
field('endAt', () => +(new Date) + faker.datatype.number({ min: 30_000, max: 1800_000 }) )
// trait can have different fields
field('extraFieldFromTrait', () => 'something')
})
// define a trait to describe events that already ended
trait('ended', () => {
const endBefore = faker.datatype.number({ min: 30_000, max: 1800_000 })
const endAt = new Date(+(new Date) - endBefore)
field('endAt', () => endAt )
const eventDuration = faker.datatype.number({ min: 30_000, max: 1800_000 })
field('startAt', () => new Date(+endAt - eventDuration))
})
})
eventFactory.create()
// this generate
{
title: 'Enim corrupti.',
location: 'Hammond',
startAt: 2065-06-16T12:26:34.463Z,
endAt: 2065-06-16T16:39:33.257Z
}
// the "overwrite" options will merge the object into generated one
eventFactory.create({ trait: 'started' })
// this generate
{
title: 'Doloribus doloremque.',
location: 'Santa Rosa',
startAt: 2023-04-25T12:35:20.122Z,
endAt: 2023-04-25T13:05:58.640Z,
extraFieldFromTrait: 'something'
}
// you can also overwrite(or merge) fields with "overwrite" option
eventFactory.create({ trait: 'ended', overwrite: { label: 'ended' } })
// this generate
{
title: 'Sequi totam.',
location: 'Tulare',
startAt: 2023-04-25T12:38:22.997Z,
endAt: 2023-04-25T12:39:13.800Z,
label: 'ended'
}