feat: добавить базовые сервисы image-platform

- добавлены backend, admin, gateway и worker skeleton
- добавлены Drizzle schema, database package и initial migration
- добавлены shared packages для RabbitMQ topology и S3 helpers
- обновлены dev-инфраструктура, env example, scripts и dependencies
- обновлена документация под versioned image URLs и read-through flow
This commit is contained in:
2026-05-05 09:59:21 +03:00
parent 37592c8b81
commit bcadb85a83
66 changed files with 8698 additions and 213 deletions

View File

@@ -0,0 +1,12 @@
import { defineConfig } from "drizzle-kit"
export default defineConfig({
dbCredentials: {
url: process.env.DATABASE_URL ?? "postgres://image:image-password@localhost:5433/image_platform",
},
dialect: "postgresql",
out: "./drizzle",
schema: "./src/schema.ts",
strict: true,
verbose: true,
})

View File

@@ -0,0 +1,72 @@
CREATE TYPE "public"."asset_status" AS ENUM('active', 'disabled', 'deleted');--> statement-breakpoint
CREATE TYPE "public"."requested_format" AS ENUM('auto', 'avif', 'webp', 'jpg', 'png');--> statement-breakpoint
CREATE TYPE "public"."variant_format" AS ENUM('avif', 'webp', 'jpg', 'png');--> statement-breakpoint
CREATE TYPE "public"."variant_status" AS ENUM('pending', 'processing', 'ready', 'failed');--> statement-breakpoint
CREATE TABLE "allowed_image_hosts" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"hostname" text NOT NULL,
"enabled" boolean DEFAULT true NOT NULL,
"description" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "image_asset_versions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"asset_id" uuid NOT NULL,
"version" integer NOT NULL,
"source_url" text NOT NULL,
"source_host" text NOT NULL,
"source_hash" text NOT NULL,
"original_s3_key" text,
"width" integer,
"height" integer,
"content_type" text,
"size_bytes" bigint,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "image_assets" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"public_id" text NOT NULL,
"current_version" integer DEFAULT 1 NOT NULL,
"status" "asset_status" DEFAULT 'active' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "image_variants" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"asset_id" uuid NOT NULL,
"asset_version_id" uuid NOT NULL,
"asset_version" integer NOT NULL,
"preset" text NOT NULL,
"variant_hash" text NOT NULL,
"requested_format" "requested_format" DEFAULT 'auto' NOT NULL,
"format" "variant_format" NOT NULL,
"width" integer NOT NULL,
"height" integer,
"quality" integer NOT NULL,
"s3_key" text NOT NULL,
"content_type" text,
"etag" text,
"status" "variant_status" DEFAULT 'pending' NOT NULL,
"size_bytes" bigint,
"error" text,
"attempt_count" integer DEFAULT 0 NOT NULL,
"last_accessed_at" timestamp with time zone,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "image_asset_versions" ADD CONSTRAINT "image_asset_versions_asset_id_image_assets_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."image_assets"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "image_variants" ADD CONSTRAINT "image_variants_asset_id_image_assets_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."image_assets"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "image_variants" ADD CONSTRAINT "image_variants_asset_version_id_image_asset_versions_id_fk" FOREIGN KEY ("asset_version_id") REFERENCES "public"."image_asset_versions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "allowed_image_hosts_hostname_idx" ON "allowed_image_hosts" USING btree ("hostname");--> statement-breakpoint
CREATE UNIQUE INDEX "image_asset_versions_asset_version_idx" ON "image_asset_versions" USING btree ("asset_id","version");--> statement-breakpoint
CREATE INDEX "image_asset_versions_source_hash_idx" ON "image_asset_versions" USING btree ("source_hash");--> statement-breakpoint
CREATE UNIQUE INDEX "image_assets_public_id_idx" ON "image_assets" USING btree ("public_id");--> statement-breakpoint
CREATE UNIQUE INDEX "image_variants_lookup_idx" ON "image_variants" USING btree ("asset_id","asset_version","preset","width","quality","format");--> statement-breakpoint
CREATE UNIQUE INDEX "image_variants_s3_key_idx" ON "image_variants" USING btree ("s3_key");--> statement-breakpoint
CREATE UNIQUE INDEX "image_variants_variant_hash_idx" ON "image_variants" USING btree ("variant_hash");--> statement-breakpoint
CREATE INDEX "image_variants_status_idx" ON "image_variants" USING btree ("status");

View File

@@ -0,0 +1,604 @@
{
"id": "72292622-d326-46fe-8e6a-90096c7e6634",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.allowed_image_hosts": {
"name": "allowed_image_hosts",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"hostname": {
"name": "hostname",
"type": "text",
"primaryKey": false,
"notNull": true
},
"enabled": {
"name": "enabled",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": true
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"allowed_image_hosts_hostname_idx": {
"name": "allowed_image_hosts_hostname_idx",
"columns": [
{
"expression": "hostname",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_asset_versions": {
"name": "image_asset_versions",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"asset_id": {
"name": "asset_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"version": {
"name": "version",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"source_url": {
"name": "source_url",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_host": {
"name": "source_host",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_hash": {
"name": "source_hash",
"type": "text",
"primaryKey": false,
"notNull": true
},
"original_s3_key": {
"name": "original_s3_key",
"type": "text",
"primaryKey": false,
"notNull": false
},
"width": {
"name": "width",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"content_type": {
"name": "content_type",
"type": "text",
"primaryKey": false,
"notNull": false
},
"size_bytes": {
"name": "size_bytes",
"type": "bigint",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_asset_versions_asset_version_idx": {
"name": "image_asset_versions_asset_version_idx",
"columns": [
{
"expression": "asset_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "version",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_asset_versions_source_hash_idx": {
"name": "image_asset_versions_source_hash_idx",
"columns": [
{
"expression": "source_hash",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"image_asset_versions_asset_id_image_assets_id_fk": {
"name": "image_asset_versions_asset_id_image_assets_id_fk",
"tableFrom": "image_asset_versions",
"tableTo": "image_assets",
"columnsFrom": [
"asset_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_assets": {
"name": "image_assets",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"public_id": {
"name": "public_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"current_version": {
"name": "current_version",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 1
},
"status": {
"name": "status",
"type": "asset_status",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'active'"
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_assets_public_id_idx": {
"name": "image_assets_public_id_idx",
"columns": [
{
"expression": "public_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.image_variants": {
"name": "image_variants",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"asset_id": {
"name": "asset_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"asset_version_id": {
"name": "asset_version_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"asset_version": {
"name": "asset_version",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"preset": {
"name": "preset",
"type": "text",
"primaryKey": false,
"notNull": true
},
"variant_hash": {
"name": "variant_hash",
"type": "text",
"primaryKey": false,
"notNull": true
},
"requested_format": {
"name": "requested_format",
"type": "requested_format",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'auto'"
},
"format": {
"name": "format",
"type": "variant_format",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"width": {
"name": "width",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"quality": {
"name": "quality",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"s3_key": {
"name": "s3_key",
"type": "text",
"primaryKey": false,
"notNull": true
},
"content_type": {
"name": "content_type",
"type": "text",
"primaryKey": false,
"notNull": false
},
"etag": {
"name": "etag",
"type": "text",
"primaryKey": false,
"notNull": false
},
"status": {
"name": "status",
"type": "variant_status",
"typeSchema": "public",
"primaryKey": false,
"notNull": true,
"default": "'pending'"
},
"size_bytes": {
"name": "size_bytes",
"type": "bigint",
"primaryKey": false,
"notNull": false
},
"error": {
"name": "error",
"type": "text",
"primaryKey": false,
"notNull": false
},
"attempt_count": {
"name": "attempt_count",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 0
},
"last_accessed_at": {
"name": "last_accessed_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"image_variants_lookup_idx": {
"name": "image_variants_lookup_idx",
"columns": [
{
"expression": "asset_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "asset_version",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "preset",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "width",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "quality",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "format",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_variants_s3_key_idx": {
"name": "image_variants_s3_key_idx",
"columns": [
{
"expression": "s3_key",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_variants_variant_hash_idx": {
"name": "image_variants_variant_hash_idx",
"columns": [
{
"expression": "variant_hash",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
},
"image_variants_status_idx": {
"name": "image_variants_status_idx",
"columns": [
{
"expression": "status",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"image_variants_asset_id_image_assets_id_fk": {
"name": "image_variants_asset_id_image_assets_id_fk",
"tableFrom": "image_variants",
"tableTo": "image_assets",
"columnsFrom": [
"asset_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
},
"image_variants_asset_version_id_image_asset_versions_id_fk": {
"name": "image_variants_asset_version_id_image_asset_versions_id_fk",
"tableFrom": "image_variants",
"tableTo": "image_asset_versions",
"columnsFrom": [
"asset_version_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {
"public.asset_status": {
"name": "asset_status",
"schema": "public",
"values": [
"active",
"disabled",
"deleted"
]
},
"public.requested_format": {
"name": "requested_format",
"schema": "public",
"values": [
"auto",
"avif",
"webp",
"jpg",
"png"
]
},
"public.variant_format": {
"name": "variant_format",
"schema": "public",
"values": [
"avif",
"webp",
"jpg",
"png"
]
},
"public.variant_status": {
"name": "variant_status",
"schema": "public",
"values": [
"pending",
"processing",
"ready",
"failed"
]
}
},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1777963363578,
"tag": "0000_calm_magdalene",
"breakpoints": true
}
]
}

View File

@@ -0,0 +1,30 @@
{
"name": "@image-platform/database",
"version": "0.1.0",
"private": true,
"type": "module",
"exports": {
".": {
"types": "./src/index.ts",
"default": "./dist/index.js"
}
},
"types": "./src/index.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"db:generate": "drizzle-kit generate --config drizzle.config.ts",
"db:migrate": "drizzle-kit migrate --config drizzle.config.ts",
"db:studio": "drizzle-kit studio --config drizzle.config.ts",
"typecheck": "tsc --noEmit -p tsconfig.json"
},
"dependencies": {
"drizzle-orm": "^0.45.2",
"pg": "^8.20.0"
},
"devDependencies": {
"@types/node": "^25.6.0",
"@types/pg": "^8.20.0",
"drizzle-kit": "^0.31.10",
"typescript": "^6.0.3"
}
}

View File

@@ -0,0 +1,21 @@
import { drizzle } from "drizzle-orm/node-postgres"
import pg from "pg"
import * as schema from "./schema.js"
const { Pool } = pg
export type DatabasePool = pg.Pool
export type Database = ReturnType<typeof createDatabase>
export function createDatabasePool(databaseUrl = process.env.DATABASE_URL) {
if (!databaseUrl) {
throw new Error("DATABASE_URL is required")
}
return new Pool({ connectionString: databaseUrl })
}
export function createDatabase(pool: DatabasePool) {
return drizzle(pool, { schema })
}

View File

@@ -0,0 +1,2 @@
export { createDatabase, createDatabasePool } from "./client.js"
export * from "./schema.js"

View File

@@ -0,0 +1,113 @@
import {
bigint,
boolean,
index,
integer,
pgEnum,
pgTable,
text,
timestamp,
uniqueIndex,
uuid,
} from "drizzle-orm/pg-core"
export const assetStatusEnum = pgEnum("asset_status", ["active", "disabled", "deleted"])
export const variantFormatEnum = pgEnum("variant_format", ["avif", "webp", "jpg", "png"])
export const requestedFormatEnum = pgEnum("requested_format", ["auto", "avif", "webp", "jpg", "png"])
export const variantStatusEnum = pgEnum("variant_status", ["pending", "processing", "ready", "failed"])
const timestamps = {
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
}
export const allowedImageHosts = pgTable(
"allowed_image_hosts",
{
id: uuid("id").primaryKey().defaultRandom(),
hostname: text("hostname").notNull(),
enabled: boolean("enabled").notNull().default(true),
description: text("description"),
...timestamps,
},
(table) => [uniqueIndex("allowed_image_hosts_hostname_idx").on(table.hostname)],
)
export const imageAssets = pgTable(
"image_assets",
{
id: uuid("id").primaryKey().defaultRandom(),
publicId: text("public_id").notNull(),
currentVersion: integer("current_version").notNull().default(1),
status: assetStatusEnum("status").notNull().default("active"),
...timestamps,
},
(table) => [uniqueIndex("image_assets_public_id_idx").on(table.publicId)],
)
export const imageAssetVersions = pgTable(
"image_asset_versions",
{
id: uuid("id").primaryKey().defaultRandom(),
assetId: uuid("asset_id")
.notNull()
.references(() => imageAssets.id, { onDelete: "cascade" }),
version: integer("version").notNull(),
sourceUrl: text("source_url").notNull(),
sourceHost: text("source_host").notNull(),
sourceHash: text("source_hash").notNull(),
originalS3Key: text("original_s3_key"),
width: integer("width"),
height: integer("height"),
contentType: text("content_type"),
sizeBytes: bigint("size_bytes", { mode: "number" }),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
},
(table) => [
uniqueIndex("image_asset_versions_asset_version_idx").on(table.assetId, table.version),
index("image_asset_versions_source_hash_idx").on(table.sourceHash),
],
)
export const imageVariants = pgTable(
"image_variants",
{
id: uuid("id").primaryKey().defaultRandom(),
assetId: uuid("asset_id")
.notNull()
.references(() => imageAssets.id, { onDelete: "cascade" }),
assetVersionId: uuid("asset_version_id")
.notNull()
.references(() => imageAssetVersions.id, { onDelete: "cascade" }),
assetVersion: integer("asset_version").notNull(),
preset: text("preset").notNull(),
variantHash: text("variant_hash").notNull(),
requestedFormat: requestedFormatEnum("requested_format").notNull().default("auto"),
format: variantFormatEnum("format").notNull(),
width: integer("width").notNull(),
height: integer("height"),
quality: integer("quality").notNull(),
s3Key: text("s3_key").notNull(),
contentType: text("content_type"),
etag: text("etag"),
status: variantStatusEnum("status").notNull().default("pending"),
sizeBytes: bigint("size_bytes", { mode: "number" }),
error: text("error"),
attemptCount: integer("attempt_count").notNull().default(0),
lastAccessedAt: timestamp("last_accessed_at", { withTimezone: true }),
...timestamps,
},
(table) => [
uniqueIndex("image_variants_lookup_idx").on(
table.assetId,
table.assetVersion,
table.preset,
table.width,
table.quality,
table.format,
),
uniqueIndex("image_variants_s3_key_idx").on(table.s3Key),
uniqueIndex("image_variants_variant_hash_idx").on(table.variantHash),
index("image_variants_status_idx").on(table.status),
],
)

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false
},
"exclude": ["dist", "node_modules", "drizzle.config.ts"]
}

View File

@@ -0,0 +1,21 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2023"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noUncheckedIndexedAccess": true,
"outDir": "./dist",
"rootDir": "./src",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "ES2023",
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules"]
}