Skip to content

Commit

Permalink
feat: implement abstract paging logic using prisma-extension-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Sep 8, 2023
1 parent e9e6e56 commit 38a3917
Show file tree
Hide file tree
Showing 168 changed files with 2,147 additions and 2,377 deletions.
177 changes: 94 additions & 83 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,57 @@ input AdminCreateUserInput {
username: String!
}

input AdminFindAssetsInput {
input AdminFindEmailsInput {
ownerId: String!
}

input AdminFindManyAssetInput {
collectionAccount: String
limit: Int = 10
network: NetworkType
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

input AdminFindCollectionCombosInput {
input AdminFindManyCollectionComboInput {
collectionId: String!
limit: Int = 10
network: NetworkType
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

input AdminFindCollectionsInput {
input AdminFindManyCollectionInput {
limit: Int = 10
network: NetworkType
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

input AdminFindDiscordServersInput {
input AdminFindManyDiscordServerInput {
limit: Int = 10
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

input AdminFindEmailsInput {
ownerId: String!
}

input AdminFindIdentitiesInput {
input AdminFindManyIdentityInput {
ownerId: String
provider: IdentityProvider
}

input AdminFindNetworksInput {
input AdminFindManyNetworkInput {
limit: Int = 10
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
type: NetworkType
}

input AdminFindUsersInput {
input AdminFindManyUserInput {
limit: Int = 10
page: Int = 1
role: UserRole
search: String
skip: Int = 0
status: UserStatus
take: Int = 10
}

input AdminReportDiscordMemberWalletsInput {
Expand Down Expand Up @@ -176,6 +176,11 @@ input AssetAttributeInput {
value: String!
}

type AssetPaging {
data: [Asset!]!
meta: PagingMeta!
}

type Collection {
account: String
assetCount: Int
Expand All @@ -199,6 +204,16 @@ type CollectionCombo {
updatedAt: DateTime!
}

type CollectionComboPaging {
data: [CollectionCombo!]!
meta: PagingMeta!
}

type CollectionPaging {
data: [Collection!]!
meta: PagingMeta!
}

"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
Expand All @@ -212,7 +227,7 @@ type DiscordRole {
managed: Boolean
mentionable: Boolean
name: String
permissions: Float
permissions: String
position: Float
serverId: String
}
Expand All @@ -237,7 +252,7 @@ type DiscordServer {
id: String!
name: String
owner: Boolean
permissions: Float
permissions: String
roles: [DiscordRole!]
serverUrl: String
}
Expand All @@ -248,6 +263,11 @@ type DiscordServerChannel {
type: String!
}

type DiscordServerPaging {
data: [DiscordServer!]!
meta: PagingMeta!
}

type Email {
createdAt: DateTime!
default: Boolean
Expand Down Expand Up @@ -387,6 +407,11 @@ type Network {
updatedAt: DateTime!
}

type NetworkPaging {
data: [Network!]!
meta: PagingMeta!
}

type NetworkToken {
address: String!
createdAt: DateTime!
Expand All @@ -405,56 +430,50 @@ enum NetworkType {
SolanaMainnet
}

type Paging {
count: Int
skip: Int
take: Int
total: Int
type PagingMeta {
currentPage: Int!
isFirstPage: Boolean!
isLastPage: Boolean!
nextPage: Int
pageCount: Int
previousPage: Int
totalCount: Int
}

type Query {
adminDevCheckAccount(address: String!, type: NetworkType!): JSON
adminDevCheckIdentity(provider: IdentityProvider!, providerId: String!): JSON
adminFindAssets(input: AdminFindAssetsInput!): [Asset!]
adminFindAssetsCount(input: AdminFindAssetsInput!): Paging
adminFindCollectionCombos(input: AdminFindCollectionCombosInput!): [CollectionCombo!]
adminFindCollectionCombosCount(input: AdminFindCollectionCombosInput!): Paging
adminFindCollections(input: AdminFindCollectionsInput!): [Collection!]
adminFindCollectionsCount(input: AdminFindCollectionsInput!): Paging
adminFindDiscordServers(input: AdminFindDiscordServersInput!): [DiscordServer!]
adminFindDiscordServersCount(input: AdminFindDiscordServersInput!): Paging
adminFindEmails(input: AdminFindEmailsInput!): [Email!]
adminFindIdentities(input: AdminFindIdentitiesInput!): [Identity!]
adminFindNetworks(input: AdminFindNetworksInput!): [Network!]
adminFindNetworksCount(input: AdminFindNetworksInput!): Paging
adminFindUsers(input: AdminFindUsersInput!): [User!]
adminFindUsersCount(input: AdminFindUsersInput!): Paging
adminGetAsset(assetId: String!): Asset
adminFindManyAsset(input: AdminFindManyAssetInput!): AssetPaging!
adminFindManyCollection(input: AdminFindManyCollectionInput!): CollectionPaging!
adminFindManyCollectionCombo(input: AdminFindManyCollectionComboInput!): CollectionComboPaging!
adminFindManyDiscordServer(input: AdminFindManyDiscordServerInput!): DiscordServerPaging!
adminFindManyDiscordServerChannel(serverId: String!): [DiscordServerChannel!]
adminFindManyIdentity(input: AdminFindManyIdentityInput!): [Identity!]
adminFindManyNetwork(input: AdminFindManyNetworkInput!): NetworkPaging!
adminFindManyUser(input: AdminFindManyUserInput!): UserPaging!
adminFindOneAsset(assetId: String!): Asset
adminFindOneCollection(collectionId: String!): Collection
adminFindOneCollectionCombo(collectionComboId: String!): CollectionCombo
adminFindOneDiscordServer(serverId: String!): DiscordServer
adminFindOneNetwork(networkId: String!): Network
adminFindOneUser(userId: String!): User
adminGetBotInviteUrl: String
adminGetCollection(collectionId: String!): Collection
adminGetCollectionCombo(collectionComboId: String!): CollectionCombo
adminGetDiscordServer(serverId: String!): DiscordServer
adminGetDiscordServerChannels(serverId: String!): [DiscordServerChannel!]
adminGetNetwork(networkId: String!): Network
adminGetQueue(type: QueueType!): Queue
adminGetQueueJobs(statuses: [JobStatus!]!, type: QueueType!): [Job!]
adminGetQueues: [Queue!]
adminGetUser(userId: String!): User
adminReportDiscordMemberWallets(input: AdminReportDiscordMemberWalletsInput!): JSON
appConfig: AppConfig!
me: User
uptime: Float!
userFindAssets(input: UserFindAssetsInput!): [Asset!]
userFindAssetsCount(input: UserFindAssetsInput!): Paging
userFindCollections(input: UserFindCollectionsInput!): [Collection!]
userFindCollectionsCount(input: UserFindCollectionsInput!): Paging
userFindIdentities: [Identity!]
userFindUsers(input: UserFindUsersInput!): [User!]
userFindUsersCount(input: UserFindUsersInput!): Paging
userGetAsset(assetId: String!): Asset
userGetCollection(collectionId: String!): Collection
userGetDiscordServers: [DiscordServer!]
userGetUserByUsername(username: String!): User
userFindManyAsset(input: UserFindManyAssetInput!): AssetPaging!
userFindManyCollection(input: UserFindManyCollectionInput!): CollectionPaging!
userFindManyDiscordServer: [DiscordServer!]
userFindManyIdentity: [Identity!]
userFindManyUser(input: UserFindManyUserInput!): UserPaging!
userFindOneAsset(assetId: String!): Asset
userFindOneCollection(collectionId: String!): Collection
userFindOneUser(username: String!): User
userRequestIdentityChallenge(input: RequestIdentityChallengeInput!): IdentityChallenge
}

Expand Down Expand Up @@ -495,52 +514,44 @@ input RequestIdentityChallengeInput {
}

type User {
allowDm: Boolean
avatarUrl: String
createdAt: DateTime
description: String
devStack: String
devType: String
devYears: String
developer: Boolean
discordUrl: String
githubUrl: String
id: String!
language: String
location: String
name: String
profileUrl: String
role: UserRole
status: UserStatus
telegramUrl: String
twitterUrl: String
updatedAt: DateTime
username: String
verified: Boolean
websiteUrl: String
}

input UserFindAssetsInput {
input UserFindManyAssetInput {
attributes: [AssetAttributeInput!]
collectionAccount: String
limit: Int = 10
network: NetworkType
ownerId: String
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

input UserFindCollectionsInput {
input UserFindManyCollectionInput {
limit: Int = 10
network: NetworkType
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

input UserFindUsersInput {
input UserFindManyUserInput {
limit: Int = 10
page: Int = 1
search: String
skip: Int = 0
take: Int = 10
}

type UserPaging {
data: [User!]!
meta: PagingMeta!
}

enum UserRole {
Expand Down
10 changes: 5 additions & 5 deletions libs/api/asset/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './lib/api-asset-data-access.module'
export * from './lib/api-asset.service'
export * from './lib/dto/admin-find-assets.input'
export * from './lib/dto/user-find-assets.input'
export * from './lib/entity/asset.entity'
export * from './lib/entity/asset-attribute.entity'
export * from './lib/dto/admin-find-many-asset.input'
export * from './lib/dto/user-find-many-asset.input'
export * from './lib/entity/asset-attribute-input'
export * from './lib/helpers/parse-user-find-assets.input'
export * from './lib/entity/asset-attribute.entity'
export * from './lib/entity/asset-paging'
export * from './lib/entity/asset.entity'
36 changes: 14 additions & 22 deletions libs/api/asset/data-access/src/lib/api-asset-admin.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, Logger } from '@nestjs/common'
import { Asset as PrismaAsset } from '@prisma/client'
import { ApiCoreService, Paging } from '@pubkey-link/api/core/data-access'
import { ApiCoreService } from '@pubkey-link/api/core/data-access'
import { ApiNetworkService } from '@pubkey-link/api/network/data-access'
import { AdminFindAssetsInput } from './dto/admin-find-assets.input'
import { parseAdminFindAssetsInput } from './helpers/parse-admin-find-assets.input'
import { AdminFindManyAssetInput } from './dto/admin-find-many-asset.input'
import { AssetPaging } from './entity/asset-paging'
import { getAdminASsetWhereInput } from './helpers/get-admin-a-sset-where-input'

@Injectable()
export class ApiAssetAdminService {
Expand All @@ -23,28 +23,20 @@ export class ApiAssetAdminService {
return true
}

async findAssets(adminId: string, input: AdminFindAssetsInput): Promise<PrismaAsset[]> {
async findManyAsset(adminId: string, input: AdminFindManyAssetInput): Promise<AssetPaging> {
await this.core.ensureUserAdmin(adminId)

const { where, orderBy, take, skip, include } = parseAdminFindAssetsInput(input)
const items = await this.core.data.asset.findMany({ where, orderBy, take, skip, include })

return items ?? []
}

async findAssetsCount(adminId: string, input: AdminFindAssetsInput): Promise<Paging> {
await this.core.ensureUserAdmin(adminId)

const { where, orderBy, take, skip } = parseAdminFindAssetsInput(input)
const [count, total] = await Promise.all([
this.core.data.asset.count({ where, orderBy, take, skip }),
this.core.data.asset.count({ where, orderBy }),
])

return { count, skip, take, total }
return this.core.data.asset
.paginate({
include: { identity: { include: { owner: true } } },
orderBy: [{ identityId: 'asc' }, { name: 'asc' }],
where: getAdminASsetWhereInput(input),
})
.withPages({ limit: input.limit, page: input.page })
.then(([data, meta]) => ({ data, meta }))
}

async getAsset(adminId: string, assetId: string) {
async findOneAsset(adminId: string, assetId: string) {
await this.core.ensureUserAdmin(adminId)
const found = await this.core.data.asset.findUnique({ where: { id: assetId } })
if (!found) {
Expand Down
Loading

0 comments on commit 38a3917

Please sign in to comment.