Which Approach is Better ot Get Username using ID?

Approach 1:

local username = nk.sql_query([[
        select username
        from users
        where id = $1;
    ]], { user_id })

or

Approach 2:

local user = nk.users_get_id(user_id)[1]
local username = user.username

We usually recommend using the built-in Nakama module functions like nk.users_get_id where possible. Those are supported/maintained through new server versions that may alter the schema, updated with new features, and have very carefully designed query patterns for both robustness and performance.

You should only really use direct SQL queries when addressing a use case not covered by the standard functions, and consider the behaviour and performance of your queries carefully.

2 Likes