Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lpil/sqlight
💡 Use SQLite from Gleam!
https://github.com/lpil/sqlight
database-client gleam sqlite
Last synced: 25 days ago
JSON representation
💡 Use SQLite from Gleam!
- Host: GitHub
- URL: https://github.com/lpil/sqlight
- Owner: lpil
- Created: 2022-12-28T21:06:40.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-19T13:48:29.000Z (4 months ago)
- Last Synced: 2024-10-26T21:18:04.599Z (about 2 months ago)
- Topics: database-client, gleam, sqlite
- Language: Gleam
- Homepage: https://hexdocs.pm/sqlight
- Size: 33.2 KB
- Stars: 85
- Watchers: 3
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-gleam - sqlight - [📚](https://hexdocs.pm/sqlight/) - Use SQLite from Gleam! (Packages / Databases)
README
# sqlight
[![Package Version](https://img.shields.io/hexpm/v/sqlight)](https://hex.pm/packages/sqlight)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/sqlight/)Use [SQLite](https://www.sqlite.org/index.html) from Gleam!
Works on Erlang or JavaScript running on Deno.
```sh
gleam add sqlight
``````gleam
import gleam/dynamic
import sqlightpub fn main() {
use conn <- sqlight.with_connection(":memory:")
let cat_decoder = dynamic.tuple2(dynamic.string, dynamic.int)let sql = "
create table cats (name text, age int);insert into cats (name, age) values
('Nubi', 4),
('Biffy', 10),
('Ginny', 6);
"
let assert Ok(Nil) = sqlight.exec(sql, conn)let sql = "
select name, age from cats
where age < ?
"
let assert Ok([#("Nubi", 4), #("Ginny", 6)]) =
sqlight.query(sql, on: conn, with: [sqlight.int(7)], expecting: cat_decoder)
}
```Documentation can be found at .
## Why SQLite?
SQLite is a implementation of SQL as a library. This means that you don't run a
separate SQL server that your program communicates with, but you embed the SQL
implementation directly in your program. SQLite stores its data in a single
file. The file format is portable between different machine architectures. It
supports atomic transactions and it is possible to access the file by multiple
processes and different programs.You can also use in-memory databases with SQLite, which may be useful for testing.
## Implementation
When running on Erlang is library wrapper around the excellent Erlang library
[esqlite](https://hex.pm/packages/esqlite), which in turn is a wrapper around
the SQLite C library. It is implemented as a NIF, which means that the SQLite
database engine is linked to the erlang virtual machine.When running on Deno it is a wrapper around the excellent
[x/sqlite](https://deno.land/x/[email protected]) library, which in turn is a
wrapper around the SQLite C library compiled to WASM.## On using Bool with SQLite
SQLite does not have a native boolean type. Instead, it uses ints, where 0 is
False and 1 is True. Because of this the Gleam stdlib decoder for bools will not
work, instead the `sqlight.decode_bool` function should be used as it supports
both ints and bools.