{"id":16925455,"url":"https://github.com/alejandrodevs/coushion","last_synced_at":"2025-03-21T01:28:50.118Z","repository":{"id":67914172,"uuid":"468162916","full_name":"alejandrodevs/coushion","owner":"alejandrodevs","description":"This is a simple and powerful ODM (Object-Document Mapper) for CouchDB in Ruby.","archived":false,"fork":false,"pushed_at":"2022-03-26T04:48:24.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T21:56:22.982Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alejandrodevs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-10T02:17:08.000Z","updated_at":"2022-03-10T04:43:15.000Z","dependencies_parsed_at":"2023-09-11T23:04:13.662Z","dependency_job_id":null,"html_url":"https://github.com/alejandrodevs/coushion","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Fcoushion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Fcoushion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Fcoushion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Fcoushion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alejandrodevs","download_url":"https://codeload.github.com/alejandrodevs/coushion/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244720528,"owners_count":20498809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T20:10:40.641Z","updated_at":"2025-03-21T01:28:50.099Z","avatar_url":"https://github.com/alejandrodevs.png","language":"Ruby","readme":"# Coushion\n\nThis is a simple and powerful ODM (Object-Document Mapper) for [CouchDB](https://couchdb.apache.org) in Ruby.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'coushion'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install coushion\n\n## Usage\n\n### Defining Documents\n\nJust include `Coushion::Document` in your class and start defining your\ndocument's attributes.\n\n```ruby\nclass Person\n  include Coushion::Document\n\n  attribute :first_name, type: Types::String\n  attribute :last_name, type: Types::String\n  attribute :occupation, type: Types::String\n  attribute :locale, type: Types::String, default: \"en\"\n  attribute :preferences, type: Types::Hash, default: {}\nend\n```\n\nCoushion uses `Sphene` for attributes definition. Please check [Sphene Documentation](https://github.com/alejandrodevs/sphene) for more information about its usage and the [types](https://github.com/alejandrodevs/sphene/blob/master/lib/sphene/types.rb) available.\n\n### Validations\n\nCoushion includes [ActiveModel::Validations](https://api.rubyonrails.org/classes/ActiveModel/Validations.html) to supply basic validation. Contexts for `create` and `update` actions are provided.\n\n```ruby\nclass Movie\n  include Coushion::Document\n\n  attribute :name, type: Types::String\n  attribute :genre, type: Types::String\n\n  validates :name, presence: true\n  validates :name, length: { maximum: 50 }, on: :create\n  validates :genre, inclusion: { in: [\"drama\", \"action\"] }\nend\n```\n\n### Callbacks\n\nCoushion includes [ActiveModel::Callbacks](https://api.rubyonrails.org/classes/ActiveModel/Callbacks.html) to supply callbacks. Callbacks `before` and `after` are provided for `save`, `create` and `update` actions.\n\n```ruby\nclass User\n  include Coushion::Document\n\n  attribute :email, type: Types::String\n  attribute :username, type: Types::String\n\n  after_create :send_welcome_email\n\n  private\n\n  def send_welcome_email\n    UserMailer.welcome(id).deliver_later\n  end\nend\n```\n\n### Persistence\n\n| Operation | Description |\n| ------------- | ------------- |\n| `Document.create` | Inserts a document into the database. Doesn't raise exceptions on validation errors but it still raises server errors. Returns the document no matter if it was persisted or not. |\n| `Document.create!` | Inserts a document into the database, raising an error if a validation or server error occurs. Returns the document if there was not any error. |\n| `Document#save` | Saves the document to the database, or creates it if new. Doesn't raise exceptions on validation errors but it still raises server errors. Will return true if the document was saved, false if not. |\n| `Document#save!` | Saves the document to the database, or insert it if new. Raises an exception if validations failed or there was a server error. Returns true if the document was saved. |\n| `Document#update` | Updates the document in the database. Doesn't raise exceptions on validation errors but it still raises server errors. Returns true if the document was updated, false if not. |\n| `Document#update!` | Updates the document in the database and raise an error if validations failed or there was a server error. Returns true if the document was updated. |\n| `Document#touch` | Updates the document's updated_at timestamp, optionally with one extra provided time field. This operation skips validations and callbacks. |\n| `Document#delete` | Deletes the document from the database without running callbacks. It still raises server errors. Returns true if the document was deleted, false if not. |\n| `Document#destroy` | Deletes the document from the database while running destroy callbacks. It still raises server errors. Returns true if the document was deleted, false if not. |\n| `Document#new_record?` | Returns true if the model instance has not yet been saved to the database. Opposite of `persisted?` |\n| `Document#persisted?` | Returns true if the model instance has been saved to the database. Opposite of `new_record?` |\n\n##### Examples\n\n```ruby\nclass Post\n  include Coushion::Document\n\n  attribute :title, type: Types::String\n\n  validates :title, presence: true\n  validates :title, length: { maximum: 50 }\nend\n\nPost.create(title: \"How to use CouchDB\")\n# =\u003e #\u003cPost id: \"1601a...\", rev: \"1-b91b...\", title: \"How to use CouchDB\" ...\u003e\n\nPost.create!(title: nil)\n# =\u003e raises Coushion::DocumentInvalid\n\npost = Post.new(title: \"My Second Post\")\npost.save\n# =\u003e true\n\npost.title = nil\npost.save!\n# =\u003e raises Coushion::DocumentInvalid\n\npost = Post.find(\"16011a5704ffe78e6de2afa4b3001d10\")\npost.update(title: nil)\n# =\u003e false\n\npost.update!(title: \"New post's title\")\n# =\u003e true\n\npost = Post.find(\"16011a5704ffe78e6de2afa4b3001d10\")\npost.destroy\n# =\u003e true\n```\n\n### Querying\n\n| Operation | Description |\n| ------------- | ------------- |\n| `Document.find` | Finds a document by a specific id. If the document cannot be found for the requested id, then `Coushion::DocumentNotFound` will be raised. |\n| `Document.where` | Queries documents using Mango. Mango is a declarative JSON querying language for CouchDB databases. |\n\n##### Examples\n\n```ruby\nProduct.find(\"16011a5704ffe78e6de2afa4b3001d10\")\n# =\u003e #\u003cProduct id: \"16011a...\" ...\u003e\n\nUser.where(occupation: \"Software Engineer\")\n# =\u003e [#\u003cUser id: \"\" ...\u003e, #\u003cUser id: \"\" ...\u003e, ...]\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/alejandrodevs/coushion. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alejandrodevs/coushion/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Coushion project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alejandrodevs/coushion/blob/master/CODE_OF_CONDUCT.md).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandrodevs%2Fcoushion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falejandrodevs%2Fcoushion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandrodevs%2Fcoushion/lists"}