ndelvalle/generator-api

Attribute 'Schema' in facade.js should be type of 'Model" in mongoose schema

ylcoder opened this issue · 7 comments

This is just cosmetic. I found it a bit confusing when I first read the generated code. Would it be better if we can rename that to be 'Model'? Thanks.

forgot to mention the exported thingy in schema is type 'Model' as well.

Hi @ylcoder thanks for generating the issue. Would you help me and create a PR for this? 🙂

I don't have access to push my changes in a branch. Also I have a hard time to test it locally. Every

time it is using my global install even after I uninstalled it.

See attached for two files I changed. If you think ok you can merge the changes.

// 1. C:\Users\yl\code\generator-api\generators\app\templates\lib\facade.js
const mongoose = require('mongoose');

Model Facade {
  constructor(schema) {
    this.model = mongoose.model('<%= model.pascalName %>', schema);
  }

  create(body) {
    const model = new this.model(body);
    return model.save();
  }

  find(...args) {
    return this.model
      .find(...args)
      .exec();
  }

  findOne(...args) {
    return this.model
      .findOne(...args)
      .exec();
  }

  findById(...args) {
    return this.model
      .findById(...args)
      .exec();
  }

  update(...args) {
    return this.model
      .update(...args)
      .exec();
  }

  remove(...args) {
    return this.model
      .remove(...args)
      .exec();
  }
}

module.exports = Facade;
// 2. C:\Users\yl\code\generator-api\generators\app\templates\model\schema.js

const Schema = mongoose.Schema;


const <%= model.camelName %>Schema = new Schema({
  title: { type: String, required: true },
  body: { type: String }
});


module.exports =  <%= model.camelName %>Schema;

Actually I just realized I need to change the constructor of Facade to take a model name instead in the lib so the name can be passed in in actual model files. Here are the changes in 3 template files:

// 1.generator-api\generators\app\templates\lib\facade.js
const mongoose = require('mongoose');

Model Facade {
  constructor(name, schema) {
    this.model = mongoose.model(name, schema);
  }

  create(body) {
    const model = new this.model(body);
    return model.save();
  }

  find(...args) {
    return this.model
      .find(...args)
      .exec();
  }

  findOne(...args) {
    return this.model
      .findOne(...args)
      .exec();
  }

  findById(...args) {
    return this.model
      .findById(...args)
      .exec();
  }

  update(...args) {
    return this.model
      .update(...args)
      .exec();
  }

  remove(...args) {
    return this.model
      .remove(...args)
      .exec();
  }
}

module.exports = Facade;
// 2. generator-api\generators\app\templates\model\facade.js
const Facade = require('../../lib/facade');
const <%= model.camelName %>Schema = require('./schema');

class <%= model.pascalName %>Facade extends Facade {}

module.exports = new <%= model.pascalName %>Facade('<%= model.pascalName %>', <%= model.camelName %>Schema);

3. generator-api\generators\app\templates\model\schema.js

const Schema = mongoose.Schema;


const <%= model.camelName %>Schema = new Schema({
  title: { type: String, required: true },
  body: { type: String }
});


module.exports =  <%= model.camelName %>Schema;

Thanks @ylcoder !!!

You could make a fork, work on the master branch there and then open the PR to this repository. At least with these changes that you show here, then we can test and merge it. Since you did work is better if you get the credit 😄

Btw I will update the docs on how to contribute.

I updated your comments just to give them styles, hope you don't mind.

Thanks Gill. I created a pull request.

Cool! Thanks!