Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Statically typechecks a common SQL dialect and translates it to various RDBMS backends

License

NotificationsYou must be signed in to change notification settings

fsprojects/Rezoom.SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Tutorial & full documentation

Query playground -- try out the SQL dialect live!

Statically typed SQL for F#

Rezoom.SQL is an F# ORM for SQL databases.

It integrates with the F# compiler via a generative type provider to staticallytypecheck its own dialect of SQL. It knows how to translate this SQL dialect tovarious backends. Currently it supports SQLite, SQL Server, and PostgreSQL.

The type provider makes it fast and easy to write SQL statements, run them, andconsume their results from your F# code with full type safety. You don't need toinstall any editor extensions or custom tooling, just add a NuGet package andyou're off and running writing code like this:

animated example usage to write queries

Database schema inferred from migration scripts

In order to typecheck your queries, Rezoom.SQL has to know your database schema(so it can know, for example, that theId column in theUsers table is anint). It learns the schema by reading your migration scripts and observing whattables and views are created, columns added, and so on.

When developing the first iteration of your application (or a new feature withits own migration script), it's easy to sketch out a model then go back andchange it as you code, without having to touch a real database until you'reready to run.

Here's an example. You might want to refresh the page to start the GIF from thebeginning.

animated example usage to write queries

Because this is a generative type provider, it makes plain old .NET types youcan use from other languages. That is, you can write an F# project that usesRezoom.SQL and defines your migrations and queries, then reference that from C#or VB.NET projects and use the generated query types with no problem in thoselangages. There is even an option to represent nullable types with C#-styleSystem.Nullable<T> instead ofFSharpOption<T> to make this scenario workextra smoothly.

Check out thequeryplayground to seewhat kinds of SQL you can write.

The productivity of static typing

When you make schema changes -- for example, replacingFirstName andLastName fields with a singleFullName field -- it's comforting to know thecompiler will point out the queries you need to update.

The typechecker also tightens up the feedback loop, so you don't waste your timetracking down typos and trivial SQL mistakes you'd normally only encounter at runtime.

Here are just a handful of possible errors you'll be informed of at compile timeand can fix in seconds. There are currently over 45 different error types thatcan be detected at compile time.

Mistyped table names

example error on mistyped table name

Incompatible data types

example error on comparing string to int

Selecting columns not included in aGROUP BY clause

example error on selecting column not found in group by clause

Flexible migration order for working in teams

Since Rezoom.SQL understands the language, it knows that some migrations likealter table X add column Y andalter table X add column Z can be run in anyorder and produce the same effects.

When you're working with a team, you can take advantage of this to add thetables and columns you need for the feature you're coding, while your other teammembers do the same for their features --without having to decide theOneTrue Migration Order when you merge.

See detailshere.

Integration with Rezoom

You can use Rezoom.SQL by itself, as in the example code above.

But as the name implies, it's designed to work withRezoom. When you use it with Rezoom, you cantake advantage of automatic caching and combine units of business logic to share round tripsto the database.

Automatic batching

With Rezoom, you build up aPlan to represent a transaction, which may involvemultiple SQL commands (or web API calls, or other high-latency data manipulation).

If you have onePlan calledthreeTrip that makes 3 queries, and anothercalledtwoTrip that makes 2 queries, you can choose whether to combine themsequentially for 5 round trips to the database...

letsequential=    plan{let!x= threeTriplet!y= twoTripreturn(x, y)}

sequential execution diagram

Or concurrently, for 3 round trips to the database. The first two query batchessent to the database will include pending queries fromboththreePlan andtwoTrip:

letconcurrent=    plan{let!x,y= threeTrip, twoTripreturn(x, y)}

sequential execution diagram

Automatic caching

Each statically typed query comes with some useful info for caching:

  • A compiler-generated ID
  • A boolean indicating whether it could make sense to cache (has no side effects, does not use rand(), newid(), etc)
  • A bitmask of the tables it reads from
  • A bitmask of the tables it writes to

Rezoom uses this cache info to avoid unnecessarily re-querying for the same dataduring the execution of aPlan (i.e. within a transaction).

This means if you have 30 different functions that callLoadUserPermissions(currentUserId), only 1 query for permissions will actuallybe run when you use those functions together in a transaction. Unless, ofcourse, you edit the permissions table during the course of the transaction, inwhich case the cached result will automatically be invalidated and thepermissions re-queried next time they are requested.

This lets you safely check all the invariants you need for each method in yourdomain layer, without fear of causing mountains of redundant queries, andwithout any of the effort of writing your own caching layer.

Get started

To get started using RZSQL, read thetutorial. It'll get youup and running in 5 minutes or your money back.

About

Statically typechecks a common SQL dialect and translates it to various RDBMS backends

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp