Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add synchronous build method #110

Merged
merged 1 commit into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ mocker()
},
err => console.error(err)
)

// Synchronously

// This returns an object
// {
// user:[array of users],
// group: [array of groups],
// conditionalField: [array of conditionalFields]
// }
var data = mocker()
.schema('user', user, 2)
.schema('group', group, 2)
.schema('conditionalField', conditionalField, 2)
.buildSync()

console.log(util.inspect(data, { depth: 10 }))
```

## Documentation
Expand All @@ -144,6 +160,7 @@ Data generation goes with model based composed by generators, the generators can
* **_reset()_**: Clean the internal DB.
* **_restart()_**: Clean the internal DB and all the schemas inside.
* **_build(callback)_**: This methods start to produce the data and wrap it to the callback function, the callback funtion have 2 parameters, error and data generated.
- **_buildSync()_**: Synchronous version of `build(callback)`. Returns generated data or throws an error.

### Schema definition

Expand Down
57 changes: 33 additions & 24 deletions src/lib/Mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,43 @@ export class Mocker {
return this
}

build(cb?: (error: Error | null, _?: any) => void): Promise<any>
build(cb?: (error: Error | null, _?: any) => void): void
build(cb?: (error: Error | null, _?: any) => void): any {
try {
this.schemas.reduce((acc, schema) => {
let instances
private _buildSync() {
this.schemas.reduce((acc, schema) => {
let instances

try {
instances = schema.build(acc)
} catch (e) {
throw new Error('Schema: "' + schema.name + '" ' + e)
}

// Clean virtuals
if (schema.virtualPaths.length > 0) {
instances.forEach((x) =>
cleanVirtuals(schema.virtualPaths, x, {
strict: true,
symbol: ','
})
)
}

try {
instances = schema.build(acc)
} catch (e) {
throw new Error('Schema: "' + schema.name + '" ' + e)
}
// Add to db
acc[schema.name] = instances

// Clean virtuals
if (schema.virtualPaths.length > 0) {
instances.forEach((x) =>
cleanVirtuals(schema.virtualPaths, x, {
strict: true,
symbol: ','
})
)
}
return acc
}, this.DB)
}

// Add to db
acc[schema.name] = instances
buildSync() {
this._buildSync()
return this.DB
}

return acc
}, this.DB)
build(cb?: (error: Error | null, _?: any) => void): Promise<any>
build(cb?: (error: Error | null, _?: any) => void): void
build(cb?: (error: Error | null, _?: any) => void): any {
try {
this._buildSync()
} catch (e) {
return cb ? cb(e) : Promise.reject(e)
}
Expand Down
35 changes: 35 additions & 0 deletions src/lib/tests/Mocker.build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,38 @@ test('Should build with Promised old style', async (t) => {
.build()
.then((db) => t.deepEqual(db, result))
})

test('Should build synchronously', (t) => {
let result = {
users: [
{
hello: 'world'
}
]
}
let mock = new Mocker()
let db = mock.schema('users', { hello: { static: 'world' } }, 1).buildSync()

t.deepEqual(db, result)
})

test('Should throw synchronously', (t) => {
let result = {
users: [
{
hello: 'world'
}
]
}
let mock = new Mocker()
const error = t.throws(() =>
mock
.schema('users', { hello: { faker: 'worldrqwerqw' } }, 1)
.buildSync()
)

t.is(
error.message,
'Schema: "users" Error: "faker" This faker method doesnt exists \'worldrqwerqw\'.'
)
})