Skip to content

Commit

Permalink
make cid docs generic to any record dwyl/mvp#410
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 24, 2023
1 parent 79f8725 commit 356a38d
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions src/mvp/15-item-cid.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
# Use `cid` for `item` _unique_ `id`
# Use `cid` for _universally unique_ `ids`

By default the `items` table uses an auto-incrementing (`Serial`)
for the `id` (Primary Key):
By default all the tables
in a `Phoenix` Application
use an auto-incrementing (`Serial`)
for the `id` (Primary Key).
e.g the `items` table:

![items-id-integer](https://github.com/dwyl/mvp/assets/194400/d3020e53-2ad8-43ff-b0ed-95251ee21a87)

This is fine for a _server_-side rendered app
with a _single_ relational database instance.
i.e. the server/database controls the id of records.
But our ambition has always been
to build a mobile + offline-first distributed App.

Luckily our friends at
[Protocol Labs]()
(creators of )
[Protocol Labs](https://github.com/ipfs)
creators of
[`IPFS`](https://github.com/dwyl/learn-ipfs)
have done some groundwork on "Distributed Apps" problem
and we have a little library that creates `IPFS` compliant
have done some great groundwork on the
["Decentralized Apps"](https://en.wikipedia.org/wiki/Decentralized_application)
problem so we can build on that.
We have a little library that creates `IPFS` compliant
Content Identifiers:
[`cid`](https://github.com/dwyl/cid)

Expand All @@ -26,11 +32,17 @@ item = %{text: "Build PARA System App", person_id: 2, status: 2}
Cid.cid(item)
"zb2rhn92tqTt41uFZ3hh3VPnssXjYCW4yDSX7KB39dXZyMtNC"
```
This `cid` string is unique to this content
therefore creating it on the client (Mobile device)
will generate the _same_ `cid`
if the record is created _offline_.


## Add `cid` package to `deps`

Add the `cid` package to the `deps` in `mix.exs`:

## Add `excid` package to `deps`

Add the `excid` package to the `deps` in `mix.exs`:

```elixir
# Universally Unique Deterministic Content IDs: github.com/dwyl/cid
Expand All @@ -55,30 +67,31 @@ when compared to `base32`.
```elixir
"bafkreihght5nbnmn6xbwoakmjyhkiu2naxmwpxgxbp6xkpbiweuetbohde"
|> String.length()
49
59
```
vs.

```elixir
"zb2rhn92tqTt41uFZ3hh3VPnssXjYCW4yDSX7KB39dXZyMtNC"
|> String.length()
59
49
```

## Create a Migration to add `cid` field to `item`
## Create `migration`

Using the
[`mix ecto.gen.migration`](https://hexdocs.pm/ecto_sql/Mix.Tasks.Ecto.Gen.Migration.html)
command,
create a migration file:

```sh
mix ecto.gen.migration add_cid_to_item
mix ecto.gen.migration add_cid
```

You should see output similar to the following:

```sh
* creating priv/repo/migrations/20230824153220_add_cid_to_item.exs
* creating priv/repo/migrations/20230824153220_add_cid.exs
```

This tells us that the migration file was created:
Expand Down Expand Up @@ -110,7 +123,35 @@ defmodule App.Repo.Migrations.AddCidToItem do
end
```


> **Note**: if you're rusty on `migrations`,
see:
[devhints.io/phoenix-migrations](https://devhints.io/phoenix-migrations)
[devhints.io/phoenix-migrations](https://devhints.io/phoenix-migrations)

## Add `cid` field to `item` schema

Open the
`lib/app/item.ex`
file and locate the `schema items do` section.
Add the line:

```elixir
field :cid, :string
```

## Add `:cid` to `changeset/2`

Still in the
`lib/app/item.ex`
file, locate the `changeset/2` function definition
and change the line:

```elixir
|> cast(attrs, [:person_id, :status, :text])
```

To:

```elixir
|> cast(attrs, [:cid, :person_id, :status, :text])
```

0 comments on commit 356a38d

Please sign in to comment.