mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import {BaseEntity, Column} from "typeorm";
|
|
import {ApiError} from 'app/common/ApiError';
|
|
import {CommonProperties} from "app/common/UserAPI";
|
|
|
|
export class Resource extends BaseEntity {
|
|
@Column({type: String})
|
|
public name: string;
|
|
|
|
@Column({name: 'created_at', type: Date, default: () => "CURRENT_TIMESTAMP"})
|
|
public createdAt: Date;
|
|
|
|
@Column({name: 'updated_at', type: Date, default: () => "CURRENT_TIMESTAMP"})
|
|
public updatedAt: Date;
|
|
|
|
// a computed column which, when present, means the entity should be filtered out
|
|
// of results.
|
|
@Column({name: 'filtered_out', type: 'boolean', select: false, insert: false})
|
|
public filteredOut?: boolean;
|
|
|
|
public updateFromProperties(props: Partial<CommonProperties>) {
|
|
if (props.createdAt) { this.createdAt = _propertyToDate(props.createdAt); }
|
|
if (props.updatedAt) {
|
|
this.updatedAt = _propertyToDate(props.updatedAt);
|
|
} else {
|
|
this.updatedAt = new Date();
|
|
}
|
|
if (props.name) { this.name = props.name; }
|
|
}
|
|
|
|
protected checkProperties(props: any, keys: string[]): props is Partial<CommonProperties> {
|
|
for (const key of Object.keys(props)) {
|
|
if (!keys.includes(key)) {
|
|
throw new ApiError(`unrecognized property ${key}`, 400);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Ensure iso-string-or-date value is converted to a date.
|
|
function _propertyToDate(d: string|Date): Date {
|
|
if (typeof(d) === 'string') {
|
|
return new Date(d);
|
|
}
|
|
return d;
|
|
}
|