Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Cómo encadenar promesas dentro de una migración knex.js

session-pg-simple requiere que se cree una tabla para almacenar sesiones, quiero agregar esta tabla a mi script de migración knex.js.

 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");

Este es el script de creación de tablas de 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) { //... }), ]); };

Los 2 .thens después de la creación de la tabla inicial no se ejecutan, intenté agregarlos al final de Promise.all([]) pero tampoco funcionó. ¿Cómo implemento esas 2 líneas en mi script?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Promise.all no es necesario then , y te perdiste la declaración de return en los bloques. De todos modos, su código se puede simplificar enormemente, knex admite encadenamiento , lo que significa que puede crear una cadena de métodos que se llamarán uno tras otro:

 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 Report

0

Con este código, no necesita modificar la tabla o crear un índice dentro de 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!