{"id":19866130,"url":"https://github.com/tlux/file_size_ecto","last_synced_at":"2026-02-28T03:03:41.600Z","repository":{"id":62429597,"uuid":"189411874","full_name":"tlux/file_size_ecto","owner":"tlux","description":"Provides types for file sizes that you can use in your Ecto schemata","archived":false,"fork":false,"pushed_at":"2020-07-15T14:42:09.000Z","size":62,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-01T16:21:35.387Z","etag":null,"topics":["ecto","ecto-types","file-size","filesize"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tlux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-30T12:42:59.000Z","updated_at":"2024-06-22T08:48:49.000Z","dependencies_parsed_at":"2022-11-01T20:09:29.107Z","dependency_job_id":null,"html_url":"https://github.com/tlux/file_size_ecto","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Ffile_size_ecto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Ffile_size_ecto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Ffile_size_ecto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlux%2Ffile_size_ecto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tlux","download_url":"https://codeload.github.com/tlux/file_size_ecto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240010361,"owners_count":19733514,"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":["ecto","ecto-types","file-size","filesize"],"created_at":"2024-11-12T15:25:04.477Z","updated_at":"2026-02-28T03:03:41.570Z","avatar_url":"https://github.com/tlux.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# File Size for Ecto\n\n[![Build Status](https://travis-ci.org/tlux/file_size_ecto.svg?branch=master)](https://travis-ci.org/tlux/file_size_ecto)\n[![Coverage Status](https://coveralls.io/repos/github/tlux/file_size_ecto/badge.svg?branch=master)](https://coveralls.io/github/tlux/file_size_ecto?branch=master)\n[![Hex.pm](https://img.shields.io/hexpm/v/file_size_ecto.svg)](https://hex.pm/packages/file_size_ecto)\n\nProvides types for file sizes that you can use in your Ecto schemata.\n\nThis library uses [`file_size`](https://hexdocs.pm/file_size) under the hood\nthat brings a file size parser, formatter and allows calculation and comparison\nof file sizes with different units.\n\n## Prerequisites\n\n* Erlang 20 or greater\n* Elixir 1.8 or greater\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `file_size_ecto` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:file_size_ecto, \"~\u003e 2.0\"}\n  ]\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at [https://hexdocs.pm/file_size_ecto](https://hexdocs.pm/file_size_ecto).\n\n## Usage\n\n### Without Unit\n\nThe following unit-less types are available:\n\n* [`FileSize.Ecto.Bit`](https://hexdocs.pm/file_size_ecto/FileSize.Ecto.Bit.html)\n* [`FileSize.Ecto.Byte`](https://hexdocs.pm/file_size_ecto/FileSize.Ecto.Byte.html)\n\nWrite a migration to add or update your file size field. You can use `:integer`\nor `:bigint` as types. If you expect very large file sizes, `:bigint` is\nrecommended of course.\n\n```elixir\ndefmodule MyProject.Migrations.AddFileSizeToMyTable\n  use Ecto.Migration\n\n  def up do\n    alter table(:my_table) do\n      add :file_size, :bigint\n    end\n  end\nend\n```\n\nUpdate your schema:\n\n```elixir\ndefmodule MySchema do\n  use Ecto.Schema\n\n  schema \"my_table\" do\n    # ...\n\n    field :file_size, FileSize.Ecto.Byte\n  end\nend\n```\n\nYou can store your file size like this:\n\n```elixir\nrecord = Repo.get(MySchema, 123)\n\nupdated_record =\n  record\n  |\u003e Echo.Changeset.change(file_size: FileSize.new(2, :mb))\n  |\u003e Repo.update!()\n\nupdated_record.file_size\n# =\u003e #FileSize\u003c\"2000000 B\"\u003e\n```\n\nOr, when working with user input:\n\n```elixir\nupdated_record\n  record\n  |\u003e Echo.Changeset.cast(%{file_size: \"4 KiB\"}, [:file_size])\n  |\u003e Repo.update!()\n\nupdated_record.file_size\n# =\u003e #FileSize\u003c\"4096 B\"\u003e\n```\n\nNote that the file size is always converted to the base unit (bit or byte) when\nstoring it in the database, because no unit information is available. Read the\nnext section if you want to find out how to store the size unit together with\nthe value.\n\n### With Unit\n\nThe following types with units are available:\n\n* [`FileSize.Ecto.BitWithUnit`](https://hexdocs.pm/file_size_ecto/FileSize.Ecto.BitWithUnit.html)\n* [`FileSize.Ecto.ByteWithUnit`](https://hexdocs.pm/file_size_ecto/FileSize.Ecto.ByteWithUnit.html)\n\nWrite a migration to add or update your file size field. The value is stored in\na `:map` field. This map contains the as bits or bytes and the unit.\n\n```elixir\ndefmodule MyProject.Migrations.AddFileSizeToMyTable\n  use Ecto.Migration\n\n  def up do\n    alter table(:my_table) do\n      add :file_size, :map\n    end\n  end\nend\n```\n\nUpdate your schema:\n\n```elixir\ndefmodule MySchema do\n  use Ecto.Schema\n\n  schema \"my_table\" do\n    # ...\n\n    field :file_size, FileSize.Ecto.ByteWithUnit\n  end\nend\n```\n\nYou can store your file size like this:\n\n```elixir\nrecord = Repo.get(MySchema, 123)\n\nupdated_record =\n  record\n  |\u003e Echo.Changeset.change(file_size: FileSize.new(2, :mb))\n  |\u003e Repo.update!()\n\nupdated_record.file_size\n# =\u003e #FileSize\u003c\"2 MB\"\u003e\n```\n\nOr, when working with user input:\n\n```elixir\nupdated_record\n  record\n  |\u003e Echo.Changeset.cast(%{file_size: \"4 KiB\"}, [:file_size])\n  |\u003e Repo.update!()\n\nupdated_record.file_size\n# =\u003e #FileSize\u003c\"4 KiB\"\u003e\n```\n\n## Docs\n\nDocumentation is available on [HexDocs](https://hexdocs.pm/file_size_ecto).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlux%2Ffile_size_ecto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftlux%2Ffile_size_ecto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlux%2Ffile_size_ecto/lists"}