Skip to content

Commit 519681c

Browse files
committed
fix(pinia-orm): DateCast still causing unexpected result
1 parent 9de2e16 commit 519681c

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

packages/pinia-orm/src/model/Model.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,12 @@ export class Model {
998998
if (isArray(value))
999999
return this.serializeArray(value)
10001000

1001-
if (typeof value === 'object')
1002-
return this.serializeObject(value)
1001+
if (typeof value === 'object') {
1002+
if ('toISOString' in value)
1003+
return value.toISOString()
1004+
else
1005+
return this.serializeObject(value)
1006+
}
10031007

10041008
return value
10051009
}

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

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

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

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe('unit/model/Model_Casts_Date', () => {
6060
})
6161

6262
it('should cast before saved into store', () => {
63+
const exspectedIsoDate = new Date('2023-01-26')
64+
6365
class User extends Model {
6466
static entity = 'users'
6567

@@ -70,7 +72,8 @@ describe('unit/model/Model_Casts_Date', () => {
7072
@Cast(() => DateCast) @Attr(null) declare updatedAt: Date
7173

7274
static saving(model: Model) {
73-
model.updatedAt = new Date()
75+
console.log('saving')
76+
model.updatedAt = exspectedIsoDate
7477
}
7578

7679
static casts() {
@@ -88,7 +91,7 @@ describe('unit/model/Model_Casts_Date', () => {
8891

8992
assertState({
9093
users: {
91-
1: { id: 1, updated: exspectedISODate, createdAt: null, updatedAt: null },
94+
1: { id: 1, updated: exspectedISODate, createdAt: null, updatedAt: exspectedIsoDate.toISOString() },
9295
},
9396
})
9497

0 commit comments

Comments
 (0)