mirror of
https://github.com/aljazceru/landscape-template.git
synced 2025-12-18 23:04:20 +01:00
feat: add HostedImage relation in Category table + data migration to HostedImage
This commit is contained in:
@@ -122,7 +122,6 @@ export interface NexusGenObjects {
|
||||
workplan: string; // String!
|
||||
}
|
||||
Category: { // root type
|
||||
cover_image?: string | null; // String
|
||||
icon?: string | null; // String
|
||||
id: number; // Int!
|
||||
title: string; // String!
|
||||
|
||||
@@ -4,7 +4,8 @@ const {
|
||||
extendType,
|
||||
nonNull,
|
||||
} = require('nexus');
|
||||
const { prisma } = require('../../../prisma')
|
||||
const { prisma } = require('../../../prisma');
|
||||
const resolveImgObjectToUrl = require('../../../utils/resolveImageUrl');
|
||||
|
||||
|
||||
const Category = objectType({
|
||||
@@ -12,7 +13,17 @@ const Category = objectType({
|
||||
definition(t) {
|
||||
t.nonNull.int('id');
|
||||
t.nonNull.string('title');
|
||||
t.string('cover_image');
|
||||
t.string('cover_image', {
|
||||
async resolve(parent) {
|
||||
const imgObject = await prisma.hostedImage.findUnique({
|
||||
where: {
|
||||
id: parent.cover_image_id
|
||||
}
|
||||
});
|
||||
|
||||
return resolveImgObjectToUrl(imgObject);
|
||||
}
|
||||
});
|
||||
t.string('icon');
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "Category" ADD COLUMN "cover_image_id" INTEGER;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Category" ADD CONSTRAINT "Category_cover_image_id_fkey" FOREIGN KEY ("cover_image_id") REFERENCES "HostedImage"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -115,6 +115,8 @@ model Category {
|
||||
id Int @id @default(autoincrement())
|
||||
title String
|
||||
cover_image String?
|
||||
cover_image_id Int?
|
||||
cover_image_rel HostedImage? @relation("CategoryCoverImage", fields: [cover_image_id], references: [id])
|
||||
icon String?
|
||||
|
||||
project Project[]
|
||||
@@ -287,6 +289,7 @@ model HostedImage {
|
||||
|
||||
ProjectThumbnail Project[] @relation("ProjectThumbnail")
|
||||
ProjectCoverImage Project[] @relation("ProjectCoverImage")
|
||||
CategoryCoverImage Category[] @relation("CategoryCoverImage")
|
||||
}
|
||||
|
||||
// -----------------
|
||||
|
||||
@@ -92,7 +92,7 @@ async function migrateOldImages() {
|
||||
let hostedImageId = await _insertInHostedImage(screenshot)
|
||||
projectScreenshotIds.push(hostedImageId);
|
||||
}
|
||||
if(projectScreenshotIds.length > 0) {
|
||||
if (projectScreenshotIds.length > 0) {
|
||||
await _updateObjectWithHostedImageId(prisma.project, project.id, {
|
||||
screenshots_ids: projectScreenshotIds,
|
||||
})
|
||||
@@ -101,7 +101,7 @@ async function migrateOldImages() {
|
||||
/**
|
||||
* Project.cover_image to Project.cover_image_id
|
||||
**/
|
||||
if(project.cover_image) {
|
||||
if (project.cover_image) {
|
||||
let hostedImageId = await _insertInHostedImage(project.cover_image)
|
||||
await _updateObjectWithHostedImageId(prisma.project, project.id, {
|
||||
cover_image_id: hostedImageId,
|
||||
@@ -111,13 +111,31 @@ async function migrateOldImages() {
|
||||
/**
|
||||
* Project.thumbnail_image to Project.thumbnail_image_id
|
||||
**/
|
||||
if(project.cover_image) {
|
||||
if (project.cover_image) {
|
||||
let hostedImageId = await _insertInHostedImage(project.thumbnail_image)
|
||||
await _updateObjectWithHostedImageId(prisma.project, project.id, {
|
||||
thumbnail_image_id: hostedImageId,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Category
|
||||
**/
|
||||
const categories = await prisma.category.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
cover_image: true,
|
||||
}
|
||||
})
|
||||
for (const category of categories) {
|
||||
if (category.cover_image) {
|
||||
let hostedImageId = await _insertInHostedImage(category.cover_image)
|
||||
await _updateObjectWithHostedImageId(prisma.category, category.id, {
|
||||
cover_image_id: hostedImageId,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function _insertInHostedImage(url){
|
||||
|
||||
Reference in New Issue
Block a user