Ref
Can be used to create references in a query, such as column- or tablenames. This is a good and shorter alternative to using knex.raw('??', 'tableName.columName')
which essentially does the same thing.
Usage
knex.ref
can be used essentially anywhere in a build-chain. Here is an example:
knex(knex.ref('Users').withSchema('TenantId'))
.where(knex.ref('Id'), 1)
.orWhere(knex.ref('Name'), 'Admin')
.select(['Id', knex.ref('Name').as('Username')])
select `Id`, `Name` as `Username` from `TenantId`.`Users` where `Id` = 1 or `Name` = 'Admin'
select "Id", "Name" as "Username" from "TenantId"."Users" where "Id" = 1 or "Name" = 'Admin'
select [Id], [Name] as [Username] from [TenantId].[Users] where [Id] = 1 or [Name] = 'Admin'
select `Id`, `Name` as `Username` from `TenantId`.`Users` where `Id` = 1 or `Name` = 'Admin'
select `Id`, `Name` as `Username` from `TenantId`.`Users` where `Id` = 1 or `Name` = 'Admin'
select "Id", "Name" as "Username" from "TenantId"."Users" where "Id" = 1 or "Name" = 'Admin'
select "Id", "Name" as "Username" from "TenantId"."Users" where "Id" = 1 or "Name" = 'Admin'
select "Id", "Name" as "Username" from "TenantId"."Users" where "Id" = 1 or "Name" = 'Admin'
select "Id", "Name" as "Username" from "TenantId"."Users" where "Id" = 1 or "Name" = 'Admin'
select `Id`, `Name` as `Username` from `TenantId`.`Users` where `Id` = 1 or `Name` = 'Admin'
withSchema
The Ref function supports schema using .withSchema(string)
:
knex(knex.ref('users').withSchema('TenantId')).select()
alias
Alias is supported using .alias(string)
knex('users')
.select(knex.ref('Id').as('UserId'))
select `Id` as `UserId` from `users`
select "Id" as "UserId" from "users"
select [Id] as [UserId] from [users]
select `Id` as `UserId` from `users`
select `Id` as `UserId` from `users`
select "Id" as "UserId" from "users"
select "Id" as "UserId" from "users"
select "Id" as "UserId" from "users"
select "Id" as "UserId" from "users"
select `Id` as `UserId` from `users`