
Security News
Oxlint Introduces Type-Aware Linting Preview
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
rails apps are given the option of integer ids, uuids or global ids. these are all annoying in their own ways:
enter cool id: use a random string id, with a little prefix like stripe. e.g. usr_vktd1b5v84lr
for a user, or prd_vktd1b5v84lr
for a product. cool_id makes this easy to apply to all of your models and doesn't bring in any extra dependencies. your urls will be beautiful.
class User < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "usr"
end
User.create!(name: "...").id
# => "usr_vktd1b5v84lr"
CoolId.locate("usr_vktd1b5v84lr")
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
e.g. for batch inserts or upserts
User.generate_cool_id
# => "usr_vktd1b5v84lr"
parsed = CoolId.parse("usr_vktd1b5v84lr")
# => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>
parsed.model_class
# => User
it takes parameters to change the alphabet or length
class Customer < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
end
Customer.create!(name: "...").id
# => "cus_UHNYBINU"
and these can be configured globally
CoolId.configure do |config|
config.separator = "-"
config.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
config.length = 8
end
you can use cool_id with a separate field, keeping the default primary key:
class Product < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "prd", id_field: :public_id
end
product = Product.create!(name: "Cool Product")
product.id # => 1 (or a uuid or whatever primary key you like)
product.public_id # => "prd_vktd1b5v84lr"
# locate will find this
CoolId.locate("prd_vktd1b5v84lr") # => #<Product id: 1, public_id: "prd_vktd1b5v84lr", ...>
this approach allows you to avoid exposing your primary keys, read David Bryant Copeland's Create public-facing unique keys alongside your primary keys to learn why you might want to do this. it also allows you to adopt cool_id more easily in a project that already has some data.
add cool_id to your Gemfile:
bundle add cool_id
gem "cool_id"
don't want to deal with a dependency? copy it into your project:
mkdir -p app/lib
curl https://raw.githubusercontent.com/schpet/cool_id/main/lib/cool_id.rb -o app/lib/cool_id.rb
use string ids when creating a table
create_table :users, id: :string do |t|
t.string :name
end
include the CoolId::Model
concern in the active record model and set up a prefix
class User < ActiveRecord::Base
include CoolId::Model
cool_id prefix: "usr"
end
note: if you prefer more traditional primary keys (like bigints or uuids) you can use the id_field
on a different column.
you have drank the coolaid. setup rails to use string ids on all new generated migrations
# config/initializers/generators.rb
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :string
end
then setup ApplicationRecord
to include cool id and ensure it's setup in classes that inherit from it
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
include CoolId::Model
primary_abstract_class
enforce_cool_id_for_descendants
end
if you use the graphql ruby node interface, you can implement object identification
# app/graphql/app_schema.rb
class AppSchema < GraphQL::Schema
def self.id_from_object(object, type_definition, query_ctx)
object.id
end
def self.object_from_id(id, query_ctx)
CoolId.locate(id)
end
end
FAQs
Unknown package
We found that cool_id demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.