• Stars
    star
    103
  • Rank 332,978 (Top 7 %)
  • Language
    Elixir
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create elixir functions with SQL as a body.

Defql Package Version Code Climate Build Status

Create elixir functions with SQL as a body.

Installation

If available in Hex, the package can be installed by adding defql to your list of dependencies in mix.exs:

defp deps do
  [
    {:defql, "~> 0.1.1"},
    {:postgrex, ">= 0.13.0"}, # optional
  ]
end

Configuration

It requires adapter key, and adapter specific options.

Use with ecto:

config :defql, connection: [
  adapter: Defql.Adapter.Ecto.Postgres,
  repo: YourApp.Repo
]

Use standalone connection:

config :defql, connection: [
  adapter: Defql.Adapter.Postgres,
  hostname: "localhost",
  username: "username",
  password: "password",
  database: "my_db",
  pool: DBConnection.Poolboy,
  pool_size: 1
]

Usage

We can define module to have access to our database:

defmodule UserQuery do
  use Defql

  defselect get(conds), table: :users, columns: [:name, :email]
  definsert add(params), table: :users
  defupdate update(params, conds), table: :users
  defdelete delete(conds), table: :users

  defquery get_by_name(name, limit) do
    "SELECT * FROM users WHERE name = $name LIMIT $limit"
  end
end

Right now we have easy access to users in database:

UserQuery.get(id: "3") # => {:ok, [%{...}]}
UserQuery.add(name: "Smbdy") # => {:ok, [%{...}]}
UserQuery.update([name: "Other"],[id: "2"]) # => {:ok, [%{...}]}
UserQuery.delete(id: "2") # => {:ok, [%{...}]}

UserQuery.get_by_name("Ela", 4) # => {:ok, [%{...}, %{...}]}

We can also define common table for the whole module.

defmodule UserQuery do
  use Defql, table: :users

  defselect get(conds), columns: [:name, :email]
  definsert add(params)
  defupdate update(params, conds)
  defdelete delete(conds)
end

%{...} It's a hash with user properties straight from database.

Supported condition statements:

  • user_id: [1,2,3,4]
  • user_id: {:in, [1,2,3,4,5]}
  • name: {:like, "%john%"}
  • name: {:ilike, "%john"}

TODO

  • MySQL support
  • Cleanup ECTO adapter
  • Support database errors
  • Transactions

Thank you!

Become Patreon

More Repositories

1

colorize

Ruby string class extension. It add some methods to set color, background color and text effect on console easier using ANSI escape sequences.
Ruby
1,222
star
2

export

Erlport wrapper for Elixir
Elixir
178
star
3

awesome-opal

Awesome Opal
97
star
4

elixir_desktop_application

Code for story: https://puddleofcode.com/story/how-to-create-desktop-application-with-elixir
Elixir
52
star
5

tensorflow.cr

Crystal binding for TensorFlow.
Crystal
39
star
6

airbrakex

Elixir client for the Airbrake service.
Elixir
27
star
7

rust_to_js

An example of Rust code that compiles to javascript.
HTML
26
star
8

codeclimate-credo

Code Climate Credo Engine
Elixir
25
star
9

opal-virtual-dom

virtual-dom wrapper for opal
Ruby
24
star
10

ex_ical

ICalendar parser for Elixir.
Elixir
23
star
11

gedit-mterminal

Terminal with multiple windows plugin for GEdit
Python
22
star
12

fazic

fazic.fazibear.me
Rust
17
star
13

crystal_lib_gen

Automatic binding generator for Crystal
Crystal
15
star
14

Rubocop.tmbundle

Rubocop Textmate 2 Bundle
Ruby
11
star
15

codeclimate-dogma

Elixir
10
star
16

mrubyvst

VST plugin scriptable with MRuby
C++
9
star
17

asciify-me

Asciify yourself!
Ruby
8
star
18

gruby

Gadu-Gadu (polish communicator) protocol implementation in Ruby language.
Ruby
8
star
19

cmp-nerdfonts

nvim-cmp source for nerdfonts.
Lua
7
star
20

codeclimate-elixir

Code Climate Engine for Elixir.
Elixir
7
star
21

inesid

Retro Web SID Player
HTML
6
star
22

opal-phoenix

Opal wrapper for Phoenix Framework javascript library.
Ruby
6
star
23

opal-slim-sass-sprockets-example

Opal environment with sprockets, slim, sass, and rails-assets
Ruby
5
star
24

exrbbench

Elixir
4
star
25

rubyscript

attempt to convert ruby syntax to JS syntax
Ruby
3
star
26

Crystal.tmbundle

Shell
3
star
27

mrubybyexample

mruby snippets
C
3
star
28

pix_elixir

Elixir
3
star
29

opal-web-midi

Web MIDI for Opal.
Ruby
3
star
30

screen_saviour.nvim

Lua
3
star
31

twitdrone

Shut up, and listen to twitter ...
Ruby
2
star
32

frubby

Bring fuzzy_match directly into ruby.
Ruby
2
star
33

tensorflow.cr_examples

Examples for crystal binding for TensorFlow.
Crystal
2
star
34

grtail

Grouped RailsLog Tail
Ruby
1
star
35

elixirscript-loader

JavaScript
1
star
36

rebar_elixir

Rebar plugin to compile elixir code and support mix packages
Erlang
1
star
37

opal-airbrake

Opal wrapper for airbrake-js javascript library.
Ruby
1
star
38

bobi

Bobi
Ruby
1
star
39

web-audio-playground

Ruby
1
star
40

oxmlrpc

Simple, lightweight and fast XML-RPC client library for ruby. It's based on OX XML parser.
Ruby
1
star
41

asciifyme.go

Web asciifyier in go
Go
1
star
42

netvibes-widgets

UWA/Netvibes Widgets
1
star
43

ascii_chat

Simple asciiart chat web application
JavaScript
1
star