Giter Club home page Giter Club logo

meta_content's Introduction

MetaContent

Fast and queryable schemaless MySQL. MetaContent stores attributes of your model across multiple rows in a separate table. It allows you to treat attributes as ancillary data rather than incurring the load of initializing or storing them upon update.

Installation

Add to your Gemfile

gem 'meta_content'

Data

Create a table for your class

rails g meta_content [your_class_name]
rails g meta_content user

This will create a table named [your_class_name]_meta. This table will hold the meta attributes for the class as soon as you include the MetaContent module into it.

Usage

Include the MetaContent module into your class.

class User < ActiveRecord::Base
  include MetaContent
  # ...
end	

Then describe the attributes using meta blocks:

class User < ActiveRecord::Base

  meta do
    string :my_field
  end
	
  meta :scoped_meta do
    integer :visitor_count
    string  :visit_path
    
    meta :subscoped_meta do
      range :visitor_ages
    end
  end

end

Now you can access these attributes in the following way:

u = User.new

u.my_field = 'test'
u.my_field
# => 'test'

u.meta
# => {:my_field => 'test'}

u.scoped_meta
# => <MetaContent::Proxy …>

u.scoped_meta.visitor_count = 30
u.scoped_meta.visitor_count
# => 30

u.scoped_meta__visitor_count
# => 30
u.scoped_meta.meta
# => {:visitor_count => 30}

u.scoped_meta = {:visitor_count => 3, :visit_path => '/test'}
u.scoped_meta__visitor_count
# => 3

u.scoped_meta__subscoped_meta__visitor_ages = (30..40)
u.scoped_meta__subscoped_meta__visitor_ages
# => (30..40)
u.scoped_meta.subscoped_meta.visitor_ages
# => (30..40)

You'll notice accessors are provided for all meta content, even if it's scoped. You can apply this data just like any other attribute:

User.new(:scoped_meta__visitor_count => 3)

Fast, Slow, Huh?

It's fast. It uses MySQL's ON DUPLICATE KEY UPDATE syntax to only ever conduct 1 or 2 queries when changes are made. If no changes are made, nothing is written. Unlike serialized content, it will not impact your table's performance.

Scalability

Not sure what the upper limit of this statement is but up through a few million records in the meta table I've seen no change in query times. I'll try to push this up to a few hundred million records and try again at some point.

meta_content's People

Watchers

 avatar

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.