Skip to content

Commit

Permalink
feat: Creating generic UserTable module with ETS. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Dec 24, 2022
1 parent 5cc8933 commit 6f20305
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1129,8 +1129,76 @@ def deps do
end
```

With that installed,
we are going to be creating a module
that will *manage the users table*.
We are going to create it on startup,
and create users/edit them.

For that, create a file in `lib/app/users.ex`.

```elixir
defmodule UsersTable do

alias ETS.Set

@table :users_table

def init do
Set.new(name: @table)
end

def list_users do
@table
|> Set.wrap_existing!()
|> Set.to_list()
end

def create_user(stripe_id) do
@table
|> Set.wrap_existing!()
|> Set.put_new( {stripe_id, false})
end

def update_payment_status(%{:stripe_id => stripe_id, :status => status}) do
@table
|> Set.wrap_existing!()
|> Set.put({stripe_id, status})
end

end
```

Let's go over what we have done.
We are going to be saving our users
with a tuple containing the `**stripe_id**`
and a `**status**` boolean field,
referring to whether the user has paid or not.

All the functions being used are used
according to the [`ets` wrapper documentation](https://github.com/TheFirstAvenger/ets).
- the `init/0` function creates the table to store our users.
- `list_users` returns the list of users.
- `create_user/1` receives a `stripe_id` and creates a user object.
By default, the `status` field is created with `false`.
- `update_payment_status/1` receives a user object
and updates accordingly.

Let's make use of some of these functions.
We want to setup the `ETS` table on the process startup.
For this, we are going to initiate the table
on the `start/1` function inside `lib/app/application.ex`.
This functions is executed when the process is created,
so it fits right our needs!

Inside this function, add:

```elixir
# Creating ETS user table
{:ok, _set} = UsersTable.init()
```

Awesome!

# Thanks!

Expand Down
3 changes: 3 additions & 0 deletions lib/app/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ defmodule App.Application do
# {App.Worker, arg}
]

# Creating ETS user table
{:ok, _set} = UsersTable.init()

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: App.Supervisor]
Expand Down
29 changes: 29 additions & 0 deletions lib/app/users.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule UsersTable do

alias ETS.Set

@table :users_table

def init do
Set.new(name: @table)
end

def list_users do
@table
|> Set.wrap_existing!()
|> Set.to_list()
end

def create_user(stripe_id) do
@table
|> Set.wrap_existing!()
|> Set.put_new( {stripe_id, false})
end

def update_payment_status(%{:stripe_id => stripe_id, :status => status}) do
@table
|> Set.wrap_existing!()
|> Set.put({stripe_id, status})
end

end

0 comments on commit 6f20305

Please sign in to comment.