简体   繁体   English

使用种子文件将JSON文件中的数据添加到Prisma数据库

[英]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 我想用JSON文件中的一些数据为我的数据库建立种子

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

Here is my seed.js file 这是我的seed.js文件

//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 当我运行node seed.js
It fails and it throws the following error 它失败并抛出以下错误

UnhandledPromiseRejectionWarning: Error: Variable '$data' expected value of type 'ContinentCreateInput!' UnhandledPromiseRejectionWarning:错误:'ContinentCreateInput!'类型的变量'$ data'预期值 but got: {"data":{"name":"far east","imageURL":"#","destinations":[]}}. 但得到:{“data”:{“name”:“far east”,“imageURL”:“#”,“destination”:[]}}。 Reason: 'name' Expected non-null value, found null. 原因:'name'预期的非null值,找到null。

I believe I am passing correct data there in the correct format. 我相信我正在以正确的格式传递正确的数据。 but It says name field got a null value. 但它说name字段的值为null。 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. 我也提供了Prisma架构的必要部分。

在此输入图像描述

try changing the way you feed data to the db.createContinent function. 尝试更改将数据提供给db.createContinent函数的方式。

 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. 你可以把你在顶部导入的Prisma对象(Prisma客户端)看作辅助类。 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. 在实例化之后,您可以访问javascript函数,这些函数主要反映了根据您的datamodel.graphql文件为您生成的Prisma Server CRUD函数。

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 . 如果查看Prisma客户端页面上的示例文档,您可以看到有两种方法可以传递数据以获得相同的结果 On the one hand you can invoke the javascript function: 一方面你可以调用javascript函数:

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

And on the other hand, you can directly run a GraphQL query on the Prisma endpoint, 另一方面,您可以直接在Prisma端点上运行GraphQL查询,

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. 第二种方法是在GraphQL操作系统中进行,并直接与数据交互。 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. 使用Prisma客户端自动生成的javascript函数的第一个示例是一个包装器,只需要没有data:object包装器的输入数据。 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. 文档并没有真正详细介绍它,我花了一段时间才想知道为什么它不适合你在GraphQL类型中指定的形状。

The first way is the recommended way to abstract away and protect your application layer from the raw database CRUD methods. 第一种方法是从原始数据库CRUD方法中抽象和保护应用程序层的推荐方法。 See Prisma client 请参阅Prisma client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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