diff --git a/README.md b/README.md index b9877fa7..987e43c8 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,18 @@ Creates an entity for the model. const user = db.user.create() ``` +When called without arguments, `.create()` will populate the entity properties using the getter functions you've specified in the model definition. + +You can also provide a partial initial values when creating an entity: + +```js +const user = db.user.create({ + firstName: 'John', +}) +``` + +> Note that all model properties _are optional_, including [relational properties](#model-relationships). + #### `findFirst` Returns the first entity that satisfies the given query. diff --git a/src/model/defineRelationalProperties.ts b/src/model/defineRelationalProperties.ts index 05fea7c1..0f037953 100644 --- a/src/model/defineRelationalProperties.ts +++ b/src/model/defineRelationalProperties.ts @@ -36,6 +36,9 @@ export function defineRelationalProperties( `defining relational property "${entity.__type}.${property}"`, relation, ) + + if (!(property in initialValues)) return properties + // Take the relational entity reference from the initial values. const entityRefs: Entity[] = [].concat(initialValues[property]) diff --git a/test/relations/many-to-one.test.ts b/test/relations/many-to-one.test.ts index 326db309..5c977f0d 100644 --- a/test/relations/many-to-one.test.ts +++ b/test/relations/many-to-one.test.ts @@ -110,3 +110,19 @@ test('supports querying through nested relational properties', () => { expect(result).toHaveLength(3) }) + +test('should not throw error if an entity with many-to-one relation is created without it', () => { + const db = factory({ + user: { + id: primaryKey(random.uuid), + firstName: name.firstName, + }, + post: { + id: primaryKey(random.uuid), + title: random.words, + author: oneOf('user'), + }, + }) + + expect(() => db.post.create()).not.toThrow() +}) diff --git a/test/relations/one-to-many.test.ts b/test/relations/one-to-many.test.ts index 68be0e90..41bb0c4f 100644 --- a/test/relations/one-to-many.test.ts +++ b/test/relations/one-to-many.test.ts @@ -76,3 +76,18 @@ test('supports querying through one-to-many relation', () => { const userIds = users.map((user) => user.id) expect(userIds).toEqual(['user-1', 'user-3']) }) + +test('should not throw error if an entity with one-to-many relation is created without it', () => { + const db = factory({ + user: { + id: primaryKey(random.uuid), + posts: manyOf('post'), + }, + post: { + id: primaryKey(random.uuid), + title: random.words, + }, + }) + + expect(() => db.user.create()).not.toThrow() +}) diff --git a/test/relations/one-to-one.test.ts b/test/relations/one-to-one.test.ts index 7303851d..3f0185fd 100644 --- a/test/relations/one-to-one.test.ts +++ b/test/relations/one-to-one.test.ts @@ -1,7 +1,7 @@ import { random } from 'faker' import { factory, primaryKey, oneOf } from '@mswjs/data' -test.only('supports one-to-one relation', () => { +test('supports one-to-one relation', () => { const db = factory({ country: { id: primaryKey(random.uuid), @@ -57,3 +57,19 @@ test('supports querying through a one-to-one relational property', () => { }) expect(capital).toHaveProperty('name', 'Washington') }) + +test('should not throw error if an entity with one-to-one relation is created without it', () => { + const db = factory({ + country: { + id: primaryKey(random.uuid), + name: random.words, + }, + capital: { + id: primaryKey(random.uuid), + name: random.word, + country: oneOf('country'), + }, + }) + + expect(() => db.capital.create()).not.toThrow() +})