Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mongodb method for accounts api #7

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
import { accountsFindByType, AccountsFindByTypeConfig } from './find-by-type'
import { accountsFindOne, AccountsFindOneConfig } from './find-one'
import { accountsMemcmp, AccountsMemcmpConfig } from './memcmp'
import { accountsMongoDb, AccountsMongoDbConfig } from './mongodb'

export { AccountsAggregateConfig } from './aggregate'
export { AccountsFilterByTypeConfig } from './filter-by-type'
export { AccountsFilterConfig } from './filter'
export { AccountsFindByTypeConfig } from './find-by-type'
export { AccountsFindOneConfig } from './find-one'
export { AccountsMemcmpConfig } from './memcmp'
export { AccountsMongoDbConfig } from './mongodb'

/**
* The Accounts SDK provides a set of functions for querying the Ironforge
Expand All @@ -33,6 +35,10 @@ export class AccountsSdk {
public readonly host: string
) {}

mongodb<T = any>(config: AccountsMongoDbConfig) {
return accountsMongoDb<T>(this.apiKey, this.host, config)
}

/**
* Applies an aggregate function to the accounts of a program.
*
Expand Down
43 changes: 43 additions & 0 deletions src/accounts/mongodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
AccountsMongoDbBody,
AccountsRequestResult,
AccountsRequestResultWithMetadata,
} from '../types'
import { Cluster, requestHeaders, tryExtractResultFromResponse } from '../utils'
import { isAggregateRequest } from '../utils/guards'

/** Configures the accounts request for mongodb. */
export type AccountsMongoDbConfig = {
/** The request body. */
body: AccountsMongoDbBody
/** The cluster to execute the query on, i.e. mainnet or devnet. */
cluster: Cluster
/** The program whose accounts we are querying. */
program: string
/** The cache control header to use for the request. */
cacheControl?: string
}

export async function accountsMongoDb<T = any>(
apiKey: string,
host: string,
config: AccountsMongoDbConfig
) {
const { body, cluster, program, cacheControl } = config

const res = await fetch(
`https://${host}/v1/${cluster}/${program}/mongodb` + `?apiKey=${apiKey}`,
{
headers: requestHeaders({ cacheControl }),
body: JSON.stringify(body),
method: 'POST',
}
)
if (isAggregateRequest(body)) {
return tryExtractResultFromResponse<AccountsRequestResult<T>>(res)
} else {
return tryExtractResultFromResponse<AccountsRequestResultWithMetadata<T>>(
res
)
}
}
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ export type AccountsMemcmpBody = {
export type AccountsAggregateBody = {
pipeline: Record<string, any>[]
}

export type AccountsMongoDbFindBody = {
query?: object
projection?: object
options?: {
limit?: number
skip?: number
sort?: Sort
}
}
export type AccountsMongoDbAggregateBody = {
pipeline: Record<string, any>[]
}

export type AccountsMongoDbBody =
| AccountsMongoDbFindBody
| AccountsMongoDbAggregateBody
17 changes: 17 additions & 0 deletions src/utils/guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
AccountsMongoDbAggregateBody,
AccountsMongoDbBody,
AccountsMongoDbFindBody,
} from '../types'

export function isFindRequest(
body: AccountsMongoDbBody
): body is AccountsMongoDbFindBody {
return (body as AccountsMongoDbFindBody).query != null
}

export function isAggregateRequest(
body: AccountsMongoDbBody
): body is AccountsMongoDbAggregateBody {
return (body as AccountsMongoDbAggregateBody).pipeline != null
}
79 changes: 79 additions & 0 deletions test/accounts-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,82 @@ test('accounts-sdk: memcmp', async (t) => {
})
assert.equal(result.data?.length, 3)
})

// -----------------
// MongoDB
// -----------------
test('accounts-sdk: mongodb', async (t) => {
// TODO: Remove prefix
const sdk = new IronforgeSdk(API_KEY, 'dev')

await t.test('aggregation pipeline', async () => {
const { result, status } = await sdk.accounts.mongodb({
body: {
pipeline: [
{
$match: {
account_type: 'CollectionPDA',
},
},
{
$project: {
account_type: 1,
_id: 1,
},
},
{
$limit: 10,
},
],
},
cluster: 'devnet',
program: 'cndy3Z4yapfJBmL3ShUp5exZKqR3z33thTzeNMm2gRZ',
})

assert.equal(status, 200)

const data = result.data

assert.equal(data.length, 10)

data.forEach((item) => {
assert.equal(Object.keys(item).length, 2)
assert.ok(item.account_type)
assert.ok(item._id)
})
})

await t.test('query with sort + pagination', async () => {
const { result, status } = await sdk.accounts.mongodb({
body: {
query: {
account_type: 'CollectionPDA',
},
options: {
limit: 5,
skip: 10,
sort: [['size', 'asc']],
},
},
cluster: 'devnet',
program: 'cndy3Z4yapfJBmL3ShUp5exZKqR3z33thTzeNMm2gRZ',
})

assert.equal(status, 200)

spok(t, result, {
metadata: { count: 5, offset: 10, limit: 5, hasMore: true },
error: null,
})

const data = result.data
data.forEach((item) => {
assert.equal(item.account_type, 'CollectionPDA')
})

assert.equal(data.length, 5)
for (let i = 0; i < data.length - 1; i++) {
assert.ok(data[i].size <= data[i + 1].size)
}
})
})