[Day14] Boxenn 实作 Source Wrapper

Source Wrapper

source wrapper 的职责很简单,他负责将外部资源的介面转换给 Boxenn::Repository 使用。
Boxenn::Repository 总共用了三个 method :

  • find_by(hash)
    透过 primary key 找寻资料,input 为 entity 上设定的 primary key 的 hash。须回传物件,回传的物件会再传给 Boxenn::Repositories::Factory 建成 entity。
  • save(hash, hash)
    新建或更新资料库的资料,input 为 entity 上设定的 primary key 的 hash 及对应 schema column 的 hash
  • destroy(hash)
    删除资料库里的资料,input 为 entity 上设定的 primary key 的 hash

范例

范例的外部资源是使用 ActiveRecord 的 Model。

class Order < Boxenn::Repositories::SourceWrapper
  param :source, default: -> { Models::Order } # 这边的 order 是 active record 的 model

	def find_by(primary_keys)
		source.find_by(primary_keys)
	end

  def save(primary_keys, attributes)
    record = source.find_or_initialize_by(primary_keys)
    record.assign_attributes(attributes)
    record.save!
  end

	def destroy(primary_keys)
    record = source.find_by(primary_keys)
    record.destroy!
	end
end

下一篇我们就可以简单地使用 Boxenn::Repository 来操作资料库,另外会介绍 Boxenn::Repositories::Query,用来收敛复杂的 sql query。


<<:  Day29 Swagger

>>:  [想试试看JavaScript ] 阵列一些操作阵列好用的方法 some() every()

卡夫卡的藏书阁【Book23】- Kafka - KafkaJS 监控状态事件

“I miss you deeply, unfathomably, senselessly, te...

[Day 27] LeetCode - 7 Reverse Integer

本篇同步发布於Blog:[解题] LeetCode - 7 Reverse Integer 平台: ...

Day-7 Divide-and-Conquer-2 : 求解递回式

如何求解递回式 目前主要有三种方法来求解递回式(至今没有任何一个好的演算法可以有效地解决递回式) 代...

[Day03] 培养人脉,从正向思考开始

寻找舞台,除了写一份让人惊艳的履历,有时候更具临门一脚威力的,就是有力的推荐人。在徵才时,除了技术能...

EP 09 - [TDD] Message 加密及解密 (1/2)

Youtube 频道:https://www.youtube.com/c/kaochenlong ...