Git Product home page Git Product logo

warehouse's Introduction

Warehouse

Tester Pages Deployer NPM version Coverage Status

A JSON database with Models, Schemas, and a flexible querying interface. It powers the wildly successful static site generator Hexo.

Installation

$ npm install warehouse

3.0 BREAKING CHANGE

In warehouse@3, the constructor has been changed from function declaration to class declaration or definition by class expression. Derived classes of classes defined by class declarations and class expressions must also be defined in class declaration, class expression. Anyone who created their own SchemaType will need to change.

const SchemaType = require('warehouse/schematype');

class MySchemaType extends SchemaType {
  constructor(name, options = {}) {
    super(name, Object.assign({ foo: 'foo' }, options));
  }
}

It changes to a class declaration or a class expression, but it does not need to deal with other than the definition of the constructor.

// It work!

MySchemaType.prototype.cast = function (value, data) {
  let result = SchemaType.prototype.cast.call(this, value, data);
  return result ? result : '';
}

Usage

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);
});

Test

$ npm test

warehouse's People

Contributors

curbengh avatar d-sketon avatar dailyrandomphoto avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar heroicyang avatar jlhwung avatar naokiy avatar noahdragon avatar segayuu avatar stevenjoezhang avatar sukkaw avatar tcrowe avatar tomap avatar tommy351 avatar uiolee avatar yoshinorin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

warehouse's Issues

Why use stream for warehouse#save()?

The output by stream is obviously slower than fs.writeFile().
Nevertheless, stream is powerful because memory control by highWaterMark can prevent memory from being consumed too much at once.
However, this can not be realized with just Writable alone (at least Readable or Generator is almost required), you can ignore it, but in that case no memory control will be done and the stream loses its maximum advantage.
Although it is possible to use JSONStream.stringnify(), it is said that an issue has been posted, "It is 100 times slower than JSON.sringnify()".

Categories as a tree

Hello @tommy351,

I'm trying to add the generation of an index for the categories to hexo-generator-category. To do that, I want to pass to render and object representing the categories as a tree but locals.categories only gives us a flatten projection.

I've managed to obtain a tree like that : https://github.com/borisschapira/hexo-generator-category/blob/master/lib/generator.js#L36

but it's not optimal, because I'm manipulating arrays and arrays are not ideal for templating. Is there a way to use warehouse to obtain an unflatten representation of the categories from the flat one ?

How to save changes to the disk immediately after they happen?

Hey, I really like warehouse, it's been working great so far!

Is there a way (maybe hidden feature?) how to save the database to a file as soon as some changes occur? This might be very useful because now the database is just held in the memory and if the software crashes (before standard / scheduled save happens) the changes are lost.

I actually came up with a solution (methods for saving / loading from file are provided and all models are event emitters so I can easily listen for changes) but it doesn't seem very elegant. Mainly because I have to be careful about concurrent writes to the file to avoid file corruption.

Potential mistake prevents model data modification in pre-save hooks

Please consider this piece of model.js source:

warehouse/lib/model.js

Lines 161 to 164 in 067dd53

return execHooks(schema, 'pre', 'save', data).then(data => {
// Insert data
this.data[id] = result;
this.length++;

It can be seen, that pre save hook is received data variable and receives (potentially modified) data in then() (at the line 161), but ignores this information and inserts previously defined result variable instead at the line 163.

It effectively results into lack of ability to perform any data modifications in pre save hooks.

Same approach is used for updating and replacement of the model data.

Since logic of this piece of code remains untouched from the very beginning - I'm not sure if it is intentional (but rather confusing) decision or a bug that hides for a long time.

Why does hexo use this database implementation?

Hello, this might sound rather naive, but why was a custom db made for hexo? (Maybe it's not made for hexo, as it didn't used to be a part of hexojs)

I don't understand why a flat file format was chosen, as opposed to say, an already existing database like mongo or other text storage databases that get more attention. I can see that installations are easier this way especially for users, data saving/retrieval are as simple as the fs api, and perhaps disk reads might be faster than talking to a mongo daemon (I don't know though, I haven't looked at any benchmark data). I can speculate, but I won't know for sure why this decision was made.

The part where we save all the post's content into the db.json seems redundant when file itself holds the data already, and we can just load the content from the file, rather than read a copy of the content from another text file (db.json). Wouldn't we be able to save a lot of space this way?

Thanks for reading and thanks for making hexo!

API link broken

Are the docs for warehouse live somewhere? Is it still in use?

Thank you

pre-save hooks doesn't make sense when use db.load()

//db.js
import Database from "warehouse";
import path from "path";
import fs from "fs";


const dbpath = path.resolve("./db.json");

const db = new Database({ path: dbpath });

await db.load();

console.log("db loaded...");

export default db;
//category.js
import Database from "warehouse";
import db from "./db.js";

const { Schema } = Database;

const schema = new Schema({
  name: { type: String, required: true },
});

schema.pre("save", async (data) => {
  const { name, parent } = data;
  if (!name) return;
  const categories = db.model("categories");
  const cat = await categories.findOne({ name });
  if (cat) {
    throw new Error(`Category \`${name}\` has already existed!`);
  }
});

const Category = db.model("categories", schema);

export default Category;
// cli.js
import Category from "./category.js";
import db from "./db.js";

const cat = await Category.save({ name: "Javascript" });

console.log("saved:", cat);

db.save();
//db.json repeat so much!!!
{
  "meta": { "version": 0, "warehouse": "4.0.0" },
  "models": {
    "posts": [],
    "categories": [
      { "name": "Javascript", "_id": "ckwkujczh0000587nh75rhpcl" },
      { "name": "Javascript", "_id": "ckwkuqnr10000io7nb3ocey48" },
      { "name": "Javascript", "_id": "ckwkuqqpm0000u07n4gxg94r1" },
      { "name": "Javascript", "_id": "ckwkus0pb0000887nal5m0mxh" },
      { "name": "Javascript", "_id": "ckwkus2op0000dw7n5twndlvm" },
      { "name": "Javascript", "_id": "ckwkus3ko0000x07n36056e7i" },
      { "name": "Javascript", "_id": "ckwkus48c0000w47n962sfmfo" },
      { "name": "Javascript", "_id": "ckwkusz930000bg7n2rsm792b" }
    ]
  }
}

the basics

at least show examples of the very basic CRUD.

People have to climb mountain to find how to Read all.

Your documentation is 87% complete to those who are new to your database.

关于文件重复加载的问题

你好,我尝试在hexo generate之前对文件进行了一些额外的修改:去掉文件日期标题等描述/自定义的归档目录等,并在文件被修改的时候,同步至hexo,此时,如果hexo处于watch状态,第一次运行hexo server的时候,必定出错,错误一定为第一个文件已经存在。

重新执行一次hexo s可以正常的继续使用hexo,或者索性把代码中读取本地缓存的部分干掉也能达到类似的效果,然而上面的方式都是治标不治本的,请问是否考虑处理掉这个问题呢?

https://github.com/hexojs/hexo/blob/master/lib/plugins/processor/asset.js#L78

setting custom path is not working

var DB_PATH = path.join(path.dirname(__dirname), 'server/data_db', 'db.json');
var  DATA = new JSONdb({path: DB.DB_PATH, version: 1});

this is the output of the db_path /mnt/c/webserver/server/data_db/db.json

[WIP] Replace typescript

One of the reasons why warehouse is hardly maintained is that it doesn't understand the type.
However, the warehouse package is too complex to be typed with DefinitelyTyped, and it is quite possible to make typing errors.

So, experimentally change to typescript using the wip-replace-typescript branch.

Is there any way to convert it to a plain JavaScript Object?

I‘d like to show related posts which have same tags, so I use page.tags and site.posts in a template.But I am confused with the warehouse,a library for Hexo to handle JSON data. It looks like GraphQL but I know it does not.

图片

Is there any way to convert it to a plain JavaScript Object? Thanks for your help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.