Keep in mind that the term Transactions
here refers to traditional DB transaction concept, where more than one
instruction is sent to the database and if one of those instructions fails all previous executed instructions
will be rolled back.
AWS Neptune Documentation calls it Multithreaded Gremlin Writes
and can only be used for written operations.
In order to use one unique request to insert a set of data into Neptune you can use Loopback ORM transaction method and commit once.
As this feature is not actually supported as a transaction by Neptune, all instructions are stored in memory til the moment you commit it. To avoid memory issues with your application this connector has an internal timeout limit to clean up those transactions from the memory. For more details look at Datasource docs.
Inside a repository method where you have to insert related data with a rollback condition, you should do something like this:
async createRelatedPost(post: PostRequestType) {
const transaction = await this.dataSource.beginTransaction({
timeout: 1000
});
const postData = {
id: uuidv4(),
content: post.content,
createdAt: post.createdAt
};
const postOwnerData = {
owner: 'People/' + post.userId,
post: 'Post/' + postData.id
};
const resPost = await this.create(postData, {transaction});
const resPostOwner = await this.postOwnerRepository.create(postOwnerData, {transaction});
await transaction.commit();
return { post: resPost, postOwner: resPostOwner };
}
- 1 - Datasource
- 2 - Models
- 3 - Repositories
- 4 - CRUD methods
- 5 - Direct Query Execution (Gremlin Bytecode)
- 6 - Transaction