incorrect saving of nested objects
Environment
- Operating System: Linux
- Node Version: v18.16.0
- Nuxt Version: 3.4.3
- Nitro Version: 2.3.3
- Package Manager: [email protected]
- Builder: vite
- User Config: ssr, telemetry, runtimeConfig, app, appConfig, typescript, modules, lodash, buildModules, components, imports, alias, pwa, build, css, vite
- Runtime Modules: @pinia/[email protected], @pinia-orm/[email protected], @vueuse/[email protected], [email protected]
- Build Modules: @pinia/[email protected], @pinia-orm/[email protected], unplugin-icons/nuxt
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

but when saving test data there is no

Additional context
No response
Logs
No response
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 Please forgive the naive question, but why? The name ArrayCast implies, well, an array. I cannot find anything in the docs explaining this need.
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 😉