halvardssm / deno-nessie

A modular Deno library for PostgreSQL, MySQL, MariaDB and SQLite migrations
MIT License
527 stars 31 forks source link

[Bug]: Seed database #154

Closed marcosadriano05 closed 2 years ago

marcosadriano05 commented 2 years ago

Operating System

Ubuntu 20.04.4 LTS

Deno version

1.24.0

Nessie version

2.0.6

Bug description

I try to seed the database with some fake data to run integration tests, in the seed file, i try to import the Json file with the data and iterates over the array and save on the database on each value as the code below:

import { AbstractSeed, ClientPostgreSQL, Info } from "../../deps/nessie.ts";
import fakeData from "../../fake_data.json" assert { type: "json" };

const data = fakeData.pdvs;

export default class extends AbstractSeed<ClientPostgreSQL> {
  /** Runs on seed */
  async run(info: Info): Promise<void> {
    for (const value of data) {
      const savedPartner = await this.client.queryArray(
        `INSERT INTO partner (trading_name, owner_name, document) VALUES ($1, $2, $3) RETURNING id;`,
        [
          value.tradingName,
          value.ownerName,
          value.document,
        ],
      );
      const partnerId = savedPartner.rows[0][0];
      await this.client.queryArray(
        `INSERT INTO address (type, coordinates, partner_id) VALUES ($1, $2, $3);`,
        [
          value.address.type,
          value.address.coordinates,
          partnerId,
        ],
      );
      await this.client.queryArray(
        `INSERT INTO coverage_area (type, coordinates, partner_id) VALUES ($1, $2, $3);`,
        [
          value.coverageArea.type,
          value.coverageArea.coordinates,
          partnerId,
        ],
      );
    }
  }
}

When i run the seed command, this is the output:

Task seed:run deno run -A --unstable https://deno.land/x/nessie/cli.ts seed
Starting seeding of 1 files 
----

Seeding populate_partner.ts
PostgresError: bind message supplies 1 parameters, but prepared statement "" requires 3
    at Connection.#preparedQuery (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:805:19)
    at async Connection.query (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:877:16)
    at async default.run (file:///home/marcos/Documentos/Projects/deno/ze-code-challenges-backend/db/seeds/populate_partner.ts:10:28)
    at async ClientPostgreSQL._seed (https://deno.land/x/nessie@2.0.6/clients/AbstractClient.ts:199:7)
    at async ClientPostgreSQL.seed (https://deno.land/x/nessie@2.0.6/clients/ClientPostgreSQL.ts:124:5)
    at async Command.seed [as fn] (https://deno.land/x/nessie@2.0.5/cli.ts:239:3)
    at async Command.execute (https://deno.land/x/cliffy@v0.20.1/command/command.ts:1014:7)
    at async Command.parse (https://deno.land/x/cliffy@v0.20.1/command/command.ts:928:16)
    at async cli (https://deno.land/x/nessie@2.0.5/cli.ts:51:3)
    at async run (https://deno.land/x/nessie@2.0.5/cli.ts:368:5) 
 This error is most likely unrelated to Nessie, and is probably related to the client, the connection config or the query you are trying to execute.

The migrations run command works, but the seed don't. Is my seed code wrong? Maybe is the Json file import that Nessie don't suport.

Steps to reproduce

Steps to reproduce behavior:

  1. Import Json file with fake data to seed the database.
  2. Iterates over the array to insert in the database.
  3. Run seed command.

Aditional information

No response

halvardssm commented 2 years ago

Hi @marcosadriano05 ! This seems to be an issue with how you use the postgres client. I would take a look at the documentation for deno postgres to see if you are passing the arguments wrong. Maybe you can try with the object syntax instead?

const result = await client.queryArray({
    args: [10, 20],
    text: "SELECT ID, NAME FROM PEOPLE WHERE AGE > $1 AND AGE < $2",
  });

I will close this issue as it is not a bug with Nessie.