Skip to content

Commit

Permalink
feat: Add method to check if user api is provisioned
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Jul 18, 2023
1 parent 07e61b5 commit 992cc20
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ exports[`HasuraClient checks if a schema exists 1`] = `
}
`;

exports[`HasuraClient checks if datasource exists 1`] = `
{
"args": {},
"type": "export_metadata",
"version": 2,
}
`;

exports[`HasuraClient creates a schema 1`] = `
{
"args": {
Expand Down
13 changes: 12 additions & 1 deletion indexer-js-queue-handler/hasura-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class HasuraClient {
return JSON.parse(body)
};

async executeMetadataRequest (type, args) {
async executeMetadataRequest (type, args, version) {
const response = await this.deps.fetch(`${process.env.HASURA_ENDPOINT}/v1/metadata`, {
method: 'POST',
headers: {
Expand All @@ -45,6 +45,7 @@ export default class HasuraClient {
body: JSON.stringify({
type,
args,
...(version && { version })
}),
});

Expand All @@ -61,6 +62,16 @@ export default class HasuraClient {
return this.executeMetadataRequest('bulk', metadataRequests);
}

async exportMetadata() {
const { metadata } = await this.executeMetadataRequest('export_metadata', {}, 2);
return metadata;
}

async doesSourceExist(sourceName) {
const metadata = await this.exportMetadata();
return metadata.sources.filter(({ name }) => name === sourceName).length > 0;
}

async isSchemaCreated (schemaName) {
const { result } = await this.executeSql(
`SELECT schema_name FROM information_schema.schemata WHERE schema_name = '${schemaName}'`,
Expand Down
22 changes: 22 additions & 0 deletions indexer-js-queue-handler/hasura-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ describe('HasuraClient', () => {
expect(JSON.parse(fetch.mock.calls[0][1].body)).toMatchSnapshot();
});

it('checks if datasource exists', async () => {
const fetch = jest
.fn()
.mockResolvedValue({
status: 200,
text: () => JSON.stringify({
metadata: {
sources: [
{
name: 'name'
}
]
},
}),
});
const client = new HasuraClient({ fetch })

await expect(client.doesSourceExist('name')).resolves.toBe(true);
expect(fetch.mock.calls[0][1].headers['X-Hasura-Admin-Secret']).toBe(HASURA_ADMIN_SECRET)
expect(JSON.parse(fetch.mock.calls[0][1].body)).toMatchSnapshot();
})

it('runs migrations for the specified schema', async () => {
const fetch = jest
.fn()
Expand Down
4 changes: 4 additions & 0 deletions indexer-js-queue-handler/provisioner.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export default class Provisioner {
return this.hasuraClient.isSchemaCreated(schemaName);
}

async isUserApiProvisioned(databaseName) {
return this.hasuraClient.doesSourceExist(databaseName);
}

async createSchema(schemaName) {
try {
await this.hasuraClient.createSchema(schemaName);
Expand Down

0 comments on commit 992cc20

Please sign in to comment.