Knex: 약속이 λ§ˆμ΄κ·Έλ ˆμ΄μ…˜μ—μ„œ μ˜ˆμƒλŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2014λ…„ 06μ›” 16일  Β·  10μ½”λ©˜νŠΈ  Β·  좜처: knex/knex

(이것은 # 312의 쀑볡 일 수 μžˆμŠ΅λ‹ˆλ‹€.)

λ‹€μŒκ³Ό 같은 λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ 파일 :

exports.up = function(knex, Promise) {
  var first = knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  });

  var second = first.then(function() {
    return knex.schema.createTable('second', function(table) {
      table.increments('id');
      table.string('name');
    });
  });

  return Promise.all([first, second]);
};

exports.down = function(knex, Promise) {

};

λ‹€μŒ 좜λ ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.

{ __cid: '__cid1',
  sql: 'create table "first" ("id" serial primary key, "name" varchar(255))',
  bindings: [] }
{ __cid: '__cid2',
  sql: 'create table "first" ("id" serial primary key, "name" varchar(255))',
  bindings: [] }
{ __cid: '__cid3',
  sql: 'create table "second" ("id" serial primary key, "name" varchar(255))',
  bindings: [] }
error: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
    at Connection.parseE (/home/sohum/node_modules/pg/lib/connection.js:526:11)
    at Connection.parseMessage (/home/sohum/node_modules/pg/lib/connection.js:356:17)
    at Socket.<anonymous> (/home/sohum/node_modules/pg/lib/connection.js:105:22)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:745:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:407:10)
    at emitReadable (_stream_readable.js:403:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ”°λΌμ„œ μ—¬κΈ°μ„œ ν˜Όλž€μŠ€λŸ¬μš΄ 점은 createTable λ©”μ„œλ“œκ°€ promiseλ₯Ό λ°˜ν™˜ν•˜μ§€ μ•Šκ³  였히렀 "thenable"인 SchemaBuilder 객체λ₯Ό λ°˜ν™˜ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. 즉, .then κ°μ²΄λŠ” μœ νš¨ν•œ A + 약속을 λ°˜ν™˜ν•˜μ§€λ§Œ 객체 μžμ²΄λŠ” 약속이 μ•„λ‹™λ‹ˆλ‹€.

이것은 ꡬ문을 μ‚¬μš©ν•  수 μžˆλ„λ‘ νŠΉλ³„νžˆ μˆ˜ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

  return knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  })
  .createTable('second', function(table) {
      table.increments('id');
      table.string('name');
  }).then(function() {
    // all done
  });

λ™μΌν•œ μ—°κ²°μ—μ„œ μ˜ˆμƒλŒ€λ‘œ λ§ˆμ΄κ·Έλ ˆμ΄μ…˜μ„ μˆœμ„œλŒ€λ‘œ μ‹€ν–‰ν•΄μ•Όν•©λ‹ˆλ‹€.

λ˜ν•œ μ—¬κΈ°μ—μ„œ Promise.all κ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œν•˜λ©΄ λ™μΌν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

exports.up = function(knex, Promise) {
  return knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  }).then(function() {
    return knex.schema.createTable('second', function(table) {
      table.increments('id');
      table.string('name');
    });
  });
};

μ›ν•˜λŠ” 경우 λ‹€μŒμ„ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

exports.up = function(knex, Promise) {
  var first = knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  }).then(); // coerce thenable to a promise

  var second = first.then(function() {
    return knex.schema.createTable('second', function(table) {
      table.increments('id');
      table.string('name');
    });
  });

  return Promise.all([first, second]);
};

사양에 μ •μ˜ λœλŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

λͺ¨λ“  10 λŒ“κΈ€

이미 second 을 first μ—°κ²°ν–ˆκΈ° λ•Œλ¬Έμ— Promise.all([first, second]); λ°˜ν™˜ν•΄μ•Όν•œλ‹€κ³  μƒκ°ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. return first 만 μ‹œλ„ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ? 두 λ²ˆμ§ΈλΆ€ν„° createTable μ•ˆμ— then μžˆλŠ” μ˜€ν”„μ˜ μ—°κ²°λ©λ‹ˆλ‹€ first ,에 μΆ”κ°€ ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€ Promise.all 그것이 μ‹€ν–‰ 얻을 것이기 λ•Œλ¬Έμ—, μ–΄μ¨Œλ“  first κ°€ ν•΄κ²°λ˜λŠ” λ™μ•ˆ.

약속 사양은 약속이 μ΄ν–‰λ˜λ©΄ λ‹€μ‹œλŠ” μ΄ν–‰λ˜μ§€ μ•Šμ•„μ•Όν•˜λ©° ν–₯ν›„ λͺ¨λ“  then ν˜ΈμΆœμ€ μΊμ‹œ 된 κ²°κ³Όλ₯Ό λ°˜ν™˜ν•΄μ•Όν•œλ‹€κ³  λ§ν•©λ‹ˆλ‹€. first 및 second κ°€ λͺ¨λ‘ μΆ©μ‘±λ˜λŠ” 경우 Promise.all([first, second]) λŠ” ~ μž‘λ™ν•˜μ§€ μ•Šμ•„μ•Όν•©λ‹ˆλ‹€.

return first 은 λ™μΌν•œ λ™μž‘μ„ν•©λ‹ˆλ‹€. 즉, first , first , second ν…Œμ΄λΈ”μ„ μƒμ„±ν•˜λ €κ³ ν•©λ‹ˆλ‹€. ν•΄λ‹Ή μ‹œλ‚˜λ¦¬μ˜€μ—μ„œ first.then 에 전달 된 ν•¨μˆ˜ λ‚΄λΆ€μ˜ λͺ¨λ“  것을 μ‹€μ œλ‘œ ν‰κ°€ν•˜λŠ” μ½”λ“œλŠ” μ ˆλŒ€λ‘œ μ•ˆλ©λ‹ˆλ‹€.

음, μ΄μƒν•˜κ²Œ 듀리 λ„€μš”. Promise.all μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ©΄ μ„±λŠ₯이 μ•½κ°„ ν–₯상 될 수 μžˆμŠ΅λ‹ˆλ‹€.)

κ·ΈλŸ¬λ‚˜ λ„€, 이것은 잘λͺ»λœ 것 κ°™μŠ΅λ‹ˆλ‹€. μ§€κΈˆμ€ 이것을 λ²„κ·Έλ‘œ ν‘œμ‹œν•˜κ³  @tgriesserκ°€ 울릴 λ•ŒκΉŒμ§€ 기닀릴 κ²ƒμž…λ‹ˆλ‹€.보고 ν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€!

ν•˜ν•˜, λ‚˜λŠ” λ§ˆμ΄κ·Έλ ˆμ΄μ…˜μ—μ„œ 1 ν‹±μ˜ μ„±λŠ₯에 νŠΉλ³„νžˆ μ‹ κ²½ 쓰지 μ•ŠλŠ”λ‹€.

μ£„μ†‘ν•©λ‹ˆλ‹€. μœ„μ˜ μ„€λͺ…을 잘λͺ» μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€. return first 은 (λŠ”) μ—¬μ „νžˆ 두 번째 ν…Œμ΄λΈ”μ„ μƒμ„±ν•΄μ•Όν•©λ‹ˆλ‹€. ν•¨μˆ˜ 결과에 λŒ€ν•΄ .then λ₯Ό 호좜 ν•œ 결과가이λ₯Ό 기닀리지 μ•ŠκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. w / e-μ—¬μ „νžˆ first , first , second !

μ™„μ „νžˆ μ΄ν•΄ν•˜μ§€ λͺ»ν•¨ : 두 번째 ν…Œμ΄λΈ”μ΄ 첫 번째 ν…Œμ΄λΈ”μ΄ 생성 될 λ•ŒκΉŒμ§€ 기닀리지 μ•ŠλŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? then 을 (λ₯Ό) μ‚¬μš©ν•˜μ—¬ 첫 번째 약속을 μ—°κ²°ν•˜κΈ° λ•Œλ¬Έμ— 효과적으둜 μ—°μ†μ μœΌλ‘œ μ‹€ν–‰λ©λ‹ˆλ‹€. 그것은 λ¬Έμ œκ°€λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

두 번째 ν…Œμ΄λΈ” 생성은 κ°€λŠ₯ν•˜μ§€λ§Œ exports.up ν•¨μˆ˜μ˜ κ²°κ³ΌλŠ” 그렇지 μ•ŠμŠ΅λ‹ˆλ‹€. 즉, exports.up().then λ₯Ό ν˜ΈμΆœν•˜λ©΄ first.then κ°€ μ•„λ‹ˆλΌ second.then first.then λ₯Ό ν˜ΈμΆœν•˜λŠ” 것과 κ°™μŠ΅λ‹ˆλ‹€. _is_ first.then !

μ•„, κ±°κΈ°μ—μ„œ 당신을 μ˜€ν•΄ν–ˆμŠ΅λ‹ˆλ‹€-그래 당신이 λ§žμŠ΅λ‹ˆλ‹€!

λ”°λΌμ„œ μ—¬κΈ°μ„œ ν˜Όλž€μŠ€λŸ¬μš΄ 점은 createTable λ©”μ„œλ“œκ°€ promiseλ₯Ό λ°˜ν™˜ν•˜μ§€ μ•Šκ³  였히렀 "thenable"인 SchemaBuilder 객체λ₯Ό λ°˜ν™˜ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. 즉, .then κ°μ²΄λŠ” μœ νš¨ν•œ A + 약속을 λ°˜ν™˜ν•˜μ§€λ§Œ 객체 μžμ²΄λŠ” 약속이 μ•„λ‹™λ‹ˆλ‹€.

이것은 ꡬ문을 μ‚¬μš©ν•  수 μžˆλ„λ‘ νŠΉλ³„νžˆ μˆ˜ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

  return knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  })
  .createTable('second', function(table) {
      table.increments('id');
      table.string('name');
  }).then(function() {
    // all done
  });

λ™μΌν•œ μ—°κ²°μ—μ„œ μ˜ˆμƒλŒ€λ‘œ λ§ˆμ΄κ·Έλ ˆμ΄μ…˜μ„ μˆœμ„œλŒ€λ‘œ μ‹€ν–‰ν•΄μ•Όν•©λ‹ˆλ‹€.

λ˜ν•œ μ—¬κΈ°μ—μ„œ Promise.all κ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œν•˜λ©΄ λ™μΌν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

exports.up = function(knex, Promise) {
  return knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  }).then(function() {
    return knex.schema.createTable('second', function(table) {
      table.increments('id');
      table.string('name');
    });
  });
};

μ›ν•˜λŠ” 경우 λ‹€μŒμ„ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

exports.up = function(knex, Promise) {
  var first = knex.schema.createTable('first', function(table) {
    table.increments('id');
    table.string('name');
  }).then(); // coerce thenable to a promise

  var second = first.then(function() {
    return knex.schema.createTable('second', function(table) {
      table.increments('id');
      table.string('name');
    });
  });

  return Promise.all([first, second]);
};

사양에 μ •μ˜ λœλŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

μ•Œκ² μŠ΅λ‹ˆλ‹€. λͺ°λžμŠ΅λ‹ˆλ‹€-λ°”λ‘œ 닡변을 ν•  수 μ—†μ–΄ ν˜Όλž€ μŠ€λŸ¬μ›Œμ„œ μ£„μ†‘ν•©λ‹ˆλ‹€ @SohumB

λ„€, μš°λ¦¬λŠ” 그것보닀 더 λ³΅μž‘ν•œ μ˜μ‘΄μ„± κ·Έλž˜ν”„λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€. 이것은 μ΅œμ†Œν•œμ˜ ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ˜€μŠ΅λ‹ˆλ‹€! 도와 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš”.

sohum<strong i="8">@diurnal</strong> ~ % cat test.js
var Promise = require('bluebird');

function promises() {
  var first = Promise.resolve('wee');
  var second = first.then(function() {
    console.log('delayin');
    return Promise.delay(1000);
  }).then(function() {
    console.log('done!');
  });
  return first;
}

promises().then(function() { console.log('yep, first is finished'); });

sohum<strong i="9">@diurnal</strong> ~ % node test.js
delayin
yep, first is finished
done!
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰