Giter VIP home page Giter VIP logo

shrine-sql's Introduction

Shrine::Storage::Sql

Provides Shrine storage for storing files in any SQL database. It uses Sequel under the hood.

Installation

gem "shrine-sql"

Usage

We first need to create the table for our files, with "id" and "content" columns:

# 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
# 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.

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:

# Assuming :store uses the SQL storage.
Shrine.plugin :download_endpoint, storages: [:store]
Rails.application.routes.draw do
  mount Shrine.download_endpoint => "/attachments"
end
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!:

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:

sql.clear! { |dataset| dataset.where{created_at < Time.now - 7*24*60*60} }

Contributing

You can run the tests with Rake:

$ bundle exec rake test

License

MIT

shrine-sql's People

Contributors

artofhuman avatar janko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

artofhuman

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.