-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e22974
commit 04b4724
Showing
28 changed files
with
339 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test/__fixtures__/hasura/metadata/databases/additional/tables/public_additional_table.yaml | ||
test/__fixtures__/hasura/metadata/databases/default/tables/public_default_table.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
Columns, | ||
EventTriggerDefinition, | ||
TableEntry, | ||
} from '../hasura-metadata-dist/HasuraMetadataV2'; | ||
import { | ||
HasuraModuleConfig, | ||
ScheduledEventRetryConfig, | ||
TrackedHasuraEventHandlerConfig, | ||
} from '../hasura.interfaces'; | ||
|
||
const convertEventTriggerDefinition = ( | ||
configDef: TrackedHasuraEventHandlerConfig['definition'] | ||
): EventTriggerDefinition => { | ||
if (configDef.type === 'insert') { | ||
return { | ||
enable_manual: false, | ||
insert: { | ||
columns: Columns.Empty, | ||
}, | ||
}; | ||
} | ||
|
||
if (configDef.type === 'delete') { | ||
return { | ||
enable_manual: false, | ||
delete: { | ||
columns: Columns.Empty, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
enable_manual: false, | ||
update: { | ||
columns: configDef.columns ?? Columns.Empty, | ||
}, | ||
}; | ||
}; | ||
|
||
export const mergeEventHandlerConfig = ( | ||
config: TrackedHasuraEventHandlerConfig, | ||
moduleConfig: HasuraModuleConfig, | ||
defaultRetryConfig: ScheduledEventRetryConfig, | ||
existingTableEntry: TableEntry | ||
): TableEntry['event_triggers'] => { | ||
const { managedMetaDataConfig } = moduleConfig; | ||
|
||
const { triggerName, definition, retryConfig = defaultRetryConfig } = config; | ||
|
||
const eventTriggers = (existingTableEntry.event_triggers ?? []).filter( | ||
(x) => x.name !== triggerName | ||
); | ||
|
||
const { intervalInSeconds, numRetries, timeoutInSeconds } = retryConfig; | ||
|
||
return [ | ||
...eventTriggers, | ||
{ | ||
name: triggerName, | ||
definition: convertEventTriggerDefinition(definition), | ||
retry_conf: { | ||
num_retries: numRetries, | ||
interval_sec: intervalInSeconds, | ||
timeout_sec: timeoutInSeconds, | ||
}, | ||
webhook_from_env: managedMetaDataConfig?.nestEndpointEnvName, | ||
headers: [ | ||
{ | ||
name: moduleConfig.webhookConfig.secretHeader, | ||
value_from_env: managedMetaDataConfig?.secretHeaderEnvName, | ||
}, | ||
], | ||
}, | ||
]; | ||
}; |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { HasuraModule } from '../hasura.module'; | ||
import * as path from 'path'; | ||
import { INestApplication, Injectable } from '@nestjs/common'; | ||
import { TrackedHasuraEventHandler } from '../hasura.decorators'; | ||
import * as fs from 'fs'; | ||
import { safeLoad } from 'js-yaml'; | ||
|
||
@Injectable() | ||
class TestEventHandlerService { | ||
@TrackedHasuraEventHandler({ | ||
databaseName: 'default', | ||
tableName: 'default_table', | ||
triggerName: 'default_table_event_handler', | ||
definition: { | ||
type: 'insert', | ||
}, | ||
}) | ||
public defaultHandler() { | ||
console.log('default'); | ||
} | ||
|
||
@TrackedHasuraEventHandler({ | ||
databaseName: 'additional', | ||
tableName: 'additional_table', | ||
triggerName: 'additional_table_event_handler', | ||
definition: { | ||
type: 'delete', | ||
}, | ||
}) | ||
public additionalHandler() { | ||
console.log('additional'); | ||
} | ||
} | ||
|
||
describe('Hasura Metadata', () => { | ||
describe('v3 metadata', () => { | ||
let app: INestApplication; | ||
|
||
const v3MetadataPath = path.join( | ||
__dirname, | ||
'../../test/__fixtures__/hasura/metadata' | ||
); | ||
|
||
// Ensure that the filesystem is clean so that we can ensure proper metadata comparison | ||
const tables = ['default', 'additional']; | ||
tables.forEach((x) => { | ||
const destinationPath = path.join( | ||
v3MetadataPath, | ||
`databases/${x}/tables/public_${x}_table.yaml` | ||
); | ||
if (fs.existsSync(destinationPath)) { | ||
fs.unlinkSync(destinationPath); | ||
} | ||
fs.copyFileSync(`${destinationPath}.tmpl`, destinationPath); | ||
}); | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
HasuraModule.forRoot(HasuraModule, { | ||
webhookConfig: { | ||
secretFactory: 'secret', | ||
secretHeader: 'NESTJS_SECRET_HEADER', | ||
}, | ||
managedMetaDataConfig: { | ||
dirPath: v3MetadataPath, | ||
secretHeaderEnvName: 'NESTJS_WEBHOOK_SECRET_HEADER_VALUE', | ||
nestEndpointEnvName: 'NESTJS_EVENT_WEBHOOK_ENDPOINT', | ||
defaultEventRetryConfig: { | ||
intervalInSeconds: 15, | ||
numRetries: 3, | ||
timeoutInSeconds: 100, | ||
toleranceSeconds: 21600, | ||
}, | ||
}, | ||
}), | ||
], | ||
providers: [TestEventHandlerService], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
it.each([['default'], ['additional']])( | ||
'manages event handler metadata: %s database', | ||
(d) => { | ||
const tablePath = path.join( | ||
v3MetadataPath, | ||
`databases/${d}/tables/public_${d}_table.yaml` | ||
); | ||
|
||
const actual = fs.readFileSync(tablePath, 'utf-8'); | ||
const expected = fs.readFileSync(`${tablePath}.expected`, 'utf-8'); | ||
|
||
expect(safeLoad(actual)).toEqual(safeLoad(expected)); | ||
} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.