New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update/Create DateColumn as Unix Timestamp #400
Comments
|
Yes I think this can be additional feature to those decorators. |
|
And here we have a name convention battle again 😆 I don't like an idea of introducing a new decorator @CreateDateColumn()
createdAt: string;
@UpdateDateColumn({ type: "timestamp" })
updatedAt: number;And of course it would be set by default to |
|
nope we won't introduce a new decorator for this. Just type for |
|
Any update on this? |
|
Please check this issue in latest @CreateDateColumn({type: "timestamp"})
createdAt: Date;
@UpdateDateColumn({type: "timestamp"})
updatedAt: Date; |
|
The deserialization still has some problems. timestamp still deserializes to Date even if I go createdAt: number |
|
If this is a UNIX timestamp shouldn't there be the option to use a |
|
I believe OP was asking if they could get the raw timestamp instead of a |
|
Yeah, this is still an issue somewhat. To get the correct unix time from a timestamp, the column has to be selected like this in MySQL. SELECT UNIX_TIMESTAMP(createdAt) FROM round ORDER BY id DESC LIMIT 1 Which will give you the correct result, no matter what the timezone setting is on your MySQL server. Is it possible to setup createdAt and updatedAt to be selected this way, so it'll return a unix timestamp? |
|
I also have the same problem, in my case I'd like to get an ISO8601 string instead of Date for a timestamptz column, e.g.: UPDATE: This is a bit cumbersome though, also I'm not sure of the performance overhead in using a transformer vs something that could be provided natively by typeorm. |
|
Any updates on this? I would like my date_updated and date_created columns in MySQL to be defined as type |
|
You probably want to use native MySQL dates, and mutate to a unix timestamp if/when you need that. |
|
I have the same problem and unfortunatily I can't update the database (it's a shared database between multiple services). I don't know how I can do it without using the classic @column, @beforeInsert and @BeforeUpdate decorators for now |
|
@matthewlilley MySQL and other databases stores date/time values as numbers internally. The OP and others on this thread are asking to read/write date/time values as numbers via TypeORM. Are you seriously suggesting that it's better to ask MySQL to serialize these values as date strings and then parse every single date value retrieved in JavaScript to get it back into a number? |
|
@svicalifornia I understand what you're saying, but typeorm doesn't support this functionality, at least it didn't the last time I checked. You can SELECT UNIX_TIMESTAMP(createdAt), or mutate the date in your own way depending on your needs. I don't save dates as unix in MySQL, but sometimes I want to access a date as unix. |
|
that's what I did, if it can help import { BeforeInsert, BeforeUpdate, Column } from 'typeorm';
import { Type } from 'class-transformer';
export class BaseEntity {
@Column({
type: 'int',
width: 11,
nullable: false,
readonly: true,
default: () => '0',
transformer: {
to: (value?: Date) => (!value ? value : Math.round(value.getTime() / 1000)),
from: (value?: number) => (!value ? value : new Date(value * 1000))
}
})
@Type(() => Date)
createdAt: Date;
@Column({
type: 'int',
width: 11,
nullable: true,
default: () => null,
transformer: {
to: (value?: Date) => (!value ? value : Math.round(value.getTime() / 1000)),
from: (value?: number) => (!value ? value : new Date(value * 1000))
}
})
@Type(() => Date)
updatedAt?: Date;
@BeforeInsert()
updateDateCreation() {
this.createdAt = new Date();
}
@BeforeUpdate()
updateDateUpdate() {
this.updatedAt = new Date();
}
} |
|
I tried to use the construct by @dhessler as in specifying the type explicitly and using a transformer, but typeorm is complaining that postgres doesn't support |
|
Any update on this? |
|
I don't know if it's helpful, but this works for me: |
|
@leandrofreire08 solutions works for me too, at least for a mysql db I would say... I saw other posts that proposes () => "now()" |
|
I use the following for unix timestamps on sqlite |
|
Here my solution; import {
Column,
PrimaryGeneratedColumn,
BeforeUpdate,
BeforeInsert,
BeforeRemove,
} from 'typeorm';
export class AppEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'int', width: 11, nullable: true, readonly: true })
deleted_at: number;
@Column({ type: 'int', width: 11, nullable: false, readonly: true })
created_at: number;
@Column({ type: 'int', width: 11, nullable: true })
updated_at: number;
@BeforeUpdate()
public setUpdatedAt() {
this.updated_at = Math.floor(Date.now() / 1000);
}
@BeforeInsert()
public setCreatedAt() {
this.created_at = Math.floor(Date.now() / 1000);
}
@BeforeRemove()
public setDeletedAt() {
this.deleted_at = Math.floor(Date.now() / 1000);
}
} |
I am currently working with a database which has a
created_atandupdated_atcolumn formatted as a unix timestamp.It would be pretty easy to simply write those on every write/update I do but this would pretty much defeat the purpose of a orm right? So basically wouldn't it be nice if there would also be a option to let those decorators update the columns as a timestamp instead of a
Dateobject?I guess it could either work by overwriting the type to a number of create another decorator called something like
UpdateTimestampColumn&CreateTimestampColumn?The text was updated successfully, but these errors were encountered: