Original link: https://www.ixiqin.com/2022/08/02/json-database-warehouse-a-simple-and-easy-to-use/
Under Hexo’s Github organization, there is an obscure, but very useful repository – warehouse.
Warehouse is a JSON database. Based on JSON, various SQL-like queries are implemented, which can help us to query based on a JSON file. The warehouse helped a lot during the static generation of Hexo.
In official words, warehouse is A JSON database with Models, Schemas, and a flexible querying interface.
In actual use, warehouse is indeed as convenient as he said (although some methods are not, but it still does not affect his convenience).
Example
For example, the following code defines a Post model and the corresponding table. And realized inserting a new data in this table.
var Database = require('warehouse');var db = new Database();var Post = db.model('posts', { title: String, created: {type: Date, default: Date.now}});Post.insert({ title: 'Hello world'}).then(function(post){ console.log(post);});
If you need to save these data as a separate file, you only need to modify the initialization parameters and execute the save method to export JSON to the specified file
var db = new Database({ path: "./test.json", // 将数据存储在test.json 当中});db.save();
Similarly, if the data has been constructed, it is only necessary to execute the load method to load the data.
var db = new Database({ path: "./test.json", // 将数据存储在test.json 当中});db.load();
Scenes
If you want a better way of operating JSON in memory, then warehouse is a good choice. You don’t need to install a separate database, you can implement a query method similar to a database, and the experience is still very good.
If you want to know more, you can check
- Documentation for warehouse: https://hexojs.github.io/warehouse/index.html
- Model definitions in hexo: https://github.com/hexojs/hexo/tree/master/lib/models
- Test case for warehouse: https://github.com/hexojs/warehouse/tree/master/test/scripts
This article is reprinted from: https://www.ixiqin.com/2022/08/02/json-database-warehouse-a-simple-and-easy-to-use/
This site is for inclusion only, and the copyright belongs to the original author.