Couch Potato 技术文档
2024-12-23 14:25:54作者:魏侃纯Zoe
1. 安装指南
1.1 安装 Couch Potato Gem
Couch Potato 是一个用于 CouchDB 的 Ruby 持久层库。你可以通过以下命令安装 Couch Potato:
gem install couch_potato
1.2 在 Ruby 应用程序中使用
在你的 Ruby 应用程序中,首先需要引入 Couch Potato:
require 'rubygems'
require 'couch_potato'
然后配置数据库名称:
CouchPotato::Config.database_name = 'name_of_the_db'
默认情况下,服务器 URL 为 http://localhost:5984/,你可以通过以下方式指定数据库主机和名称:
CouchPotato::Config.database_host = "http://example.com:5984"
CouchPotato::Config.database_name = "name_of_the_db"
或者使用带有认证的 URL:
CouchPotato::Config.database_name = "http://username:password@example.com:5984/name_of_the_db"
1.3 在 Rails 中使用
在 Rails 项目中,创建一个 config/couchdb.yml 文件:
default: &default
split_design_documents_per_view: true # 可选,默认是 false
digest_view_names: true # 可选,默认是 false
default_language: :erlang # 可选,默认是 javascript
database_host: "http://127.0.0.1:5984"
development:
<<: *default
database: development_db_name
test:
<<: *default
database: test_db_name
production:
<<: *default
database: <%= ENV['DB_NAME'] %>
additional_databases:
db1: db1_production
db2: https://db2.example.com/db
在 Gemfile 中添加以下依赖:
gem 'railties'
gem 'actionpack'
gem 'actionmailer'
gem 'activemodel'
gem "couch_potato"
gem 'tzinfo'
2. 项目使用说明
2.1 保存和加载对象
首先,定义一个类:
class User
end
为了使该类的实例持久化,需要包含 CouchPotato::Persistence 模块:
class User
include CouchPotato::Persistence
end
如果需要存储属性,必须声明它们:
class User
include CouchPotato::Persistence
property :name
end
属性可以指定类型:
class User
include CouchPotato::Persistence
property :address, :type => Address
end
2.2 数据库操作
所有数据库操作都封装在 CouchPotato::Database 类中。你可以保存对象:
user = User.new :name => 'joe'
CouchPotato.database.save_document user # 或者 save_document!
也可以加载对象:
CouchPotato.database.load_document "id_of_the_user_document" # => <#User 0x3075>
2.3 处理冲突
CouchDB 使用 MVCC 来检测写冲突。如果发生冲突,Couch Potato 会自动重新加载文档并重试保存:
CouchPotato.database.save_document user do |user|
user.name = 'joe'
end
2.4 缓存加载请求
你可以为数据库实例添加缓存,以缓存后续的 #load 调用:
db = CouchPotato.database
db.cache = {}
db.load '1'
db.load '1' # 从缓存中获取,而不是数据库
3. 项目 API 使用文档
3.1 属性访问
你可以通过正常的属性访问器访问声明的属性:
user.name # => 'joe'
user.name = {:first => ['joe', 'joey'], :last => 'doe', :middle => 'J'}
user._id # => "02097f33a0046123f1ebc0ebb6937269"
user._rev # => "2769180384"
user.created_at # => Fri Oct 24 19:05:54 +0200 2008
user.updated_at # => Fri Oct 24 19:05:54 +0200 2008
user.new? # => false
3.2 脏跟踪
CouchPotato 跟踪属性的脏状态,类似于 ActiveRecord:
user = User.create :name => 'joe'
user.name # => 'joe'
user.name_changed? # => false
user.name_was # => nil
你可以强制设置脏状态:
user.name = 'jane'
user.name_changed? # => true
user.name_not_changed
user.name_changed? # => false
CouchPotato.database.save_document user # 不会执行任何操作,因为没有属性是脏的
3.3 深度脏跟踪
你可以选择启用更高级的脏跟踪,以便更可靠地检查数组和嵌入文档属性的变化:
class User
include CouchPotato::DeepDirtyAttributes
end
4. 项目安装方式
4.1 通过 Gem 安装
gem install couch_potato
4.2 在 Ruby 应用程序中配置
require 'rubygems'
require 'couch_potato'
CouchPotato::Config.database_name = 'name_of_the_db'
4.3 在 Rails 中配置
创建 config/couchdb.yml 文件,并在 Gemfile 中添加依赖。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0138- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
MiniCPM-V-4.6这是 MiniCPM-V 系列有史以来效率与性能平衡最佳的模型。它以仅 1.3B 的参数规模,实现了性能与效率的双重突破,在全球同尺寸模型中登顶,全面超越了阿里 Qwen3.5-0.8B 与谷歌 Gemma4-E2B-it。Jinja00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00
MusicFreeDesktop插件化、定制化、无广告的免费音乐播放器TypeScript00
项目优选
收起
暂无描述
Dockerfile
726
4.66 K
Ascend Extension for PyTorch
Python
597
750
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
427
377
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
992
986
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
993
138
昇腾LLM分布式训练框架
Python
161
190
暂无简介
Dart
969
246
deepin linux kernel
C
29
16
Oohos_react_native
React Native鸿蒙化仓库
C++
345
393
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.65 K
970