Skip to content

Commit

Permalink
fix(hydra-processor): set createdAt, updatedAt to the block timestamp…
Browse files Browse the repository at this point in the history
… by default (#426)

affects: hydra-e2e-tests, @joystream/hydra-processor

ISSUES CLOSED: #400
  • Loading branch information
dzhelezov authored Jun 21, 2021
1 parent 4add087 commit 18ee4c2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/hydra-e2e-tests/test/e2e/api/graphql-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export const FETCH_INSERTED_AT_FIELD_FROM_TRANSFER = gql`
query {
transfers(limit: 1) {
insertedAt
createdAt
updatedAt
timestamp
}
}
`
Expand Down
12 changes: 10 additions & 2 deletions packages/hydra-e2e-tests/test/e2e/api/processor-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ export async function findTransfersByValue(
return result.transfers
}

export async function fetchDateTimeFieldFromTransfer(): Promise<Date> {
export async function fetchDateTimeFieldFromTransfer(): Promise<{
insertedAt: string
updatedAt: string
createdAt: string
timestamp: string
}> {
const result = await getGQLClient().request<{
transfers: {
insertedAt: string
updatedAt: string
createdAt: string
timestamp: string
}[]
}>(FETCH_INSERTED_AT_FIELD_FROM_TRANSFER)

return new Date(result.transfers[0].insertedAt)
return result.transfers[0]
}

export async function findTransfersByCommentAndWhereCondition(
Expand Down
24 changes: 22 additions & 2 deletions packages/hydra-e2e-tests/test/e2e/transfer-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,28 @@ describe('end-to-end transfer tests', () => {
})

it('fetch datetime field from transfer', async () => {
const date = await fetchDateTimeFieldFromTransfer()
expect(date.getTime()).to.be.lessThan(Date.now())
const {
insertedAt,
createdAt,
updatedAt,
timestamp,
} = await fetchDateTimeFieldFromTransfer()
const ts = Number.parseInt(timestamp)

console.log(`Timestamp: ${timestamp}, ts: ${ts}`)

expect(new Date(updatedAt).getTime()).to.be.equal(
new Date(ts).getTime(),
'should set updatedAt'
)
expect(new Date(createdAt).getTime()).to.be.equal(
new Date(ts).getTime(),
'should set createdAt'
)
expect(new Date(insertedAt).getTime()).to.be.equal(
new Date(ts).getTime(),
'should set insertedAt'
)
})

it('performs full-text-search with filtering options with no result', async () => {
Expand Down
35 changes: 31 additions & 4 deletions packages/hydra-processor/src/executor/TransactionalExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class TransactionalExecutor implements IMappingExecutor {

const { pre, post, mappings } = allMappings

const store = makeDatabaseManager(entityManager)
const store = makeDatabaseManager(entityManager, blockData)

for (const hook of pre) {
await this.mappingsLookup.call(hook, {
Expand Down Expand Up @@ -83,11 +83,12 @@ export class TransactionalExecutor implements IMappingExecutor {
* @param entityManager EntityManager
*/
export function makeDatabaseManager(
entityManager: EntityManager
entityManager: EntityManager,
blockData: BlockData
): DatabaseManager {
return {
save: async <T>(entity: DeepPartial<T>): Promise<void> => {
entity = fillRequiredWarthogFields(entity)
entity = fillRequiredWarthogFields(entity, blockData)
await entityManager.save(entity)
},
remove: async <T>(entity: DeepPartial<T>): Promise<void> => {
Expand Down Expand Up @@ -119,7 +120,10 @@ export function makeDatabaseManager(
*
* @param entity: DeepPartial<T>
*/
function fillRequiredWarthogFields<T>(entity: DeepPartial<T>): DeepPartial<T> {
function fillRequiredWarthogFields<T>(
entity: DeepPartial<T>,
{ block }: BlockData
): DeepPartial<T> {
// eslint-disable-next-line no-prototype-builtins
if (!entity.hasOwnProperty('id')) {
Object.assign(entity, { id: shortid.generate() })
Expand All @@ -132,5 +136,28 @@ function fillRequiredWarthogFields<T>(entity: DeepPartial<T>): DeepPartial<T> {
if (!entity.hasOwnProperty('version')) {
Object.assign(entity, { version: 1 })
}

// set createdAt to the block timestamp if not set
if (
// eslint-disable-next-line no-prototype-builtins
!entity.hasOwnProperty('createdAt') ||
(entity as { createdAt: unknown }).createdAt === undefined
) {
Object.assign(entity, {
createdAt: new Date(block.timestamp),
})
}

// set updatedAt to the block timestamp if not set
if (
// eslint-disable-next-line no-prototype-builtins
!entity.hasOwnProperty('updatedAt') ||
(entity as { updatedAt: unknown }).updatedAt === undefined
) {
Object.assign(entity, {
updatedAt: new Date(block.timestamp),
})
}

return entity
}

0 comments on commit 18ee4c2

Please sign in to comment.