Importing data into rds using Prisma


#1

Hi, I want to import data from a JSON file into rds using Prisma. This is my current code using Node.js, but it’s not working. Any advice how my code should look like so I can insert multiple nodes? Thanks in advance!:

Mutation: {
        async createPlayer(parent, args, { prisma, db }, info) {
            await db.map(i => {
                if (i.title === '209-players.json') {
                    i.list.map(j => {
                        return prisma.mutation.createPlayer({
                            data: {
                                id: j.id,
                                nickname: j.name
                            },
                        },
                            info
                        )
                    })
                }
            })
        },
}

This is code for the server (I know this one works):

const server = new GraphQLServer({
    typeDefs: './src/graphql-server/schema.graphql'
    resolvers,
    context(req) { 
      return {
        ...req,
        db
        prisma: new Prisma({
          typeDefs: './src/graphql-server/generated/prisma.graphql',
          endpoint: 'https://us1.prisma.sh/public-purplecentaur-310/prisma-graphql/dev',
          debug: true,
        }),
      }
    },
  });

  server.start({ port: process.env.PORT || 4000 }, () => {
    console.log('The server is running at port 4000!')
  })

My db looks like this:

[ { title: '209-players.json',
    list:
     [ { id: 2048,
         name: 'gostaria',
         rankAverage: 1154,
         rank: 1154,
         pointsAverage: 0,
         points: 0 },
       { id: 2,
         name: 'thanh',
         ....
        },
    { title: '209-alliances.json',
    list:
     [ { id: 2048,
         nick: 'anh',
         ...
        }
       { id: 2,
         nick: 'nathan',
         ....
        }
]

My schema.graphql for this looks like this:

type Mutation {
  createPlayer(data: CreatePlayerInput!): Player!
}

input CreatePlayerInput {
  id: ID!
  nick: String!
}

type Player {
  id: ID! 
  nick: String!
  habitatIDs: [Habitat!]!
}

#2

Hi,
Can you please provide your prisma datamodel too so that we may able to help you :blush:


#3

Here’s my datamodel:

type Player {
  id: ID! @unique
  nick: String!
  habitatIDs: [Habitat!]!
  alliance: Alliance!
}

type Alliance {
  id: ID! @unique
  name: String!
  playerArray: [Player!]!
  points: Float!
}

type Habitat {
  id: ID! @unique
  mapX: Float!
  mapY: Float!
  creationDate: String!
  player: Player!
}

Thank you!


#4

Ok, there were many syntax errors in your code, I don’t know that was the cause of your problems

If your movtive is to import data to prisma you dont need to a make a graphQL yoga server for that.
You can do it like so, just run this using node:

// IF you motive is only to import data you dont need a graphql server

// import you db json file too !!!

const prisma = new Prisma({
  typeDefs: "./src/graphql-server/generated/prisma.graphql",
  endpoint: "https://us1.prisma.sh/public-purplecentaur-310/prisma-graphql/dev",
  debug: true
});

async function inserDataToPrisma() {
  db.forEach(root => {
    if (root.title === "209-players.json") {
      root.list.forEach(l => {
        await prisma.mutation.createPlayer({
          data: {
            id: l.id,
            nickname: l.name
          }
        });
      });
    }
  });
}

inserDataToPrisma();

Also remember If you are not planning to use prisma to access that data later, prisma stores relational data a bit differently and you may get into trouble later on. You are fine if you don’t have any relations

Let me know if this helped you and if you have any other issue :slightly_smiling_face:


#5

Thanks so much! I do need to access the data later. But this is great! Thanks again!