diff --git a/docs/content/1.guide/4.repository/3.inserting-data.md b/docs/content/1.guide/4.repository/3.inserting-data.md index 85db1188b..897242465 100644 --- a/docs/content/1.guide/4.repository/3.inserting-data.md +++ b/docs/content/1.guide/4.repository/3.inserting-data.md @@ -161,3 +161,18 @@ const user = useRepo(User).make({ age: 30 }) ``` + +You can even create multiple at one. + +````js +const users = useRepo(User).make([ + { + id: 1, + name: 'Jane Doe', + }, + { + id: 2, + name: 'John Doe', + }, + ]) +```` diff --git a/docs/content/2.api/3.query/orderBy.md b/docs/content/2.api/3.query/orderBy.md index b32698cd0..d42904d02 100644 --- a/docs/content/2.api/3.query/orderBy.md +++ b/docs/content/2.api/3.query/orderBy.md @@ -27,5 +27,5 @@ useRepo(User).orderBy(user => user.name[2]).get() ## Typescript Declarations ````ts -function orderBy(field: OrderBy, direction: OrderDirection = 'asc'): Query +function orderBy(field: OrderBy, direction: OrderDirection = 'asc'): Query ```` diff --git a/docs/content/2.api/3.query/with.md b/docs/content/2.api/3.query/with.md index b3b35a9d9..84af9b414 100644 --- a/docs/content/2.api/3.query/with.md +++ b/docs/content/2.api/3.query/with.md @@ -22,5 +22,5 @@ const usersWithCommentsOnlyActive = userRepo.with('comments', (query) => { ## Typescript Declarations ````ts -function with(name: string, callback: EagerLoadConstraint = () => {}): Query +function with(name: string, callback: EagerLoadConstraint = () => {}): Query ```` diff --git a/docs/content/2.api/3.query/withAll.md b/docs/content/2.api/3.query/withAll.md index 05a05b321..a6a5d2a58 100644 --- a/docs/content/2.api/3.query/withAll.md +++ b/docs/content/2.api/3.query/withAll.md @@ -22,5 +22,5 @@ const usersWithCommentsOnlyActive = userRepo.withAll((query) => { ## Typescript Declarations ````ts -function withAll(callback: EagerLoadConstraint = () => {}): Query +function withAll(callback: EagerLoadConstraint = () => {}): Query ```` diff --git a/docs/content/2.api/3.query/withAllRecursive.md b/docs/content/2.api/3.query/withAllRecursive.md index fff86836c..d00a85623 100644 --- a/docs/content/2.api/3.query/withAllRecursive.md +++ b/docs/content/2.api/3.query/withAllRecursive.md @@ -19,5 +19,5 @@ const usersWithRelations = userRepo.withAllRecursive(2).get() // User[] with all ## Typescript Declarations ````ts -function withAllRecursive(depth = 3): Query +function withAllRecursive(depth = 3): Query ```` diff --git a/docs/content/2.api/4.repository/make.md b/docs/content/2.api/4.repository/make.md new file mode 100644 index 000000000..696dfc23e --- /dev/null +++ b/docs/content/2.api/4.repository/make.md @@ -0,0 +1,44 @@ +--- +title: 'make()' +description: 'Creates a new model instance' +--- + +This method will not save the model to the store. It's pretty much the alternative to `new Model()`, but it injects +the store instance to support model instance methods in SSR environment. + +## Usage + +````ts +import { useRepo } from 'pinia-orm' +import User from './models/User' + +const userRepo = useRepo(User) + +// Make a model with default values +userRepo.make() + +// Make a model with values +userRepo.make({ + id: 1, + name: 'Jane Doe', +}) + +// Make many models with values +userRepo.make([ + { + id: 1, + name: 'Jane Doe', + }, + { + id: 2, + name: 'John Doe', + }, +]) + +```` + +## Typescript Declarations + +````ts +function make(records?: Element | Element[]): M | M[] +```` diff --git a/packages/pinia-orm/src/repository/Repository.ts b/packages/pinia-orm/src/repository/Repository.ts index 9257cb2e9..04aa55b11 100644 --- a/packages/pinia-orm/src/repository/Repository.ts +++ b/packages/pinia-orm/src/repository/Repository.ts @@ -1,6 +1,6 @@ import type { Store } from 'pinia' import type { Constructor } from '../types' -import { assert } from '../support/Utils' +import { assert, isArray } from '../support/Utils' import type { Collection, Element, Item } from '../data/Data' import type { Database } from '../database/Database' import type { Model } from '../model/Model' @@ -257,8 +257,16 @@ export class Repository { * store. It's pretty much the alternative to `new Model()`, but it injects * the store instance to support model instance methods in SSR environment. */ - make(attributes?: Element): M { - return this.getModel().$newInstance(attributes, { + make(records: Element[]): M[] + make(record?: Element): M + make(records?: Element | Element[]): M | M[] { + if (isArray(records)) { + return records.map(record => this.getModel().$newInstance(record, { + relations: true, + })) + } + + return this.getModel().$newInstance(records, { relations: true, }) } diff --git a/packages/pinia-orm/tests/unit/repository/Repository.spec.ts b/packages/pinia-orm/tests/unit/repository/Repository.spec.ts index b0539311c..ee78f16d9 100644 --- a/packages/pinia-orm/tests/unit/repository/Repository.spec.ts +++ b/packages/pinia-orm/tests/unit/repository/Repository.spec.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from 'vitest' import { Attr, Model, Repository, Str, useRepo } from '../../../src' -import { assertModel } from '../../helpers' +import { assertInstanceOf, assertModel, assertModels } from '../../helpers' describe('unit/repository/Repository', () => { class User extends Model { @@ -39,6 +39,33 @@ describe('unit/repository/Repository', () => { assertModel(user, { id: 1, name: 'Jane Doe' }) }) + it('creates many new model instances with default values', () => { + const userRepo = useRepo(User) + + const users = userRepo.make([ + { + id: 1, + name: 'Jane Doe', + }, + { + id: 2, + name: 'John Doe', + }, + ]) + + assertInstanceOf(users, User) + assertModels(users, [ + { + id: 1, + name: 'Jane Doe', + }, + { + id: 2, + name: 'John Doe', + }, + ]) + }) + it('can create a new repository from the model', () => { const userRepo = useRepo(User)