then ()์ ์ฌ์ฉํ์ฌ ํ
์ด๋ธ ์์ฑ๊ณผ ํ ์ฝ์
์ ์ฐ๊ฒฐํ๋ ค๊ณ ํฉ๋๋ค.
ํ
์ด๋ธ์ด ์์ฑ๋์์ง๋ง ์ฝ์
์ด ๋ฐ์ํ์ง ์์ต๋๋ค (์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์).
๋๊ตฐ๊ฐ ๋ด๊ฐ ์ด๋์์ ์๋ชป๋์๋์ง ๋งํด ์ค ์ ์์ต๋๊น?
๊ฐ์ฌ
MN
var Promise = require ( 'bluebird');
var Knex = require ( 'knex');
Knex.knex = Knex.initialize ({
ํด๋ผ์ด์ธํธ : 'pg',
์ฐ๊ฒฐ : {
โฆ
},
๋๋ฒ๊ทธ : ์ฐธ
});
var knex = require ( 'knex'). knex;
knex.schema.createTable ( 'department', function (table) {
table.increments ( 'department_id'). primary (). notNullable ();
table.string ( 'department_name', 80) .unique (). notNullable ();
table.string ( '๊ฑด๋ฌผ _ ์ด๋ฆ', 40);
}). then (function () {
console.log ( '๋ถ์ ํ
์ด๋ธ์ด ์์ฑ๋์์ต๋๋ค');
knex ( 'department'). insert (
[{department_name : 'Nano Technology', building_name : 'block-A'},
{department_name : '๊ธฐ๊ณ ๊ณตํ', building_name : 'block-C'}],
'department_id'
);
}). then (function () {
console.log ( '๋ถ์๊ฐ ์ฝ์
๋จ');
}). catch (function (e) {
console.log (e);
});
return
knex
์ฝ์
๋ฌธ ๋๋ ์ฌ๊ธฐ์ ์ฒ๋ฆฌ๊ธฐ๋ฅผ ์ถ๊ฐํด์ผํฉ๋๋ค ( then
๋๋ catch
/ caught
). ๊ทธ๋ ์ง ์์ผ๋ฉด builder
์ธ์คํด์ค๋ฅผ ๋ง๋ค์ง ๋ง ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ง๋ ์์ต๋๋ค. insert
, select
, from
๋ฑ๊ณผ ๊ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถ ํ ๋-๊ธฐ๋ณธ์ ์ผ๋ก ์ฟผ๋ฆฌ ์์ฑ๊ธฐ์ ๋ชจ๋ ๋ฉ์๋-๋ด๋ถ ์ํ ๋ง ์์ ํ์ง๋ง ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ง๋ ์์ต๋๋ค. . ์ด๋ then
, catch
/ caught
, lastly
/ finally
์ฌ์ฉํ์ฌ ์ธ์คํด์ค์ ํ๋ ์ด์์ ํธ๋ค๋ฌ๋ฅผ ์ถ๊ฐ ํ ๋๋ง ๋ฐ์ํฉ๋๋ค. tap
๋๋ exec
. ๊ทธ๋ฌ๋ฉด ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด๋ ์ค์ Promise
์์ฑ๋๊ธฐ ๋๋ฌธ์
๋๋ค.
Knex์ Bookshelf๋ ํ๋ฅญํฉ๋๋ค. ๊ณ ๋ง์
2014 ๋ 1 ์ 16 ์ผ 13:17์ Johannes Lumpe [email protected] ์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
knex insert ๋ฌธ์ ๋ฐํํ๊ฑฐ๋ ์ฌ๊ธฐ์ ํธ๋ค๋ฌ๋ฅผ ์ถ๊ฐํด์ผํฉ๋๋ค (then ๋๋ catch / catch ์ฌ์ฉ). ๊ทธ๋ ์ง ์์ผ๋ฉด ๋น๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ์์ง๋ง ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ง๋ ์์ต๋๋ค. insert, select, frometc์ ๊ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถ ํ ๋. -๊ธฐ๋ณธ์ ์ผ๋ก ์ฟผ๋ฆฌ ์์ฑ๊ธฐ์ ๋ชจ๋ ๋ฉ์๋-๋ด๋ถ ์ํ ๋ง ์์ ํ์ง๋ง ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ง๋ ์์ต๋๋ค. ์ด๋ then, catch / caught, lastly / finally, tap ๋๋ exec๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์คํด์ค์ ํ๋ ์ด์์ ํธ๋ค๋ฌ๋ฅผ ์ถ๊ฐ ํ ๋๋ง ๋ฐ์ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค ํด๋ผ์ด์ธํธ์ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ด๋ ์ค์ Promise๊ฐ ์์ฑ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
์ฒ๋ง์์!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
return
knex
์ฝ์ ๋ฌธ ๋๋ ์ฌ๊ธฐ์ ์ฒ๋ฆฌ๊ธฐ๋ฅผ ์ถ๊ฐํด์ผํฉ๋๋ค (then
๋๋catch
/caught
). ๊ทธ๋ ์ง ์์ผ๋ฉดbuilder
์ธ์คํด์ค๋ฅผ ๋ง๋ค์ง ๋ง ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ง๋ ์์ต๋๋ค.insert
,select
,from
๋ฑ๊ณผ ๊ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถ ํ ๋-๊ธฐ๋ณธ์ ์ผ๋ก ์ฟผ๋ฆฌ ์์ฑ๊ธฐ์ ๋ชจ๋ ๋ฉ์๋-๋ด๋ถ ์ํ ๋ง ์์ ํ์ง๋ง ์ค์ ๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ง๋ ์์ต๋๋ค. . ์ด๋then
,catch
/caught
,lastly
/finally
์ฌ์ฉํ์ฌ ์ธ์คํด์ค์ ํ๋ ์ด์์ ํธ๋ค๋ฌ๋ฅผ ์ถ๊ฐ ํ ๋๋ง ๋ฐ์ํฉ๋๋ค.tap
๋๋exec
. ๊ทธ๋ฌ๋ฉด ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด๋ ์ค์ Promise
์์ฑ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.