-
-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(@effect/sql): SqlQuery module #3309
Conversation
Co-authored-by: Tim <[email protected]>
🦋 Changeset detectedLatest commit: bd25c9c The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you"re a maintainer who wants to add another changeset to this PR |
Not sure if we really need |
There is also these schema APIs https://effect-ts.github.io/effect/sql/SqlSchema.ts.html#findall |
Of course i"ve seen it:) And implemented this PR because Also, i use "ports and adapters" or "hexagonal" (call as you wish) architecture, where my persistence layer is abstracted, so my application should not know about the underlying driver. It could be SQL DB, redis, or filesystem. export const FetchBotLaunchConfigPostgres = Layer.effect(
PersistenceFetchBotLaunchConfigImpl,
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient
return {
fetchLaunchConfig: (instanceId: string) => {
return SqlSchema.single({
Request: Schema.String,
Result: PersistenceFetchBotLaunchConfigRes,
execute: (instanceId) =>
sql`SELECT *
FROM fxngine.instance_launch_config
WHERE instance_id = ${instanceId}::bigint`
})(instanceId).pipe(
Effect.catchTags({
SqlError: (cause) =>
new PersistenceError({ source: "fetchBotLaunchConfig", cause }),
NoSuchElementException: () =>
new PersistenceFetchBotLaunchConfigNotFoundError({ instanceId })
})
)
}
}
})
) Which makes no sense |
80c8158
to
361d897
Compare
f9b6732
to
ca34a85
Compare
Why not? This makes perfect sense. "ports and adapters" thats the adapting part. export const FetchBotLaunchConfigPostgres = Layer.effect(
PersistenceFetchBotLaunchConfigImpl,
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient
const byId = SqlSchema.single({
Request: Schema.String,
Result: PersistenceFetchBotLaunchConfigRes,
execute: (instanceId) =>
sql`SELECT *
FROM fxngine.instance_launch_config
WHERE instance_id = ${instanceId}::bigint`
})
return {
fetchLaunchConfig: (instanceId: string) => byId(instanceId).pipe(
Effect.catchTags({
SqlError: (cause) =>
new PersistenceError({ source: "fetchBotLaunchConfig", cause }),
NoSuchElementException: () =>
new PersistenceFetchBotLaunchConfigNotFoundError({ instanceId })
})
)
}
})
)
this is just a misunderstanding of the role of the SqlSchema.single({
Request: Foo,
...,
execute: (foo) => sql`...${foo.id}`
}) Now lets say that There is no question about if "it"s a very rare case when you pass input directly to SQL", as this is an implicit assumption that input that reached this level would be valid. Remember, schema also does validation, but it"s much more than that. You should bring your ideas up to debate more often, I promise effect is very well thought out and I feel really bad when you put in this much effort.. |
95019bb
to
263c2b6
Compare
ed5a635
to
71323be
Compare
aacbb7a
to
2234d76
Compare
Going to close this, I don"t think it adds enough value to be an addition to /sql. |
I understand that the schema is also about encoding, not just validation. However, in my case, encoding is unnecessary for SQL queries. I deal with dozens of SQL queries, and i am forced to use Let"s view at this PR from another perspective – how would you suggest to make an SQL that does not requires input encoding? |
Add
SqlQuery
module withfindAll
,void
,findOne
, andsingle
dual APIs for running SQL queries with schema validation.SqlQuery.findAll
Runs an SQL query and validates the results against a provided schema, returning all results as an array.
SqlQuery.void
Runs an SQL query and discards the result.
SqlQuery.findOne
Runs an SQL query and validates the results against a provided schema, returning the first result as an
Option
.SqlQuery.single
Runs an SQL query and validates the results against a provided schema, returning the first result.
If no result is found, it throws a
NoSuchElementException
.