Skip to content
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
2 changes: 1 addition & 1 deletion packages/pinia-orm/.eslintcache

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export class Model {
parentKey = parentKey ?? model.$getLocalKey()
relatedKey = relatedKey ?? instance.$getLocalKey()

this.schemas[related.entity][`pivot_${relatedPivotKey}_${pivotInstance.$entity()}`] = new HasOne(instance, pivotInstance, relatedPivotKey, relatedKey)
this.schemas[related.modelEntity()][`pivot_${relatedPivotKey}_${pivotInstance.$entity()}`] = new HasOne(instance, pivotInstance, relatedPivotKey, relatedKey)

return new BelongsToMany(
model,
Expand Down Expand Up @@ -427,7 +427,7 @@ export class Model {
parentKey = parentKey ?? model.$getLocalKey()
relatedKey = relatedKey ?? instance.$getLocalKey()

this.schemas[related.entity][`pivot_${relatedId}_${pivotInstance.$entity()}`] = new MorphOne(instance, pivotInstance, relatedId, model.$entity(), relatedKey)
this.schemas[related.modelEntity()][`pivot_${relatedId}_${pivotInstance.$entity()}`] = new MorphOne(instance, pivotInstance, relatedId, model.$entity(), relatedKey)

return new MorphToMany(
model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class BelongsToMany extends Relation {
match (relation: string, models: Collection<any>, query: Query<any>): void {
const relatedModels = query.get(false)
const pivotModels = query
.newQuery(this.pivot.$entity())
.newQuery(this.pivot.$modelEntity())
.whereIn(this.relatedPivotKey, this.getKeys(relatedModels, this.relatedKey))
.whereIn(this.foreignPivotKey, this.getKeys(models, this.parentKey))
.groupBy(this.foreignPivotKey, this.relatedPivotKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class MorphToMany extends Relation {
match (relation: string, models: Collection<any>, query: Query<any>): void {
const relatedModels = query.get(false)
const pivotModels = query
.newQuery(this.pivot.$entity())
.newQuery(this.pivot.$modelEntity())
.whereIn(this.relatedId, this.getKeys(relatedModels, this.relatedKey))
.whereIn(this.morphId, this.getKeys(models, this.parentKey))
.groupBy(this.morphId, this.relatedId, this.morphType)
Expand Down
61 changes: 60 additions & 1 deletion packages/pinia-orm/tests/unit/PiniaORM.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { Model, useRepo } from '../../src'
import { Attr, BelongsTo, Num, Str } from '../../src/decorators'
import { Attr, BelongsTo, BelongsToMany, Num, Str } from '../../src/decorators'
import { createPiniaORM } from '../helpers'
import { WeakCache } from '../../src/cache/WeakCache'

Expand Down Expand Up @@ -175,4 +175,63 @@ describe('unit/PiniaORM', () => {
expect(user.user.$storeName()).toBe('orm/users')
expect(user.user.username).toBe(undefined)
})

it('saves correctly with belongsToMany with namespace', () => {
class BaseModel extends Model {
static entity: string
static namespace: string = 'orm'
}

class User extends BaseModel {
@Attr()
declare id: number

@Str(null)
declare name: string

@BelongsToMany(() => Role, () => UserRole, 'user_id', 'role_id')
declare roles: Role[] | null

static entity: string = 'users'
}

class Role extends BaseModel {
@Attr()
declare id: number

@Str(null)
declare name: string

static entity: string = 'roles'
}

class UserRole extends BaseModel {
@Attr()
declare user_id: number

@Attr()
declare role_id: number

static entity = 'user_roles'
static primaryKey = ['user_id', 'role_id']
}

createPiniaORM({ model: { namespace: 'orm' } })

const userData = new User({
id: 1,
name: 'Foo',
roles: [
{ id: 1, name: 'Reader' },
{ id: 2, name: 'Editor' },
],
})

const userRepo = useRepo(User)
userRepo.save(userData)

const user = userRepo.with('roles').find(1)

expect(user?.roles?.length).toBe(2)
})
})