Skip to content

v0.13.0 Computed Attribute values

Compare
Choose a tag to compare
@marcgrue marcgrue released this 08 Dec 23:01
· 5 commits to main since this release

Molecule can now update and query computed attribute values.

Computed update operations

A classical example is increasing a counter where the current value is increased by 1:

// Current counter == c
val (id, c) = Game.id.counter.query.get.head

// Increase counter value with 1
Game(id).counter.+(1).update.transact

// Counter has increased
Game.counter.query.get.head == c + 1

Number updates

If a number attribute has value 4, we can see what update operations we can perform on it:

// Arithmetic
Something(id).number.+(1).update.transact // 4 + 1 => 5 
Something(id).number.-(1).update.transact // 4 - 1 => 3
Something(id).number.*(2).update.transact // 4 * 2 => 8
Something(id).number./(2).update.transact // 4 / 2 => 2

//  4 => -4
// -4 =>  4
Something(id).number.negate.update.transact

//  4 => 4
// -4 => 4
Something(id).number.abs.update.transact

//  4 => -4
// -4 => -4
Something(id).number.absNeg.update.transact

//  4.4 =>  5
// -4.4 => -4
Something(id).number.ceil.update.transact

//  4.4 =>  4
// -4.4 => -5
Something(id).number.floor.update.transact

String updates

With a string attribute having value Hello we can do the following update operations:

Something(id).string.+(" World").update.transact // => "Hello World" 

Something(id).string.prepend("Say ").update.transact // => "Say Hello" 

// Substring from start to end indexes
Something(id).string.substring(2, 4).update.transact // => "ll" 

Something(id).string.replaceAll("l", "_").update.transact // => "He__o"
// regex
Something(id).string.replaceAll("[h-z]", "-").update.transact // => "He---" 

Something(id).string.toLower.update.transact // => "hello" 
Something(id).string.toUpper.update.transact // => "HELLO" 

Boolean updates

With a bool attribute having value true we can do the following update operations:

// AND
Something(id).bool.&&(true).update.transact // =>  true 
Something(id).bool.&&(false).update.transact // => false

// OR
Something(id).bool.||(true).update.transact // =>  true 
Something(id).bool.||(false).update.transact // => true 

// NOT
Something(id).bool.!.update.transact // => false 

Computed query filters

Some operations on numbers were added to generate computed query filters based on the attribute value:

Integers

Int, Long, BigInt, Short, Byte

Something.int.even.query.get // List(0, 2, 4) 
Something.int.odd.query.get //  List(1, 3, 5) 

// Modulo
Something.int.%(3, 0).query.get //  List(0, 3) 
Something.int.%(3, 1).query.get //  List(1, 4) 
Something.int.%(3, 2).query.get //  List(2, 5)

Decimals

Double, BigDecimal, Float

Something.dec.query.get // List(1.1, 2.2, 3.3) 

Something.dec.floor.query.get // List(1, 2, 3) 
Something.dec.ceil.query.get //  List(2, 3, 4)