• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

401
Vistas
How to seed/upload images in KeystoneJS 6?

Using the example here, if I then add an image field to Post:

// schema.ts
import { list } from '@keystone-6/core';
import { select, relationship, text, timestamp } from '@keystone-6/core/fields';

export const lists = {
  Post: list({
    fields: {
       featureImage: image(),
    }),
    /* ... */
  },
  /* ... */
});

How can I then adjust the seed/index.ts file to upload an image form the local drive?

// seed/index.ts
await context.query.Post.createOne({
    data: {
        ...postData,
        featureImage: { /* ??? What goes here ??? */ }
    },
    query: 'id',
});

Or otherwise, how can I programmatically add images so that keystonejs is aware of them?

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Got from here https://github.com/keystonejs/keystone-discussions-archive/discussions/54

// prepareToUpload.ts
import mime from 'mime'
import fs from 'fs'
import path from 'path'
import { Upload } from 'graphql-upload'

export const prepareToUpload = (filePath: string) => {
  const filename = path.basename(filePath)

  const createReadStream = () => fs.createReadStream(filePath)
  // @ts-ignore
  const mimetype = mime.getType(filePath)
  const encoding = 'utf-8'

  const image = {
    createReadStream,
    filename,
    mimetype,
    encoding,
  }

  const upload = new Upload()
  // @ts-ignore
  upload.resolve(image)

  return upload
}

  await context.query.Film.createOne({
      data: {
        name: filmData.name,
        imagePoster: {
          // imagePoster MUST by a object with `upload` field
          upload: prepareToUpload(__dirname + '/folder_to_local_file/here.jpg'),
        },
      },
      query: 'id',
    })
about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda