Skip to content

Commit 28e133b

Browse files
committed
added new crud example
1 parent 0db0b0d commit 28e133b

File tree

11 files changed

+204
-4
lines changed

11 files changed

+204
-4
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/_build
2-
/doc
3-
/deps
1+
_build/
2+
doc/
3+
deps/
44
erl_crash.dump
55
*.ez
6-
/tmp
6+
tmp/
77
.tool-versions
88
.elixir_ls
99
plt_core_path/*.plt

examples/crud_example/.formatter.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

examples/crud_example/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# CrudExample
2+
3+
This project shows basic CRUD examples. You can use your own MongoDB instance or just use Docker to start a MongoDb container by calling:
4+
5+
docker-compose up -d mongodb
6+
7+
After that go to the project folder an start
8+
9+
#> mix deps.get
10+
#> iex -S mix
11+
12+
iex(1)> CrudExample.example_1()
13+
iex(1)> CrudExample.example_2()
14+
15+
## `example_1`
16+
17+
In this function we are using one connection to the database without using the application supervisor.
18+
19+
## `example_2`
20+
21+
The same operation like `example_1` but now using the connection pooling and application supervisor.
22+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# third-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure your application as:
12+
#
13+
# config :crud_example, key: :value
14+
#
15+
# and access this configuration in your application as:
16+
#
17+
# Application.get_env(:crud_example, :key)
18+
#
19+
# You can also configure a third-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env()}.exs"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
services:
3+
4+
mongodb:
5+
image: mongo:4
6+
ports:
7+
- "27017:27017"
8+
volumes:
9+
- mongodb-volume:/data/db
10+
networks:
11+
- crud
12+
logging:
13+
options:
14+
max-size: "10k"
15+
max-file: "1"
16+
17+
volumes:
18+
mongodb-volume:
19+
20+
networks:
21+
crud:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
defmodule CrudExample do
2+
3+
def create_vcard() do
4+
%{
5+
firstname: "Alexander",
6+
lastname: "Abendroth",
7+
contact: %{
8+
9+
telephone: "+49 111938947373",
10+
mobile: "+49 222192938383",
11+
fax: "+49 3332929292"
12+
},
13+
addess: %{
14+
street: "Fasanenweg 5",
15+
postal_code: "12345",
16+
city: "Berlin",
17+
country: "de"
18+
}
19+
}
20+
end
21+
22+
def example_1() do
23+
24+
{:ok, top} = Mongo.start_link(url: "mongodb://localhost:27017/db-1")
25+
26+
result = Mongo.insert_one(top, "people", create_vcard())
27+
28+
IO.puts "#{inspect result}\n"
29+
30+
result = Mongo.find_one(top, "people", %{})
31+
IO.puts "#{inspect result}\n"
32+
33+
result = Mongo.update_one(top, "people", %{lastname: "Abendroth"}, ["$set": ["address.postal_code": "20000"]])
34+
IO.puts "#{inspect result}\n"
35+
36+
result = Mongo.find_one(top, "people", %{"contact.email": "[email protected]"})
37+
IO.puts "#{inspect result}\n"
38+
39+
result = Mongo.delete_one(top, "people", %{lastname: "Abendroth"})
40+
IO.puts "#{inspect result}\n"
41+
42+
end
43+
44+
def example_2() do
45+
46+
{:ok, %Mongo.InsertOneResult{acknowledged: true, inserted_id: id}} = Mongo.insert_one(:mongo, "people", create_vcard())
47+
48+
IO.puts "ID is #{inspect id}\n"
49+
50+
result = Mongo.find_one(:mongo, "people", %{_id: id})
51+
IO.puts "#{inspect result}\n"
52+
53+
result = Mongo.update_one(:mongo, "people", %{_id: id}, ["$set": ["address.postal_code": "20000"]])
54+
IO.puts "#{inspect result}\n"
55+
56+
result = Mongo.find_one(:mongo, "people",%{_id: id})
57+
IO.puts "#{inspect result}\n"
58+
59+
result = Mongo.delete_one(:mongo, "people", %{_id: id})
60+
IO.puts "#{inspect result}\n"
61+
62+
end
63+
64+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule CrudExample.Application do
2+
@moduledoc false
3+
4+
use Application
5+
6+
def start(_type, _args) do
7+
import Supervisor.Spec
8+
9+
children = [
10+
worker(Mongo, [[name: :mongo, database: "db-1", pool_size: 3]])
11+
]
12+
13+
opts = [strategy: :one_for_one, name: CrudExample.Supervisor]
14+
Supervisor.start_link(children, opts)
15+
end
16+
end

examples/crud_example/mix.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule CrudExample.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :crud_example,
7+
version: "0.1.0",
8+
elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger],
18+
mod: {CrudExample.Application, []}
19+
]
20+
end
21+
22+
# Run "mix help deps" to learn about dependencies.
23+
defp deps do
24+
[
25+
{:mongodb_driver, "~> 0.5"}
26+
]
27+
end
28+
end

examples/crud_example/mix.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%{
2+
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
3+
"db_connection": {:hex, :db_connection, "2.0.6", "bde2f85d047969c5b5800cb8f4b3ed6316c8cb11487afedac4aa5f93fd39abfa", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
4+
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
5+
"mongodb_driver": {:hex, :mongodb_driver, "0.5.7", "f29cab9a011f685210c472888ee3fde3573459323f839b67c4d942e067d9eda1", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0.6", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule CrudExampleTest do
2+
use ExUnit.Case
3+
doctest CrudExample
4+
5+
test "greets the world" do
6+
assert CrudExample.hello() == :world
7+
end
8+
end

0 commit comments

Comments
 (0)