Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Tạo Bookshelf app với Lotus Framework (Phần 3)
Tiếp tục phần 2 Bây giờ chúng ta đi vào giai đoạn
Tạo database cho app
Cũng như các framework khác việc điều khiển và xử lý các vấn đề liên quan đến database được thông qua Model. Lotus Framework cung cấp Lotus model để giúp thao tác với database.
Lotus Model có 2 khái niệm đó là entity và repository.
Entity là nơi để chúng ta define các thông tin về một đối tượng để tạo nên một table cho đối tượng mà bạn muốn quản lý
Hình trên là những thông tin mà một quyển sách cần thì những attribute cần cho Book entity của chúng ta là gì?
attributes :image_url, :link, :description, :publisher, :by, :isbn, :year, :pages, :languages, :file_size, :file_type, :created_at, :updated_at
-
Repository là nơi giúp ta quản lý tất cả các query đối với database
Một số method mà Lotus cung cấp sẵn cho chúng ta như persit, create, delete...
Tìm hiểu thêm ở link này để biết thêm chi tiết http://lotusrb.org/guides/models/repositories/
Okay, Khi các bạn đã hiểu về ý nghĩa của Entity và Repository rồi thì chúng ta sẽ bắt đầu vào chi tiết.
Config Database
Trước khi bắt tay vào làm mình sẽ hướng dẫn một tí về cách config
Add Gem sqlite3
group :development, :test do
gem 'sqlite3'
end
Config
# adapter type: :file_system, uri: ENV['BOOKSHELF_DATABASE_URL']
Sửa :file_system
thành :sql
Nó sẽ trở thành
adapter type: :sql, uri: ENV['BOOKSHELF_DATABASE_URL']
Update .env.development & .env.test
Tại .env.development
BOOKSHELF_DATABASE_URL="file:///db/bookshelf_development"
Sửa file:///db/bookshelf_development
thành sqlite://db/bookshelf_development.db
BOOKSHELF_DATABASE_URL="sqlite://db/bookshelf_development.db"
Tại .env.test
BOOKSHELF_DATABASE_URL="file:///db/bookshelf_test"
Edit file:///db/bookshelf_test
into sqlite://db/bookshelf_test.db
BOOKSHELF_DATABASE_URL="sqlite://db/bookshelf_test.db"
Rồi xong, bây giờ chúng ta chính thức bắt tay vào làm
Code thôi
Entity
Truy cập lib/bookshelf/entities create book.rb
và define các attributes cần thiết của một đối tượng. Ở trong demo này đối tượng của chúng ta là Book
require 'lotus/entity'
class Book
include Lotus::Entity
attributes :image_url, :link, :description, :publisher, :by, :isbn, :year,
:page, :languages, :file_size, :file_type, :created_at, :updated_at
end
Repository
Truy cập lib/bookshelf/repository create book.rb
Đây là nơi giúp chúng ta xử lý các query để tương tác với database
require 'lotus/repository'
class BookRepository
include Lotus::Repository
end
Mapping
Mapping đây là nơi bạn để bạn mapping datatype cho đối tượng Book. Để mapping bạn vào lib/config/mapping.rb
collection :books do
entity Book
repository BookRepository
attribute :id, Integer
attribute :image_url, String
attribute :link, String
attribute :description, String
attribute :publisher, String
attribute :by, String
attribute :isbn, String
attribute :year, DateTime
attribute :pages, Integer
attribute :languages, String
attribute :file_size, String
attribute :file_type, Integer
attribute :created_at, DateTime
attribute :updated_at, DateTime
end
Tạo Database
Sau khi hoàn tất việc config, tạo entity cho đối tượng Book ta tiếp tục với việc tạo database. Migrate trong Lotus giúp chúng ta làm những công việc này khá nhanh
lotus db create
Sau khi tạo database thành công, ta có generator một migration
Generator table Book
lotus g migration create_books
Tại bookshelf/db/migrations/[prefix]_create_books.rb
ta tạo một table book như sau:
collection :books do
entity Book
repository BookRepository
attribute :id, Integer
attribute :image_url, String
attribute :link, String
attribute :description, String
attribute :publisher, String
attribute :by, String
attribute :isbn, String
attribute :year, DateTime
attribute :pages, Integer
attribute :languages, String
attribute :file_size, String
attribute :file_type, Integer
attribute :created_at, DateTime
attribute :updated_at, DateTime
end
Run migration
lotus db migration
Đã tạo xong Database cho Book app
Tạo data seed
In file db/seed.db
BookRepository.clear
20.times do
book_attr = {
image_url: 'http://it-ebooks.org/img/books/wrox/professional_php6.jpg',
link: 'http://it-ebooks.org/book/wrox/professional_php6',
description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
publisher: 'Wrox',
by: 'Ed Lecky-Thompson, Steven D. Nowicki',
isbn: '9780470395097',
year: DateTime.new(2009),
pages: 744,
languages: 'English',
file_size: '10.2MB',
file_type: 1,
created_at: DateTime.new(2015,2,4,4,5,6,'+7'),
updated_at: DateTime.new(2015,2,4,4,5,6,'+7')
}
book = Book.new(book_attr)
BookRepository.persist(book)
end
Define rake task db
Bây giờ ta viết thêm một rake task vào file Rakefile
để giúp chúng ta load file db seed tạo data fake cho app
Tại file Rakefile
update như sau
require 'rake'
require 'rake/testtask'
require 'lotus/environment'
require Lotus::Environment.new.env_config
Rake::TestTask.new do |t|
t.pattern = 'spec/**/*_spec.rb'
t.libs << 'spec'
end
task default: :test
task spec: :test
namespace :db do
task :seed do
load 'db/seed.rb'
end
end
Kiểm tra data
Ở terminal truy cập vào folder project và gõ lệnh sau
lotus c # để truy cập vào Lotus console
Kiểm tra database
BookRepository.all
Các bạn đã thấy kết quả chưa?
Kết luận
Các bạn có thể xem code tại đây:https://github.com/mymai91/bookshelf/tree/create-seed-data-book
Hy vọng qua bài này có thể chia sẽ được với các bạn rõ hơn về Lotus






