Packages
gleam_mongo
0.1.3
0.18.1
retired
0.18.0
retired
0.17.0
retired
0.16.0
retired
0.15.1
retired
0.15.0
retired
0.14.0
retired
0.13.0
retired
0.12.0
retired
0.11.0
retired
0.10.0
retired
0.9.0
retired
0.8.0
retired
0.7.0
retired
0.6.0
retired
0.5.0
retired
0.4.0
retired
0.3.0
0.2.0
retired
0.1.3
retired
0.1.2
retired
0.1.1
retired
0.1.0
retired
A mongodb driver for gleam
Retired package: Renamed - Republished as mungo
Current section
Files
Jump to
Current section
Files
src/mongo/client.gleam
import tcp
import bson/types
import gleam/bit_string
import bson.{decode, encode}
pub opaque type Connection {
Connection(socket: tcp.Socket)
}
pub opaque type Database {
Database(connection: Connection, name: String)
}
pub type Collection {
Collection(db: Database, name: String)
}
pub fn connect(ip: String, port: Int) -> Result(Connection, Nil) {
case tcp.connect(ip, port) {
Ok(socket) -> Ok(Connection(socket))
Error(Nil) -> Error(Nil)
}
}
pub fn db(connection: Connection, name: String) -> Database {
Database(connection, name)
}
pub fn collection(db: Database, name: String) -> Collection {
Collection(db, name)
}
pub fn execute(
collection: Collection,
cmd: types.Value,
) -> Result(List(#(String, types.Value)), Nil) {
assert types.Document(body) = cmd
let cmd = [#("$db", types.Str(collection.db.name)), ..body]
let encoded = encode(cmd)
let size = bit_string.byte_size(encoded) + 21
let packet =
[<<size:32-little, 0:32, 0:32, 2013:32-little, 0:32, 0>>, encoded]
|> bit_string.concat
case collection.db.connection.socket
|> tcp.send(packet) {
tcp.OK ->
case collection.db.connection.socket
|> tcp.receive() {
Ok(response) -> {
let <<_:168, rest:bit_string>> = response
case decode(rest) {
Ok(result) -> Ok(result)
Error(Nil) -> Error(Nil)
}
}
Error(Nil) -> Error(Nil)
}
_ -> Error(Nil)
}
}