@@ -2009,5 +2009,230 @@ describe('sync handlers', () => {
20092009 }
20102010 } ) ;
20112011 } ) ;
2012+ describe ( 'Handle IUD for values' , ( ) => {
2013+ it ( 'Handle IUD for strings' , async ( ) => {
2014+ const debug = log . extend ( 'HandleIUDStrings' ) ;
2015+
2016+ const typeId = await deep . id ( '@deep-foundation/core' , 'Operation' ) ;
2017+ const handleInsertId = await deep . id ( '@deep-foundation/core' , 'HandleInsert' ) ;
2018+ const handleUpdateId = await deep . id ( '@deep-foundation/core' , 'HandleUpdate' ) ;
2019+ const supportsId = await deep . id ( '@deep-foundation/core' , 'plv8SupportsJs' ) ;
2020+
2021+ const anyTypeId = await deep . id ( '@deep-foundation/core' , 'Any' ) ;
2022+ const typeToHandle = await deep . insert ( { type_id : 1 , from_id : anyTypeId , to_id : anyTypeId } ) ;
2023+ const typeToInsertInHandler = await deep . insert ( { type_id : 1 , from_id : anyTypeId , to_id : anyTypeId } ) ;
2024+ const typeIdToInsertInHandler = typeToInsertInHandler ?. data ?. [ 0 ] ?. id ;
2025+ const typeIdToHandle = typeToHandle ?. data ?. [ 0 ] ?. id ;
2026+ debug ( 'typeIdToHandle' , typeIdToHandle ) ;
2027+
2028+ const onInsertHandler = await insertHandler (
2029+ handleInsertId ,
2030+ typeIdToHandle ,
2031+ `({deep, data}) => {
2032+ const inserted = deep.insert({type_id: ${ typeIdToInsertInHandler } , to_id: ${ typeIdToInsertInHandler } , from_id: ${ typeIdToInsertInHandler } , string: 'HelloBugFixers'});
2033+ deep.update({link_id: inserted.data[0].id}, { value: 'GoodbyeBugFixers'}, { table: 'strings' });
2034+ }` . trim ( ) ,
2035+ undefined ,
2036+ supportsId
2037+ ) ;
2038+ debug ( 'onInsertHandler' , onInsertHandler ) ;
2039+
2040+ try {
2041+ const linkId = ( await deep . insert ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ) ?. data ?. [ 0 ] . id ;
2042+ debug ( 'linkId' , linkId ) ;
2043+ const insertedLink = await deep . select ( linkId ) ;
2044+ debug ( 'insertedLink' , insertedLink ?. data ) ;
2045+ } catch ( e ) {
2046+ debug ( 'insert error: ' , e ) ;
2047+ }
2048+
2049+ const insertedByHandler = await deep . select ( { type_id : { _eq : typeIdToInsertInHandler } , to_id : { _eq : typeIdToInsertInHandler } , from_id : { _eq : typeIdToInsertInHandler } } ) ;
2050+ const insertedByHandlerId = insertedByHandler . data [ 0 ] . id ;
2051+ debug ( 'insertedByHandler' , insertedByHandler ?. data ) ;
2052+ const value = insertedByHandler ?. data ?. [ 0 ] ?. value ;
2053+ assert . equal ( value ?. value , 'GoodbyeBugFixers' ) ;
2054+ debug ( 'delete insert handler' , await deleteHandler ( onInsertHandler ) ) ;
2055+
2056+ const onUpdateHandler = await insertHandler (
2057+ handleUpdateId ,
2058+ typeIdToHandle ,
2059+ `({deep, data}) => {
2060+ deep.delete({link_id: ${ insertedByHandlerId } }, { table: 'strings' });
2061+ }` . trim ( ) ,
2062+ undefined ,
2063+ supportsId
2064+ ) ;
2065+ debug ( 'onUpdateHandler' , onUpdateHandler ) ;
2066+
2067+ try {
2068+ const linkId = ( await deep . select ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ) ?. data ?. [ 0 ] . id ;
2069+ await deep . update ( { link_id : linkId } , { value : 'TERMINATING' } , { table : 'strings' } ) ;
2070+ debug ( 'linkId' , linkId ) ;
2071+
2072+ const linkWithoutValue = await deep . select ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ;
2073+ const value = linkWithoutValue ?. data ?. [ 0 ] ?. value ;
2074+ debug ( 'value' , value ) ;
2075+ assert . equal ( value ?. value , undefined ) ;
2076+
2077+ const deletedLink = await deep . select ( linkId ) ;
2078+ debug ( 'delete linkid' , await deep . delete ( { id : { _eq : linkId } } ) ) ;
2079+
2080+ } catch ( e ) {
2081+ debug ( 'insert error: ' , e ) ;
2082+ }
2083+
2084+ debug ( 'delete update handler' , await deleteHandler ( onUpdateHandler ) ) ;
2085+ } ) ;
2086+ it ( 'Handle IUD for numbers' , async ( ) => {
2087+ const debug = log . extend ( 'HandleIUDStrings' ) ;
2088+
2089+ const typeId = await deep . id ( '@deep-foundation/core' , 'Operation' ) ;
2090+ const handleInsertId = await deep . id ( '@deep-foundation/core' , 'HandleInsert' ) ;
2091+ const handleUpdateId = await deep . id ( '@deep-foundation/core' , 'HandleUpdate' ) ;
2092+ const supportsId = await deep . id ( '@deep-foundation/core' , 'plv8SupportsJs' ) ;
2093+
2094+ const anyTypeId = await deep . id ( '@deep-foundation/core' , 'Any' ) ;
2095+ const typeToHandle = await deep . insert ( { type_id : 1 , from_id : anyTypeId , to_id : anyTypeId } ) ;
2096+ const typeToInsertInHandler = await deep . insert ( { type_id : 1 , from_id : anyTypeId , to_id : anyTypeId } ) ;
2097+ const typeIdToInsertInHandler = typeToInsertInHandler ?. data ?. [ 0 ] ?. id ;
2098+ const typeIdToHandle = typeToHandle ?. data ?. [ 0 ] ?. id ;
2099+ debug ( 'typeIdToHandle' , typeIdToHandle ) ;
2100+
2101+ const onInsertHandler = await insertHandler (
2102+ handleInsertId ,
2103+ typeIdToHandle ,
2104+ `({deep, data}) => {
2105+ const inserted = deep.insert({type_id: ${ typeIdToInsertInHandler } , to_id: ${ typeIdToInsertInHandler } , from_id: ${ typeIdToInsertInHandler } , number: 1});
2106+ deep.update({link_id: inserted.data[0].id}, { value: 2 }, { table: 'numbers' });
2107+ }` . trim ( ) ,
2108+ undefined ,
2109+ supportsId
2110+ ) ;
2111+ debug ( 'onInsertHandler' , onInsertHandler ) ;
2112+
2113+ try {
2114+ const linkId = ( await deep . insert ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ) ?. data ?. [ 0 ] . id ;
2115+ debug ( 'linkId' , linkId ) ;
2116+ const insertedLink = await deep . select ( linkId ) ;
2117+ debug ( 'insertedLink' , insertedLink ?. data ) ;
2118+ } catch ( e ) {
2119+ debug ( 'insert error: ' , e ) ;
2120+ }
2121+
2122+ const insertedByHandler = await deep . select ( { type_id : { _eq : typeIdToInsertInHandler } , to_id : { _eq : typeIdToInsertInHandler } , from_id : { _eq : typeIdToInsertInHandler } } ) ;
2123+ const insertedByHandlerId = insertedByHandler . data [ 0 ] . id ;
2124+ debug ( 'insertedByHandler' , insertedByHandler ?. data ) ;
2125+ const value = insertedByHandler ?. data ?. [ 0 ] ?. value ;
2126+ assert . equal ( value ?. value , 2 ) ;
2127+ debug ( 'delete insert handler' , await deleteHandler ( onInsertHandler ) ) ;
2128+
2129+ const onUpdateHandler = await insertHandler (
2130+ handleUpdateId ,
2131+ typeIdToHandle ,
2132+ `({deep, data}) => {
2133+ deep.delete({link_id: ${ insertedByHandlerId } }, { table: 'numbers' });
2134+ }` . trim ( ) ,
2135+ undefined ,
2136+ supportsId
2137+ ) ;
2138+ debug ( 'onUpdateHandler' , onUpdateHandler ) ;
2139+
2140+ try {
2141+ const linkId = ( await deep . select ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ) ?. data ?. [ 0 ] . id ;
2142+ await deep . update ( { link_id : linkId } , { value : 3 } , { table : 'numbers' } ) ;
2143+ debug ( 'linkId' , linkId ) ;
2144+
2145+ const linkWithoutValue = await deep . select ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ;
2146+ const value = linkWithoutValue ?. data ?. [ 0 ] ?. value ;
2147+ debug ( 'value' , value ) ;
2148+ assert . equal ( value ?. value , undefined ) ;
2149+
2150+ const deletedLink = await deep . select ( linkId ) ;
2151+ debug ( 'delete linkid' , await deep . delete ( { id : { _eq : linkId } } ) ) ;
2152+
2153+ } catch ( e ) {
2154+ debug ( 'insert error: ' , e ) ;
2155+ }
2156+
2157+ debug ( 'delete update handler' , await deleteHandler ( onUpdateHandler ) ) ;
2158+ } ) ;
2159+ it ( 'Handle IUD for object' , async ( ) => {
2160+ const debug = log . extend ( 'HandleIUDStrings' ) ;
2161+
2162+ const typeId = await deep . id ( '@deep-foundation/core' , 'Operation' ) ;
2163+ const handleInsertId = await deep . id ( '@deep-foundation/core' , 'HandleInsert' ) ;
2164+ const handleUpdateId = await deep . id ( '@deep-foundation/core' , 'HandleUpdate' ) ;
2165+ const supportsId = await deep . id ( '@deep-foundation/core' , 'plv8SupportsJs' ) ;
2166+
2167+ const anyTypeId = await deep . id ( '@deep-foundation/core' , 'Any' ) ;
2168+ const typeToHandle = await deep . insert ( { type_id : 1 , from_id : anyTypeId , to_id : anyTypeId } ) ;
2169+ const typeToInsertInHandler = await deep . insert ( { type_id : 1 , from_id : anyTypeId , to_id : anyTypeId } ) ;
2170+ const typeIdToInsertInHandler = typeToInsertInHandler ?. data ?. [ 0 ] ?. id ;
2171+ const typeIdToHandle = typeToHandle ?. data ?. [ 0 ] ?. id ;
2172+ debug ( 'typeIdToHandle' , typeIdToHandle ) ;
2173+
2174+ const onInsertHandler = await insertHandler (
2175+ handleInsertId ,
2176+ typeIdToHandle ,
2177+ `({deep, data}) => {
2178+ const inserted = deep.insert({
2179+ type_id: ${ typeIdToInsertInHandler } ,
2180+ to_id: ${ typeIdToInsertInHandler } ,
2181+ from_id: ${ typeIdToInsertInHandler } ,
2182+ object: { string: 'HelloBugFixers' }
2183+ });
2184+ }` . trim ( ) ,
2185+ undefined ,
2186+ supportsId
2187+ ) ;
2188+ debug ( 'onInsertHandler' , onInsertHandler ) ;
2189+
2190+ try {
2191+ const linkId = ( await deep . insert ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ) ?. data ?. [ 0 ] . id ;
2192+ debug ( 'linkId' , linkId ) ;
2193+ const insertedLink = await deep . select ( linkId ) ;
2194+ debug ( 'insertedLink' , insertedLink ?. data ) ;
2195+ } catch ( e ) {
2196+ debug ( 'insert error: ' , e ) ;
2197+ }
2198+
2199+ const insertedByHandler = await deep . select ( { type_id : { _eq : typeIdToInsertInHandler } , to_id : { _eq : typeIdToInsertInHandler } , from_id : { _eq : typeIdToInsertInHandler } } ) ;
2200+ const insertedByHandlerId = insertedByHandler . data [ 0 ] . id ;
2201+ debug ( 'insertedByHandler' , insertedByHandler ?. data ) ;
2202+ const value = insertedByHandler ?. data ?. [ 0 ] ?. value ;
2203+ assert . equal ( value ?. value ?. string , 'HelloBugFixers' ) ;
2204+ debug ( 'delete insert handler' , await deleteHandler ( onInsertHandler ) ) ;
2205+
2206+ const onUpdateHandler = await insertHandler (
2207+ handleUpdateId ,
2208+ typeIdToHandle ,
2209+ `({deep, data}) => {
2210+ deep.delete({link_id: ${ insertedByHandlerId } }, { table: 'objects' });
2211+ }` . trim ( ) ,
2212+ undefined ,
2213+ supportsId
2214+ ) ;
2215+ debug ( 'onUpdateHandler' , onUpdateHandler ) ;
2216+
2217+ try {
2218+ const linkId = ( await deep . select ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ) ?. data ?. [ 0 ] . id ;
2219+ await deep . update ( { link_id : linkId } , { value : { string : 'TERMINATING' } } , { table : 'objects' } ) ;
2220+ debug ( 'linkId' , linkId ) ;
2221+
2222+ const linkWithoutValue = await deep . select ( { type_id : typeIdToHandle , to_id : typeIdToHandle , from_id : typeIdToHandle } ) ;
2223+ const value = linkWithoutValue ?. data ?. [ 0 ] ?. value ;
2224+ debug ( 'value' , value ) ;
2225+ assert . equal ( value ?. value , undefined ) ;
2226+
2227+ const deletedLink = await deep . select ( linkId ) ;
2228+ debug ( 'delete linkid' , await deep . delete ( { id : { _eq : linkId } } ) ) ;
2229+
2230+ } catch ( e ) {
2231+ debug ( 'insert error: ' , e ) ;
2232+ }
2233+
2234+ debug ( 'delete update handler' , await deleteHandler ( onUpdateHandler ) ) ;
2235+ } ) ;
2236+ } ) ;
20122237 } ) ;
20132238} ) ;
0 commit comments