Mondocks.Fable 0.4.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mondocks.Fable --version 0.4.8
NuGet\Install-Package Mondocks.Fable -Version 0.4.8
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mondocks.Fable" Version="0.4.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mondocks.Fable --version 0.4.8
#r "nuget: Mondocks.Fable, 0.4.8"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Mondocks.Fable as a Cake Addin
#addin nuget:?package=Mondocks.Fable&version=0.4.8

// Install Mondocks.Fable as a Cake Tool
#tool nuget:?package=Mondocks.Fable&version=0.4.8

Mondocks

nuget Binder

dotnet add package Mondocks.Net
# or for fable/nodejs
dotnet add package Mondocks.Fable

This library is based on the mongodb extended json spec and mongodb manual reference

https://docs.mongodb.com/manual/reference/mongodb-extended-json/ > https://docs.mongodb.com/manual/reference/command/

This library provides a set of familiar tools if you work with mongo databases and can be a step into more F# goodies, it doesn't prevent you from using the usual MongoDB/.NET driver so you can use them side by side. It also can help you if you have a lot of flexible data inside your database as oposed to the usual strict schemas that F#/C# are used to from SQL tools, this provides a DSL that allow you to create MongoDB Commands (raw queries) leveraging the dynamism of anonymous records since they behave almost like javascript objects. Writing commands should be almost painless these commands produce a JSON string that can be utilized directly on your application or even copy/pasted into the mongo shell. Commands are kind of a version of raw sql queries but they allow you to do what you already know how to do without much changes to the objects you might be manipulating already. Ideally this library is meant to be used mostly with records and anonymous records to imitate mongodb queries

Sample Usage

Check out this quick sample of what you can do right now

You can also check this gist

open System
open MongoDB.Driver
open MongoDB.Bson
open Mondocks.Queries
open Mondocks.Types

type User = { _id: ObjectId; name: string; age: int }
let createUsers minAge maxAge =
    let random  = Random()
    insert "users" {
        documents
            [   // an anonymous object that does not include a null _id
                {| name = "Peter"; age = random.Next(minAge, maxAge); |}
                {| name = "Sandra"; age = random.Next(minAge, maxAge); |}
                {| name = "Mike"; age = random.Next(minAge, maxAge); |}
                {| name = "Perla"; age = random.Next(minAge, maxAge); |}
                {| name = "Updateme"; age = 1; |}
                {| name = "Deleteme"; age = 50; |}
            ]
    }
let getUsersOverAge (age: int) =
    find "users" {
        // equivalent to a mongo query filter
        // { age: { $gt: age } }
        filter {| age = {| ``$gt``= age |} |}
        limit 2
        skip 1
    }
let updateUser (name: string) (newName: string) =
    update "users" {
        updates
            [ { // you can do mongo queries like
                // {| ``$or`` = [] |} -> { $or: [] }
                q = {| name = name |}
                u = {| name = newName; age = 5 |}
                multi = Some false
                upsert = Some false
                collation = None
                arrayFilters = None
                hint = None }
            ]
    }
let deleteUser (name: string) =
    delete "users" {
        deletes
           [ { q = {| name = name |}
               limit = 1
               collation = None
               hint = None
               comment = None }
        ]
    }
[<EntryPoint>]
let main argv =
    let client = MongoClient("mongodb://localhost:27017")
    let db = client.GetDatabase("mondocks")

    let userscmd = createUsers 15 50
    let result = db.RunCommand<InsertResult>(JsonCommand userscmd)
    printfn $"InsertResult: %A{result}"

    let over20 = getUsersOverAge 20
    let result = db.RunCommand<FindResult<User>>(JsonCommand over20)
    printfn $"FindResult Ok: %f{result.ok}"
    result.cursor.firstBatch |> Seq.iter (fun value -> printfn $"%A{value}")

    let updatecmd = updateUser "Updateme" "Updated"
    let result = db.RunCommand<UpdateResult>(JsonCommand updatecmd)
    printfn $"UpdateResult: %A{result}"

    let deletecmd = deleteUser "Deleteme"
    let result = db.RunCommand<DeleteResult>(JsonCommand deletecmd)
    printfn $"DeleteResult: %A{result}"

    0

If you want to see what else is available check the samples directory

Documentation

Right now the documentation is provided via .NET Interactive notebooks. There are three ways ways to ineract with it

  • The Notebooks directory contains jupyter notebooks that you can download and open them with the VSCode Extension to interact with them in real time.

  • If you don't want to download anything you can still check the notebooks online, click on Binder to go to the binder website and check the notebooks online

  • If your editor supports doc comments, you should be able to see samples as well, part of the source code is documented that way, you should even see examples in the editor tooltips in some cases

Goals

Non Required Extras

  • provide helpers to write different syntax (e.g. filter (fun m -> m.prop = value), filter ("prop" gt 10))

Non Goals

  • Convert this into a document mapper
  • Provide 100% of the mongo commands
  • Provide a 100% F# idiomatic solution

This is a work in progress, you can help providing feedback about it's usage

Thanks for the early feedback in twitter from Isaac, Zaid, Alexey, Alexander, and the F# community you can follow it on the first issue

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.0 302 11/28/2022
0.4.8 410 2/14/2022
0.4.2 310 7/27/2021