Model with Zongel
Last updated: 4 - 5 - 2019

You can attach a "Zinky made" model to your module by setting 'useZongel' to true.
This feature is only available for projects using MongoDB.
Zongel is a wrapper for 'collection' function of MongoDB instance.
It wraps insertOne, insertMany, updateOne and updateMany to validate schema before performing DB operations.
It wraps find to return array
It uses AJV for schema validation, and adds a special field called "private", (eg: password).

Example

Here is how to use it, module index.js:

 const Zinko = require('zinko');

class Crud extends Zinko {
 
  get useZongel() { return true; }
 
  async POST_root(req, res) {
    await this.model.insertOne({ name: 'rruk', age: 'r' })
    return "ok"
  }
 
  async GET_root(req, res) {
    let j = await this.model.findOne({ name: 'rruk' });
    return j
  }
 
  async PUT_root(req, res) {
    let j = await this.model.updateOne({ name: 'rruk' }, { $set: { age: 1 } });
    console.log(j.ops)
    return 'ok'
  }

}
 
module.exports = Crud;

And model.js in the same folder:

 const Zongel = require("zongel");
 
class Model extends Zongel {
 
  get collectionName() { return "crud" }
 
  get schema() {
    return {
      properties: {
        name: { type: "string" },
        age: { type: "integer" }
      },
      required: ["name"],
      private: ["age"]
    }
  }
 
}
 
module.exports = Model;