์๋
ํ์ธ์,
๋ ๊ฐ์ ์๋ก ๋ค๋ฅธ ์คํค๋ง์ด์ง๋ง ๋์ผํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ฑธ์ณ ํ
์ด๋ธ ์กฐ์ธ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์
๋๊น?
๊ธฐ์ ์ ์ผ๋ก ์คํค๋ง๋ ๋์ผํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์ ํ
์ด๋ธ์ ๋
ผ๋ฆฌ์ ์ผ๋ก ๋ถ๋ฆฌํ ๊ฒ์ด๋ฏ๋ก SQL ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ ๋์ ์คํค๋ง ์ด๋ฆ์ ์ ๋์ฌ๋ก ๋ถ์ด๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด,
select *
from user_schema.users as users
inner join account_schema.accounts as accounts
on users.users.id = accounts.user_id
์๋ง๋ kenx.js ์๋์ ์ธ ์ ์์ ๊ฒ์ ๋๋ค. ์ข์ ๋ณด์ด๋์?
knex.from('user_schema.users as users').innerJoin('account_schema.accounts as accounts',
'users.id', 'accounts.user_id')
๊ทธ๋ฌ๋ ๊ทธ๋ ๋๋ ๋ณด์๋ค. withSchema
knex.js ์ต์
ํ
์ด๋ธ ์ด๋ฆ์ ์คํค๋ง๋ฅผ ์ ๋์ฌ๋ก ์ฌ์ฉํ๋ ๋์ ํด๋น ์ต์
์ ์ฌ์ฉํ๋ฉด ์ด๋ค ์ด์ ์ด ์์ต๋๊น? .withSchema
์ต์
์ ์ฌ์ฉํ์ฌ ์๋ก ๋ค๋ฅธ ๋ ์คํค๋ง์ ์กฐ์ธ ํ
์ด๋ธ์ ์กฐ์ธํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํฉ๋๊น?
Knex.js Documentation
------------------------
.withSchema([schemaName])
Specifies the schema to be used as prefix of table name.
knex.withSchema('public').select('*').from('users')
Outputs:
select * from `public`.`users`
์ด๊ฒ์ ๋ํ ์ด๋ค ํด๊ฒฐ์ฑ ?
๋ฌธ์์๋ Identifier Syntax, ์ฆ ํ ์ด๋ธ ์ด๋ฆ, ์ด ์ด๋ฆ์๋ ์คํค๋ง๋ฅผ ์ ํํ ์ ์๋ ๊ณต๊ฐ์ด ์์ผ๋ฏ๋ก schemaName.tableName์ ์ํํ๋ ๊ฒฝ์ฐ ์ฟผ๋ฆฌ๊ฐ ์๋ชป ๋ ๋๋ง๋ ์ ์๋ค๊ณ ๋ฌธ์์ ๋์ ์์ต๋๋ค.
๋์ .withSchema('schemaName')
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๊ทธ๋ฌ๋ .withSchema
๋ ๋น๋ ๋ธ๋ก ์ ์ฒด์์ ํ๋์ ์คํค๋ง์์๋ง ์๋ํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค.
์ง๊ธ๊น์ง ๋ค์๊ณผ ๊ฐ์ ์์ ํด๊ฒฐ์ฑ ์ด ์์ต๋๋ค.
speasure ์ฟผ๋ฆฌ์ .withSchema
๋ฅผ ์ ํ ์ฌ์ฉํ์ง ๋ง๊ณ ํ
์ด๋ธ ์ด๋ฆ ๋ฌธ์์ด์ ์คํค๋ง ์ด๋ฆ์ ํฌํจํ์ญ์์ค.
์ด๊ฒ์ ๋ฌธ์์ ๋ฐ๋ํฉ๋๋ค.
.withSchema
์ ํจ๊ป ํ์ ์ฟผ๋ฆฌ ์ฌ์ฉ
{
let rows = await knex.withSchema("sandbox1")
.select('first_name', 'last_name', 'phone', 'site', "sites.name as sname")
.from('users')
.leftJoin(
knex.withSchema('public')
.table('sites')
.select()
.as("sites"),
"sites.id",
"users.site")
console.log(rows)
}
์ด ๋ฐฉ๋ฒ์ ํ์ ์ฟผ๋ฆฌ์์ .as
๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
.joinRaw()
์ฌ์ฉ {
let queryBuilder = knex.withSchema("sandbox1")
.select('first_name', 'last_name', 'phone', 'site', "sites.name as sname")
.from('users')
.joinRaw("left join public.sites as sites ON sites.id = users.site", [])
let sql = queryBuilder.toSQL().toNative()
let rows = await queryBuilder
console.log((sql))
console.log(rows)
}
.raw()
์ฌ์ฉ {
let { rows ,rowCount } = await knex
.raw('SELECT users.first_name, users.last_name, users.phone, users.site, sites.name as sname FROM sandbox1.users, public.sites WHERE sites.id = users.site;');
console.log(rows)
}
์ด๊ฒ์ knex๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ด ์ ์ต๋๋ค.
์ตํ์ ํ.
@codinggirl ์ ๋ต๋ณ์ ์ธ๋ถ ์ ๋ณด๋ฅผ ํ๋ ๋ ์ถ๊ฐํฉ๋๋ค.
joinRaw() ๋ฅผ ์ฌ์ฉํ์ฌ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ธ๋ฉํ ์ ์์ต๋๋ค. ์ฟผ๋ฆฌ ์ ์ฒด์์ ๋์ผํ ๊ตฌ๋ฌธ์ผ๋ก ์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
{
const queryBuilder = knex.withSchema("sandbox1")
.select('first_name', 'last_name', 'phone', 'site', "sites.name as sname")
.from('users')
.joinRaw("left join ?? ON ?? = ??", ["public.sites", "sites.id", "users.site"])
const sql = queryBuilder.toString()
console.log(sql)
}
์ฐธ๊ณ : ์์น ๋ฐ์ธ๋ฉ ? ๊ฐ์ผ๋ก ํด์๋๊ณ ?? ์๋ณ์๋ก ํด์๋ฉ๋๋ค.
PG์ ๋ฐ์ธ๋ฉ์ด ์๋ ์ถ๋ ฅ:
"first_name", "last_name", "phone", "site", "sites"."name"์ "sandbox1"์์ "sname"์ผ๋ก ์ ํํฉ๋๋ค."users" ์ผ์ชฝ ์กฐ์ธ "public"."sites" ON "sites". "id" = "์ฌ์ฉ์"."์ฌ์ดํธ"
MSSQL์ ๋ํ ๋ฐ์ธ๋ฉ์ด ์๋ ์ถ๋ ฅ:
[์ด๋ฆ], [์ฑ], [์ ํ], [์ฌ์ดํธ], [์ฌ์ดํธ].[์ด๋ฆ]์ [sandbox1].[์ฌ์ฉ์] ์ผ์ชฝ ์กฐ์ธ [๊ณต๊ฐ].[์ฌ์ดํธ] ON [์ฌ์ดํธ]์์ [์ด๋ฆ]์ผ๋ก ์ ํํฉ๋๋ค. [id] = [์ฌ์ฉ์].[์ฌ์ดํธ]
ํํธ, ๋ฐ์ธ๋ฉ ์์ด๋ ํญ์ ๋ค์์ ์์ฑํฉ๋๋ค.
"first_name", "last_name", "phone", "site", "sites"๋ฅผ ์ ํํฉ๋๋ค. "sandbox1"์์ "sname"์ผ๋ก "name"์ ์ ํํฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ฌธ์์๋ Identifier Syntax, ์ฆ ํ ์ด๋ธ ์ด๋ฆ, ์ด ์ด๋ฆ์๋ ์คํค๋ง๋ฅผ ์ ํํ ์ ์๋ ๊ณต๊ฐ์ด ์์ผ๋ฏ๋ก schemaName.tableName์ ์ํํ๋ ๊ฒฝ์ฐ ์ฟผ๋ฆฌ๊ฐ ์๋ชป ๋ ๋๋ง๋ ์ ์๋ค๊ณ ๋ฌธ์์ ๋์ ์์ต๋๋ค.
๋์
.withSchema('schemaName')
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.๊ทธ๋ฌ๋
.withSchema
๋ ๋น๋ ๋ธ๋ก ์ ์ฒด์์ ํ๋์ ์คํค๋ง์์๋ง ์๋ํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค.์ง๊ธ๊น์ง ๋ค์๊ณผ ๊ฐ์ ์์ ํด๊ฒฐ์ฑ ์ด ์์ต๋๋ค.
speasure ์ฟผ๋ฆฌ์
.withSchema
๋ฅผ ์ ํ ์ฌ์ฉํ์ง ๋ง๊ณ ํ ์ด๋ธ ์ด๋ฆ ๋ฌธ์์ด์ ์คํค๋ง ์ด๋ฆ์ ํฌํจํ์ญ์์ค.์ด๊ฒ์ ๋ฌธ์์ ๋ฐ๋ํฉ๋๋ค.
.withSchema
์ ํจ๊ป ํ์ ์ฟผ๋ฆฌ ์ฌ์ฉ์ด ๋ฐฉ๋ฒ์ ํ์ ์ฟผ๋ฆฌ์์
.as
๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค..joinRaw()
์ฌ์ฉ.raw()
์ฌ์ฉ์ด๊ฒ์ knex๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ด ์ ์ต๋๋ค.
์ตํ์ ํ.