{"id":22741743,"url":"https://github.com/bedus-creation/hiive-elixir","last_synced_at":"2025-03-30T04:14:12.714Z","repository":{"id":267074604,"uuid":"899445969","full_name":"bedus-creation/hiive-elixir","owner":"bedus-creation","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-08T06:26:22.000Z","size":124,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-05T06:33:49.356Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bedus-creation.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-06T09:32:55.000Z","updated_at":"2024-12-08T06:26:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"e8b3b98f-e955-47a7-a69e-1a304608a313","html_url":"https://github.com/bedus-creation/hiive-elixir","commit_stats":null,"previous_names":["bedus-creation/hiive-elixir"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fhiive-elixir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fhiive-elixir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fhiive-elixir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Fhiive-elixir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bedus-creation","download_url":"https://codeload.github.com/bedus-creation/hiive-elixir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246273552,"owners_count":20750906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-11T00:16:44.946Z","updated_at":"2025-03-30T04:14:12.708Z","avatar_url":"https://github.com/bedus-creation.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learning Phoenix Framework as Laravel Developer\n\n### Installation\n\n```shell\nmix phx.new hiive --no-ecto --no-assets --no-live\n```\n\n### Directory Structure\n\n* lib: all the source code will stay in this folder\n\n### MVC architecture\n\nPhoenix and Laravel share a similar structure when it comes to routing. In Laravel, routes are defined in a dedicated file like this:\n\n```php\nuse App\\Controllers\\HomeController;\n\nRoute::get('/home', [HomeController::class, 'index']);\n```\n\nSimilarly, you can define a route in Phoenix using this syntax:\n\n```elixir\nget \"/home\", HomeController, :index\n```\n\nOne notable difference is that Laravel does not explicitly enforce a separation between the domain and application layers—you have the flexibility to configure your application structure as you see\nfit. In contrast, Phoenix imposes a more opinionated folder structure. For example, in Phoenix, we define `hiive_web` to serve as the application layer and `hiive` as the domain layer, making this\nseparation more explicit.\n\n### Database\n\nI didn't set up the database initially due to some errors, but now let's quickly add a SQLite database. I found that these two dependencies are required to set up SQLite:\n\n```elixir\ndefp deps do\n[\n{:ecto_sql, \"~\u003e 3.10\"},\n{:exqlite, \"~\u003e 0.13.11\"}\n]\nend\n```\n\nand then let's pull the dependencies as:\n\n```shell\nmix deps.get\n```\n\nNow we need to configure the database as:\n\n```elixir\n# config/config.exs\nconfig :hiive, Hiive.Repo,\n  database: \"my_app_dev.sqlite3\",\n  pool_size: 5,\n  show_sensitive_data_on_connection_error: true\n\nconfig :hiive, ecto_repos: [Hiive.Repo]\n```\n\nNow we need to generate a repo module as:\n\n```shell\nmix ecto.gen.repo -r Hiive.Repo\n```\n\nand also need to add `Hiive.Repo` in `lib/hiive/application.ex`\n\n```elixir\n  def start(_type, _args) do\n    children = [\n      HiiveWeb.Telemetry,\n      {DNSCluster, query: Application.get_env(:hiive, :dns_cluster_query) || :ignore},\n      {Phoenix.PubSub, name: Hiive.PubSub},\n      # Start the Finch HTTP client for sending emails\n      {Finch, name: Hiive.Finch},\n      # Start a worker by calling: Hiive.Worker.start_link(arg)\n      # {Hiive.Worker, arg},\n      # Start to serve requests, typically the last entry\n      HiiveWeb.Endpoint,\n      Hiive.Repo\n    ]\nend\n```\n\n### Generate a migrations\nThe migration and model generation functionality of the Phoenix framework is similar to Laravel. Additionally, there are a few frameworks, such as .NET and Django, that generate migrations based on entity classes.\n```shell\nmix phx.gen.schema Post post title:string description:text\n```\nThe schema definition parts are also very much similar, in Phoenix:\n```elixir\ndefmodule Hiive.Repo.Migrations.CreatePost do\n  use Ecto.Migration\n\n  def change do\n    create table(:post) do\n      add :title, :string\n      add :description, :text\n      add :status, :string\n\n      timestamps(type: :utc_datetime)\n    end\n  end\nend\n```\n\nIn Laravel\n```php\nSchema::table('post', function (Blueprint $table) {\n    $table-\u003estring('title');\n    $table-\u003estring('description');\n    $table-\u003estring('status');\n    $table-\u003etimestamps();\n});\n```\n\n### Tinkering into the phoenix application\n\n```shell\niex -S mix\n```\n\n### Inserting some records\n\n```elixir\n# iex -S mix\nalias Hello.User\n```\n\nand records can be added as:\n\n```elixir\nparams = %{name: \"Joe Example\", email: \"joe@example.com\", bio: \"An example to all\", number_of_pets: 5, random_key: \"random value\"}\n\nchangeset = User.changeset(%User{}, params)\nchangeset.valid?\nchangeset.changes\n```\n\nInserting a record\n\n```elixir\nRepo.insert(%User{email: \"user1@example.com\"})\n```\n\nFetching all the records from a table\n\n```elixir\nRepo.all(User)\n```\n\nWhere condition:\n\n```elixir\nRepo.one(\n  from u in User, \n  where: ilike(u.email, \"%1%\"),\n  select: count(u.id)\n)\n```\n\n### The Json API\nPhoenix includes a built-in generator for creating APIs, which automatically generates migrations, schemas, controllers, tests, and more. In contrast, Laravel doesn't have a built-in generator that provides such comprehensive features.\n```shell\nmix phx.gen.json Posts Post post title:string description:string\n```\n\n### Testing\nConfigure the test database in `config/test.exs` as:\n```elixir\nconfig :hiive, Hiive.Repo,\n database: \"database/database-testing.sqlite\",\n pool: Ecto.Adapters.SQL.Sandbox\n\nconfig :hiive, ecto_repos: [Hiive.Repo]\n```\n\nAfter setting up database, we should run the migration before running tests as:\n```shell\nMIX_ENV=test mix ecto.migrate\n```\n\nTo reset the database:\n```shell\nMIX_ENV=test mix ecto.drop; MIX_ENV=test mix ecto.migrate\n```\n\nWhich I found similar to the Laravel\n```shell\nphp artisan migrate:fresh\n```\n\n#### The first Test\nLet's test whether our API endpoint accepts user-submitted data and successfully stores it in the SQLite database.\n\nOut test would look like:\n```elixir\ndefmodule HiiveWeb.PostControllerTest do\n  use HiiveWeb.ConnCase\n  use Hiive.DataCase, async: true\n\n  alias Hiive.Posts\n\n  @create_attrs %{\n    title: \"This is my test blog\",\n    description: \"the description of a test blog\"\n  }\n\n  describe \"creates post\" do\n    test \"user can create a post\", %{conn: conn} do\n      conn = post(conn, ~p\"/api/posts\", @create_attrs)\n      assert %{\"id\" =\u003e id} = json_response(conn, 201)[\"data\"]\n\n      post = Posts.get_post!(id)\n      assert @create_attrs[:title] == post.title\n      assert @create_attrs[:description] == post.description\n    end\n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedus-creation%2Fhiive-elixir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbedus-creation%2Fhiive-elixir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedus-creation%2Fhiive-elixir/lists"}