Replies: 3 comments 27 replies
-
Help is on the way #1107. Righ now it's not possible to have what you need. Even if you use You can thank me if you like with a cup of ☕. |
Beta Was this translation helpful? Give feedback.
-
It seems like we have a problem - the
import { Model } from "pinia-orm"
import { Attr, Cast, Str, Uid } from "pinia-orm/dist/decorators"
import { DateCast } from "pinia-orm/dist/casts"
class User extends Model {
static entity = "user_profiles"
@Uid() declare id: string
@Str("") declare name: string
@Cast(() => DateCast) @Attr(null) declare createdAt: Date
@Cast(() => DateCast) @Attr(null) declare updatedAt: Date
static creating(model: Model) {
console.log("creating")
if (model.createdAt == null) {
model.createdAt = new Date()
}
if (!model.name) {
model.name = "set some name"
}
}
static updating(model: Model) {
console.log("updating")
model.updatedAt = new Date()
}
static saving(model: Model) {
console.log("saving")
model.updatedAt = new Date()
}
}
export { User } I encountered an error that I did not expect:
import { afterEach, beforeEach, describe, expect, it } from "vitest"
import { createPinia, type Pinia } from "pinia"
import { createORM, Model, Repository, useRepo } from "pinia-orm"
import { User } from "./User"
describe("User", () => {
// Define some variables that will be used in the tests
let pinia: Pinia
let userProfileRepo: Repository<User>
beforeEach(() => {
pinia = createPinia().use(createORM())
userProfileRepo = useRepo(User, pinia)
})
afterEach(async () => {
Model.clearRegistries()
})
it("userProfileRepo.new(true)", () => {
expect(userProfileRepo.all().length).toBe(0) // double check
const user = userProfileRepo.new(true)!
expect(userProfileRepo.all().length).toBe(1) // should save
expect(user.createdAt).toBeInstanceOf(Date)
expect(user.updatedAt).toBeInstanceOf(Date)
expect(user.name).toBe("set some name")
})
it("userProfileRepo.new(false), broken test", () => {
expect(userProfileRepo.all().length).toBe(0) // double check
const user = userProfileRepo.new(false)!
expect(userProfileRepo.all().length).toBe(0) // should not save
expect(user.createdAt).toBeInstanceOf(Date)
expect(user.updatedAt).toBeNull() // test broken is here
expect(user.name).toBe("set some name")
})
})
|
Beta Was this translation helpful? Give feedback.
-
I also encountered a strange error with another test, and I'm not sure if it's related to the new feature or not.
import { Model } from "pinia-orm"
import { Attr, Cast, Str, Uid } from "pinia-orm/dist/decorators"
import { DateCast } from "pinia-orm/dist/casts"
class User extends Model {
static entity = "user_profiles"
@Uid() declare id: string
@Str("") declare name: string
@Cast(() => DateCast) @Attr(null) declare createdAt: Date
@Cast(() => DateCast) @Attr(null) declare updatedAt: Date
static creating(model: Model) {
console.log("creating")
if (model.createdAt == null) {
model.createdAt = new Date()
}
if (!model.name) {
model.name = "set some name"
}
}
static updating(model: Model) {
console.log("updating")
model.updatedAt = new Date()
}
static saving(model: Model) {
console.log("saving")
model.updatedAt = new Date()
}
}
export { User }
It happens on a code: class DateCast extends CastAttribute {
/**
* Create a new String attribute instance.
*/
constructor(attributes) {
super(attributes);
}
get(value) {
return value === null ? null : new Date(value);
}
/**
* Make the value for the attribute.
*/
set(value) {
if (value === null)
return null;
return (typeof value === "string" ? new Date(Date.parse(value)) : value).toISOString(); // <<<<<== HERE
}
}
import { afterEach, beforeEach, describe, expect, it } from "vitest"
import { createPinia, type Pinia } from "pinia"
import { createORM, Model, Repository, useRepo } from "pinia-orm"
import { User } from "./User"
describe("User", () => {
// Define some variables that will be used in the tests
let pinia: Pinia
let userProfileRepo: Repository<User>
beforeEach(() => {
pinia = createPinia().use(createORM())
userProfileRepo = useRepo(User, pinia)
})
afterEach(async () => {
Model.clearRegistries()
})
it("userProfileRepo.new(true): error with toISOString is not a function", () => {
expect(userProfileRepo.all().length).toBe(0) // double check
const user = userProfileRepo.new(true)!
expect(userProfileRepo.all().length).toBe(1)
expect(user.createdAt).toBeInstanceOf(Date)
expect(user.updatedAt).toBeInstanceOf(Date)
expect(user.name).toBe("set some name")
userProfileRepo.where("id", user.id).update({ name: "Modified name" }) ///<< ERROR IS HERE
})
it("userProfileRepo.new(false): error with toISOString is not a function", () => {
expect(userProfileRepo.all().length).toBe(0) // double check
const user = userProfileRepo.new(false)!
expect(userProfileRepo.all().length).toBe(0)
expect(user.createdAt).toBeInstanceOf(Date)
expect(user.updatedAt).toBeInstanceOf(Date)
expect(user.name).toBe("set some name")
userProfileRepo.save(user)
expect(userProfileRepo.all().length).toBe(1)
userProfileRepo.where("id", user.id).update({ name: "Modified name" }) ///<< ERROR IS HERE
})
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I completely do not understand how hooks like
saving
andcreating
work. I want to create some hooks to populate my model fields with custom logic in the following scenarios:I have a hook named
creating
that populates models with default fields using my logic, and a hook namedsaving
with some logic too. Please note that the code I provided is only a simple example. In reality, I have more complex logic that I need to implement.What I did:
const user = useRepo(User).new()
, all of my hooks fire but the models are saved, but I don't need them to be saved. This seems to fulfill all of my needs, but without saving the instance.const user = useRepo(User).make()
, none of my hooks seem to fire, even though I expect thecreating
hook to be fired.I am trying to find a way to create a new instance of a model in Pinia-ORM without saving it, while still firing the
creating
hook, similar to usinguseRepo(User).new()
. Is there a way to accomplish this using Pinia-ORM?P.S.
I have read all the documentation, issues, Google searches, etc. multiple times over the past two days, but I didn't find any instructions.
Beta Was this translation helpful? Give feedback.
All reactions