Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
How to chain promises inside a knex.js migration

session-pg-simple requires a table to be created for storing sessions, I want to add this table to my knex.js migration script.

CREATE TABLE "session" (
  "sid" varchar NOT NULL COLLATE "default",
    "sess" json NOT NULL,
    "expire" timestamp(6) NOT NULL
)
WITH (OIDS=FALSE);

ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;

CREATE INDEX "IDX_session_expire" ON "session" ("expire");

This above is the table creation script from https://github.com/voxpelli/node-connect-pg-simple/blob/HEAD/table.sql

exports.up = function (knex) {
  return Promise.all([
    knex.schema
      .createTable("session", function (table) {
        table.text("sid").notNullable().collate("default");
        table.json("sess").notNullable();
        table.timestamp("expire", { precision: 6 }).notNullable();
      })
      .then(
        knex.schema.raw(
          'ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;'
        )
      )
      .then(
        knex.schema.raw(
          'CREATE INDEX "IDX_session_expire" ON "session" ("expire");'
        )
      ),

    knex.schema.createTable("users", function (table) {
       //...
    }),
  ]);
};

The 2 .thens after the initial table creation don't run, I've tried adding them at the end of Promise.all([]) but it didn't work either. How do I implement those 2 lines into my script?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Promise.all don't need there, and you missed return statement in then blocks. Anyway your code can be greatly simplified, knex supports chaining, that means that you can create a chain of methods that will be called one after another:

exports.up = function (knex) {
  return knex.schema
    .createTable("session", function (table) {
      table.text("sid").notNullable().collate("default");
      table.json("sess").notNullable();
      table.timestamp("expire", { precision: 6 }).notNullable();
    })
    .raw(
      'ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;'
    )
    .raw(
      'CREATE INDEX "IDX_session_expire" ON "session" ("expire");'
    )
    .createTable("users", function (table) {
       //...
    })
};

about 4 years ago · Juan Pablo Isaza Relatório

0

With this code you don't need to alter the table or create an index inside raw:

exports.up = knex => {
    return Promise.all([
        knex.schema.createTable('session', function (table) {
            table.string('sid').primary().notNullable();
            table.json('sess').notNullable();
            table.timestamp('expire', { precision: 6 }).notNullable();
        })
    ]);
};

exports.down = knex => {
    return Promise.all([knex.schema.dropTableIfExists('sessions')
    ]);
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda