pinia-orm icon indicating copy to clipboard operation
pinia-orm copied to clipboard

incorrect saving of nested objects

Open wormen opened this issue 2 years ago • 2 comments

Environment



Describe the bug

when saving data, where objects are used as a value, saving occurs incorrectly

examples:

model

import { Model } from 'pinia-orm';
import { Attr, Bool, Str } from 'pinia-orm/dist/decorators';
import { Uid } from 'pinia-orm/dist/uuid/v4';

export default class Workspace extends Model {
	static entity = 'workspaces';
	static primaryKey = '_id';

	@Uid() declare _id: string;
	@Str('') declare name: string;
	@Str(null) declare icon: string;
	@Str('') declare description: string;
	@Attr({ active: false, date: '', time: '' }) declare earlyStart: Record<string, any>;
	@Attr({ date: '', time: '' }) declare start: Record<string, any>;
	@Attr({ date: '', time: '' }) declare end: Record<string, any>;
	@Attr({ active: false, date: '', time: '' }) declare reserveTime: Record<string, any>;
}

usage

const w = new Workspace({
  name: 'test',
  start: { date: 'test value', time: 'test value' }
});
workspaceRepo.save(w);
console.log(w);

displayed correctly in the console Снимок экрана от 2023-05-05 14-03-37

but when saving test data there is no Снимок экрана от 2023-05-05 14-04-32

Additional context

No response

Logs

No response

wormen avatar May 05 '23 11:05 wormen

Hello, your model is wron. You need to use the ArrayCast for the object fields.

import { Model } from 'pinia-orm';
import { Attr, Bool, Str, Cast } from 'pinia-orm/dist/decorators';
import { ArrayCast } from 'pinia-orm/dist/casts';
import { Uid } from 'pinia-orm/dist/uuid/v4';

export default class Workspace extends Model {
	static entity = 'workspaces';
	static primaryKey = '_id';

	@Uid() declare _id: string;
	@Str('') declare name: string;
	@Str(null) declare icon: string;
	@Str('') declare description: string;
	@Cast(() => ArrayCast) @Attr({ active: false, date: '', time: '' }) declare earlyStart: Record<string, any>;
	@Cast(() => ArrayCast) @Attr({ date: '', time: '' }) declare start: Record<string, any>;
	@Cast(() => ArrayCast) @Attr({ date: '', time: '' }) declare end: Record<string, any>;
	@Cast(() => ArrayCast) @Attr({ active: false, date: '', time: '' }) declare reserveTime: Record<string, any>;
}

CodeDredd avatar Jun 30 '23 15:06 CodeDredd

@CodeDredd Please forgive the naive question, but why? The name ArrayCast implies, well, an array. I cannot find anything in the docs explaining this need.

joel-schou-maersk avatar Jul 18 '23 17:07 joel-schou-maersk

It's done that way because an object can be more than just a simple object with attributes. there is also the date object and others. I guess that's the reason why....but maybe i find an "autodetect" for this 😉

CodeDredd avatar May 01 '24 20:05 CodeDredd