Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

149
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?

7 months 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',
    })
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos