Reputation: 548
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?
Upvotes: 1
Views: 1301
Reputation: 1195
You can get reference from here https://github.com/keystonejs/keystone/blob/main/packages/cloudinary/src/test-fixtures.skip.ts
import { exampleValue } from '../test/@keystone-6/cloudinary/src/test-fixtures.skip';
....
const { context } = req as typeof req & { context: Context };
const data = await context.db.Db_Modal_Name.createOne({
data: {
field: exampleValue(),
},
});
Upvotes: -1
Reputation: 61
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',
})
Upvotes: 1