Skip to content

Commit c926a61

Browse files
authored
Merge pull request #6 from juampi92/dev
2.0.0 merge
2 parents 7912366 + e5b2ef7 commit c926a61

File tree

11 files changed

+557
-121
lines changed

11 files changed

+557
-121
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ This package brings you a MongooseProvider that connects to the database, a Mong
55

66
<img src="http://res.cloudinary.com/adonisjs/image/upload/q_100/v1497112678/adonis-purple_pzkmzt.svg" width="200px" align="right" hspace="30px" vspace="50px">
77

8-
## What's in the box?
8+
## Features
99

1010
1. Support for Mongoose model creation.
11-
2. Support for a Mongoose serializer.
12-
3. Adonis command for creating mongoose models
11+
2. Strong BaseModel that's Hook compatible, indexable and easy es6 inheritance
12+
3. Support for a Mongoose serializer.
13+
4. Adonis command for creating mongoose models
1314

1415
## Installation
1516
You can install the package from npm.

commands/MakeModel.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ class MakeMongoose extends Command {
4545
* @return {String}
4646
*/
4747
static get signature () {
48-
return 'make:mongoose {name : Name of the Mongoose Model}'
48+
return `make:mongoose
49+
{ name? : Name of the Mongoose Model }
50+
{ --simple : Ignore prompts when creating the model }
51+
{ --raw : Creates a vanilla mongoose model }
52+
`
4953
}
5054

5155
/* istanbul ignore next */
@@ -69,11 +73,18 @@ class MakeMongoose extends Command {
6973
*
7074
* @return {void}
7175
*/
72-
async handle ({ name }) {
76+
async handle ({ name = null }, { simple = false, raw = false }) {
77+
78+
if (!name) {
79+
name = await this.ask('Enter model name')
80+
}
81+
82+
const mustachePath = raw ? 'raw_model' : 'model'
83+
7384
/**
7485
* Reading template as a string form the mustache file
7586
*/
76-
const template = await this.readFile(path.join(__dirname, './templates/model.mustache'), 'utf8')
87+
const template = await this.readFile(path.join(__dirname, `./templates/${mustachePath}.mustache`), 'utf8')
7788

7889
/**
7990
* Directory paths
@@ -89,12 +100,29 @@ class MakeMongoose extends Command {
89100
*/
90101
name = name.split('/').pop()
91102

103+
let options = {}
104+
105+
if (!simple && !raw) {
106+
options.timestamps = await this
107+
.confirm('Include timestamps?')
108+
109+
options.boot = await this
110+
.confirm('Include boot method?')
111+
112+
}
113+
114+
const templ_options = {
115+
name,
116+
exclude_timestamps: !simple ? !options.timestamps : false,
117+
include_boot: !simple ? options.boot : true
118+
}
119+
92120
/**
93121
* If command is not executed via command line, then return
94122
* the response
95123
*/
96124
if (!this.viaAce) {
97-
return this.generateFile(validatorPath, template, { name })
125+
return this.generateFile(validatorPath, template, templ_options)
98126
}
99127

100128
/* istanbul ignore next */
@@ -103,7 +131,7 @@ class MakeMongoose extends Command {
103131
* to the end user.
104132
*/
105133
try {
106-
await this.generateFile(validatorPath, template, { name })
134+
await this.generateFile(validatorPath, template, templ_options)
107135
this.completed('create', relativePath)
108136
} catch (error) {
109137
this.error(`${relativePath} validator already exists`)

commands/templates/model.mustache

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
'use strict'
22

3-
const mongoose = use('Mongoose')
4-
const { Schema } = mongoose
5-
const Model = use('Model')
3+
const BaseModel = use('Model')
64

75
/**
8-
* {{ name }}'s db Schema
6+
* @class {{ name }}
97
*/
10-
const {{ name }}Schema = new Schema({
11-
12-
})
13-
14-
/**
15-
* {{ name }}'s instance and static methods
16-
* @class
17-
*/
18-
class {{ name }} extends Model {
8+
class {{ name }} extends BaseModel {
9+
{{#exclude_timestamps}}
10+
/**
11+
* Exclude created_at and updated_at from the model
12+
*/
13+
static get timestamps () {
14+
return false
15+
}
16+
{{/exclude_timestamps}}
17+
{{#include_boot}}
18+
static boot () {
19+
// Hooks:
20+
// this.addHook('preSave', () => {})
21+
// this.addHook('preSave', '{{ name }}Hook.method')
22+
// Indexes:
23+
// this.index({}, {background: true})
24+
}
25+
{{/include_boot}}
26+
/**
27+
* {{ name }}'s schema
28+
*/
29+
static get schema () {
30+
return {
1931
32+
}
33+
}
2034
}
2135

22-
{{ name }}Schema.loadClass({{ name }})
23-
24-
module.exports = mongoose.model('{{ name }}', {{ name }}Schema)
36+
module.exports = {{ name }}.buildModel('{{ name }}')
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const mongoose = use('Mongoose')
4+
const { Schema } = mongoose
5+
6+
/**
7+
* {{ name }}'s db Schema
8+
*/
9+
const {{ name }}Schema = new Schema({
10+
11+
})
12+
13+
/**
14+
* {{ name }}'s instance and static methods
15+
* @class
16+
*/
17+
class {{ name }} {
18+
19+
}
20+
21+
{{ name }}Schema.loadClass({{ name }})
22+
23+
module.exports = mongoose.model('{{ name }}', {{ name }}Schema)

0 commit comments

Comments
 (0)