Code Monkey home page Code Monkey logo

sqlight's Introduction

sqlight

Package Version Hex Docs

Use SQLite from Gleam!

Works on Erlang or JavaScript running on Deno.

gleam add sqlight
import gleam/dynamic
import sqlight

pub 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 https://hexdocs.pm/sqlight.

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, 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 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.

sqlight's People

Contributors

lpil avatar ukd1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.