@ -1,7 +1,8 @@
"use strict" ;
const Promise = require ( "bluebird" ) ;
const { UniqueViolationError } = require ( "objection" ) ;
// const { UniqueViolationError } = require("objection");
const dateFns = require ( "date-fns" ) ;
const { validateArguments } = require ( "@validatem/core" ) ;
const required = require ( "@validatem/required" ) ;
@ -45,7 +46,13 @@ function taskResultsToObject(taskResults) {
module . exports = function ( { db , knex } ) {
return {
// FIXME: Make object API instead
getItem : function ( tx , id , optional = false ) {
getItem : function ( _tx , _id , _optional ) {
let [ tx , id , optional ] = validateArguments ( arguments , {
tx : [ required , isTX ] ,
id : [ required , isString ] ,
optional : [ defaultTo ( false ) , isBoolean ]
} ) ;
return Promise . try ( ( ) => {
return db . Alias . relatedQuery ( "item" , tx )
. for ( id )
@ -120,7 +127,7 @@ module.exports = function ({ db, knex }) {
insertMissing : true
} ) ;
}
} ) . catch ( { name : "UniqueViolationError" , table : " items" } , ( error ) => {
} ) . catch ( { name : "UniqueViolationError" , table : " srap_ items" } , ( error ) => {
if ( failIfExists ) {
throw error ;
} else {
@ -281,16 +288,20 @@ module.exports = function ({ db, knex }) {
} ]
} ) ;
let promise = db . Alias . query ( tx ) . insert ( {
alias : from ,
itemId : to ,
updatedAt : new Date ( )
// Isolate this operation into a savepoint so that it can fail without breaking the entire transaction
let promise = tx . transaction ( ( tx ) => {
return db . Alias . query ( tx ) . insert ( {
alias : from ,
itemId : to ,
updatedAt : new Date ( )
} ) ;
} ) ;
if ( failIfExists ) {
return promise ;
} else {
return promise . catch ( UniqueViolationError , noop ) ;
return Promise . resolve ( promise )
. catch ( { name : "UniqueViolationError" } , noop ) ;
}
} ,
deleteAlias : function ( _tx , _options ) {
@ -411,9 +422,16 @@ module.exports = function ({ db, knex }) {
} ]
} ) ;
// NOTE: This is a hacky workaround - if we don't do this, then for some reason also entries *at* the exact timestamp are included, which is not what we want.
// FIXME: Verify that this doesn't break anything, eg. when an entry is created inbetween the original timestamp and +1ms.
let actualTimestamp = ( timestamp != null )
? dateFns . addMilliseconds ( timestamp , 1 )
: undefined ;
function applyWhereClauses ( query , idField ) {
if ( timestamp != null ) {
query = query . whereRaw ( ` updated_at > ? ` , [ timestamp ] ) ;
// FIXME: An error in the query here throws an error, resulting in an abort handling bug in a promistream
query = query . whereRaw ( ` updated_at > ? ` , [ actualTimestamp ] ) ;
}
if ( prefix != null ) {