Skip to main content

Events

The resolver generator triggers events when a certain operation is completed.

Below is a list of events that can be hooked into:

afterWrite#

Unlike middleware, this callback function is called immediately after a write operation was performed (create or update). This is a good time to make any additional writes to your database that the Collaboration system did not make, or perform some special server functionality after a write was performed.

This callback function can be provided to each table entity.

Entity.afterWrite(params): Promise<UpdatedEntity | void>

  • params (Object)
  • params.data (Object) The data that was written to the database.
  • params.knex (Knex) An instance of Knex that can be used to make additional database queries if needed. This Knex object will already be configured to talk to your database.
  • params.context (Object) The context of this request. Any data set in your middleware will be available here.
  • params.operation (string, one of create or update) The operation that was performed

You can make additional transformations to the data by editing the data object and returning it. If no data is returned, then no transformations are made. See the examples below for more info.

Examples#

Making additional writes to your database

import SQLResolverGenerator, { MutationOperationType } from '@pdftron/collab-sql-resolver-generator';
const resolvers = SQLResolverGenerator({
info: {
Annotations: {
table: 'Annotations',
afterWrite: async ({ data, knex }) => {
// Add some data to a join table outside the context of collaboration
await knex('DocumentAnnotations').insert({
DocumentId: data.documentId,
MarkupId: data.id,
AuthorId: data.authorId
})
},
columns: {
...columnMap
},
},
...etc
}
})

Transforming data

import SQLResolverGenerator, { MutationOperationType } from '@pdftron/collab-sql-resolver-generator';
const resolvers = SQLResolverGenerator({
info: {
Annotations: {
table: 'Annotations',
afterWrite: async ({ data }) => {
// you can transform data here
data.authorId = 'some value'
return data;
},
columns: {
...columnMap
},
},
...etc
}
})