0.0.7 • Published 4 years ago
yodb v0.0.7
YoDB
YoDB là một database được viết ra trên nền Javascript, nhẹ và nhanh để phát triển một ứng dụng web quy mô nhỏ hoặc test thử.
Installation
Using npm:
$ npm i -g npm
$ npm i --save yodb
In Node.js:
// Load the full build.
const { DataType, YoQuery } = require("yodb");
Quick start
Hướng dẫn sau đây sẽ bạn cấu hình sử dụng YoDB một cách nhanh nhất.
your_project/
├── data/
│ ├── book.json
│ └── ....
├── model/
│ ├── book.js
│ └── ....
├── yoconfig.json
├── index.js
└── ....
Tạo file config:
Tạo file yoconfig.json
.
{
"data_file_name": "tên file lưu data"
}
Note: Sau khi hoàn thành file yoconfig.json
.
Tạo model:
const { DataType } = require("yodb");
model = {
table: {
name: "books"
},
field: {
title: DataType.String,
author: DataType.String,
quantity: DataType.Number
}
};
module.exports = model;
Note: Chỉ có 2 kiểu Number
, String
.
Thực hiện Query
YoDB gồm có 4 phương thức tương tác với database: GET
, PUT
, POST
, DELETE
GET
const booksModel = require("./model/books");
const { YoQuery } = require("yodb");
// SELECT * FROM books
new YoQuery(BookModel)
.get()
.exec()
.then(res => res)
.catch(console.log);
// SELECT title, quantity FROM books
new YoQuery(BookModel, ["title", "quantity"])
.get()
.exec()
.then(res => res)
.catch(console.log);
// SELECT * FROM books WHERE id = "acb12"
new YoQuery(BookModel ,_, { id: "acb12"})
.get()
.exec()
.then(res => res)
.catch(console.log);
// SELECT * FROM books WHERE id = "acb12" AND title = "Harry Potter and the Chamber of Secrets"
new YoQuery(BookModel ,_, { and: [{ id: "acb12" }, { title: "Harry Potter and the Chamber of Secrets" }]})
.get()
.exec()
.then(res => res)
.catch(console.log);
// SELECT * FROM books WHERE id = "1" OR title = "Harry Potter and the Chamber of Secrets"
new YoQuery(BookModel ,_, { or: [{ id: "acb12" }, { title: "Harry Potter and the Chamber of Secrets" }]})
.get()
.exec()
.then(res => res)
.catch(console.log);
// SELECT * FROM books WHERE quantity > 1
new YoQuery(BookModel ,_, { quantity: { greate: 1 } })
.get()
.exec()
.then(res => res)
.catch(console.log);
// { greate: > , less: < , notEqual: !== }
// SELECT * FROM books WHERE quantity > 1 LIMIT 10
new YoQuery(BookModel ,_, { quantity: { greate: 1 } })
.get().
.limit(10)
.exec()
.then(res => res)
.catch(console.log);
// SELECT * FROM books LIMIT 10, 10
new YoQuery(BookModel)
.get().
.skip(10)
.limit(10)
.exec()
.then(res => res)
.catch(console.log);
POST
const booksModel = require("./model/books");
// INSERT
new YoQuery(BookModel, ["id", "title", "author", "quantity"])
.post({ title, author, quantity })
.exec()
.then(res => res)
.catch(console.log);
id
sẽ được tạo tự động["id", "title", "author", "quantity"]
sẽ chỉ định dữ liệu trả về sau khiINSERT
thành công.
PUT
const booksModel = require("./model/books");
// UPDATE
new YoQuery(BookModel, ["id", "title"], { id:"asd23" })
.put({ quantity: 100 })
.exec()
.then(res => res)
.catch(console.log);
Note: Nếu bạn không truyền một { id: "asd23" }
thì toàn bộ dữ liệu sẽ được cập nhật theo { quantity: 100 }
bạn đã truyền.
DELETE
const booksModel = require("./model/books");
// DELETE
new YoQuery(BookModel, ["id", "title"], { id:"asd23" })
.delete()
.exec()
.then(res => res)
.catch(console.log);
Note: Nếu bạn không truyền một { id: "asd23" }
thì toàn bộ dữ liệu sẽ bị xóa.