-- NOTE: This file is auto generated by ./sql-generator

-- ApiKeyRepository.getKey
select
  "api_keys"."id",
  "api_keys"."key",
  "api_keys"."userId",
  "api_keys"."permissions",
  to_json("user") as "user"
from
  "api_keys"
  inner join lateral (
    select
      "users".*,
      (
        select
          array_agg("user_metadata") as "metadata"
        from
          "user_metadata"
        where
          "users"."id" = "user_metadata"."userId"
      ) as "metadata"
    from
      "users"
    where
      "users"."id" = "api_keys"."userId"
      and "users"."deletedAt" is null
  ) as "user" on true
where
  "api_keys"."key" = $1

-- ApiKeyRepository.getById
select
  "id",
  "name",
  "userId",
  "createdAt",
  "updatedAt",
  "permissions"
from
  "api_keys"
where
  "id" = $1::uuid
  and "userId" = $2

-- ApiKeyRepository.getByUserId
select
  "id",
  "name",
  "userId",
  "createdAt",
  "updatedAt",
  "permissions"
from
  "api_keys"
where
  "userId" = $1
order by
  "createdAt" desc
