Skip to content

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

License

Notifications You must be signed in to change notification settings

TheNeatCompany/mongoid-sequence

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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, :organization_id
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 © 2010 Gonçalo Silva, released under the MIT license

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%