immich/server/src/schema/tables/asset.table.ts

145 lines
4.1 KiB
TypeScript
Raw Normal View History

import { UpdatedAtTrigger, UpdateIdColumn } from 'src/decorators';
import { AssetStatus, AssetType, AssetVisibility } from 'src/enum';
2025-05-07 17:14:20 -04:00
import { asset_visibility_enum, assets_status_enum } from 'src/schema/enums';
import { assets_delete_audit } from 'src/schema/functions';
2025-03-29 09:26:24 -04:00
import { LibraryTable } from 'src/schema/tables/library.table';
import { StackTable } from 'src/schema/tables/stack.table';
import { UserTable } from 'src/schema/tables/user.table';
2025-03-28 10:40:09 -04:00
import {
AfterDeleteTrigger,
2025-03-28 10:40:09 -04:00
Column,
CreateDateColumn,
DeleteDateColumn,
ForeignKeyColumn,
2025-06-30 13:19:16 -04:00
Generated,
2025-03-28 10:40:09 -04:00
Index,
PrimaryGeneratedColumn,
Table,
2025-06-30 13:19:16 -04:00
Timestamp,
2025-03-28 10:40:09 -04:00
UpdateDateColumn,
} from 'src/sql-tools';
2025-04-18 23:39:56 +02:00
import { ASSET_CHECKSUM_CONSTRAINT } from 'src/utils/database';
2025-03-28 10:40:09 -04:00
@Table('assets')
@UpdatedAtTrigger('assets_updated_at')
@AfterDeleteTrigger({
scope: 'statement',
function: assets_delete_audit,
referencingOldTableAs: 'old',
when: 'pg_trigger_depth() = 0',
})
2025-03-28 10:40:09 -04:00
// Checksums must be unique per user and library
@Index({
name: ASSET_CHECKSUM_CONSTRAINT,
columns: ['ownerId', 'checksum'],
unique: true,
where: '("libraryId" IS NULL)',
})
@Index({
name: 'UQ_assets_owner_library_checksum' + '',
columns: ['ownerId', 'libraryId', 'checksum'],
unique: true,
where: '("libraryId" IS NOT NULL)',
})
@Index({
name: 'idx_local_date_time',
expression: `(("localDateTime" at time zone 'UTC')::date)`,
})
2025-03-28 10:40:09 -04:00
@Index({
name: 'idx_local_date_time_month',
expression: `(date_trunc('MONTH'::text, ("localDateTime" AT TIME ZONE 'UTC'::text)) AT TIME ZONE 'UTC'::text)`,
})
@Index({ name: 'IDX_originalPath_libraryId', columns: ['originalPath', 'libraryId'] })
@Index({ name: 'IDX_asset_id_stackId', columns: ['id', 'stackId'] })
@Index({
name: 'idx_originalfilename_trigram',
2025-03-28 10:40:09 -04:00
using: 'gin',
expression: 'f_unaccent("originalFileName") gin_trgm_ops',
2025-03-28 10:40:09 -04:00
})
// For all assets, each originalpath must be unique per user and library
export class AssetTable {
@PrimaryGeneratedColumn()
2025-06-30 13:19:16 -04:00
id!: Generated<string>;
2025-03-28 10:40:09 -04:00
@Column()
deviceAssetId!: string;
@ForeignKeyColumn(() => UserTable, { onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: false })
ownerId!: string;
@Column()
deviceId!: string;
@Column()
type!: AssetType;
@Column()
originalPath!: string;
2025-04-17 14:41:06 -04:00
@Column({ type: 'timestamp with time zone', indexName: 'idx_asset_file_created_at' })
2025-06-30 13:19:16 -04:00
fileCreatedAt!: Timestamp;
2025-03-28 10:40:09 -04:00
@Column({ type: 'timestamp with time zone' })
2025-06-30 13:19:16 -04:00
fileModifiedAt!: Timestamp;
2025-03-28 10:40:09 -04:00
@Column({ type: 'boolean', default: false })
2025-06-30 13:19:16 -04:00
isFavorite!: Generated<boolean>;
2025-03-28 10:40:09 -04:00
@Column({ type: 'character varying', nullable: true })
duration!: string | null;
2025-03-28 10:40:09 -04:00
@Column({ type: 'character varying', nullable: true, default: '' })
encodedVideoPath!: string | null;
2025-03-28 10:40:09 -04:00
2025-04-17 14:41:06 -04:00
@Column({ type: 'bytea', index: true })
2025-03-28 10:40:09 -04:00
checksum!: Buffer; // sha1 checksum
@ForeignKeyColumn(() => AssetTable, { nullable: true, onUpdate: 'CASCADE', onDelete: 'SET NULL' })
livePhotoVideoId!: string | null;
@UpdateDateColumn()
2025-06-30 13:19:16 -04:00
updatedAt!: Generated<Timestamp>;
@CreateDateColumn()
2025-06-30 13:19:16 -04:00
createdAt!: Generated<Timestamp>;
2025-04-17 14:41:06 -04:00
@Column({ index: true })
2025-03-28 10:40:09 -04:00
originalFileName!: string;
@Column({ nullable: true })
sidecarPath!: string | null;
@Column({ type: 'bytea', nullable: true })
thumbhash!: Buffer | null;
@Column({ type: 'boolean', default: false })
2025-06-30 13:19:16 -04:00
isOffline!: Generated<boolean>;
@ForeignKeyColumn(() => LibraryTable, { onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: true })
2025-06-30 13:19:16 -04:00
libraryId!: string | null;
@Column({ type: 'boolean', default: false })
2025-06-30 13:19:16 -04:00
isExternal!: Generated<boolean>;
@DeleteDateColumn()
2025-06-30 13:19:16 -04:00
deletedAt!: Timestamp | null;
@Column({ type: 'timestamp with time zone' })
2025-06-30 13:19:16 -04:00
localDateTime!: Timestamp;
2025-03-28 10:40:09 -04:00
@ForeignKeyColumn(() => StackTable, { nullable: true, onDelete: 'SET NULL', onUpdate: 'CASCADE' })
2025-06-30 13:19:16 -04:00
stackId!: string | null;
2025-03-28 10:40:09 -04:00
2025-04-17 14:41:06 -04:00
@Column({ type: 'uuid', nullable: true, indexName: 'IDX_assets_duplicateId' })
2025-03-28 10:40:09 -04:00
duplicateId!: string | null;
@Column({ enum: assets_status_enum, default: AssetStatus.ACTIVE })
2025-06-30 13:19:16 -04:00
status!: Generated<AssetStatus>;
2025-04-17 14:41:06 -04:00
@UpdateIdColumn({ indexName: 'IDX_assets_update_id' })
2025-06-30 13:19:16 -04:00
updateId!: Generated<string>;
@Column({ enum: asset_visibility_enum, default: AssetVisibility.TIMELINE })
2025-06-30 13:19:16 -04:00
visibility!: Generated<AssetVisibility>;
2025-03-28 10:40:09 -04:00
}