Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda