Socket
Book a DemoInstallSign in
Socket

mongoid_upk

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoid_upk

0.1.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

Mongoid::UPK

Mongoid::UPK generates more unique _id by using UUID.

UPK stands for "unique primary key" or "uuid primary key".

Install

gem install mongoid_upk

Gemfile

gem "mongoid_upk"

Usage

Basic

class MyModel
  include Mongoid::Document
  include Mongoid::UPK
end

or:

class MyModel
  include Mongoid::Document
  include Mongoid::UPK
  uuid_pk # you can leave this out, because it's the default generator
end

Will use UUID (uuid gem by assaf), :compact mode per default.

Pretty UUID

class MyModel
  include Mongoid::Document
  include Mongoid::UPK
  pretty_uuid_pk
end

Almost same as above but with hyphens in it. (Example: c1b457b0-3214-012f-316d-10e6baa29f2c)

Mixed ID

class MyModel
  include Mongoid::Document
  include Mongoid::UPK
  bson_uuid_pk
end

Very long _id with both BSON::ObjectId and UUID. (Example: 4f2e55a2ee911332c4000001-95faf560320f012f315510e6baa29f2c)

Own pk generator

class MyModel
  include Mongoid::Document
  include Mongoid::UPK
  unique_pk do
    # code goes here
    # result must be a string
    # example:
    Digest::MD5.hexdigest(Time.now.to_f.to_s)
  end
end

Example is self-describing.

Motivation

While writing very fast to MongoDB it is possible to take a BSON::ObjectId twice. This will lead to big troubles because you never know which document will be returned.

This gem utilizes the key method and overwrites the automatically given default id with the choosen generator on save. So the real id can be read first after a successful save (I don't want to hack deeper here, because saves are more important than getting a unique id on instance initialization).

UUID is in most cases more reliable, especially if the app instances are set up correctly (and/or running on different machines).

Read more about the UUID gem and how to set up your project (e.g. using unicorn or other threaded webservers).

License

MIT/X11

2012 — Christoph 'asaaki' Grabo

FAQs

Package last updated on 05 Feb 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.