diff --git a/Gemfile.lock b/Gemfile.lock index dc62022..da00ba2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - cool_id (0.1.4) + cool_id (0.1.5) activerecord (>= 6.0) activesupport (>= 6.0) nanoid (~> 2.0) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ec4e833 --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) Peter Schilling + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md index af18036..97d3f4a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # cool_id -a gem for rails that generates string ids for active record models with a per-model prefix followed by a nanoid. +a gem for rails that generates string primary key ids for active record models with a per-model prefix followed by a nanoid. this lets you create stripe style ids in your own rails app, and they'll be the same ids you see in your database. ```ruby class User < ActiveRecord::Base @@ -10,26 +10,18 @@ end User.create!(name: "...").id # => "usr_vktd1b5v84lr" - -class Customer < ActiveRecord::Base - include CoolId::Model - cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8 -end - -Customer.create!(name: "...").id -# => "cus_UHNYBINU" ``` -it can also lookup records by ids, similar to global id: +it can also lookup records similar to global id: ```ruby -user = User.create!(name: "John Doe") -# => # - CoolId.locate("usr_vktd1b5v84lr") # => # +``` -# You can also parse the id without fetching the record +and parse ids + +```ruby parsed = CoolId.parse("usr_vktd1b5v84lr") # => # @@ -37,8 +29,41 @@ parsed.model_class # => User ``` +and generate ids without creating a record + +```ruby +# generate an id, e.g. for batch inserts or upserts +User.generate_cool_id +# => "usr_vktd1b5v84lr" + +``` + +it takes parameters to change the alphabet or length + +```ruby +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 + +```ruby +CoolId.configure do |config| + config.separator = "-" + config.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + config.length = 8 +end +``` + ## installation +add cool_id to your Gemfile: + ```bash bundle add cool_id ``` @@ -47,7 +72,7 @@ bundle add cool_id gem "cool_id" ``` -### per-model +### using cool_id in one model use string ids when creating a table @@ -66,9 +91,9 @@ class User < ActiveRecord::Base end ``` -### all models +### using cool_id on all models -use string ids on all new generated migrations +you have drank the coolaid. setup rails to use string ids on all new generated migrations ```ruby # config/initializers/generators.rb @@ -77,7 +102,7 @@ Rails.application.config.generators do |g| end ``` -setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it +then setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it ```ruby # app/models/application_record.rb @@ -87,3 +112,21 @@ class ApplicationRecord < ActiveRecord::Base enforce_cool_id_for_descendants end ``` + +### graphql + +if you use the graphql ruby node interface, you can implement [object identification](https://graphql-ruby.org/schema/object_identification) + + +```ruby +# 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 +``` diff --git a/cool_id.gemspec b/cool_id.gemspec index 57e5417..cea01d8 100644 --- a/cool_id.gemspec +++ b/cool_id.gemspec @@ -11,6 +11,7 @@ Gem::Specification.new do |spec| spec.summary = "generates cool ids" spec.description = "generates primary keys using prefixed nanoids for ActiveRecord models" spec.homepage = "https://github.com/schpet/cool_id" + spec.license = "ISC" spec.required_ruby_version = ">= 3.0.0" # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" diff --git a/lib/cool_id/version.rb b/lib/cool_id/version.rb index 17ec29e..81c5fcc 100644 --- a/lib/cool_id/version.rb +++ b/lib/cool_id/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module CoolId - VERSION = "0.1.4" + VERSION = "0.1.5" end