Functional way of implementing DB access with additional logic
Currently, my goal is to communicate with DB using CRUD approach. Between simple operation i may have some extra steps (additional business-logic which may throw some errors).
import cats.effect.IO
import scalikejdbc._
IO(NamedDB(MyDB) localTx {
implicit session => {
val x = for {
a <- IO(select()) // Option[Something] (1)
_ <- IO(log(a)) // Option[Something] (2)
_ <- IO(insert()) // Something (3)
_ <- IO(insert()) // Something (4)
c <- IO(select()) // Option[Something] (5)
r <- IO(fileSystemOperation(a, c)) // (6)
_ <- operation(r) // Either[Throwable, Unit] (7)
} yield ()
x.unsafeRunSync() // (8)
}
}).attempt.unsafeRunSync() match { // (9)
case Left(value) => println("Error")
case Right(value) => println("Success") // in reality i want to return some result here
}
The first problem comes in (2)
: i got error:
Error type mismatch; found : cats.effect.IO[Unit]
required: Option[?]
_ <- IO(log(a))
But if i log result of any insert ((3)
or (4)
) then all is ok. Probably, it's because of difference in result type Option[Something]
vs Something
, but i thought that it would be the same as this:
for {
a <- Option(1)
....
}
The second question is how to correcly handle errors in (7)
to rollback transaction? Do i have to use pattern matching and do something like this:
operation(r) match {
case Left(e) => throw e
}
or i can throw error inside my operation
and return Unit
instead of Either[Throwable, Unit]
?
Also, i have question about (8)
and (9)
. Is it a correct way to start actual processing and handle any error or not?
I tried to do the same steps using this approach:
select() match {
case Some(_) =>
.....
operation(r) match {
case Left(e) => throw e
case Right(_) => ....
}
case None => ....
}
But in result i got too deep match-case
clauses and it was hard to read and understand.
I'm new to FP, but i want to implement this in FP way.
scala functional-programming scala-cats scalikejdbc
|
show 1 more comment
Currently, my goal is to communicate with DB using CRUD approach. Between simple operation i may have some extra steps (additional business-logic which may throw some errors).
import cats.effect.IO
import scalikejdbc._
IO(NamedDB(MyDB) localTx {
implicit session => {
val x = for {
a <- IO(select()) // Option[Something] (1)
_ <- IO(log(a)) // Option[Something] (2)
_ <- IO(insert()) // Something (3)
_ <- IO(insert()) // Something (4)
c <- IO(select()) // Option[Something] (5)
r <- IO(fileSystemOperation(a, c)) // (6)
_ <- operation(r) // Either[Throwable, Unit] (7)
} yield ()
x.unsafeRunSync() // (8)
}
}).attempt.unsafeRunSync() match { // (9)
case Left(value) => println("Error")
case Right(value) => println("Success") // in reality i want to return some result here
}
The first problem comes in (2)
: i got error:
Error type mismatch; found : cats.effect.IO[Unit]
required: Option[?]
_ <- IO(log(a))
But if i log result of any insert ((3)
or (4)
) then all is ok. Probably, it's because of difference in result type Option[Something]
vs Something
, but i thought that it would be the same as this:
for {
a <- Option(1)
....
}
The second question is how to correcly handle errors in (7)
to rollback transaction? Do i have to use pattern matching and do something like this:
operation(r) match {
case Left(e) => throw e
}
or i can throw error inside my operation
and return Unit
instead of Either[Throwable, Unit]
?
Also, i have question about (8)
and (9)
. Is it a correct way to start actual processing and handle any error or not?
I tried to do the same steps using this approach:
select() match {
case Some(_) =>
.....
operation(r) match {
case Left(e) => throw e
case Right(_) => ....
}
case None => ....
}
But in result i got too deep match-case
clauses and it was hard to read and understand.
I'm new to FP, but i want to implement this in FP way.
scala functional-programming scala-cats scalikejdbc
Could you create a Minimal, Complete, and Verifiable example of your problem?
– Yuval Itzchakov
Nov 20 '18 at 11:10
@YuvalItzchakov ok, i'll add runnable example
– Nick Ryan
Nov 20 '18 at 11:14
I think you have several question. It might be good to break them into separate questions on this site, too. You're more likely to get responses to small, constrained questions.
– erip
Nov 20 '18 at 12:17
@erip, yes, probably, you are right. I will decompose this question into severla smaller
– Nick Ryan
Nov 20 '18 at 12:29
1
I'm not sure if you have flexibility in switching around tools, but people really like doobie for JDBC programming. You might find it useful and compositional.
– erip
Nov 20 '18 at 13:42
|
show 1 more comment
Currently, my goal is to communicate with DB using CRUD approach. Between simple operation i may have some extra steps (additional business-logic which may throw some errors).
import cats.effect.IO
import scalikejdbc._
IO(NamedDB(MyDB) localTx {
implicit session => {
val x = for {
a <- IO(select()) // Option[Something] (1)
_ <- IO(log(a)) // Option[Something] (2)
_ <- IO(insert()) // Something (3)
_ <- IO(insert()) // Something (4)
c <- IO(select()) // Option[Something] (5)
r <- IO(fileSystemOperation(a, c)) // (6)
_ <- operation(r) // Either[Throwable, Unit] (7)
} yield ()
x.unsafeRunSync() // (8)
}
}).attempt.unsafeRunSync() match { // (9)
case Left(value) => println("Error")
case Right(value) => println("Success") // in reality i want to return some result here
}
The first problem comes in (2)
: i got error:
Error type mismatch; found : cats.effect.IO[Unit]
required: Option[?]
_ <- IO(log(a))
But if i log result of any insert ((3)
or (4)
) then all is ok. Probably, it's because of difference in result type Option[Something]
vs Something
, but i thought that it would be the same as this:
for {
a <- Option(1)
....
}
The second question is how to correcly handle errors in (7)
to rollback transaction? Do i have to use pattern matching and do something like this:
operation(r) match {
case Left(e) => throw e
}
or i can throw error inside my operation
and return Unit
instead of Either[Throwable, Unit]
?
Also, i have question about (8)
and (9)
. Is it a correct way to start actual processing and handle any error or not?
I tried to do the same steps using this approach:
select() match {
case Some(_) =>
.....
operation(r) match {
case Left(e) => throw e
case Right(_) => ....
}
case None => ....
}
But in result i got too deep match-case
clauses and it was hard to read and understand.
I'm new to FP, but i want to implement this in FP way.
scala functional-programming scala-cats scalikejdbc
Currently, my goal is to communicate with DB using CRUD approach. Between simple operation i may have some extra steps (additional business-logic which may throw some errors).
import cats.effect.IO
import scalikejdbc._
IO(NamedDB(MyDB) localTx {
implicit session => {
val x = for {
a <- IO(select()) // Option[Something] (1)
_ <- IO(log(a)) // Option[Something] (2)
_ <- IO(insert()) // Something (3)
_ <- IO(insert()) // Something (4)
c <- IO(select()) // Option[Something] (5)
r <- IO(fileSystemOperation(a, c)) // (6)
_ <- operation(r) // Either[Throwable, Unit] (7)
} yield ()
x.unsafeRunSync() // (8)
}
}).attempt.unsafeRunSync() match { // (9)
case Left(value) => println("Error")
case Right(value) => println("Success") // in reality i want to return some result here
}
The first problem comes in (2)
: i got error:
Error type mismatch; found : cats.effect.IO[Unit]
required: Option[?]
_ <- IO(log(a))
But if i log result of any insert ((3)
or (4)
) then all is ok. Probably, it's because of difference in result type Option[Something]
vs Something
, but i thought that it would be the same as this:
for {
a <- Option(1)
....
}
The second question is how to correcly handle errors in (7)
to rollback transaction? Do i have to use pattern matching and do something like this:
operation(r) match {
case Left(e) => throw e
}
or i can throw error inside my operation
and return Unit
instead of Either[Throwable, Unit]
?
Also, i have question about (8)
and (9)
. Is it a correct way to start actual processing and handle any error or not?
I tried to do the same steps using this approach:
select() match {
case Some(_) =>
.....
operation(r) match {
case Left(e) => throw e
case Right(_) => ....
}
case None => ....
}
But in result i got too deep match-case
clauses and it was hard to read and understand.
I'm new to FP, but i want to implement this in FP way.
scala functional-programming scala-cats scalikejdbc
scala functional-programming scala-cats scalikejdbc
asked Nov 20 '18 at 10:07
Nick Ryan
376213
376213
Could you create a Minimal, Complete, and Verifiable example of your problem?
– Yuval Itzchakov
Nov 20 '18 at 11:10
@YuvalItzchakov ok, i'll add runnable example
– Nick Ryan
Nov 20 '18 at 11:14
I think you have several question. It might be good to break them into separate questions on this site, too. You're more likely to get responses to small, constrained questions.
– erip
Nov 20 '18 at 12:17
@erip, yes, probably, you are right. I will decompose this question into severla smaller
– Nick Ryan
Nov 20 '18 at 12:29
1
I'm not sure if you have flexibility in switching around tools, but people really like doobie for JDBC programming. You might find it useful and compositional.
– erip
Nov 20 '18 at 13:42
|
show 1 more comment
Could you create a Minimal, Complete, and Verifiable example of your problem?
– Yuval Itzchakov
Nov 20 '18 at 11:10
@YuvalItzchakov ok, i'll add runnable example
– Nick Ryan
Nov 20 '18 at 11:14
I think you have several question. It might be good to break them into separate questions on this site, too. You're more likely to get responses to small, constrained questions.
– erip
Nov 20 '18 at 12:17
@erip, yes, probably, you are right. I will decompose this question into severla smaller
– Nick Ryan
Nov 20 '18 at 12:29
1
I'm not sure if you have flexibility in switching around tools, but people really like doobie for JDBC programming. You might find it useful and compositional.
– erip
Nov 20 '18 at 13:42
Could you create a Minimal, Complete, and Verifiable example of your problem?
– Yuval Itzchakov
Nov 20 '18 at 11:10
Could you create a Minimal, Complete, and Verifiable example of your problem?
– Yuval Itzchakov
Nov 20 '18 at 11:10
@YuvalItzchakov ok, i'll add runnable example
– Nick Ryan
Nov 20 '18 at 11:14
@YuvalItzchakov ok, i'll add runnable example
– Nick Ryan
Nov 20 '18 at 11:14
I think you have several question. It might be good to break them into separate questions on this site, too. You're more likely to get responses to small, constrained questions.
– erip
Nov 20 '18 at 12:17
I think you have several question. It might be good to break them into separate questions on this site, too. You're more likely to get responses to small, constrained questions.
– erip
Nov 20 '18 at 12:17
@erip, yes, probably, you are right. I will decompose this question into severla smaller
– Nick Ryan
Nov 20 '18 at 12:29
@erip, yes, probably, you are right. I will decompose this question into severla smaller
– Nick Ryan
Nov 20 '18 at 12:29
1
1
I'm not sure if you have flexibility in switching around tools, but people really like doobie for JDBC programming. You might find it useful and compositional.
– erip
Nov 20 '18 at 13:42
I'm not sure if you have flexibility in switching around tools, but people really like doobie for JDBC programming. You might find it useful and compositional.
– erip
Nov 20 '18 at 13:42
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390600%2ffunctional-way-of-implementing-db-access-with-additional-logic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390600%2ffunctional-way-of-implementing-db-access-with-additional-logic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you create a Minimal, Complete, and Verifiable example of your problem?
– Yuval Itzchakov
Nov 20 '18 at 11:10
@YuvalItzchakov ok, i'll add runnable example
– Nick Ryan
Nov 20 '18 at 11:14
I think you have several question. It might be good to break them into separate questions on this site, too. You're more likely to get responses to small, constrained questions.
– erip
Nov 20 '18 at 12:17
@erip, yes, probably, you are right. I will decompose this question into severla smaller
– Nick Ryan
Nov 20 '18 at 12:29
1
I'm not sure if you have flexibility in switching around tools, but people really like doobie for JDBC programming. You might find it useful and compositional.
– erip
Nov 20 '18 at 13:42