Skip to content

Commit

Permalink
feat(createMany): add createMany api
Browse files Browse the repository at this point in the history
  • Loading branch information
Rory Jennings committed Sep 2, 2022
1 parent 951b2c8 commit 8192aa5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ drop(db)
Each model has the following methods:

- [`create()`](#create)
- [`createMany()](#createMany)
- [`findFirst()`](#findFirst)
- [`findMany()`](#findMany)
- [`count()`](#count)
Expand Down Expand Up @@ -236,6 +237,24 @@ const user = db.user.create({

> Note that all model properties _are optional_, including [relational properties](#model-relationships).
### `createMany`

Creates multiple entities for the model.

```js
const users = db.user.createMany({
firstName: 'Alice',
}, {
firstName: 'Bob',
})
```

At least one set of initial values must be passed to this method. To create entities using the getter functions specified in the model definition, you can pass empty objects:

```js
const users = db.user.createMany({}, {}, {}, {})
```

### `findFirst`

Returns the first entity that satisfies the given query.
Expand Down
3 changes: 3 additions & 0 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ function createModelApi<
db.create(modelName, entity)
return entity
},
createMany(firstInitialValue, ...otherInitialValues) {
return [firstInitialValue, ...otherInitialValues].map(api.create);
},
count(query) {
if (!query) {
return db.count(modelName)
Expand Down
7 changes: 7 additions & 0 deletions src/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ export interface ModelAPI<
create(
initialValues?: InitialValues<Dictionary, ModelName>,
): Entity<Dictionary, ModelName>
/**
* Creates multiple entities for the model.
*/
createMany(
firstInitialValue: InitialValues<Dictionary, ModelName>,
...otherInitialValues: InitialValues<Dictionary, ModelName>[]
): Entity<Dictionary, ModelName>[]
/**
* Return the total number of entities.
*/
Expand Down
33 changes: 33 additions & 0 deletions test/model/createMany.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import faker from '@faker-js/faker';
import { factory, nullable, primaryKey } from '../../src';

const db = factory({
user: {
id: primaryKey(String),
firstName: String,
lastName: nullable(faker.name.lastName),
age: nullable<number>(() => null),
address: {
billing: {
country: String,
},
},
},
company: {
name: primaryKey(String),
},
})

// Most assertions are covered by create.test-d.ts, as
// createMany calls it internally

// @ts-expect-error Expected at least 1 arguments, but got 0.
db.user.createMany()

db.user.createMany(
{},
{
// @ts-expect-error Property "secondName" does not exist on "user".
secondName: 'any'
}
)
46 changes: 46 additions & 0 deletions test/model/createMany.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import faker from '@faker-js/faker'
import { factory, identity, primaryKey } from '../../src'

test('creates a single new entity', () => {
const userId = faker.datatype.uuid()
const db = factory({
user: {
id: primaryKey(identity(userId)),
},
})

const users = db.user.createMany({})
expect(users.length).toEqual(1)
expect(users[0].id).toEqual(userId)
})

test('creates multiple new entities', () => {
const db = factory({
user: {
id: primaryKey(faker.datatype.uuid),
},
})

const users = db.user.createMany({}, {}, {})
expect(users.length).toEqual(3)
})

test('creates entities using the set values', () => {
const db = factory({
user: {
id: primaryKey(faker.datatype.uuid),
},
})

const users = db.user.createMany({
id: 'abc-123',
}, {
id: 'def-456',
}, {
id: 'ghi-789',
})
expect(users.length).toEqual(3)
expect(users[0].id).toEqual('abc-123')
expect(users[1].id).toEqual('def-456')
expect(users[2].id).toEqual('ghi-789')
})

0 comments on commit 8192aa5

Please sign in to comment.