Skip to content

Commit e305fbd

Browse files
committed
List or Delete CRUD
1 parent dc29fd4 commit e305fbd

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22
This change log is started in 0.0.9;
33

4+
## 0.0.12 (2016-04-21)
5+
- List CRUDS with ``` basel-crud -l ```
6+
- Delete CRUD by id with ``` base-crud -d <id> ```
7+
8+
## 0.0.11 (2016-04-21)
9+
- Create table when create CRUD
10+
411
## 0.0.10 (2016-04-20)
512
- Change creating tables
613
- Create utils module

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ basel-crud users --table USERS
6969
-p, --pk <primary> Primary key of new table
7070
-f, --references <references> Refences of new table. Ex: "profile:profiles.id"
7171
-i, --incremental <incremental> incremental columns. Ex: id or "id, number, ..."
72-
-d, --database <database> Database
72+
-b, --database <database> Database
7373
-c, --controller <controller> Controller name
7474
-v, --view <view> View name (.html)
7575
-r, --route <route> Route (Ex.: persons)
7676
-m, --menu <menu> Show in main menu (1 - Yes, 0 - No) Default 1
77+
-d, --delete <id> To delete CRUD by ID
78+
-l, --list To list CRUDS
7779
```
7880

7981
#### Examples

bin/basel-crud.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ program
1313
.option('-p, --pk <primary>', 'Primary key of new table')
1414
.option('-f, --references <references>', 'Refences of new table. Ex: "profile:profiles.id"')
1515
.option('-i, --incremental <incremental>', 'incremental columns. Ex: id or "id, number, ..." ')
16-
.option('-d, --database <database>', 'Database')
16+
.option('-b, --database <database>', 'Database')
1717
.option('-c, --controller <controller>', 'Controller name')
1818
.option('-v, --view <view>', 'View name (.html)')
1919
.option('-r, --route <route>', 'Route (Ex.: persons)')
2020
.option('-m, --menu <menu>', 'Show in main menu (1 - Yes, 0 - No) Default 1')
2121

2222
.option('-l, --list ', 'List cruds')
23+
.option('-d, --delete <id>', 'Delete CRUD by id')
2324
.parse(process.argv);
2425

2526

@@ -36,14 +37,34 @@ basel.config.route = program.route;
3637
basel.config.show_menu = program.menu || 1;
3738

3839
basel.config.list = program.list;
40+
basel.config.id = program.delete;
3941

4042

4143
if(basel.error){
4244
console.log(basel.error)
4345
}else{
4446
if(basel.config.list){
47+
crud.list(basel.config)
48+
}
49+
else if(basel.config.id){
50+
// delete crud
51+
var options = basel.config;
52+
console.log("Your files of view and controller will be deleted!");
53+
var questions =[
54+
{
55+
type:'input',
56+
name:'yes_no',
57+
message:'Are you sure (y/n) ?',
58+
default:'y'
59+
}
60+
];
4561

46-
}else{
62+
inquirer.prompt(questions, function(results) {
63+
_.assign(options, results);
64+
crud.delete(options);
65+
});
66+
}
67+
else{
4768
wizard(basel.config);
4869
}
4970
}

lib/crud.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,62 @@ exports.init = function (options) {
2626

2727
exports.list = function(options){
2828
options.db = _database.init(options);
29-
29+
var list = options.db.run("SELECT * FROM crud WHERE ativo = 1");
30+
console.log(list);
3031
options.db.close();
3132
}
3233

34+
exports.delete = function(options){
35+
if(options.yes_no == 'y' || options.yes_no == 'Y'){
36+
var db = _database.init(options);
37+
var crud = db.run("SELECT * FROM crud WHERE id=?",[options.id]);
38+
if(crud.length>0){
39+
crud = crud[0];
40+
var viewFile = path.join(process.cwd(),'views',crud.view);
41+
var controllerFile = path.join(process.cwd(),'controllers',crud.controller+'.js');
42+
fs.unlink(viewFile,function(err){
43+
if(err){
44+
console.error(err)
45+
}else{
46+
console.log("View file deleted");
47+
}
48+
});
49+
fs.unlink(controllerFile,function(err){
50+
if(err){
51+
console.error(err)
52+
}else{
53+
console.log("Controller file deleted");
54+
}
55+
});
56+
57+
fs.readFile(path.join(process.cwd(),'index.html'), 'utf8', function(error, data) {
58+
jsdom.env(data, function (errors, window) {
59+
var $ = require('jquery')(window);
60+
$('script[src="controllers/'+crud.controller+'.js"]').remove()
61+
if( ! $('script[src="controllers/'+crud.controller+'.js"]').length){
62+
console.log("Reference removed from index.html");
63+
db.delete('crud',{id:options.id}, function(res){
64+
console.log();
65+
console.log("CRUD deleted")
66+
});
67+
}
68+
fs.writeFile(path.join(process.cwd(),'index.html'), window.document.documentElement.outerHTML, function (error){
69+
if (error) throw error;
70+
console.log();
71+
});
72+
73+
});
74+
});
75+
76+
77+
78+
}else{
79+
console.log("ID "+options.id+" not found!")
80+
}
81+
db.close();
82+
}
83+
}
84+
3385
var routeExists = function(options){
3486
var res = options.db.run("SELECT * FROM crud WHERE route = ? ",[options.route]);
3587
return res.length>0;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "basel",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"description": "Framework for Bootstrap, AngularJS, SQLite, Electron",
55
"author": "Jayr Alencar <http://github.com/jayralencar>",
66
"readmeFilename": "README.md" ,

0 commit comments

Comments
 (0)