Skip to content

Commit

Permalink
feat: Query connections for only a specific connectionId. (#596)
Browse files Browse the repository at this point in the history
* feat: Query connections for only a specific connection.

* docs: update api docs

---------

Co-authored-by: Bastien Beurier <bastienbeurier@gmail.com>
  • Loading branch information
Oluwatobi Adenekan and bastienbeurier authored Apr 27, 2023
1 parent b80c81d commit 92a9269
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
27 changes: 27 additions & 0 deletions docs/docs/reference/connections-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,30 @@ The return value looks like this:
];
}
```



## Getting connections for a specific connection id

Request type: `GET`
Endpoint: `/connection?connectionId=<connectonId>`

Returns a list of minimalistic Connection objects for only a single connectionId. This can be useful if you want to get all the connections that have been created for only a specific connection id. Note that the list does not contain any access credentials and fetching it also does not refresh the access tokens of any Connections.

The return value looks like this:

```js
{
connections: [
{
connection_id: 'connectonId',
provider: '<PROVIDER-CONFIG-KEY-1>',
created: '2023-03-08T09:43:03.725Z'
},
{
//....
}
// Additional objects like the one above
];
}
```
23 changes: 23 additions & 0 deletions docs/docs/reference/node-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,26 @@ The return value looks like this:
];
}
```

## Getting a list of all Connections for a specific connectionId

In the case where you want to get a list of all the connections for only a specific connectionId you can pass the connection ID
```js
let connectionsList = await nango.listConnections("userId");
```

```js
{
connections: [
{
connection_id: 'userId',
provider: '<CONFIG-KEY-1>',
created: '2023-03-08T09:43:03.725Z'
},
{
//....
}
// Additional objects like the one above but only for the same connection Id
];
}
```
11 changes: 7 additions & 4 deletions packages/node-client/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,16 @@ export class Nango {
/**
* Get the list of Connections, which does not contain access credentials.
*/
public async listConnections() {
let response = await this.listConnectionDetails();
public async listConnections(connectionId?: string) {
let response = await this.listConnectionDetails(connectionId);
return response.data;
}

private async listConnectionDetails() {
let url = `${this.serverUrl}/connection`;
private async listConnectionDetails(connectionId?: string) {
let url = `${this.serverUrl}/connection?`;
if (connectionId) {
url.concat(`connectionId=${connectionId}`);
}

let headers = {
'Content-Type': 'application/json',
Expand Down
5 changes: 3 additions & 2 deletions packages/server/lib/controllers/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ class ConnectionController {
}
}

async listConnections(_: Request, res: Response, next: NextFunction) {
async listConnections(req: Request, res: Response, next: NextFunction) {
try {
let accountId = getAccount(res);
let connections: Object[] = await connectionService.listConnections(accountId);
const { connectionId } = req.query;
let connections: Object[] = await connectionService.listConnections(accountId, connectionId as string);

analytics.track('server:connection_list_fetched', accountId);

Expand Down
4 changes: 2 additions & 2 deletions packages/server/lib/db/db.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let dbConfig: { development: Knex.Config<any>; production: Knex.Config<any> } =
extension: 'ts'
},
pool: {
min: parseInt(process.env['NANGO_DB_POOL_MIN'] || "2"),
max: parseInt(process.env['NANGO_DB_POOL_MAX'] || "7") ,
min: parseInt(process.env['NANGO_DB_POOL_MIN'] || '2'),
max: parseInt(process.env['NANGO_DB_POOL_MAX'] || '7')
}
},

Expand Down
8 changes: 6 additions & 2 deletions packages/server/lib/services/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ class ConnectionService {
return connection;
}

async listConnections(accountId: number): Promise<{ id: number; connection_id: number; provider: string; created: string }[]> {
return db.knex
async listConnections(accountId: number, connectionId?: string): Promise<{ id: number; connection_id: number; provider: string; created: string }[]> {
let queryBuilder = db.knex
.withSchema(db.schema())
.from<Connection>(`_nango_connections`)
.select({ id: 'id' }, { connection_id: 'connection_id' }, { provider: 'provider_config_key' }, { created: 'created_at' })
.where({ account_id: accountId });
if (connectionId) {
queryBuilder.where({ connection_id: connectionId });
}
return queryBuilder;
}

async deleteConnection(connectionId: string, providerConfigKey: string, accountId: number): Promise<number> {
Expand Down

0 comments on commit 92a9269

Please sign in to comment.