-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split logic for related model, new command added
- Loading branch information
1 parent
0a1010b
commit 6988264
Showing
23 changed files
with
188 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ require('babel-register')({ | |
] | ||
}); | ||
require('./src'); | ||
|
||
// require('./lib'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Command from 'cmd-line/lib/Command'; | ||
import inflector from '../support/inflector'; | ||
import persistmodel from '../functions/persistmodel'; | ||
|
||
let linkmodes = { | ||
'hasmany': 'hasmany', | ||
'belongsto': 'belongsto' | ||
}; | ||
let emberrel = { | ||
'hasmany': 'hasMany', | ||
'belongsto': 'belongsTo' | ||
}; | ||
|
||
export default (new Command(__filename, 'makes a relation betwen two models\n linkmode: hasMany|belongsTo')) | ||
.Args('sourcemodel', 'hasMany|hasOne', 'targetmodel') | ||
.Options([ | ||
['-re', '--relation-property', 'Selects the relation property'], | ||
['-fe', '--foreing-key', 'Selects the foreing key to use'], | ||
['-e', '--ember <app>', 'Generates the model also for the app especified.'], | ||
['-f', '--force', 'Deletes the model if exists.'], | ||
['-r', '--rest', 'Makes the model REST enabled.'] | ||
]) | ||
.Action(async function (sourcemodel, linkmode, targetmodel, options) { | ||
let mode = linkmodes[linkmode.toLowerCase()]; | ||
if (!mode) { | ||
console.log(`Invalid mode selected: ${linkmode.red}\nAvaliable options ${'hasMany, belongsTo'.cyan}.`); | ||
return 501; | ||
} | ||
let relationProperty = options.relationProperty || mode === linkmodes.hasmany ? inflector.pluralize(targetmodel) : targetmodel; | ||
let foreignKey = options.foreignKey || inflector.singularize(targetmodel) + 'Id'; | ||
let model = scfg.database[sourcemodel]; | ||
if (!model || !scfg.database[targetmodel]) { | ||
if (!model) { | ||
console.log(`Source model (${sourcemodel}) does not exists.`); | ||
} else { | ||
console.log(`Target model (${targetmodel}) does not exists.`); | ||
} | ||
return 404; | ||
} | ||
model.relation(relationProperty, targetmodel, emberrel[mode], foreignKey); | ||
return await persistmodel(model, sourcemodel, options); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.