简体   繁体   中英

Add data from a JSON file to the Prisma database with a seed file

I want to seed my database with some data from a JSON file

//continent-data.json
[
  {
    "continent": "far east",
    "imageURL": "#"
  },
  {
    "continent": "ASIA",
    "imageURL": "#"
  },
]

Here is my seed.js file

//seed.js
const dotenv = require('dotenv');
const { Prisma } = require('./src/generated/prisma-client');
const continentData = require('./continent-data.json');

dotenv.config();

const db = new Prisma({
  secret: process.env.PRISMA_SECRET,
  endpoint: process.env.PRISMA_ENDPOINT
});

const seedContinents = () => {
  // adding continents to the data
  Promise.all(
    continentData.map(async continentItem => {
      const { imageURL, continent } = continentItem;
      const response = await db.createContinent({
        data: {
          name: continent || 'default name',
          imageURL,
        }
      });
      return response;
    })
  );
};

seedContinents();

When I run node seed.js
It fails and it throws the following error

UnhandledPromiseRejectionWarning: Error: Variable '$data' expected value of type 'ContinentCreateInput!' but got: {"data":{"name":"far east","imageURL":"#","destinations":[]}}. Reason: 'name' Expected non-null value, found null.

I believe I am passing correct data there in the correct format. but It says name field got a null value. but the error message itself says that name field has got a string value "far east"

I have provided necessary portions of the Prisma schema as well.

在此输入图像描述

try changing the way you feed data to the db.createContinent function.

 const response = await db.createContinent({
          name: continent || 'default name',
          imageURL,
      });

just pass data instead of creating a wrapper data object.

You can think of Prisma object (Prisma client) which you have imported at the top as a helper class. After your instantiate it you then have access to the javascript functions which mostly mirror the Prisma Server CRUD functions which have been generated for you based on your datamodel.graphql file.

If you look at the example docs on the Prisma client page you can see that there are two ways to pass data to get the same result . On the one hand you can invoke the javascript function:

db.createContinent({
      name: continent || 'default name',
      imageURL,
  });

And on the other hand, you can directly run a GraphQL query on the Prisma endpoint,

mutation {
  createContinent(data: {
    name: continent || 'default name',
    imageURL
  }) {
    name
    continent
  }
} 

The second way you can do in the GraphQL playground and is directly interacting with the data. The first example which uses the Prisma client auto generated javascript functions is a wrapper and only requires the input data without the data: object wrapper. So without the

data: { { ContinentCreateInput object shape }}

The docs don't really cover it in detail, and it took me a while to also wonder why it wouldn't fit the shape you have specified in your GraphQL types.

The first way is the recommended way to abstract away and protect your application layer from the raw database CRUD methods. See Prisma client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM