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

feat(pinia-orm): add the possibility to create many models with "make" in repository #133

Merged
merged 3 commits into from
Aug 1, 2022
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
15 changes: 15 additions & 0 deletions docs/content/1.guide/4.repository/3.inserting-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
])
````
2 changes: 1 addition & 1 deletion docs/content/2.api/3.query/orderBy.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ useRepo(User).orderBy(user => user.name[2]).get()
## Typescript Declarations

````ts
function orderBy(field: OrderBy, direction: OrderDirection = 'asc'): Query<M>
function orderBy(field: OrderBy, direction: OrderDirection = 'asc'): Query
````
2 changes: 1 addition & 1 deletion docs/content/2.api/3.query/with.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ const usersWithCommentsOnlyActive = userRepo.with('comments', (query) => {
## Typescript Declarations

````ts
function with(name: string, callback: EagerLoadConstraint = () => {}): Query<M>
function with(name: string, callback: EagerLoadConstraint = () => {}): Query
````
2 changes: 1 addition & 1 deletion docs/content/2.api/3.query/withAll.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ const usersWithCommentsOnlyActive = userRepo.withAll((query) => {
## Typescript Declarations

````ts
function withAll(callback: EagerLoadConstraint = () => {}): Query<M>
function withAll(callback: EagerLoadConstraint = () => {}): Query
````
2 changes: 1 addition & 1 deletion docs/content/2.api/3.query/withAllRecursive.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ const usersWithRelations = userRepo.withAllRecursive(2).get() // User[] with all
## Typescript Declarations

````ts
function withAllRecursive(depth = 3): Query<M>
function withAllRecursive(depth = 3): Query
````
44 changes: 44 additions & 0 deletions docs/content/2.api/4.repository/make.md
Original file line number Diff line number Diff line change
@@ -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[]
````
14 changes: 11 additions & 3 deletions packages/pinia-orm/src/repository/Repository.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -257,8 +257,16 @@ export class Repository<M extends Model = Model> {
* 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,
})
}
Expand Down
29 changes: 28 additions & 1 deletion packages/pinia-orm/tests/unit/repository/Repository.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)

Expand Down