Lotus 提供了一个 repository 仓库,感觉还是比较容易理解和使用的,看下面代码,我用 memory 完全没问题。
我觉得这个可以搭配 rails 来用,好处是:
我试验 mysql 没成功,可有知道的码友么?
解决了,改为 mysql2://
require 'lotus/model'
#model and repository
class User
include Lotus::Entity
attributes :name, :age
end
class UserRepository
include Lotus::Repository
end
#config
Lotus::Model.configure do
adapter type: :memory, uri: 'memory://bookshelf_development'
# adapter type: :file_system, uri: "file:///db/bookshelf_development"
# adapter type: :sql, uri: 'mysql://localhost/bookshelf_development?user=lotus&password=123456'
mapping do
collection :users do
entity User
repository UserRepository
attribute :id, Integer
attribute :name, String
attribute :age, Integer
end
end
end
Lotus::Model.load!
#play
user = User.new(name: 'Luca', age: 32)
p user
user1 = UserRepository.create(user)
user2 = UserRepository.create(user)
user = UserRepository.find(2)
p user == user2 # => true
user2.name = 'John'
user2.age = '3'
UserRepository.update(user2)
p user
users = UserRepository.all
p users