Skip to content

Commit 2caa97a

Browse files
committed
fix(pinia-orm): DateCast with null causes type error with empty object
1 parent 23583e9 commit 2caa97a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

packages/pinia-orm/src/model/casts/DateCast.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export class DateCast extends CastAttribute {
2121
if (value === null)
2222
return null
2323

24-
return (typeof value === 'string' ? new Date(Date.parse(value)) : value).toISOString()
24+
const newDate = ((typeof value === 'string') ? new Date(Date.parse(value)) : value)
25+
26+
return 'toISOString' in newDate ? newDate.toISOString() : null
2527
}
2628
}

packages/pinia-orm/tests/unit/model/Model_Casts_Date.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,15 @@ describe('unit/model/Model_Casts_Date', () => {
6363
class User extends Model {
6464
static entity = 'users'
6565

66-
@Attr(0) id!: number
67-
@Attr('') updated!: Date
66+
@Attr(0) declare id: number
67+
@Attr('') declare updated: Date
68+
69+
@Cast(() => DateCast) @Attr(null) declare createdAt: Date
70+
@Cast(() => DateCast) @Attr(null) declare updatedAt: Date
71+
72+
static saving(model: Model) {
73+
model.updatedAt = new Date()
74+
}
6875

6976
static casts() {
7077
return {
@@ -81,7 +88,7 @@ describe('unit/model/Model_Casts_Date', () => {
8188

8289
assertState({
8390
users: {
84-
1: { id: 1, updated: exspectedISODate },
91+
1: { id: 1, updated: exspectedISODate, createdAt: null, updatedAt: null },
8592
},
8693
})
8794

0 commit comments

Comments
 (0)