Knex์ Promise ์ธํฐํ์ด์ค๊ฐ ์์ผ๋ฏ๋ก ๋น๋๊ธฐ/๋๊ธฐ์ ํจ๊ป Knex๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํฉ๋๋ค. ๋ด ์ฝ๋๋ ์๋์ ์์ต๋๋ค.
const db = makeKnex({
client: 'mysql',
connection: {
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
},
pool: { min: 0, max: 100 },
});
async function getUsers() {
return await db.select()
.from('users')
.limit(10);
}
const res = getUsers();
console.log('KNEX', res);
๋ด ์ฟผ๋ฆฌ์ ํ์ ๋ค์ ๊ฐ์ ธ์ฌ ๊ฒ์ผ๋ก ์์ํ์ง๋ง ์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
KNEX Promise {
_c: [],
_a: undefined,
_s: 0,
_d: false,
_v: undefined,
_h: 0,
_n: false }
๋น์ ์ ์๋ง ํฌ์ฅํด์ผ
const res = getUsers();
console.log('KNEX', res);
async
ํจ์์ ๋ฃ๊ณ getUsers();
(async function() {
const res = await getUsers();
console.log('KNEX', res);
}())
๋ํ ์ค๋ฅ ์ฒ๋ฆฌ(๋ฒ๋ธ๋ง)๋ฅผ ์ํ try/catch์ ๋ํด ์๊ฐํฉ๋๋ค.
ํ ์๋ํฉ๋๋ค
async function test(){
const res = await getUsers();
console.log('KNEX', res);
}
test();
์ด๊ฒ์ knex์ ๊ด๋ จ์ด ์์ง๋ง async/wait ๋ฐ promise์ ๋ํ ์ผ๋ฐ์ ์ธ ๋ด์ฉ์ ๋๋ค. ํ์...
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฒ์ knex์ ๊ด๋ จ์ด ์์ง๋ง async/wait ๋ฐ promise์ ๋ํ ์ผ๋ฐ์ ์ธ ๋ด์ฉ์ ๋๋ค. ํ์...