@@ -15,7 +15,7 @@ import PeopleRoomsCustomIdSeparator from './people-rooms-custom-id-separator';
1515import Company from './company' ;
1616import Employee from './employee' ;
1717import Client from './client' ;
18- import { Model } from 'objection' ;
18+ import { Model , UniqueViolationError } from 'objection' ;
1919
2020const testSuite = adapterTests ( [
2121 '.options' ,
@@ -238,6 +238,7 @@ function clean (done) {
238238 table . json ( 'jsonArray' ) ;
239239 table . jsonb ( 'jsonbObject' ) ;
240240 table . jsonb ( 'jsonbArray' ) ;
241+ table . unique ( 'name' ) ;
241242 } ) ;
242243 } ) ;
243244 } ) ;
@@ -260,6 +261,7 @@ function clean (done) {
260261 table . increments ( 'id' ) ;
261262 table . integer ( 'companyId' ) ;
262263 table . string ( 'name' ) ;
264+ table . unique ( 'name' ) ;
263265 } )
264266 . then ( ( ) => done ( ) ) ;
265267 } ) ;
@@ -1063,18 +1065,18 @@ describe('Feathers Objection Service', () => {
10631065 return companies
10641066 . create ( [
10651067 {
1066- name : 'Google ' ,
1068+ name : 'Facebook ' ,
10671069 clients : [
10681070 {
1069- name : 'Dan Davis '
1071+ name : 'Danny Lapierre '
10701072 } ,
10711073 {
1072- name : 'Ken Patrick '
1074+ name : 'Kirck Filty '
10731075 }
10741076 ]
10751077 } ,
10761078 {
1077- name : 'Apple '
1079+ name : 'Yubico '
10781080 }
10791081 ] ) . then ( ( ) => {
10801082 companies . createUseUpsertGraph = false ;
@@ -1844,6 +1846,157 @@ describe('Feathers Objection Service', () => {
18441846 } ) ;
18451847 } ) ;
18461848 } ) ;
1849+
1850+ it ( 'works with atomic' , ( ) => {
1851+ return people . create ( { name : 'Rollback' } , { transaction, $atomic : true } ) . then ( ( ) => {
1852+ expect ( transaction . trx . isCompleted ( ) ) . to . equal ( false ) ; // Atomic must be ignored and transaction still running
1853+ return transaction . trx . rollback ( ) . then ( ( ) => {
1854+ return people . find ( { query : { name : 'Rollback' } } ) . then ( ( data ) => {
1855+ expect ( data . length ) . to . equal ( 0 ) ;
1856+ } ) ;
1857+ } ) ;
1858+ } ) ;
1859+ } ) ;
1860+ } ) ;
1861+
1862+ describe ( 'Atomic Transactions' , ( ) => {
1863+ before ( async ( ) => {
1864+ await companies
1865+ . create ( [
1866+ {
1867+ name : 'Google' ,
1868+ clients : [
1869+ {
1870+ name : 'Dan Davis'
1871+ } ,
1872+ {
1873+ name : 'Ken Patrick'
1874+ }
1875+ ]
1876+ } ,
1877+ {
1878+ name : 'Apple'
1879+ }
1880+ ] ) ;
1881+ } ) ;
1882+
1883+ after ( async ( ) => {
1884+ await clients . remove ( null ) ;
1885+ await companies . remove ( null ) ;
1886+ } ) ;
1887+
1888+ it ( 'Rollback on sub insert failure' , ( ) => {
1889+ // Dan Davis already exists
1890+ return companies . create ( { name : 'Compaq' , clients : [ { name : 'Dan Davis' } ] } , { $atomic : true } ) . catch ( ( error ) => {
1891+ expect ( error instanceof errors . GeneralError ) . to . be . ok ;
1892+ expect ( error . message ) . to . match ( / S Q L I T E _ C O N S T R A I N T : U N I Q U E / ) ;
1893+ return companies . find ( { query : { name : 'Compaq' , $eager : 'clients' } } ) . then (
1894+ ( data ) => {
1895+ expect ( data . length ) . to . equal ( 0 ) ;
1896+ } ) ;
1897+ } ) ;
1898+ } ) ;
1899+
1900+ it ( 'Rollback on multi insert failure' , ( ) => {
1901+ // Google already exists
1902+ return companies . create ( [ { name : 'Google' } , { name : 'Compaq' } ] , { $atomic : true } ) . catch ( ( error ) => {
1903+ expect ( error instanceof errors . GeneralError ) . to . be . ok ;
1904+ expect ( error . message ) . to . match ( / S Q L I T E _ C O N S T R A I N T : U N I Q U E / ) ;
1905+ return companies . find ( { query : { name : 'Compaq' } } ) . then (
1906+ ( data ) => {
1907+ expect ( data . length ) . to . equal ( 0 ) ;
1908+ } ) ;
1909+ } ) ;
1910+ } ) ;
1911+
1912+ it ( 'Rollback on update failure' , ( ) => {
1913+ // Dan Davis appears twice, so clients must stay as it is
1914+ return companies . find ( { query : { name : 'Google' } } ) . then ( data => {
1915+ return companies . update ( data [ 0 ] . id , {
1916+ name : 'Google' ,
1917+ clients : [
1918+ {
1919+ name : 'Dan Davis'
1920+ } ,
1921+ {
1922+ name : 'Dan Davis'
1923+ } ,
1924+ {
1925+ name : 'Kirk Maelström'
1926+ }
1927+ ]
1928+ } , { $atomic : true } ) . catch ( ( error ) => {
1929+ expect ( error instanceof errors . GeneralError ) . to . be . ok ;
1930+ expect ( error . message ) . to . match ( / S Q L I T E _ C O N S T R A I N T : U N I Q U E / ) ;
1931+ return companies . find ( { query : { name : 'Google' , $eager : 'clients' } } ) . then (
1932+ ( data ) => {
1933+ expect ( data . length ) . to . equal ( 1 ) ;
1934+ expect ( data [ 0 ] . clients . length ) . to . equal ( 2 ) ;
1935+ expect ( data [ 0 ] . clients [ 0 ] . name ) . to . equal ( 'Dan Davis' ) ;
1936+ expect ( data [ 0 ] . clients [ 1 ] . name ) . to . equal ( 'Ken Patrick' ) ;
1937+ } ) ;
1938+ } ) ;
1939+ } ) ;
1940+ } ) ;
1941+
1942+ it ( 'Rollback on patch failure' , ( ) => {
1943+ // Dan Davis appears twice, so clients must stay as it is
1944+ return companies . find ( { query : { name : 'Google' } } ) . then ( data => {
1945+ return companies . patch ( data [ 0 ] . id , {
1946+ clients : [
1947+ {
1948+ name : 'Dan Davis'
1949+ } ,
1950+ {
1951+ name : 'Dan Davis'
1952+ } ,
1953+ {
1954+ name : 'Kirk Maelström'
1955+ }
1956+ ]
1957+ } , { $atomic : true } ) . catch ( ( error ) => {
1958+ expect ( error instanceof UniqueViolationError ) . to . be . ok ;
1959+ expect ( error . message ) . to . match ( / S Q L I T E _ C O N S T R A I N T : U N I Q U E / ) ;
1960+ return companies . find ( { query : { name : 'Google' , $eager : 'clients' } } ) . then (
1961+ ( data ) => {
1962+ expect ( data . length ) . to . equal ( 1 ) ;
1963+ expect ( data [ 0 ] . clients . length ) . to . equal ( 2 ) ;
1964+ expect ( data [ 0 ] . clients [ 0 ] . name ) . to . equal ( 'Dan Davis' ) ;
1965+ expect ( data [ 0 ] . clients [ 1 ] . name ) . to . equal ( 'Ken Patrick' ) ;
1966+ } ) ;
1967+ } ) ;
1968+ } ) ;
1969+ } ) ;
1970+
1971+ it ( 'Commit on patch success' , ( ) => {
1972+ // Dan Davis appears twice, so clients must stay as it is
1973+ return companies . find ( { query : { name : 'Google' } } ) . then ( data => {
1974+ return companies . patch ( data [ 0 ] . id , {
1975+ clients : [
1976+ {
1977+ name : 'Dan David'
1978+ } ,
1979+ {
1980+ name : 'Dan Davis'
1981+ } ,
1982+ {
1983+ name : 'Kirk Maelström'
1984+ }
1985+ ]
1986+ } , { $atomic : true } ) . catch ( ( error ) => {
1987+ expect ( error instanceof UniqueViolationError ) . to . be . ok ;
1988+ expect ( error . message ) . to . match ( / S Q L I T E _ C O N S T R A I N T : U N I Q U E / ) ;
1989+ return companies . find ( { query : { name : 'Google' , $eager : 'clients' } } ) . then (
1990+ ( data ) => {
1991+ expect ( data . length ) . to . equal ( 1 ) ;
1992+ expect ( data [ 0 ] . clients . length ) . to . equal ( 3 ) ;
1993+ expect ( data [ 0 ] . clients [ 0 ] . name ) . to . equal ( 'Dan David' ) ;
1994+ expect ( data [ 0 ] . clients [ 0 ] . name ) . to . equal ( 'Dan Davis' ) ;
1995+ expect ( data [ 0 ] . clients [ 1 ] . name ) . to . equal ( 'Kirk Maelström' ) ;
1996+ } ) ;
1997+ } ) ;
1998+ } ) ;
1999+ } ) ;
18472000 } ) ;
18482001
18492002 describe ( '$noSelect' , ( ) => {
0 commit comments