GithubHelp home page GithubHelp logo

kaorukobo / mongoid-sequence Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johaned/mongoid-sequence

0.0 0.0 0.0 34 KB

Specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors) while using Mongoid.

License: MIT License

Ruby 100.00%

mongoid-sequence's Introduction

Mongoid Sequence

Mongoid Sequence allows you to specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors).

Credits

This gem was inspired by a couple of gists by masatomo and ShogunPanda.

Usage

Include Mongoid::Sequence in your class and call sequence(:field).

Like this:

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence

	field :my_sequence, :type => Integer
	sequence :my_sequence
end

s1 = Sequenced.create
s1.sequence #=> 1

s2 = Sequenced.create
s2.sequence #=> 2 # and so on

It is possible to add an additional discriminator to the sequence (e.g. a tenant id)

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence

  field :my_sequence, :type => Integer
  belongs_to :organization

	sequence :my_sequence, prefix: :organization_id
end

If you have an embedded document, you don't have worrying about fake increment inside each child, mongoid-secuence will recognize this association and segregate the count per each child

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence

  field :my_sequence, :type => Integer

  embedded_in :parent

	sequence :my_sequence
end

If you have an inheritance relation and you want to keep the sequence associated with one of your parents, you can specify what ancestors level you want using the parent_level option, below scenario will keep the sequence called parent_my_sequence sequence for Child instances

class Parent
	include Mongoid::Document
	include Mongoid::Sequence

  field :my_sequence, :type => Integer
end

class Child
	include Mongoid::Document
	include Mongoid::Sequence

  sequence :my_sequence, parent_level: 1
end

It's also possible to make the id field behave like this:

class Sequenced
	include Mongoid::Document
	include Mongoid::Sequence

	sequence :_id
end

s1 = Sequenced.create
s1.id #=> 1

s2 = Sequenced.create
s2.id #=> 2 # and so on

Consistency

Mongoid::Sequence uses the atomic findAndModify command, so you shouldn't have to worry about the sequence's consistency.

Installation

Just add it to your projects' Gemfile:

gem "mongoid-sequence"

Copyright © 2017 Gonçalo Silva, Johan Tique released under the MIT license

mongoid-sequence's People

Contributors

goncalossilva avatar johaned avatar cblock avatar mauricio 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.