{"id":13879458,"url":"https://github.com/shrinerb/shrine-sql","last_synced_at":"2025-06-16T13:03:35.793Z","repository":{"id":56895476,"uuid":"47853588","full_name":"shrinerb/shrine-sql","owner":"shrinerb","description":"SQL database storage for Shrine","archived":false,"fork":false,"pushed_at":"2019-09-26T14:13:27.000Z","size":31,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-22T17:32:59.547Z","etag":null,"topics":["blob","file","sequel","shrine","sql"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/shrinerb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-11T22:12:05.000Z","updated_at":"2020-09-04T06:29:37.000Z","dependencies_parsed_at":"2022-08-21T01:50:20.026Z","dependency_job_id":null,"html_url":"https://github.com/shrinerb/shrine-sql","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shrinerb%2Fshrine-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shrinerb","download_url":"https://codeload.github.com/shrinerb/shrine-sql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224177583,"owners_count":17268703,"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":["blob","file","sequel","shrine","sql"],"created_at":"2024-08-06T08:02:21.744Z","updated_at":"2024-11-12T05:27:22.018Z","avatar_url":"https://github.com/shrinerb.png","language":"Ruby","readme":"# Shrine::Storage::Sql\n\nProvides [Shrine] storage for storing files in any SQL database. It uses\n[Sequel] under the hood.\n\n## Installation\n\n```ruby\ngem \"shrine-sql\"\n```\n\n## Usage\n\nWe first need to create the table for our files, with \"id\" and \"content\" columns:\n\n```rb\n# for Sequel users\nSequel.migration do\n  change do\n    create_table :files do\n      primary_key :id\n      File :content\n      String :metadata, text: true\n      Time :created_at\n    end\n  end\nend\n```\n```rb\n# for ActiveRecord users\nclass CreateFiles \u003c ActiveRecord::Migration\n  def change\n    create_table :files do |t|\n      t.binary :content\n      t.text :metadata\n      t.datetime :created_at\n    end\n  end\nend\n```\n\nWe should instantiate the storage with a `Sequel::Database` object and the\nname of the table, regardless of the ORM you're actually using in your app.\n\n```rb\nrequire \"shrine/storage/sql\"\nrequire \"sequel\"\n\nDB = Sequel.connect(\"postgres:///my-database\")\nShrine::Storage::Sql.new(database: DB, table: :files)\n```\n\nYou can see [Connecting to a database] on how connect to any database with\nSequel.\n\n### URL\n\nBy itself shrine-sql doesn't provide URLs to files, but they can be streamed\nvia a URL with the `download_endpoint` plugin:\n\n```rb\n# Assuming :store uses the SQL storage.\nShrine.plugin :download_endpoint, storages: [:store]\n```\n```rb\nRails.application.routes.draw do\n  mount Shrine.download_endpoint =\u003e \"/attachments\"\nend\n```\n```rb\nuser.avatar_url #=\u003e \"/attachments/store/938432984643.jpg\"\n```\n\n### Indices\n\nIt is recommended that you add a unique index to the \"id\" column, for faster\nlookups.\n\n## Copying\n\nIf you're using the SQL storage for both cache and store, moving from cache to\nstore will copy the record using SQL instead \"reuploading\" it, which means the\nfile contents won't be read into memory.\n\n## Clearing\n\nYou can delete all data from the storage via `Shrine::Storage::Sql#clear!`:\n\n```rb\nsql = Shrine::Storage::Sql.new(database: DB, table: :files)\nsql.clear!\n```\n\nIf you're using SQL as temporary storage, you can clear old files by passing\na block to `#clear!` and querying the `created_at` column:\n\n```rb\nsql.clear! { |dataset| dataset.where{created_at \u003c Time.now - 7*24*60*60} }\n```\n\n## Contributing\n\nYou can run the tests with Rake:\n\n```sh\n$ bundle exec rake test\n```\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n\n[Shrine]: https://github.com/shrinerb/shrine\n[Sequel]: https://github.com/jeremyevans/sequel\n[Connecting to a database]: http://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshrinerb%2Fshrine-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshrinerb%2Fshrine-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshrinerb%2Fshrine-sql/lists"}