forked from Cutlery/immich
Compare commits
4 Commits
main
...
feat/prism
Author | SHA1 | Date | |
---|---|---|---|
|
388e132fca | ||
|
43e0a59f93 | ||
|
da34bd714e | ||
|
77da10e3ab |
@ -10,13 +10,17 @@ RUN npm ci && \
|
||||
rm -rf node_modules/@img/sharp-libvips* && \
|
||||
rm -rf node_modules/@img/sharp-linuxmusl-x64
|
||||
COPY server .
|
||||
|
||||
WORKDIR /usr/src/app/server
|
||||
RUN npm run prisma:generate
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
ENV PATH="${PATH}:/usr/src/app/bin" \
|
||||
NODE_ENV=development \
|
||||
NVIDIA_DRIVER_CAPABILITIES=all \
|
||||
NVIDIA_VISIBLE_DEVICES=all
|
||||
ENTRYPOINT ["tini", "--", "/bin/sh"]
|
||||
|
||||
|
||||
FROM dev AS prod
|
||||
|
||||
RUN npm run build
|
||||
|
2427
server/package-lock.json
generated
2427
server/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -30,7 +30,8 @@
|
||||
"typeorm:migrations:revert": "typeorm migration:revert -d ./dist/database.config.js",
|
||||
"typeorm:schema:drop": "typeorm query -d ./dist/database.config.js 'DROP schema public cascade; CREATE schema public;'",
|
||||
"typeorm:schema:reset": "npm run typeorm:schema:drop && npm run typeorm:migrations:run",
|
||||
"sql:generate": "node ./dist/utils/sql.js"
|
||||
"sql:generate": "node ./dist/utils/sql.js",
|
||||
"prisma:generate": "prisma generate --schema=./src/prisma/schema.prisma"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.22.11",
|
||||
@ -49,6 +50,7 @@
|
||||
"@opentelemetry/auto-instrumentations-node": "^0.43.0",
|
||||
"@opentelemetry/exporter-prometheus": "^0.49.0",
|
||||
"@opentelemetry/sdk-node": "^0.49.0",
|
||||
"@prisma/client": "^5.11.0",
|
||||
"@socket.io/postgres-adapter": "^0.3.1",
|
||||
"@types/picomatch": "^2.3.3",
|
||||
"archiver": "^7.0.0",
|
||||
@ -70,6 +72,7 @@
|
||||
"ioredis": "^5.3.2",
|
||||
"joi": "^17.10.0",
|
||||
"js-yaml": "^4.1.0",
|
||||
"kysely": "^0.27.3",
|
||||
"lodash": "^4.17.21",
|
||||
"luxon": "^3.4.2",
|
||||
"mnemonist": "^0.39.8",
|
||||
@ -78,6 +81,7 @@
|
||||
"openid-client": "^5.4.3",
|
||||
"pg": "^8.11.3",
|
||||
"picomatch": "^4.0.0",
|
||||
"prisma-extension-kysely": "^2.1.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rxjs": "^7.8.1",
|
||||
"sanitize-filename": "^1.6.3",
|
||||
@ -120,6 +124,8 @@
|
||||
"mock-fs": "^5.2.0",
|
||||
"prettier": "^3.0.2",
|
||||
"prettier-plugin-organize-imports": "^3.2.3",
|
||||
"prisma": "^5.11.0",
|
||||
"prisma-kysely": "^1.8.0",
|
||||
"rimraf": "^5.0.1",
|
||||
"source-map-support": "^0.5.21",
|
||||
"sql-formatter": "^15.0.0",
|
||||
|
@ -123,6 +123,7 @@ import { TrashService } from 'src/services/trash.service';
|
||||
import { UserService } from 'src/services/user.service';
|
||||
import { otelConfig } from 'src/utils/instrumentation';
|
||||
import { ImmichLogger } from 'src/utils/logger';
|
||||
import { PrismaRepository } from './repositories/prisma.repository';
|
||||
|
||||
const commands = [
|
||||
ResetAdminPasswordCommand,
|
||||
@ -221,6 +222,7 @@ const repositories: Provider[] = [
|
||||
{ provide: IMediaRepository, useClass: MediaRepository },
|
||||
{ provide: IUserRepository, useClass: UserRepository },
|
||||
{ provide: IUserTokenRepository, useClass: UserTokenRepository },
|
||||
PrismaRepository,
|
||||
];
|
||||
|
||||
const middleware = [
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { AssetOrder } from 'src/entities/album.entity';
|
||||
import { AssetJobStatusEntity } from 'src/entities/asset-job-status.entity';
|
||||
import { AssetEntity, AssetType } from 'src/entities/asset.entity';
|
||||
@ -78,22 +79,6 @@ export interface TimeBucketItem {
|
||||
count: number;
|
||||
}
|
||||
|
||||
export type AssetCreate = Pick<
|
||||
AssetEntity,
|
||||
| 'deviceAssetId'
|
||||
| 'ownerId'
|
||||
| 'libraryId'
|
||||
| 'deviceId'
|
||||
| 'type'
|
||||
| 'originalPath'
|
||||
| 'fileCreatedAt'
|
||||
| 'localDateTime'
|
||||
| 'fileModifiedAt'
|
||||
| 'checksum'
|
||||
| 'originalFileName'
|
||||
> &
|
||||
Partial<AssetEntity>;
|
||||
|
||||
export type AssetWithoutRelations = Omit<
|
||||
AssetEntity,
|
||||
| 'livePhotoVideo'
|
||||
@ -109,6 +94,22 @@ export type AssetWithoutRelations = Omit<
|
||||
| 'tags'
|
||||
>;
|
||||
|
||||
export type AssetCreate = Pick<
|
||||
AssetEntity,
|
||||
| 'deviceAssetId'
|
||||
| 'ownerId'
|
||||
| 'libraryId'
|
||||
| 'deviceId'
|
||||
| 'type'
|
||||
| 'originalPath'
|
||||
| 'fileCreatedAt'
|
||||
| 'localDateTime'
|
||||
| 'fileModifiedAt'
|
||||
| 'checksum'
|
||||
| 'originalFileName'
|
||||
> &
|
||||
Partial<AssetWithoutRelations>;
|
||||
|
||||
export type AssetUpdateOptions = Pick<AssetWithoutRelations, 'id'> & Partial<AssetWithoutRelations>;
|
||||
|
||||
export type AssetUpdateAllOptions = Omit<Partial<AssetWithoutRelations>, 'id'>;
|
||||
@ -139,18 +140,13 @@ export const IAssetRepository = 'IAssetRepository';
|
||||
|
||||
export interface IAssetRepository {
|
||||
create(asset: AssetCreate): Promise<AssetEntity>;
|
||||
getByDate(ownerId: string, date: Date): Promise<AssetEntity[]>;
|
||||
getByIds(
|
||||
ids: string[],
|
||||
relations?: FindOptionsRelations<AssetEntity>,
|
||||
select?: FindOptionsSelect<AssetEntity>,
|
||||
): Promise<AssetEntity[]>;
|
||||
getByIds(ids: string[], relations?: Prisma.AssetsInclude): Promise<AssetEntity[]>;
|
||||
getByIdsWithAllRelations(ids: string[]): Promise<AssetEntity[]>;
|
||||
getByDayOfYear(ownerIds: string[], monthDay: MonthDay): Promise<AssetEntity[]>;
|
||||
getByChecksum(userId: string, checksum: Buffer): Promise<AssetEntity | null>;
|
||||
getByAlbumId(pagination: PaginationOptions, albumId: string): Paginated<AssetEntity>;
|
||||
getByUserId(pagination: PaginationOptions, userId: string, options?: AssetSearchOptions): Paginated<AssetEntity>;
|
||||
getById(id: string, relations?: FindOptionsRelations<AssetEntity>): Promise<AssetEntity | null>;
|
||||
getById(id: string, relations?: Prisma.AssetsInclude): Promise<AssetEntity | null>;
|
||||
getWithout(pagination: PaginationOptions, property: WithoutProperty): Paginated<AssetEntity>;
|
||||
getWith(pagination: PaginationOptions, property: WithProperty, libraryId?: string): Paginated<AssetEntity>;
|
||||
getRandom(userId: string, count: number): Promise<AssetEntity[]>;
|
||||
@ -162,7 +158,7 @@ export interface IAssetRepository {
|
||||
getAll(pagination: PaginationOptions, options?: AssetSearchOptions): Paginated<AssetEntity>;
|
||||
getAllByDeviceId(userId: string, deviceId: string): Promise<string[]>;
|
||||
updateAll(ids: string[], options: Partial<AssetUpdateAllOptions>): Promise<void>;
|
||||
update(asset: AssetUpdateOptions): Promise<void>;
|
||||
update(asset: AssetUpdateOptions): Promise<AssetEntity>;
|
||||
remove(asset: AssetEntity): Promise<void>;
|
||||
softDeleteAll(ids: string[]): Promise<void>;
|
||||
restoreAll(ids: string[]): Promise<void>;
|
||||
|
@ -175,7 +175,7 @@ export type SmartSearchOptions = SearchDateOptions &
|
||||
export interface FaceEmbeddingSearch extends SearchEmbeddingOptions {
|
||||
hasPerson?: boolean;
|
||||
numResults: number;
|
||||
maxDistance?: number;
|
||||
maxDistance: number;
|
||||
}
|
||||
|
||||
export interface FaceSearchResult {
|
||||
@ -188,7 +188,7 @@ export interface ISearchRepository {
|
||||
searchMetadata(pagination: SearchPaginationOptions, options: AssetSearchOptions): Paginated<AssetEntity>;
|
||||
searchSmart(pagination: SearchPaginationOptions, options: SmartSearchOptions): Paginated<AssetEntity>;
|
||||
searchFaces(search: FaceEmbeddingSearch): Promise<FaceSearchResult[]>;
|
||||
upsert(smartInfo: Partial<SmartInfoEntity>, embedding?: Embedding): Promise<void>;
|
||||
upsert(assetId: string, embedding: number[]): Promise<void>;
|
||||
searchPlaces(placeName: string): Promise<GeodataPlacesEntity[]>;
|
||||
getAssetsByCity(userIds: string[]): Promise<AssetEntity[]>;
|
||||
deleteAllSearchEmbeddings(): Promise<void>;
|
||||
|
@ -71,6 +71,18 @@ async function bootstrapApi() {
|
||||
logger.log(`Immich Server is listening on ${await app.getUrl()} [v${serverVersion}] [${envName}] `);
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface BigInt {
|
||||
toJSON(): number | string;
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
BigInt.prototype.toJSON = function () {
|
||||
return this.valueOf() > MAX_SAFE_INTEGER ? this.toString() : Number(this.valueOf());
|
||||
};
|
||||
|
||||
const immichApp = process.argv[2] || process.env.IMMICH_APP;
|
||||
|
||||
if (process.argv[2] === immichApp) {
|
||||
|
28
server/src/prisma/find-non-deleted.ts
Normal file
28
server/src/prisma/find-non-deleted.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
const excludeDeleted = ({ args, query }: { args: any; query: any }) => {
|
||||
if (args.where === undefined) {
|
||||
args.where = { deletedAt: null };
|
||||
} else if (args.where.deletedAt === undefined) {
|
||||
args.where.deletedAt = null;
|
||||
}
|
||||
|
||||
return query(args);
|
||||
};
|
||||
|
||||
const findNonDeleted = {
|
||||
findFirst: excludeDeleted,
|
||||
findFirstOrThrow: excludeDeleted,
|
||||
findMany: excludeDeleted,
|
||||
findUnique: excludeDeleted,
|
||||
findUniqueOrThrow: excludeDeleted,
|
||||
};
|
||||
|
||||
export const findNonDeletedExtension = Prisma.defineExtension({
|
||||
query: {
|
||||
albums: findNonDeleted,
|
||||
assets: findNonDeleted,
|
||||
libraries: findNonDeleted,
|
||||
users: findNonDeleted,
|
||||
},
|
||||
});
|
292
server/src/prisma/generated/types.ts
Normal file
292
server/src/prisma/generated/types.ts
Normal file
@ -0,0 +1,292 @@
|
||||
import type { ColumnType } from "kysely";
|
||||
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
|
||||
? ColumnType<S, I | undefined, U>
|
||||
: ColumnType<T, T | undefined, T>;
|
||||
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
|
||||
|
||||
export type Activity = {
|
||||
id: Generated<string>;
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
albumId: string;
|
||||
userId: string;
|
||||
assetId: string | null;
|
||||
comment: string | null;
|
||||
isLiked: Generated<boolean>;
|
||||
};
|
||||
export type Albums = {
|
||||
id: Generated<string>;
|
||||
ownerId: string;
|
||||
albumName: Generated<string>;
|
||||
createdAt: Generated<Timestamp>;
|
||||
albumThumbnailAssetId: string | null;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
description: Generated<string>;
|
||||
deletedAt: Timestamp | null;
|
||||
isActivityEnabled: Generated<boolean>;
|
||||
order: Generated<string>;
|
||||
};
|
||||
export type AlbumsAssetsAssets = {
|
||||
albumsId: string;
|
||||
assetsId: string;
|
||||
};
|
||||
export type AlbumsSharedUsersUsers = {
|
||||
albumsId: string;
|
||||
usersId: string;
|
||||
};
|
||||
export type ApiKeys = {
|
||||
name: string;
|
||||
key: string;
|
||||
userId: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
id: Generated<string>;
|
||||
};
|
||||
export type AssetFaces = {
|
||||
assetId: string;
|
||||
personId: string | null;
|
||||
imageWidth: Generated<number>;
|
||||
imageHeight: Generated<number>;
|
||||
boundingBoxX1: Generated<number>;
|
||||
boundingBoxY1: Generated<number>;
|
||||
boundingBoxX2: Generated<number>;
|
||||
boundingBoxY2: Generated<number>;
|
||||
id: Generated<string>;
|
||||
};
|
||||
export type AssetJobStatus = {
|
||||
assetId: string;
|
||||
facesRecognizedAt: Timestamp | null;
|
||||
metadataExtractedAt: Timestamp | null;
|
||||
};
|
||||
export type Assets = {
|
||||
id: Generated<string>;
|
||||
deviceAssetId: string;
|
||||
ownerId: string;
|
||||
deviceId: string;
|
||||
type: string;
|
||||
originalPath: string;
|
||||
resizePath: string | null;
|
||||
fileCreatedAt: Timestamp;
|
||||
fileModifiedAt: Timestamp;
|
||||
isFavorite: Generated<boolean>;
|
||||
duration: string | null;
|
||||
webpPath: Generated<string | null>;
|
||||
encodedVideoPath: Generated<string | null>;
|
||||
checksum: Buffer;
|
||||
isVisible: Generated<boolean>;
|
||||
livePhotoVideoId: string | null;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
createdAt: Generated<Timestamp>;
|
||||
isArchived: Generated<boolean>;
|
||||
originalFileName: string;
|
||||
sidecarPath: string | null;
|
||||
isReadOnly: Generated<boolean>;
|
||||
thumbhash: Buffer | null;
|
||||
isOffline: Generated<boolean>;
|
||||
libraryId: string;
|
||||
isExternal: Generated<boolean>;
|
||||
deletedAt: Timestamp | null;
|
||||
localDateTime: Timestamp;
|
||||
stackId: string | null;
|
||||
truncatedDate: Generated<Timestamp>;
|
||||
};
|
||||
export type AssetStack = {
|
||||
id: Generated<string>;
|
||||
primaryAssetId: string;
|
||||
};
|
||||
export type Audit = {
|
||||
id: Generated<number>;
|
||||
entityType: string;
|
||||
entityId: string;
|
||||
action: string;
|
||||
ownerId: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
};
|
||||
export type Exif = {
|
||||
assetId: string;
|
||||
make: string | null;
|
||||
model: string | null;
|
||||
exifImageWidth: number | null;
|
||||
exifImageHeight: number | null;
|
||||
fileSizeInByte: string | null;
|
||||
orientation: string | null;
|
||||
dateTimeOriginal: Timestamp | null;
|
||||
modifyDate: Timestamp | null;
|
||||
lensModel: string | null;
|
||||
fNumber: number | null;
|
||||
focalLength: number | null;
|
||||
iso: number | null;
|
||||
latitude: number | null;
|
||||
longitude: number | null;
|
||||
city: string | null;
|
||||
state: string | null;
|
||||
country: string | null;
|
||||
description: Generated<string>;
|
||||
fps: number | null;
|
||||
exposureTime: string | null;
|
||||
livePhotoCID: string | null;
|
||||
timeZone: string | null;
|
||||
projectionType: string | null;
|
||||
profileDescription: string | null;
|
||||
colorspace: string | null;
|
||||
bitsPerSample: number | null;
|
||||
autoStackId: string | null;
|
||||
};
|
||||
export type GeodataPlaces = {
|
||||
id: number;
|
||||
name: string;
|
||||
longitude: number;
|
||||
latitude: number;
|
||||
countryCode: string;
|
||||
admin1Code: string | null;
|
||||
admin2Code: string | null;
|
||||
modificationDate: Timestamp;
|
||||
admin1Name: string | null;
|
||||
admin2Name: string | null;
|
||||
alternateNames: string | null;
|
||||
};
|
||||
export type Libraries = {
|
||||
id: Generated<string>;
|
||||
name: string;
|
||||
ownerId: string;
|
||||
type: string;
|
||||
importPaths: string[];
|
||||
exclusionPatterns: string[];
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
deletedAt: Timestamp | null;
|
||||
refreshedAt: Timestamp | null;
|
||||
isVisible: Generated<boolean>;
|
||||
};
|
||||
export type MoveHistory = {
|
||||
id: Generated<string>;
|
||||
entityId: string;
|
||||
pathType: string;
|
||||
oldPath: string;
|
||||
newPath: string;
|
||||
};
|
||||
export type Partners = {
|
||||
sharedById: string;
|
||||
sharedWithId: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
inTimeline: Generated<boolean>;
|
||||
};
|
||||
export type Person = {
|
||||
id: Generated<string>;
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
ownerId: string;
|
||||
name: Generated<string>;
|
||||
thumbnailPath: Generated<string>;
|
||||
isHidden: Generated<boolean>;
|
||||
birthDate: Timestamp | null;
|
||||
faceAssetId: string | null;
|
||||
};
|
||||
export type SharedLinkAsset = {
|
||||
assetsId: string;
|
||||
sharedLinksId: string;
|
||||
};
|
||||
export type SharedLinks = {
|
||||
id: Generated<string>;
|
||||
description: string | null;
|
||||
userId: string;
|
||||
key: Buffer;
|
||||
type: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
expiresAt: Timestamp | null;
|
||||
allowUpload: Generated<boolean>;
|
||||
albumId: string | null;
|
||||
allowDownload: Generated<boolean>;
|
||||
showExif: Generated<boolean>;
|
||||
password: string | null;
|
||||
};
|
||||
export type SmartInfo = {
|
||||
assetId: string;
|
||||
tags: string[];
|
||||
objects: string[];
|
||||
};
|
||||
export type SmartSearch = {
|
||||
assetId: string;
|
||||
};
|
||||
export type SocketIoAttachments = {
|
||||
id: Generated<string>;
|
||||
created_at: Generated<Timestamp | null>;
|
||||
payload: Buffer | null;
|
||||
};
|
||||
export type SystemConfig = {
|
||||
key: string;
|
||||
value: string | null;
|
||||
};
|
||||
export type SystemMetadata = {
|
||||
key: string;
|
||||
value: Generated<unknown>;
|
||||
};
|
||||
export type TagAsset = {
|
||||
assetsId: string;
|
||||
tagsId: string;
|
||||
};
|
||||
export type Tags = {
|
||||
id: Generated<string>;
|
||||
type: string;
|
||||
name: string;
|
||||
userId: string;
|
||||
renameTagId: string | null;
|
||||
};
|
||||
export type Users = {
|
||||
id: Generated<string>;
|
||||
email: string;
|
||||
password: Generated<string>;
|
||||
createdAt: Generated<Timestamp>;
|
||||
profileImagePath: Generated<string>;
|
||||
isAdmin: Generated<boolean>;
|
||||
shouldChangePassword: Generated<boolean>;
|
||||
deletedAt: Timestamp | null;
|
||||
oauthId: Generated<string>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
storageLabel: string | null;
|
||||
memoriesEnabled: Generated<boolean>;
|
||||
name: Generated<string>;
|
||||
avatarColor: string | null;
|
||||
quotaSizeInBytes: string | null;
|
||||
quotaUsageInBytes: Generated<string>;
|
||||
status: Generated<string>;
|
||||
};
|
||||
export type UserToken = {
|
||||
id: Generated<string>;
|
||||
token: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
userId: string;
|
||||
deviceType: Generated<string>;
|
||||
deviceOS: Generated<string>;
|
||||
};
|
||||
export type DB = {
|
||||
activity: Activity;
|
||||
albums: Albums;
|
||||
albums_assets_assets: AlbumsAssetsAssets;
|
||||
albums_shared_users_users: AlbumsSharedUsersUsers;
|
||||
api_keys: ApiKeys;
|
||||
asset_faces: AssetFaces;
|
||||
asset_job_status: AssetJobStatus;
|
||||
asset_stack: AssetStack;
|
||||
assets: Assets;
|
||||
audit: Audit;
|
||||
exif: Exif;
|
||||
geodata_places: GeodataPlaces;
|
||||
libraries: Libraries;
|
||||
move_history: MoveHistory;
|
||||
partners: Partners;
|
||||
person: Person;
|
||||
shared_link__asset: SharedLinkAsset;
|
||||
shared_links: SharedLinks;
|
||||
smart_info: SmartInfo;
|
||||
smart_search: SmartSearch;
|
||||
socket_io_attachments: SocketIoAttachments;
|
||||
system_config: SystemConfig;
|
||||
system_metadata: SystemMetadata;
|
||||
tag_asset: TagAsset;
|
||||
tags: Tags;
|
||||
user_token: UserToken;
|
||||
users: Users;
|
||||
};
|
16
server/src/prisma/kysely.ts
Normal file
16
server/src/prisma/kysely.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { DeduplicateJoinsPlugin, Kysely, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler } from 'kysely';
|
||||
import kyselyExt from 'prisma-extension-kysely';
|
||||
import type { DB } from './generated/types';
|
||||
|
||||
export const kyselyExtension = kyselyExt({
|
||||
kysely: (driver) =>
|
||||
new Kysely<DB>({
|
||||
dialect: {
|
||||
createDriver: () => driver,
|
||||
createAdapter: () => new PostgresAdapter(),
|
||||
createIntrospector: (db) => new PostgresIntrospector(db),
|
||||
createQueryCompiler: () => new PostgresQueryCompiler(),
|
||||
},
|
||||
plugins: [new DeduplicateJoinsPlugin()],
|
||||
}),
|
||||
});
|
17
server/src/prisma/metrics.ts
Normal file
17
server/src/prisma/metrics.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Prisma } from '@prisma/client';
|
||||
import util from 'node:util';
|
||||
|
||||
export const metricsExtension = Prisma.defineExtension({
|
||||
query: {
|
||||
$allModels: {
|
||||
async $allOperations({ operation, model, args, query }) {
|
||||
const start = performance.now();
|
||||
const result = await query(args);
|
||||
const end = performance.now();
|
||||
const time = end - start;
|
||||
console.log(util.inspect({ model, operation, args, time }, { showHidden: false, depth: null, colors: true }));
|
||||
return result;
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
467
server/src/prisma/schema.prisma
Normal file
467
server/src/prisma/schema.prisma
Normal file
@ -0,0 +1,467 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
previewFeatures = ["postgresqlExtensions", "relationJoins"]
|
||||
}
|
||||
|
||||
generator kysely {
|
||||
provider = "prisma-kysely"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DB_URL")
|
||||
extensions = [cube, earthdistance, pg_trgm, unaccent, uuid_ossp(map: "uuid-ossp", schema: "public"), vectors(map: "vectors", schema: "vectors")]
|
||||
}
|
||||
|
||||
/// This table contains check constraints and requires additional setup for migrations. Visit https://pris.ly/d/check-constraints for more info.
|
||||
model Activity {
|
||||
id String @id(map: "PK_24625a1d6b1b089c8ae206fe467") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
albumId String @db.Uuid
|
||||
userId String @db.Uuid
|
||||
assetId String? @db.Uuid
|
||||
comment String?
|
||||
isLiked Boolean @default(false)
|
||||
albums Albums @relation(fields: [albumId], references: [id], onDelete: Cascade, map: "FK_1af8519996fbfb3684b58df280b")
|
||||
users Users @relation(fields: [userId], references: [id], onDelete: Cascade, map: "FK_3571467bcbe021f66e2bdce96ea")
|
||||
assets Assets? @relation(fields: [assetId], references: [id], onDelete: Cascade, map: "FK_8091ea76b12338cb4428d33d782")
|
||||
|
||||
@@map(name: "activity")
|
||||
}
|
||||
|
||||
/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
|
||||
model Albums {
|
||||
id String @id(map: "PK_7f71c7b5bc7c87b8f94c9a93a00") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
ownerId String @db.Uuid
|
||||
albumName String @default("Untitled Album") @db.VarChar
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
albumThumbnailAssetId String? @db.Uuid
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
description String @default("")
|
||||
deletedAt DateTime? @db.Timestamptz(6)
|
||||
isActivityEnabled Boolean @default(true)
|
||||
order String @default("desc") @db.VarChar
|
||||
activity Activity[]
|
||||
assets Assets? @relation(fields: [albumThumbnailAssetId], references: [id], map: "FK_05895aa505a670300d4816debce")
|
||||
users Users @relation(fields: [ownerId], references: [id], onDelete: Cascade, map: "FK_b22c53f35ef20c28c21637c85f4")
|
||||
albums_assets_assets AlbumsAssetsAssets[]
|
||||
albums_shared_users_users AlbumsSharedUsersUsers[]
|
||||
shared_links SharedLinks[]
|
||||
|
||||
@@map(name: "albums")
|
||||
}
|
||||
|
||||
model AlbumsAssetsAssets {
|
||||
albumsId String @db.Uuid
|
||||
assetsId String @db.Uuid
|
||||
assets Assets @relation(fields: [assetsId], references: [id], onDelete: Cascade, map: "FK_4bd1303d199f4e72ccdf998c621")
|
||||
albums Albums @relation(fields: [albumsId], references: [id], onDelete: Cascade, map: "FK_e590fa396c6898fcd4a50e40927")
|
||||
|
||||
@@id([albumsId, assetsId], map: "PK_c67bc36fa845fb7b18e0e398180")
|
||||
@@index([assetsId], map: "IDX_4bd1303d199f4e72ccdf998c62")
|
||||
@@index([albumsId], map: "IDX_e590fa396c6898fcd4a50e4092")
|
||||
@@map(name: "albums_assets_assets")
|
||||
}
|
||||
|
||||
model AlbumsSharedUsersUsers {
|
||||
albumsId String @db.Uuid
|
||||
usersId String @db.Uuid
|
||||
albums Albums @relation(fields: [albumsId], references: [id], onDelete: Cascade, map: "FK_427c350ad49bd3935a50baab737")
|
||||
users Users @relation(fields: [usersId], references: [id], onDelete: Cascade, map: "FK_f48513bf9bccefd6ff3ad30bd06")
|
||||
|
||||
@@id([albumsId, usersId], map: "PK_7df55657e0b2e8b626330a0ebc8")
|
||||
@@index([albumsId], map: "IDX_427c350ad49bd3935a50baab73")
|
||||
@@index([usersId], map: "IDX_f48513bf9bccefd6ff3ad30bd0")
|
||||
@@map(name: "albums_shared_users_users")
|
||||
}
|
||||
|
||||
model ApiKeys {
|
||||
name String @db.VarChar
|
||||
key String @db.VarChar
|
||||
userId String @db.Uuid
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
id String @id(map: "PK_5c8a79801b44bd27b79228e1dad") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
users Users @relation(fields: [userId], references: [id], onDelete: Cascade, map: "FK_6c2e267ae764a9413b863a29342")
|
||||
|
||||
@@map(name: "api_keys")
|
||||
}
|
||||
|
||||
model AssetFaces {
|
||||
assetId String @db.Uuid
|
||||
personId String? @db.Uuid
|
||||
embedding Unsupported("vector")
|
||||
imageWidth Int @default(0)
|
||||
imageHeight Int @default(0)
|
||||
boundingBoxX1 Int @default(0)
|
||||
boundingBoxY1 Int @default(0)
|
||||
boundingBoxX2 Int @default(0)
|
||||
boundingBoxY2 Int @default(0)
|
||||
id String @id(map: "PK_6df76ab2eb6f5b57b7c2f1fc684") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
assets Assets @relation(fields: [assetId], references: [id], onDelete: Cascade, map: "FK_02a43fd0b3c50fb6d7f0cb7282c")
|
||||
person Person? @relation("asset_faces_personIdToperson", fields: [personId], references: [id], map: "FK_95ad7106dd7b484275443f580f9")
|
||||
person_person_faceAssetIdToasset_faces Person[] @relation("person_faceAssetIdToasset_faces")
|
||||
|
||||
@@index([assetId, personId], map: "IDX_asset_faces_assetId_personId")
|
||||
@@index([assetId], map: "IDX_asset_faces_on_assetId")
|
||||
@@index([personId], map: "IDX_asset_faces_personId")
|
||||
@@index([personId, assetId], map: "IDX_bf339a24070dac7e71304ec530")
|
||||
@@index([embedding], map: "face_index")
|
||||
@@map(name: "asset_faces")
|
||||
}
|
||||
|
||||
model AssetJobStatus {
|
||||
assetId String @id(map: "PK_420bec36fc02813bddf5c8b73d4") @db.Uuid
|
||||
facesRecognizedAt DateTime? @db.Timestamptz(6)
|
||||
metadataExtractedAt DateTime? @db.Timestamptz(6)
|
||||
assets Assets @relation(fields: [assetId], references: [id], onDelete: Cascade, map: "FK_420bec36fc02813bddf5c8b73d4")
|
||||
|
||||
@@map(name: "asset_job_status")
|
||||
}
|
||||
|
||||
model AssetStack {
|
||||
id String @id(map: "PK_74a27e7fcbd5852463d0af3034b") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
primaryAssetId String @unique(map: "REL_91704e101438fd0653f582426d") @db.Uuid
|
||||
primaryAsset Assets @relation("asset_stack_primaryAssetIdToassets", fields: [primaryAssetId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_91704e101438fd0653f582426dc")
|
||||
assets Assets[] @relation("assets_stackIdToasset_stack")
|
||||
|
||||
@@map(name: "asset_stack")
|
||||
}
|
||||
|
||||
/// This model contains an expression index which requires additional setup for migrations. Visit https://pris.ly/d/expression-indexes for more info.
|
||||
model Assets {
|
||||
id String @id(map: "PK_da96729a8b113377cfb6a62439c") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
deviceAssetId String @db.VarChar
|
||||
ownerId String @db.Uuid
|
||||
deviceId String @db.VarChar
|
||||
type String @db.VarChar
|
||||
originalPath String @db.VarChar
|
||||
resizePath String? @db.VarChar
|
||||
fileCreatedAt DateTime @db.Timestamptz(6)
|
||||
fileModifiedAt DateTime @db.Timestamptz(6)
|
||||
isFavorite Boolean @default(false)
|
||||
duration String? @db.VarChar
|
||||
webpPath String? @default("") @db.VarChar
|
||||
encodedVideoPath String? @default("") @db.VarChar
|
||||
checksum Bytes
|
||||
isVisible Boolean @default(true)
|
||||
livePhotoVideoId String? @unique(map: "UQ_16294b83fa8c0149719a1f631ef") @db.Uuid
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
isArchived Boolean @default(false)
|
||||
originalFileName String @db.VarChar
|
||||
sidecarPath String? @db.VarChar
|
||||
isReadOnly Boolean @default(false)
|
||||
thumbhash Bytes?
|
||||
isOffline Boolean @default(false)
|
||||
libraryId String @db.Uuid
|
||||
isExternal Boolean @default(false)
|
||||
deletedAt DateTime? @db.Timestamptz(6)
|
||||
localDateTime DateTime @db.Timestamptz(6)
|
||||
stackId String? @db.Uuid
|
||||
truncatedDate DateTime @default(dbgenerated("date_trunc('day', \"localDateTime\" at time zone 'UTC') at time zone 'UTC'")) @db.Timestamptz(6)
|
||||
activity Activity[]
|
||||
albums Albums[]
|
||||
albumsAssetsAssets AlbumsAssetsAssets[]
|
||||
faces AssetFaces[]
|
||||
assetJobStatus AssetJobStatus?
|
||||
assetStackAssetStackPrimaryAssetIdToAssets AssetStack? @relation("asset_stack_primaryAssetIdToassets")
|
||||
livePhotoVideo Assets? @relation("assetsToassets", fields: [livePhotoVideoId], references: [id], map: "FK_16294b83fa8c0149719a1f631ef")
|
||||
otherAssets Assets? @relation("assetsToassets")
|
||||
owner Users @relation(fields: [ownerId], references: [id], onDelete: Cascade, map: "FK_2c5ac0d6fb58b238fd2068de67d")
|
||||
library Libraries @relation(fields: [libraryId], references: [id], onDelete: Cascade, map: "FK_9977c3c1de01c3d848039a6b90c")
|
||||
stack AssetStack? @relation("assets_stackIdToasset_stack", fields: [stackId], references: [id], map: "FK_f15d48fa3ea5e4bda05ca8ab207")
|
||||
exifInfo Exif?
|
||||
sharedLinks SharedLinkAsset[]
|
||||
smartInfo SmartInfo?
|
||||
smartSearch SmartSearch?
|
||||
tags TagAsset[]
|
||||
|
||||
@@unique([ownerId, libraryId, checksum], map: "UQ_assets_owner_library_checksum")
|
||||
@@index([originalFileName], map: "IDX_4d66e76dada1ca180f67a205dc")
|
||||
@@index([checksum], map: "IDX_8d3efe36c0755849395e6ea866")
|
||||
@@index([id, stackId], map: "IDX_asset_id_stackId")
|
||||
@@index([originalPath, libraryId], map: "IDX_originalPath_libraryId")
|
||||
@@index([fileCreatedAt], map: "idx_asset_file_created_at")
|
||||
@@map(name: "assets")
|
||||
}
|
||||
|
||||
model Audit {
|
||||
id Int @id(map: "PK_1d3d120ddaf7bc9b1ed68ed463a") @default(autoincrement())
|
||||
entityType String @db.VarChar
|
||||
entityId String @db.Uuid
|
||||
action String @db.VarChar
|
||||
ownerId String @db.Uuid
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
|
||||
@@index([ownerId, createdAt], map: "IDX_ownerId_createdAt")
|
||||
@@map(name: "audit")
|
||||
}
|
||||
|
||||
model Exif {
|
||||
assetId String @id(map: "PK_c0117fdbc50b917ef9067740c44") @db.Uuid
|
||||
make String? @db.VarChar
|
||||
model String? @db.VarChar
|
||||
exifImageWidth Int?
|
||||
exifImageHeight Int?
|
||||
fileSizeInByte BigInt?
|
||||
orientation String? @db.VarChar
|
||||
dateTimeOriginal DateTime? @db.Timestamptz(6)
|
||||
modifyDate DateTime? @db.Timestamptz(6)
|
||||
lensModel String? @db.VarChar
|
||||
fNumber Float?
|
||||
focalLength Float?
|
||||
iso Int?
|
||||
latitude Float?
|
||||
longitude Float?
|
||||
city String? @db.VarChar
|
||||
state String? @db.VarChar
|
||||
country String? @db.VarChar
|
||||
description String @default("")
|
||||
fps Float?
|
||||
exposureTime String? @db.VarChar
|
||||
livePhotoCID String? @db.VarChar
|
||||
timeZone String? @db.VarChar
|
||||
exifTextSearchableColumn Unsupported("tsvector") @default(dbgenerated("to_tsvector('english'::regconfig, (((((((((((((COALESCE(make, ''::character varying))::text || ' '::text) || (COALESCE(model, ''::character varying))::text) || ' '::text) || (COALESCE(orientation, ''::character varying))::text) || ' '::text) || (COALESCE(\"lensModel\", ''::character varying))::text) || ' '::text) || (COALESCE(city, ''::character varying))::text) || ' '::text) || (COALESCE(state, ''::character varying))::text) || ' '::text) || (COALESCE(country, ''::character varying))::text))"))
|
||||
projectionType String? @db.VarChar
|
||||
profileDescription String? @db.VarChar
|
||||
colorspace String? @db.VarChar
|
||||
bitsPerSample Int?
|
||||
autoStackId String? @db.VarChar
|
||||
assets Assets @relation(fields: [assetId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_c0117fdbc50b917ef9067740c44")
|
||||
|
||||
@@index([autoStackId], map: "IDX_auto_stack_id")
|
||||
@@index([livePhotoCID], map: "IDX_live_photo_cid")
|
||||
@@index([city], map: "exif_city")
|
||||
@@map(name: "exif")
|
||||
}
|
||||
|
||||
/// This model contains an expression index which requires additional setup for migrations. Visit https://pris.ly/d/expression-indexes for more info.
|
||||
model GeodataPlaces {
|
||||
id Int @id(map: "PK_c29918988912ef4036f3d7fbff4")
|
||||
name String @db.VarChar(200)
|
||||
longitude Float
|
||||
latitude Float
|
||||
countryCode String @db.Char(2)
|
||||
admin1Code String? @db.VarChar(20)
|
||||
admin2Code String? @db.VarChar(80)
|
||||
modificationDate DateTime @db.Date
|
||||
earthCoord Unsupported("cube")? @default(dbgenerated("ll_to_earth(latitude, longitude)"))
|
||||
admin1Name String? @db.VarChar
|
||||
admin2Name String? @db.VarChar
|
||||
alternateNames String? @db.VarChar
|
||||
|
||||
@@index([earthCoord], map: "IDX_geodata_gist_earthcoord", type: Gist)
|
||||
@@map(name: "geodata_places")
|
||||
}
|
||||
|
||||
model Libraries {
|
||||
id String @id(map: "PK_505fedfcad00a09b3734b4223de") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
name String @db.VarChar
|
||||
ownerId String @db.Uuid
|
||||
type String @db.VarChar
|
||||
importPaths String[]
|
||||
exclusionPatterns String[]
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
deletedAt DateTime? @db.Timestamptz(6)
|
||||
refreshedAt DateTime? @db.Timestamptz(6)
|
||||
isVisible Boolean @default(true)
|
||||
assets Assets[]
|
||||
owner Users @relation(fields: [ownerId], references: [id], onDelete: Cascade, map: "FK_0f6fc2fb195f24d19b0fb0d57c1")
|
||||
|
||||
@@map(name: "libraries")
|
||||
}
|
||||
|
||||
model MoveHistory {
|
||||
id String @id(map: "PK_af608f132233acf123f2949678d") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
entityId String @db.VarChar
|
||||
pathType String @db.VarChar
|
||||
oldPath String @db.VarChar
|
||||
newPath String @unique(map: "UQ_newPath") @db.VarChar
|
||||
|
||||
@@unique([entityId, pathType], map: "UQ_entityId_pathType")
|
||||
@@map(name: "move_history")
|
||||
}
|
||||
|
||||
model Partners {
|
||||
sharedById String @db.Uuid
|
||||
sharedWithId String @db.Uuid
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
inTimeline Boolean @default(false)
|
||||
sharedBy Users @relation("partners_sharedByIdTousers", fields: [sharedById], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_7e077a8b70b3530138610ff5e04")
|
||||
sharedWith Users @relation("partners_sharedWithIdTousers", fields: [sharedWithId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_d7e875c6c60e661723dbf372fd3")
|
||||
|
||||
@@id([sharedById, sharedWithId], map: "PK_f1cc8f73d16b367f426261a8736")
|
||||
@@map(name: "partners")
|
||||
}
|
||||
|
||||
/// This table contains check constraints and requires additional setup for migrations. Visit https://pris.ly/d/check-constraints for more info.
|
||||
model Person {
|
||||
id String @id(map: "PK_5fdaf670315c4b7e70cce85daa3") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
ownerId String @db.Uuid
|
||||
name String @default("") @db.VarChar
|
||||
thumbnailPath String @default("") @db.VarChar
|
||||
isHidden Boolean @default(false)
|
||||
birthDate DateTime? @db.Date
|
||||
faceAssetId String? @db.Uuid
|
||||
asset_faces_asset_faces_personIdToperson AssetFaces[] @relation("asset_faces_personIdToperson")
|
||||
asset_faces_person_faceAssetIdToasset_faces AssetFaces? @relation("person_faceAssetIdToasset_faces", fields: [faceAssetId], references: [id], onUpdate: NoAction, map: "FK_2bbabe31656b6778c6b87b61023")
|
||||
users Users @relation(fields: [ownerId], references: [id], onDelete: Cascade, map: "FK_5527cc99f530a547093f9e577b6")
|
||||
|
||||
@@map(name: "person")
|
||||
}
|
||||
|
||||
model SharedLinkAsset {
|
||||
assetsId String @db.Uuid
|
||||
sharedLinksId String @db.Uuid
|
||||
assets Assets @relation(fields: [assetsId], references: [id], onDelete: Cascade, map: "FK_5b7decce6c8d3db9593d6111a66")
|
||||
sharedLinks SharedLinks @relation(fields: [sharedLinksId], references: [id], onDelete: Cascade, map: "FK_c9fab4aa97ffd1b034f3d6581ab")
|
||||
|
||||
@@id([assetsId, sharedLinksId], map: "PK_9b4f3687f9b31d1e311336b05e3")
|
||||
@@index([assetsId], map: "IDX_5b7decce6c8d3db9593d6111a6")
|
||||
@@index([sharedLinksId], map: "IDX_c9fab4aa97ffd1b034f3d6581a")
|
||||
@@map(name: "shared_link__asset")
|
||||
}
|
||||
|
||||
model SharedLinks {
|
||||
id String @id(map: "PK_642e2b0f619e4876e5f90a43465") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
description String? @db.VarChar
|
||||
userId String @db.Uuid
|
||||
key Bytes @unique(map: "UQ_sharedlink_key")
|
||||
type String @db.VarChar
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
expiresAt DateTime? @db.Timestamptz(6)
|
||||
allowUpload Boolean @default(false)
|
||||
albumId String? @db.Uuid
|
||||
allowDownload Boolean @default(true)
|
||||
showExif Boolean @default(true)
|
||||
password String? @db.VarChar
|
||||
assets SharedLinkAsset[]
|
||||
albums Albums? @relation(fields: [albumId], references: [id], onDelete: Cascade, map: "FK_0c6ce9058c29f07cdf7014eac66")
|
||||
users Users @relation(fields: [userId], references: [id], onDelete: Cascade, map: "FK_66fe3837414c5a9f1c33ca49340")
|
||||
|
||||
@@index([albumId], map: "IDX_sharedlink_albumId")
|
||||
@@index([key], map: "IDX_sharedlink_key")
|
||||
@@map(name: "shared_links")
|
||||
}
|
||||
|
||||
model SmartInfo {
|
||||
assetId String @id(map: "PK_5e3753aadd956110bf3ec0244ac") @db.Uuid
|
||||
tags String[]
|
||||
objects String[]
|
||||
smartInfoTextSearchableColumn Unsupported("tsvector") @default(dbgenerated("to_tsvector('english'::regconfig, f_concat_ws(' '::text, (COALESCE(tags, ARRAY[]::text[]) || COALESCE(objects, ARRAY[]::text[]))))"))
|
||||
assets Assets @relation(fields: [assetId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_5e3753aadd956110bf3ec0244ac")
|
||||
|
||||
@@index([tags], map: "si_tags", type: Gin)
|
||||
@@index([smartInfoTextSearchableColumn], map: "smart_info_text_searchable_idx", type: Gin)
|
||||
@@map(name: "smart_info")
|
||||
}
|
||||
|
||||
model SmartSearch {
|
||||
assetId String @id @db.Uuid
|
||||
embedding Unsupported("vector")
|
||||
assets Assets @relation(fields: [assetId], references: [id], onDelete: Cascade, onUpdate: NoAction)
|
||||
|
||||
@@index([embedding], map: "clip_index")
|
||||
@@map(name: "smart_search")
|
||||
}
|
||||
|
||||
model SocketIoAttachments {
|
||||
id BigInt @unique @default(autoincrement())
|
||||
created_at DateTime? @default(now()) @db.Timestamptz(6)
|
||||
payload Bytes?
|
||||
|
||||
@@map(name: "socket_io_attachments")
|
||||
}
|
||||
|
||||
model SystemConfig {
|
||||
key String @id(map: "PK_aab69295b445016f56731f4d535") @db.VarChar
|
||||
value String? @db.VarChar
|
||||
|
||||
@@map(name: "system_config")
|
||||
}
|
||||
|
||||
model SystemMetadata {
|
||||
key String @id(map: "PK_fa94f6857470fb5b81ec6084465") @db.VarChar
|
||||
value Json @default("{}")
|
||||
|
||||
@@map(name: "system_metadata")
|
||||
}
|
||||
|
||||
model TagAsset {
|
||||
assetsId String @db.Uuid
|
||||
tagsId String @db.Uuid
|
||||
tags Tags @relation(fields: [tagsId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_e99f31ea4cdf3a2c35c7287eb42")
|
||||
assets Assets @relation(fields: [assetsId], references: [id], onDelete: Cascade, map: "FK_f8e8a9e893cb5c54907f1b798e9")
|
||||
|
||||
@@id([assetsId, tagsId], map: "PK_ef5346fe522b5fb3bc96454747e")
|
||||
@@index([tagsId], map: "IDX_e99f31ea4cdf3a2c35c7287eb4")
|
||||
@@index([assetsId], map: "IDX_f8e8a9e893cb5c54907f1b798e")
|
||||
@@index([assetsId, tagsId], map: "IDX_tag_asset_assetsId_tagsId")
|
||||
@@map(name: "tag_asset")
|
||||
}
|
||||
|
||||
model Tags {
|
||||
id String @id(map: "PK_e7dc17249a1148a1970748eda99") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
type String @db.VarChar
|
||||
name String @db.VarChar
|
||||
userId String @db.Uuid
|
||||
renameTagId String? @db.Uuid
|
||||
tags TagAsset[]
|
||||
users Users @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_92e67dc508c705dd66c94615576")
|
||||
|
||||
@@unique([name, userId], map: "UQ_tag_name_userId")
|
||||
@@map(name: "tags")
|
||||
}
|
||||
|
||||
model UserToken {
|
||||
id String @id(map: "PK_48cb6b5c20faa63157b3c1baf7f") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
token String @db.VarChar
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
userId String @db.Uuid
|
||||
deviceType String @default("") @db.VarChar
|
||||
deviceOS String @default("") @db.VarChar
|
||||
users Users @relation(fields: [userId], references: [id], onDelete: Cascade, map: "FK_d37db50eecdf9b8ce4eedd2f918")
|
||||
|
||||
@@map(name: "user_token")
|
||||
}
|
||||
|
||||
model Users {
|
||||
id String @id(map: "PK_a3ffb1c0c8416b9fc6f907b7433") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
|
||||
email String @unique(map: "UQ_97672ac88f789774dd47f7c8be3") @db.VarChar
|
||||
password String @default("") @db.VarChar
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
profileImagePath String @default("") @db.VarChar
|
||||
isAdmin Boolean @default(false)
|
||||
shouldChangePassword Boolean @default(true)
|
||||
deletedAt DateTime? @db.Timestamptz(6)
|
||||
oauthId String @default("") @db.VarChar
|
||||
updatedAt DateTime @default(now()) @db.Timestamptz(6)
|
||||
storageLabel String? @unique(map: "UQ_b309cf34fa58137c416b32cea3a") @db.VarChar
|
||||
memoriesEnabled Boolean @default(true)
|
||||
name String @default("") @db.VarChar
|
||||
avatarColor String? @db.VarChar
|
||||
quotaSizeInBytes BigInt?
|
||||
quotaUsageInBytes BigInt @default(0)
|
||||
status String @default("active") @db.VarChar
|
||||
activity Activity[]
|
||||
albums Albums[]
|
||||
albumsSharedUsersUsers AlbumsSharedUsersUsers[]
|
||||
apiKeys ApiKeys[]
|
||||
assets Assets[]
|
||||
libraries Libraries[]
|
||||
sharedBy Partners[] @relation("partners_sharedByIdTousers")
|
||||
sharedWith Partners[] @relation("partners_sharedWithIdTousers")
|
||||
person Person[]
|
||||
sharedLinks SharedLinks[]
|
||||
tags Tags[]
|
||||
userToken UserToken[]
|
||||
|
||||
@@map(name: "users")
|
||||
}
|
File diff suppressed because it is too large
Load Diff
32
server/src/repositories/prisma.repository.ts
Normal file
32
server/src/repositories/prisma.repository.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { findNonDeletedExtension } from '../prisma/find-non-deleted';
|
||||
import { metricsExtension } from '../prisma/metrics';
|
||||
import { kyselyExtension } from 'src/prisma/kysely';
|
||||
|
||||
function extendClient(base: PrismaClient) {
|
||||
return base.$extends(metricsExtension).$extends(findNonDeletedExtension).$extends(kyselyExtension);
|
||||
}
|
||||
|
||||
class UntypedExtendedClient extends PrismaClient {
|
||||
constructor(options?: ConstructorParameters<typeof PrismaClient>[0]) {
|
||||
super(options);
|
||||
|
||||
return extendClient(this) as this;
|
||||
}
|
||||
}
|
||||
|
||||
const ExtendedPrismaClient = UntypedExtendedClient as unknown as new (
|
||||
options?: ConstructorParameters<typeof PrismaClient>[0],
|
||||
) => ReturnType<typeof extendClient>;
|
||||
|
||||
@Injectable()
|
||||
export class PrismaRepository extends ExtendedPrismaClient implements OnModuleInit, OnModuleDestroy {
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.$disconnect();
|
||||
}
|
||||
}
|
@ -1,57 +1,35 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { vectorExt } from 'src/database.config';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ExpressionBuilder, Kysely, OrderByDirectionExpression, sql } from 'kysely';
|
||||
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
|
||||
import _ from 'lodash';
|
||||
import { DummyValue, GenerateSql } from 'src/decorators';
|
||||
import { AssetFaceEntity } from 'src/entities/asset-face.entity';
|
||||
import { AssetEntity, AssetType } from 'src/entities/asset.entity';
|
||||
import { GeodataPlacesEntity } from 'src/entities/geodata-places.entity';
|
||||
import { SmartInfoEntity } from 'src/entities/smart-info.entity';
|
||||
import { SmartSearchEntity } from 'src/entities/smart-search.entity';
|
||||
import { DatabaseExtension } from 'src/interfaces/database.interface';
|
||||
import {
|
||||
AssetSearchBuilderOptions,
|
||||
AssetSearchOptions,
|
||||
Embedding,
|
||||
FaceEmbeddingSearch,
|
||||
FaceSearchResult,
|
||||
ISearchRepository,
|
||||
SearchPaginationOptions,
|
||||
SmartSearchOptions,
|
||||
} from 'src/interfaces/search.interface';
|
||||
import { asVector, searchAssetBuilder } from 'src/utils/database';
|
||||
import { DB } from 'src/prisma/generated/types';
|
||||
import { asVector } from 'src/utils/database';
|
||||
import { Instrumentation } from 'src/utils/instrumentation';
|
||||
import { ImmichLogger } from 'src/utils/logger';
|
||||
import { getCLIPModelInfo } from 'src/utils/misc';
|
||||
import { Paginated, PaginationMode, PaginationResult, paginatedBuilder } from 'src/utils/pagination';
|
||||
import { Paginated } from 'src/utils/pagination';
|
||||
import { isValidInteger } from 'src/validation';
|
||||
import { Repository, SelectQueryBuilder } from 'typeorm';
|
||||
import { PrismaRepository } from './prisma.repository';
|
||||
|
||||
@Instrumentation()
|
||||
@Injectable()
|
||||
export class SearchRepository implements ISearchRepository {
|
||||
private logger = new ImmichLogger(SearchRepository.name);
|
||||
private faceColumns: string[];
|
||||
private assetsByCityQuery: string;
|
||||
|
||||
constructor(
|
||||
@InjectRepository(SmartInfoEntity) private repository: Repository<SmartInfoEntity>,
|
||||
@InjectRepository(AssetEntity) private assetRepository: Repository<AssetEntity>,
|
||||
@InjectRepository(AssetFaceEntity) private assetFaceRepository: Repository<AssetFaceEntity>,
|
||||
@InjectRepository(SmartSearchEntity) private smartSearchRepository: Repository<SmartSearchEntity>,
|
||||
@InjectRepository(GeodataPlacesEntity) private readonly geodataPlacesRepository: Repository<GeodataPlacesEntity>,
|
||||
) {
|
||||
this.faceColumns = this.assetFaceRepository.manager.connection
|
||||
.getMetadata(AssetFaceEntity)
|
||||
.ownColumns.map((column) => column.propertyName)
|
||||
.filter((propertyName) => propertyName !== 'embedding');
|
||||
this.assetsByCityQuery =
|
||||
assetsByCityCte +
|
||||
this.assetRepository
|
||||
.createQueryBuilder('asset')
|
||||
.innerJoinAndSelect('asset.exifInfo', 'exif')
|
||||
.withDeleted()
|
||||
.getQuery() +
|
||||
' INNER JOIN cte ON asset.id = cte."assetId"';
|
||||
}
|
||||
constructor(@Inject(PrismaRepository) private prismaRepository: PrismaRepository) {}
|
||||
|
||||
async init(modelName: string): Promise<void> {
|
||||
const { dimSize } = getCLIPModelInfo(modelName);
|
||||
@ -78,23 +56,16 @@ export class SearchRepository implements ISearchRepository {
|
||||
],
|
||||
})
|
||||
async searchMetadata(pagination: SearchPaginationOptions, options: AssetSearchOptions): Paginated<AssetEntity> {
|
||||
let builder = this.assetRepository.createQueryBuilder('asset');
|
||||
builder = searchAssetBuilder(builder, options);
|
||||
const orderDirection = (options.orderDirection?.toLowerCase() || 'desc') as OrderByDirectionExpression;
|
||||
const builder = this.searchAssetBuilder(options)
|
||||
.orderBy('assets.fileCreatedAt', orderDirection)
|
||||
.limit(pagination.size + 1)
|
||||
.offset((pagination.page - 1) * pagination.size);
|
||||
|
||||
builder.orderBy('asset.fileCreatedAt', options.orderDirection ?? 'DESC');
|
||||
return paginatedBuilder<AssetEntity>(builder, {
|
||||
mode: PaginationMode.SKIP_TAKE,
|
||||
skip: (pagination.page - 1) * pagination.size,
|
||||
take: pagination.size,
|
||||
});
|
||||
}
|
||||
|
||||
private createPersonFilter(builder: SelectQueryBuilder<AssetFaceEntity>, personIds: string[]) {
|
||||
return builder
|
||||
.select(`${builder.alias}."assetId"`)
|
||||
.where(`${builder.alias}."personId" IN (:...personIds)`, { personIds })
|
||||
.groupBy(`${builder.alias}."assetId"`)
|
||||
.having(`COUNT(DISTINCT ${builder.alias}."personId") = :personCount`, { personCount: personIds.length });
|
||||
const items = (await builder.execute()) as any as AssetEntity[];
|
||||
const hasNextPage = items.length > pagination.size;
|
||||
items.splice(pagination.size);
|
||||
return { items, hasNextPage };
|
||||
}
|
||||
|
||||
@GenerateSql({
|
||||
@ -114,35 +85,25 @@ export class SearchRepository implements ISearchRepository {
|
||||
pagination: SearchPaginationOptions,
|
||||
{ embedding, userIds, personIds, ...options }: SmartSearchOptions,
|
||||
): Paginated<AssetEntity> {
|
||||
let results: PaginationResult<AssetEntity> = { items: [], hasNextPage: false };
|
||||
if (!isValidInteger(pagination.size, { min: 1, max: 1000 })) {
|
||||
throw new Error(`Invalid value for 'size': ${pagination.size}`);
|
||||
}
|
||||
|
||||
await this.assetRepository.manager.transaction(async (manager) => {
|
||||
let builder = manager.createQueryBuilder(AssetEntity, 'asset');
|
||||
let items: AssetEntity[] = [];
|
||||
await this.prismaRepository.$transaction(async (tx) => {
|
||||
await tx.$queryRawUnsafe(`SET LOCAL vectors.hnsw_ef_search = ${pagination.size + 1}`);
|
||||
let builder = this.searchAssetBuilder(options, tx.$kysely)
|
||||
.innerJoin('smart_search', 'assets.id', 'smart_search.assetId')
|
||||
.orderBy(sql`smart_search.embedding <=> ${asVector(embedding)}::vector`)
|
||||
.limit(pagination.size + 1)
|
||||
.offset((pagination.page - 1) * pagination.size);
|
||||
|
||||
if (personIds?.length) {
|
||||
const assetFaceBuilder = manager.createQueryBuilder(AssetFaceEntity, 'asset_face');
|
||||
const cte = this.createPersonFilter(assetFaceBuilder, personIds);
|
||||
builder
|
||||
.addCommonTableExpression(cte, 'asset_face_ids')
|
||||
.innerJoin('asset_face_ids', 'a', 'a."assetId" = asset.id');
|
||||
}
|
||||
|
||||
builder = searchAssetBuilder(builder, options);
|
||||
builder
|
||||
.innerJoin('asset.smartSearch', 'search')
|
||||
.andWhere('asset.ownerId IN (:...userIds )')
|
||||
.orderBy('search.embedding <=> :embedding')
|
||||
.setParameters({ userIds, embedding: asVector(embedding) });
|
||||
|
||||
await manager.query(this.getRuntimeConfig(pagination.size));
|
||||
results = await paginatedBuilder<AssetEntity>(builder, {
|
||||
mode: PaginationMode.LIMIT_OFFSET,
|
||||
skip: (pagination.page - 1) * pagination.size,
|
||||
take: pagination.size,
|
||||
});
|
||||
items = (await builder.execute()) as any as AssetEntity[];
|
||||
});
|
||||
|
||||
return results;
|
||||
const hasNextPage = items.length > pagination.size;
|
||||
items.splice(pagination.size);
|
||||
return { items, hasNextPage };
|
||||
}
|
||||
|
||||
@GenerateSql({
|
||||
@ -155,112 +116,104 @@ export class SearchRepository implements ISearchRepository {
|
||||
},
|
||||
],
|
||||
})
|
||||
async searchFaces({
|
||||
searchFaces({
|
||||
userIds,
|
||||
embedding,
|
||||
numResults,
|
||||
maxDistance,
|
||||
hasPerson,
|
||||
}: FaceEmbeddingSearch): Promise<FaceSearchResult[]> {
|
||||
if (!isValidInteger(numResults, { min: 1 })) {
|
||||
if (!isValidInteger(numResults, { min: 1, max: 1000 })) {
|
||||
throw new Error(`Invalid value for 'numResults': ${numResults}`);
|
||||
}
|
||||
|
||||
// setting this too low messes with prefilter recall
|
||||
numResults = Math.max(numResults, 64);
|
||||
|
||||
let results: Array<AssetFaceEntity & { distance: number }> = [];
|
||||
await this.assetRepository.manager.transaction(async (manager) => {
|
||||
const cte = manager
|
||||
.createQueryBuilder(AssetFaceEntity, 'faces')
|
||||
.select('faces.embedding <=> :embedding', 'distance')
|
||||
.innerJoin('faces.asset', 'asset')
|
||||
.where('asset.ownerId IN (:...userIds )')
|
||||
.orderBy('faces.embedding <=> :embedding')
|
||||
.setParameters({ userIds, embedding: asVector(embedding) });
|
||||
|
||||
cte.limit(numResults);
|
||||
|
||||
if (hasPerson) {
|
||||
cte.andWhere('faces."personId" IS NOT NULL');
|
||||
}
|
||||
|
||||
for (const col of this.faceColumns) {
|
||||
cte.addSelect(`faces.${col}`, col);
|
||||
}
|
||||
|
||||
await manager.query(this.getRuntimeConfig(numResults));
|
||||
results = await manager
|
||||
.createQueryBuilder()
|
||||
.select('res.*')
|
||||
.addCommonTableExpression(cte, 'cte')
|
||||
.from('cte', 'res')
|
||||
.where('res.distance <= :maxDistance', { maxDistance })
|
||||
.orderBy('res.distance')
|
||||
.getRawMany();
|
||||
const vector = asVector(embedding);
|
||||
return this.prismaRepository.$transaction(async (tx) => {
|
||||
await tx.$queryRawUnsafe(`SET LOCAL vectors.hnsw_ef_search = ${numResults}`);
|
||||
return tx.$kysely
|
||||
.with('cte', (qb) =>
|
||||
qb
|
||||
.selectFrom('asset_faces')
|
||||
.select([
|
||||
(eb) => eb.fn.toJson(sql`asset_faces.*`).as('face'),
|
||||
sql<number>`asset_faces.embedding <=> ${vector}::vector`.as('distance'),
|
||||
])
|
||||
.innerJoin('assets', 'assets.id', 'asset_faces.assetId')
|
||||
.where('assets.ownerId', '=', sql<string>`ANY(ARRAY[${userIds}]::uuid[])`)
|
||||
.$if(!!hasPerson, (qb) => qb.where('asset_faces.personId', 'is not', null))
|
||||
.orderBy(sql`asset_faces.embedding <=> ${vector}::vector`)
|
||||
.limit(numResults),
|
||||
)
|
||||
.selectFrom('cte')
|
||||
.where('cte.distance', '<=', maxDistance)
|
||||
.execute() as any as Array<{ face: AssetFaceEntity; distance: number }>;
|
||||
});
|
||||
return results.map((row) => ({
|
||||
face: this.assetFaceRepository.create(row),
|
||||
distance: row.distance,
|
||||
}));
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.STRING] })
|
||||
async searchPlaces(placeName: string): Promise<GeodataPlacesEntity[]> {
|
||||
return await this.geodataPlacesRepository
|
||||
.createQueryBuilder('geoplaces')
|
||||
.where(`f_unaccent(name) %>> f_unaccent(:placeName)`)
|
||||
.orWhere(`f_unaccent("admin2Name") %>> f_unaccent(:placeName)`)
|
||||
.orWhere(`f_unaccent("admin1Name") %>> f_unaccent(:placeName)`)
|
||||
.orWhere(`f_unaccent("alternateNames") %>> f_unaccent(:placeName)`)
|
||||
.orderBy(
|
||||
`
|
||||
COALESCE(f_unaccent(name) <->>> f_unaccent(:placeName), 0) +
|
||||
COALESCE(f_unaccent("admin2Name") <->>> f_unaccent(:placeName), 0) +
|
||||
COALESCE(f_unaccent("admin1Name") <->>> f_unaccent(:placeName), 0) +
|
||||
COALESCE(f_unaccent("alternateNames") <->>> f_unaccent(:placeName), 0)
|
||||
`,
|
||||
searchPlaces(placeName: string): Promise<GeodataPlacesEntity[]> {
|
||||
const contains = '%>>' as any as 'ilike';
|
||||
return this.prismaRepository.$kysely
|
||||
.selectFrom('geodata_places')
|
||||
.selectAll()
|
||||
.where((eb) =>
|
||||
eb.or([
|
||||
eb(eb.fn('f_unaccent', ['name']), contains, eb.fn('f_unaccent', [eb.val(placeName)])),
|
||||
eb(eb.fn('f_unaccent', ['admin2Name']), contains, eb.fn('f_unaccent', [eb.val(placeName)])),
|
||||
eb(eb.fn('f_unaccent', ['admin1Name']), contains, eb.fn('f_unaccent', [eb.val(placeName)])),
|
||||
eb(eb.fn('f_unaccent', ['alternateNames']), contains, eb.fn('f_unaccent', [eb.val(placeName)])),
|
||||
]),
|
||||
)
|
||||
.orderBy(
|
||||
sql`COALESCE(f_unaccent(name) <->>> f_unaccent(${placeName}), 0) +
|
||||
COALESCE(f_unaccent("admin2Name") <->>> f_unaccent(${placeName}), 0) +
|
||||
COALESCE(f_unaccent("admin1Name") <->>> f_unaccent(${placeName}), 0) +
|
||||
COALESCE(f_unaccent("alternateNames") <->>> f_unaccent(${placeName}), 0)`,
|
||||
)
|
||||
.setParameters({ placeName })
|
||||
.limit(20)
|
||||
.getMany();
|
||||
.execute() as Promise<GeodataPlacesEntity[]>;
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [[DummyValue.UUID]] })
|
||||
async getAssetsByCity(userIds: string[]): Promise<AssetEntity[]> {
|
||||
const parameters = [userIds.join(', '), true, false, AssetType.IMAGE];
|
||||
const rawRes = await this.repository.query(this.assetsByCityQuery, parameters);
|
||||
// the performance difference between this and the normal way is too huge to ignore, e.g. 3s vs 4ms
|
||||
return this.prismaRepository.$queryRaw`WITH RECURSIVE cte AS (
|
||||
(
|
||||
SELECT city, "assetId"
|
||||
FROM exif
|
||||
INNER JOIN assets ON exif."assetId" = assets.id
|
||||
WHERE "ownerId" = ANY(ARRAY[${userIds}]::uuid[]) AND "isVisible" = true AND "isArchived" = false AND type = 'IMAGE'
|
||||
ORDER BY city
|
||||
LIMIT 1
|
||||
)
|
||||
|
||||
const items: AssetEntity[] = [];
|
||||
for (const res of rawRes) {
|
||||
const item = { exifInfo: {} as Record<string, any> } as Record<string, any>;
|
||||
for (const [key, value] of Object.entries(res)) {
|
||||
if (key.startsWith('exif_')) {
|
||||
item.exifInfo[key.replace('exif_', '')] = value;
|
||||
} else {
|
||||
item[key.replace('asset_', '')] = value;
|
||||
}
|
||||
}
|
||||
items.push(item as AssetEntity);
|
||||
}
|
||||
UNION ALL
|
||||
|
||||
return items;
|
||||
SELECT l.city, l."assetId"
|
||||
FROM cte c
|
||||
, LATERAL (
|
||||
SELECT city, "assetId"
|
||||
FROM exif
|
||||
INNER JOIN assets ON exif."assetId" = assets.id
|
||||
WHERE city > c.city AND "ownerId" = ANY(ARRAY[${userIds}]::uuid[]) AND "isVisible" = true AND "isArchived" = false AND type = 'IMAGE'
|
||||
ORDER BY city
|
||||
LIMIT 1
|
||||
) l
|
||||
)
|
||||
select "assets".*, json_strip_nulls(to_json(exif.*)) as "exifInfo"
|
||||
from "assets"
|
||||
inner join "exif" on "assets"."id" = "exif"."assetId"
|
||||
inner join "cte" on "assets"."id" = "cte"."assetId"`;
|
||||
}
|
||||
|
||||
async upsert(smartInfo: Partial<SmartInfoEntity>, embedding?: Embedding): Promise<void> {
|
||||
await this.repository.upsert(smartInfo, { conflictPaths: ['assetId'] });
|
||||
if (!smartInfo.assetId || !embedding) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.upsertEmbedding(smartInfo.assetId, embedding);
|
||||
}
|
||||
|
||||
private async upsertEmbedding(assetId: string, embedding: number[]): Promise<void> {
|
||||
await this.smartSearchRepository.upsert(
|
||||
{ assetId, embedding: () => asVector(embedding, true) },
|
||||
{ conflictPaths: ['assetId'] },
|
||||
);
|
||||
async upsert(assetId: string, embedding: number[]): Promise<void> {
|
||||
await this.prismaRepository.$kysely
|
||||
.insertInto('smart_search')
|
||||
.values({ assetId, embedding: asVector(embedding, true) } as any)
|
||||
.onConflict((oc) => oc.column('assetId').doUpdateSet({ embedding: asVector(embedding, true) } as any))
|
||||
.execute();
|
||||
}
|
||||
|
||||
private async updateDimSize(dimSize: number): Promise<void> {
|
||||
@ -275,27 +228,27 @@ export class SearchRepository implements ISearchRepository {
|
||||
|
||||
this.logger.log(`Updating database CLIP dimension size to ${dimSize}.`);
|
||||
|
||||
await this.smartSearchRepository.manager.transaction(async (manager) => {
|
||||
await manager.clear(SmartSearchEntity);
|
||||
await manager.query(`ALTER TABLE smart_search ALTER COLUMN embedding SET DATA TYPE vector(${dimSize})`);
|
||||
this.prismaRepository.$transaction(async (tx) => {
|
||||
await tx.$queryRawUnsafe(`TRUNCATE smart_search`);
|
||||
await tx.$queryRawUnsafe(`ALTER TABLE smart_search ALTER COLUMN embedding SET DATA TYPE vector(${dimSize})`);
|
||||
});
|
||||
|
||||
this.logger.log(`Successfully updated database CLIP dimension size from ${curDimSize} to ${dimSize}.`);
|
||||
}
|
||||
|
||||
deleteAllSearchEmbeddings(): Promise<void> {
|
||||
return this.smartSearchRepository.clear();
|
||||
return this.prismaRepository.$queryRawUnsafe(`TRUNCATE smart_search`);
|
||||
}
|
||||
|
||||
private async getDimSize(): Promise<number> {
|
||||
const res = await this.smartSearchRepository.manager.query(`
|
||||
const res = await this.prismaRepository.$queryRaw<[{ dimsize: number }]>`
|
||||
SELECT atttypmod as dimsize
|
||||
FROM pg_attribute f
|
||||
JOIN pg_class c ON c.oid = f.attrelid
|
||||
WHERE c.relkind = 'r'::char
|
||||
AND f.attnum > 0
|
||||
AND c.relname = 'smart_search'
|
||||
AND f.attname = 'embedding'`);
|
||||
AND f.attname = 'embedding'`;
|
||||
|
||||
const dimSize = res[0]['dimsize'];
|
||||
if (!isValidInteger(dimSize, { min: 1, max: 2 ** 16 })) {
|
||||
@ -304,43 +257,110 @@ export class SearchRepository implements ISearchRepository {
|
||||
return dimSize;
|
||||
}
|
||||
|
||||
private getRuntimeConfig(numResults?: number): string {
|
||||
if (vectorExt === DatabaseExtension.VECTOR) {
|
||||
return 'SET LOCAL hnsw.ef_search = 1000;'; // mitigate post-filter recall
|
||||
}
|
||||
private searchAssetBuilder(options: AssetSearchBuilderOptions, kysely: Kysely<DB> = this.prismaRepository.$kysely) {
|
||||
const withExif = (eb: ExpressionBuilder<DB, 'assets'>) =>
|
||||
jsonObjectFrom(eb.selectFrom('exif').selectAll().whereRef('exif.assetId', '=', 'assets.id')).as('exifInfo');
|
||||
|
||||
let runtimeConfig = 'SET LOCAL vectors.enable_prefilter=on; SET LOCAL vectors.search_mode=vbase;';
|
||||
if (numResults) {
|
||||
runtimeConfig += ` SET LOCAL vectors.hnsw_ef_search = ${numResults};`;
|
||||
}
|
||||
const withSmartInfo = (eb: ExpressionBuilder<DB, 'assets'>) =>
|
||||
jsonObjectFrom(eb.selectFrom('smart_info').selectAll().whereRef('smart_info.assetId', '=', 'assets.id')).as(
|
||||
'smartInfo',
|
||||
);
|
||||
|
||||
return runtimeConfig;
|
||||
const withFaces = (eb: ExpressionBuilder<DB, 'assets'>) =>
|
||||
jsonArrayFrom(eb.selectFrom('asset_faces').selectAll().whereRef('asset_faces.assetId', '=', 'assets.id')).as(
|
||||
'faces',
|
||||
);
|
||||
|
||||
const withPeople = (eb: ExpressionBuilder<DB, 'assets' | 'asset_faces'>) =>
|
||||
jsonObjectFrom(eb.selectFrom('person').selectAll().whereRef('asset_faces.personId', '=', 'person.id')).as(
|
||||
'people',
|
||||
);
|
||||
|
||||
options.isArchived ??= options.withArchived ? undefined : false;
|
||||
options.withDeleted ??= !!(options.trashedAfter || options.trashedBefore);
|
||||
const query = kysely
|
||||
.selectFrom('assets')
|
||||
.selectAll('assets')
|
||||
.$if(!!options.createdBefore, (qb) => qb.where('assets.createdAt', '<=', options.createdBefore as Date))
|
||||
.$if(!!options.createdAfter, (qb) => qb.where('assets.createdAt', '>=', options.createdAfter as Date))
|
||||
.$if(!!options.updatedBefore, (qb) => qb.where('assets.updatedAt', '<=', options.updatedBefore as Date))
|
||||
.$if(!!options.updatedAfter, (qb) => qb.where('assets.updatedAt', '>=', options.updatedAfter as Date))
|
||||
.$if(!!options.trashedBefore, (qb) => qb.where('assets.deletedAt', '<=', options.trashedBefore as Date))
|
||||
.$if(!!options.trashedAfter, (qb) => qb.where('assets.deletedAt', '>=', options.trashedAfter as Date))
|
||||
.$if(!!options.takenBefore, (qb) => qb.where('assets.fileCreatedAt', '<=', options.takenBefore as Date))
|
||||
.$if(!!options.takenAfter, (qb) => qb.where('assets.fileCreatedAt', '>=', options.takenAfter as Date))
|
||||
.$if(!!options.city, (qb) =>
|
||||
qb.leftJoin('exif', 'exif.assetId', 'assets.id').where('exif.city', '=', options.city as string),
|
||||
)
|
||||
.$if(!!options.country, (qb) =>
|
||||
qb.leftJoin('exif', 'exif.assetId', 'assets.id').where('exif.country', '=', options.country as string),
|
||||
)
|
||||
.$if(!!options.lensModel, (qb) =>
|
||||
qb.leftJoin('exif', 'exif.assetId', 'assets.id').where('exif.lensModel', '=', options.lensModel as string),
|
||||
)
|
||||
.$if(!!options.make, (qb) =>
|
||||
qb.leftJoin('exif', 'exif.assetId', 'assets.id').where('exif.make', '=', options.make as string),
|
||||
)
|
||||
.$if(!!options.model, (qb) =>
|
||||
qb.leftJoin('exif', 'exif.assetId', 'assets.id').where('exif.model', '=', options.model as string),
|
||||
)
|
||||
.$if(!!options.state, (qb) =>
|
||||
qb.leftJoin('exif', 'exif.assetId', 'assets.id').where('exif.state', '=', options.state as string),
|
||||
)
|
||||
.$if(!!options.checksum, (qb) => qb.where('assets.checksum', '=', options.checksum as Buffer))
|
||||
.$if(!!options.deviceAssetId, (qb) => qb.where('assets.deviceAssetId', '=', options.deviceAssetId as string))
|
||||
.$if(!!options.deviceId, (qb) => qb.where('assets.deviceId', '=', options.deviceId as string))
|
||||
.$if(!!options.id, (qb) => qb.where('assets.id', '=', options.id as string))
|
||||
.$if(!!options.libraryId, (qb) => qb.where('assets.libraryId', '=', options.libraryId as string))
|
||||
.$if(!!options.userIds, (qb) =>
|
||||
qb.where('assets.ownerId', '=', sql<string>`ANY(ARRAY[${options.userIds}]::uuid[])`),
|
||||
)
|
||||
.$if(!!options.encodedVideoPath, (qb) =>
|
||||
qb.where('assets.encodedVideoPath', '=', options.encodedVideoPath as string),
|
||||
)
|
||||
.$if(!!options.originalPath, (qb) => qb.where('assets.originalPath', '=', options.originalPath as string))
|
||||
.$if(!!options.resizePath, (qb) => qb.where('assets.resizePath', '=', options.resizePath as string))
|
||||
.$if(!!options.webpPath, (qb) => qb.where('assets.webpPath', '=', options.webpPath as string))
|
||||
.$if(!!options.originalFileName, (qb) =>
|
||||
qb.where(sql`f_unaccent(assets.originalFileName)`, 'ilike', sql`f_unaccent(${options.originalFileName})`),
|
||||
)
|
||||
.$if(!!options.isExternal, (qb) => qb.where('assets.isExternal', '=', options.isExternal as boolean))
|
||||
.$if(!!options.isFavorite, (qb) => qb.where('assets.isFavorite', '=', options.isFavorite as boolean))
|
||||
.$if(!!options.isOffline, (qb) => qb.where('assets.isOffline', '=', options.isOffline as boolean))
|
||||
.$if(!!options.isReadOnly, (qb) => qb.where('assets.isReadOnly', '=', options.isReadOnly as boolean))
|
||||
.$if(!!options.isVisible, (qb) => qb.where('assets.isVisible', '=', options.isVisible as boolean))
|
||||
.$if(!!options.type, (qb) => qb.where('assets.type', '=', options.type as AssetType))
|
||||
.$if(!!options.isArchived, (qb) => qb.where('assets.isArchived', '=', options.isArchived as boolean))
|
||||
.$if(!!options.isEncoded, (qb) => qb.where('assets.encodedVideoPath', 'is not', null))
|
||||
.$if(!!options.isMotion, (qb) => qb.where('assets.livePhotoVideoId', 'is not', null))
|
||||
.$if(!!options.isNotInAlbum, (qb) =>
|
||||
qb
|
||||
.leftJoin('albums_assets_assets', 'albums_assets_assets.assetsId', 'assets.id')
|
||||
.where('albums_assets_assets.assetsId', 'is', null),
|
||||
)
|
||||
.$if(!!options.withExif, (qb) => qb.select((eb) => withExif(eb)))
|
||||
.$if(!!options.withSmartInfo, (qb) => qb.select((eb) => withSmartInfo(eb)))
|
||||
.$if(!(!options.withFaces || options.withPeople), (qb) =>
|
||||
qb.select((eb) => withFaces(eb)).$if(!!options.withPeople, (qb) => qb.select((eb) => withPeople(eb) as any)),
|
||||
)
|
||||
.$if(!!options.personIds && options.personIds.length > 0, (qb) =>
|
||||
qb.innerJoin(
|
||||
(eb: any) =>
|
||||
eb
|
||||
.selectFrom('asset_faces')
|
||||
.select('asset_faces.assetId')
|
||||
.where('asset_faces.personId', '=', sql`ANY(ARRAY[${options.personIds}]::uuid[])`)
|
||||
.groupBy('asset_faces.assetId')
|
||||
.having(
|
||||
(eb: any) => eb.fn.count('asset_faces.personId').distinct(),
|
||||
'=',
|
||||
(options.personIds as string[]).length,
|
||||
)
|
||||
.as('personAssetIds'),
|
||||
(join) => join.onRef('personAssetIds.assetId' as any, '=', 'assets.id' as any),
|
||||
),
|
||||
)
|
||||
.$if(!options.withDeleted, (qb) => qb.where('assets.deletedAt', 'is', null));
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
// the performance difference between this and the normal way is too huge to ignore, e.g. 3s vs 4ms
|
||||
const assetsByCityCte = `
|
||||
WITH RECURSIVE cte AS (
|
||||
(
|
||||
SELECT city, "assetId"
|
||||
FROM exif
|
||||
INNER JOIN assets ON exif."assetId" = assets.id
|
||||
WHERE "ownerId" IN ($1) AND "isVisible" = $2 AND "isArchived" = $3 AND type = $4
|
||||
ORDER BY city
|
||||
LIMIT 1
|
||||
)
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT l.city, l."assetId"
|
||||
FROM cte c
|
||||
, LATERAL (
|
||||
SELECT city, "assetId"
|
||||
FROM exif
|
||||
INNER JOIN assets ON exif."assetId" = assets.id
|
||||
WHERE city > c.city AND "ownerId" IN ($1) AND "isVisible" = $2 AND "isArchived" = $3 AND type = $4
|
||||
ORDER BY city
|
||||
LIMIT 1
|
||||
) l
|
||||
)
|
||||
`;
|
||||
|
@ -341,7 +341,7 @@ export class AssetServiceV1 {
|
||||
isArchived: dto.isArchived ?? false,
|
||||
duration: dto.duration || null,
|
||||
isVisible: dto.isVisible ?? true,
|
||||
livePhotoVideo: livePhotoAssetId === null ? null : ({ id: livePhotoAssetId } as AssetEntity),
|
||||
livePhotoVideoId: livePhotoAssetId,
|
||||
originalFileName: file.originalName,
|
||||
sidecarPath: sidecarPath || null,
|
||||
isReadOnly: dto.isReadOnly ?? false,
|
||||
|
@ -280,11 +280,15 @@ export class AssetService {
|
||||
smartInfo: true,
|
||||
owner: true,
|
||||
faces: {
|
||||
person: true,
|
||||
include: { person: true },
|
||||
},
|
||||
stack: {
|
||||
assets: {
|
||||
exifInfo: true,
|
||||
include: {
|
||||
assets: {
|
||||
include: {
|
||||
exifInfo: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -316,16 +320,7 @@ export class AssetService {
|
||||
const { description, dateTimeOriginal, latitude, longitude, ...rest } = dto;
|
||||
await this.updateMetadata({ id, description, dateTimeOriginal, latitude, longitude });
|
||||
|
||||
await this.assetRepository.update({ id, ...rest });
|
||||
const asset = await this.assetRepository.getById(id, {
|
||||
exifInfo: true,
|
||||
owner: true,
|
||||
smartInfo: true,
|
||||
tags: true,
|
||||
faces: {
|
||||
person: true,
|
||||
},
|
||||
});
|
||||
const asset = await this.assetRepository.update({ id, ...rest });
|
||||
if (!asset) {
|
||||
throw new BadRequestException('Asset not found');
|
||||
}
|
||||
@ -351,14 +346,16 @@ export class AssetService {
|
||||
} else if (options.stackParentId) {
|
||||
//Creating new stack if parent doesn't have one already. If it does, then we add to the existing stack
|
||||
await this.access.requirePermission(auth, Permission.ASSET_UPDATE, options.stackParentId);
|
||||
const primaryAsset = await this.assetRepository.getById(options.stackParentId, { stack: { assets: true } });
|
||||
const primaryAsset = await this.assetRepository.getById(options.stackParentId, {
|
||||
stack: { include: { assets: true } },
|
||||
});
|
||||
if (!primaryAsset) {
|
||||
throw new BadRequestException('Asset not found for given stackParentId');
|
||||
}
|
||||
let stack = primaryAsset.stack;
|
||||
|
||||
ids.push(options.stackParentId);
|
||||
const assets = await this.assetRepository.getByIds(ids, { stack: { assets: true } });
|
||||
const assets = await this.assetRepository.getByIds(ids, { stack: { include: { assets: true } } });
|
||||
stackIdsToCheckForDelete.push(
|
||||
...new Set(assets.filter((a) => !!a.stackId && stack?.id !== a.stackId).map((a) => a.stackId!)),
|
||||
);
|
||||
@ -422,10 +419,10 @@ export class AssetService {
|
||||
|
||||
const asset = await this.assetRepository.getById(id, {
|
||||
faces: {
|
||||
person: true,
|
||||
include: { person: true },
|
||||
},
|
||||
library: true,
|
||||
stack: { assets: true },
|
||||
stack: { include: { assets: true } },
|
||||
exifInfo: true,
|
||||
});
|
||||
|
||||
@ -494,11 +491,11 @@ export class AssetService {
|
||||
const childIds: string[] = [];
|
||||
const oldParent = await this.assetRepository.getById(oldParentId, {
|
||||
faces: {
|
||||
person: true,
|
||||
include: { person: true },
|
||||
},
|
||||
library: true,
|
||||
stack: {
|
||||
assets: true,
|
||||
include: { assets: true },
|
||||
},
|
||||
});
|
||||
if (!oldParent?.stackId) {
|
||||
|
@ -308,13 +308,7 @@ export class PersonService {
|
||||
return JobStatus.SKIPPED;
|
||||
}
|
||||
|
||||
const relations = {
|
||||
exifInfo: true,
|
||||
faces: {
|
||||
person: false,
|
||||
},
|
||||
};
|
||||
const [asset] = await this.assetRepository.getByIds([id], relations);
|
||||
const [asset] = await this.assetRepository.getByIds([id], { exifInfo: true, faces: true });
|
||||
if (!asset || !asset.resizePath || asset.faces?.length > 0) {
|
||||
return JobStatus.FAILED;
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ export class SmartInfoService {
|
||||
await this.databaseRepository.wait(DatabaseLock.CLIPDimSize);
|
||||
}
|
||||
|
||||
await this.repository.upsert({ assetId: asset.id }, clipEmbedding);
|
||||
await this.repository.upsert(asset.id, clipEmbedding);
|
||||
|
||||
return JobStatus.SUCCESS;
|
||||
}
|
||||
|
@ -37,7 +37,10 @@ export async function* usePagination<T>(
|
||||
}
|
||||
}
|
||||
|
||||
function paginationHelper<Entity extends ObjectLiteral>(items: Entity[], take: number): PaginationResult<Entity> {
|
||||
export function paginationHelper<Entity extends ObjectLiteral>(
|
||||
items: Entity[],
|
||||
take: number,
|
||||
): PaginationResult<Entity> {
|
||||
const hasNextPage = items.length > take;
|
||||
items.splice(take);
|
||||
|
||||
|
@ -19,6 +19,7 @@ import { LibraryRepository } from 'src/repositories/library.repository';
|
||||
import { MoveRepository } from 'src/repositories/move.repository';
|
||||
import { PartnerRepository } from 'src/repositories/partner.repository';
|
||||
import { PersonRepository } from 'src/repositories/person.repository';
|
||||
import { PrismaRepository } from 'src/repositories/prisma.repository';
|
||||
import { SearchRepository } from 'src/repositories/search.repository';
|
||||
import { SharedLinkRepository } from 'src/repositories/shared-link.repository';
|
||||
import { SystemConfigRepository } from 'src/repositories/system-config.repository';
|
||||
@ -62,6 +63,7 @@ const repositories = [
|
||||
MoveRepository,
|
||||
PartnerRepository,
|
||||
PersonRepository,
|
||||
PrismaRepository,
|
||||
SharedLinkRepository,
|
||||
SearchRepository,
|
||||
SystemConfigRepository,
|
||||
|
@ -17,6 +17,7 @@
|
||||
"esModuleInterop": true,
|
||||
"preserveWatchOutput": true,
|
||||
"baseUrl": "./",
|
||||
"noErrorTruncation": true,
|
||||
},
|
||||
"exclude": [
|
||||
"dist",
|
||||
|
Loading…
x
Reference in New Issue
Block a user