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
7 changes: 6 additions & 1 deletion packages/pinia-orm/src/schema/Schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Schema as NormalizrSchema } from '@pinia-orm/normalizr'
import { schema as Normalizr } from '@pinia-orm/normalizr'
import { isArray, isNullish } from '../support/Utils'
import { isArray, isNullish, throwError } from '../support/Utils'
import { Uid } from '../model/attributes/types/Uid'
import { Relation } from '../model/attributes/relations/Relation'
import type { Model } from '../model/Model'
Expand Down Expand Up @@ -121,6 +121,11 @@ export class Schema {
if (isNullish(record[key])) { record[key] = uidFields[key].setKey(key).make(record[key]) }
}

// Check if a list is passed to a one to one relation and throws a error if so
if (['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo'].includes(parent.$fields()[key]?.constructor.name ?? '') && isArray(parentRecord[key])) {
throwError(['You are passing a list to "', `${parent.$entity()}.${key}`, `" which is a one to one Relation(${parent.$fields()[key]?.constructor.name}):`, JSON.stringify(parentRecord[key])])
}

// Finally, obtain the index id, attach it to the current record at the
// special `__id` key. The `__id` key is used when we try to retrieve
// the models via the `revive` method using the data that is currently
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from 'vitest'
import { describe, it, expect } from 'vitest'

import { Model, useRepo } from '../../../src'
import { Attr, BelongsTo, Str } from '../../../src/decorators'
Expand Down Expand Up @@ -62,6 +62,18 @@ describe('feature/relations/belongs_to_save', () => {
})
})

it('throws a error if a list is passed to the relation', () => {
const postsRepo = useRepo(Post)

expect(() => {
postsRepo.save({
id: 1,
title: 'Title 01',
author: [{ id: 1, name: 'John Doe' }]
})
}).toThrowError('[Pinia ORM] You are passing a list to " posts.author " which is a one to one Relation(BelongsTo): [{"id":1,"name":"John Doe"}]')
})

it('can insert a record with missing relational key', () => {
const postsRepo = useRepo(Post)

Expand Down
18 changes: 17 additions & 1 deletion packages/pinia-orm/tests/feature/relations/has_one_save.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from 'vitest'
import { describe, it, expect } from 'vitest'

import { Model, useRepo } from '../../../src'
import { Attr, HasOne, Str } from '../../../src/decorators'
Expand Down Expand Up @@ -46,6 +46,22 @@ describe('feature/relations/has_one_save', () => {
})
})

it('throws a error if a list is passed to the relation', () => {
const usersRepo = useRepo(User)

expect(() => {
usersRepo.save({
id: 1,
name: 'John Doe',
phone: [{
id: 1,
userId: 1,
number: '123-4567-8912'
}]
})
}).toThrowError('[Pinia ORM] You are passing a list to " users.phone " which is a one to one Relation(HasOne): [{"id":1,"userId":1,"number":"123-4567-8912"}]')
})

it('generates missing foreign key', () => {
const usersRepo = useRepo(User)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from 'vitest'
import { describe, it, expect } from 'vitest'

import { Model, useRepo } from '../../../src'
import { MorphOne, Num, Str } from '../../../src/decorators'
Expand Down Expand Up @@ -53,6 +53,23 @@ describe('feature/relations/morph_one_save', () => {
})
})

it('throws a error if a list is passed to the relation', () => {
const usersRepo = useRepo(User)

expect(() => {
usersRepo.save({
id: 1,
name: 'John Doe',
image: [{
id: 1,
url: '/profile.jpg',
imageableId: 1,
imageableType: 'users'
}]
})
}).toThrowError('[Pinia ORM] You are passing a list to " users.image " which is a one to one Relation(MorphOne): [{"id":1,"url":"/profile.jpg","imageableId":1,"imageableType":"users"}]')
})

it('generates missing parent id', () => {
const usersRepo = useRepo(User)

Expand Down
16 changes: 15 additions & 1 deletion packages/pinia-orm/tests/feature/relations/morph_to_save.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from 'vitest'
import { describe, it, expect } from 'vitest'

import { Model, useRepo } from '../../../src'
import { Attr, MorphTo, Num, Str } from '../../../src/decorators'
Expand Down Expand Up @@ -47,6 +47,20 @@ describe('feature/relations/morph_to_save', () => {
})
})

it('throws a error if a list is passed to the relation', () => {
const imagesRepo = useRepo(Image)

expect(() => {
imagesRepo.save({
id: 1,
url: '/profile.jpg',
imageableId: 1,
imageableType: 'users',
imageable: [{ id: 2, name: 'John Doe' }]
})
}).toThrowError('[Pinia ORM] You are passing a list to " images.imageable " which is a one to one Relation(MorphTo): [{"id":2,"name":"John Doe"}]')
})

it('generates missing relational key', () => {
const imagesRepo = useRepo(Image)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Model, useRepo } from '../../src'
import { Num, Str } from '../../src/decorators'

/* eslint-disable vue/one-component-per-file */
describe('performance/prevent_rerender_of_child_components', () => {
describe.skip('performance/prevent_rerender_of_child_components', () => {
class Post extends Model {
static entity = 'posts'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Model, useRepo } from '../../src'
import { Attr, BelongsToMany, Num, Str } from '../../src/decorators'

/* eslint-disable no-console */
describe('performance/save_belongs_to_many_relation.spec', () => {
describe.skip('performance/save_belongs_to_many_relation.spec', () => {
class Role extends Model {
static entity = 'roles'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Model, useRepo } from '../../src'
import { HasMany, Num, Str } from '../../src/decorators'

/* eslint-disable no-console */
describe('performance/save_has_many_relation', () => {
describe.skip('performance/save_has_many_relation', () => {
class Post extends Model {
static entity = 'posts'

Expand Down
2 changes: 1 addition & 1 deletion packages/pinia-orm/tests/performance/speed_find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ref } from 'vue-demi'
import { Model, useRepo } from '../../src'

/* eslint-disable no-console */
describe('performance/save_has_many_relation', () => {
describe.skip('performance/save_has_many_relation', () => {
class Todo extends Model {
static entity = 'todos'

Expand Down