Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shrinerb/shrine-sql
SQL database storage for Shrine
https://github.com/shrinerb/shrine-sql
blob file sequel shrine sql
Last synced: 7 days ago
JSON representation
SQL database storage for Shrine
- Host: GitHub
- URL: https://github.com/shrinerb/shrine-sql
- Owner: shrinerb
- License: mit
- Created: 2015-12-11T22:12:05.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-26T14:13:27.000Z (about 5 years ago)
- Last Synced: 2024-05-22T17:32:59.547Z (6 months ago)
- Topics: blob, file, sequel, shrine, sql
- Language: Ruby
- Homepage:
- Size: 30.3 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Shrine::Storage::Sql
Provides [Shrine] storage for storing files in any SQL database. It uses
[Sequel] under the hood.## Installation
```ruby
gem "shrine-sql"
```## Usage
We first need to create the table for our files, with "id" and "content" columns:
```rb
# for Sequel users
Sequel.migration do
change do
create_table :files do
primary_key :id
File :content
String :metadata, text: true
Time :created_at
end
end
end
```
```rb
# for ActiveRecord users
class CreateFiles < ActiveRecord::Migration
def change
create_table :files do |t|
t.binary :content
t.text :metadata
t.datetime :created_at
end
end
end
```We should instantiate the storage with a `Sequel::Database` object and the
name of the table, regardless of the ORM you're actually using in your app.```rb
require "shrine/storage/sql"
require "sequel"DB = Sequel.connect("postgres:///my-database")
Shrine::Storage::Sql.new(database: DB, table: :files)
```You can see [Connecting to a database] on how connect to any database with
Sequel.### URL
By itself shrine-sql doesn't provide URLs to files, but they can be streamed
via a URL with the `download_endpoint` plugin:```rb
# Assuming :store uses the SQL storage.
Shrine.plugin :download_endpoint, storages: [:store]
```
```rb
Rails.application.routes.draw do
mount Shrine.download_endpoint => "/attachments"
end
```
```rb
user.avatar_url #=> "/attachments/store/938432984643.jpg"
```### Indices
It is recommended that you add a unique index to the "id" column, for faster
lookups.## Copying
If you're using the SQL storage for both cache and store, moving from cache to
store will copy the record using SQL instead "reuploading" it, which means the
file contents won't be read into memory.## Clearing
You can delete all data from the storage via `Shrine::Storage::Sql#clear!`:
```rb
sql = Shrine::Storage::Sql.new(database: DB, table: :files)
sql.clear!
```If you're using SQL as temporary storage, you can clear old files by passing
a block to `#clear!` and querying the `created_at` column:```rb
sql.clear! { |dataset| dataset.where{created_at < Time.now - 7*24*60*60} }
```## Contributing
You can run the tests with Rake:
```sh
$ bundle exec rake test
```## License
[MIT](http://opensource.org/licenses/MIT)
[Shrine]: https://github.com/shrinerb/shrine
[Sequel]: https://github.com/jeremyevans/sequel
[Connecting to a database]: http://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html