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 ☕. |
|
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")
})
})
|
|
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
})
}) |
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
savingandcreatingwork. I want to create some hooks to populate my model fields with custom logic in the following scenarios:I have a hook named
creatingthat populates models with default fields using my logic, and a hook namedsavingwith 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 thecreatinghook 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
creatinghook, 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.
{ "dependencies": { "@mdi/font": "^7.2.96", "pinia": "^2.0.32", "pinia-orm": "^1.5.1", "vue": "^3.2.47", "vue-router": "^4.1.6", "vuetify": "^3.1.10" }, "devDependencies": { "@rushstack/eslint-patch": "^1.2.0", "@types/jsdom": "^21.1.0", "@types/node": "^18.14.2", "@vitejs/plugin-vue": "^4.0.0", "@vitejs/plugin-vue-jsx": "^3.0.0", "@vue/eslint-config-prettier": "^7.1.0", "@vue/eslint-config-typescript": "^11.0.2", "@vue/test-utils": "^2.3.0", "@vue/tsconfig": "^0.1.3", "concurrently": "^7.6.0", "cypress": "^12.7.0", "electron": "^23.1.4", "electron-builder": "^23.6.0", "eslint": "^8.34.0", "eslint-plugin-cypress": "^2.12.1", "eslint-plugin-vue": "^9.9.0", "jsdom": "^21.1.0", "npm-run-all": "^4.1.5", "prettier": "^2.8.4", "start-server-and-test": "^2.0.0", "typescript": "~4.9.5", "vite": "^4.1.4", "vitest": "^0.29.1", "vue-tsc": "^1.2.0", "wait-on": "^7.0.1" } }All reactions