24

Oct 2019

Module CRUD - Manage Aurora CRUD Function

Manage Aurora CRUD Function

AuroraJS has several functions that can be used to manipulate data, create, read update, and delete. The functions have been provided in aurora and is easy to use with several types of usage options. This alpha version only supports the use of data manipulation functions within the scope of the MYSQL database.


Aurora CRUD Query Builder

Aurora CRUD  has some predefined variable query builder, that is :

Query Builder Description
select

To get table you want from the database

Example :

'select' : ['id','name','age']

 

set

To set the target fields you want to update and contain update value Example:

'set' : [
['name','=',req.body.name],
['age','=',req.body.age]
]

 

where

Where is a function with two condition that can be used with single field or many field(AND)

Example :

'Where' : [
[“Id”, “=” , “1”] , 
]

Or:

 'Where' : [
[“Id”, “=” , “1”] , 
[“name”, “=” , “dan”] ,
[“age”, “=” , “20”]     
]

 

orWhere

orWhere is a condition function for query builder and contain another target field from table

Example :

'orWhere' : [
[“Id”, “=” , “1”] , 
[“name”, “=” , “dan”] ,
[“age”, “=” , “20”]     
]

 

 


INSERT

The insert function data or create data to create data in a database consists of 2 types of function use methods:

1. INSERT WITHOUT MODEL

The insert function without the model is used to create data in the database without going through model validation, with this function the users can easily customize the insert according to the table. 

Example: 

function create(req, res) {
      main.insert({
          'table_name' : ['membersTable'],
          'field' : ['id','name','age'],
          'result' : [
              req.body.id,
              req.body.name,
              req.body.age    
          ]
      }).then(function (q) {
      try {
              
      }catch(error){
   
      }
          },function(err){
           try{
               
              }catch(error){
   
          } 
  });
  }

 

2. INSERT WITH MODEL

The insert function with the model is used to create data in the database through model validation, with this function users can use the validation feature provided without having to make manual adjustments to the validation of the manual table. 

Example:

 function create(req, res) {
      main.insert({
          'models' : ['membersModel'],
          'field' : ['id','name','age'],
          'result' : [
              req.body.id,
              req.body.name,
              req.body.age    
          ]
      }).then(function (q) {
      try {
              
      }catch(error){
   
      }
          },function(err){
           try{
               
              }catch(error){
   
          } 
  });
  }

 


READ

AuroraJS read function is to retrieve data in the database so that data can be displayed, read function consists of 2 methods of using the function with 2 data collection conditions. The function is as follows:

1. READ WITH MODEL

The read function with the model is used to retrieve data in the database through model validation so that it can be displayed, with this function the user can use the validation feature provided without having to make manual adjustments to table validation.

Example:

function index(req, res) {
main.read({
'models' : ['membersModel'],
'select' : [‘*’],
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

2. READ WITHOUT MODEL

The read function without the model is used to retrieve data in the database without going through model validation so that it can be displayed, with this function the user can easily adjust the read function so that it matches the desired table.

Example:

function index(req, res) {
main.read({
'table_name' : ['membersTable'],
'select' : ['*']        
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

# WHEREAND

function index(req, res) {
main.read({
'table_name' : ['membersTable'],
'select' : ['id','name','age'],
'where' : [
[“Id”, “=” , “1”] , 
[“name”, “=” , “dan”] ,
[“age”, “=” , “20”]     
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

#WHEREOR

function index(req, res) {
main.read({
'table_name' : ['membersTable'],
'select' : ['id','name','age'],
'where' : [
[“Id”, “=” , “1”] , 
[“name”, “=” , “dan”] ,
[“age”, “=” , “20”]     
],
'orWhere' : [
[“Id”, “=” , “1”] , 
[“name”, “=” , “dan”] ,
[“age”, “=” , “20”]     
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 


UPDATE

The update function is used to make changes to data records in a database, the use of this function consists of 2 methods with 2 conditions for changing data. The function is as follows:

1. UPDATE WITH MODEL

The update function with the model is used to change data records in the database through model validation, with this function the user can use the validation feature provided without having to make manual adjustments to table validation.

Example:

function update(req, res) {
main.update({
'models' : ['membersModel'],
'set' : [
['name','=',req.body.name],
['age','=',req.body.age]
],
'where' : [
['id','=',req.params.id]
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

2. UPDATE WITHOUT MODEL

The update function without the model is used to change data records in the database without going through model validation, with this function the user can easily adjust the update function so that it matches the desired table.

Example:

function update(req, res) {
main.update({
'table_name' : ['membersModel'],
'set' : [
['name','=',req.body.name],
['age','=',req.body.age]
],
'where' : [
['id','=',req.params.id]
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

3. UPDATE WHEREAND

The update function with the condition WhereAND is used to change data with the condition of the data to be changed has a logic where the data in question consists of 1 data and other data.

Example:

function update(req, res) {
main.update({
'table_name' : ['membersModel'],
'set' : [
['name','=',req.body.name],
['age','=',req.body.age]
],
'where' : [
['id','=',req.params.id],
['age','=',req.params.age]
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

4. UPDATE WHEREOR

The update function with the condition WhereOR is used to change the data with the condition of the data to be changed has a logic where the intended data consists of 1 data or other data.

Example:

function update(req, res) {
main.update({
'table_name' : ['membersModel'],
'set' : [
['name','=',req.body.name],
['age','=',req.body.age]
],
'where' : [
['id','=',req.params.id]
],
'orWhere' : [
[“name”, “=” , “dan”],
[“age”, “=” , “20”]     
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 


DELETE

AuroraJS delete function is to delete data from the database, data deletion consists of 2 methods of using the function with 2 data retrieval conditions. The function is as follows:

1. DELETE WITH MODEL

The delete function with the model is used to delete data in the database through the validation model, with this function the user can use the validation feature provided without having to make manual adjustments to the validation table.

Example:

function erase(req, res) {
main.erase_query({
'models' : ['membersModel'],
'where' : [
[“Id”, “=” , req.params.id] , 
[“name”, “=” , “req.params.name”] ,
[“age”, “=” , “req.params.age”]     
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

2. DELETE WITHOUT MODEL

The delete function without a model is used to delete data in a database without going through model validation, with this function the users can easily adjust the delete function so that it matches the desired table.

Example:


function erase(req, res) {
main.erase_query({
'table_name' : ['membersTable'],
'where' : [
[“Id”, “=” , req.params.id] 
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

3. DELETE WHEREAND

Fungsi delete dengan kondisi WhereAND digunakan untuk penghapusan data dengan kondisi data yang akan dihapus memiliki logic dimana data yang dimaksud terdiri dari 1 data dan data lainnya.

function erase(req, res) {
main.erase_query({
'table_name' : ['membersTable'],
'where' : [
[“Id”, “=” , req.params.id] , 
[“name”, “=” , “req.params.name”] ,
[“age”, “=” , “req.params.age”]     
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}

 

4. DELETE WHEREOR

The delete function with condition WhereOR is used to delete data with the condition of the data to be deleted has a logic where the data in question consists of 1 data or other data.

Example:

function erase(req, res) {
main.erase_query({
'table_name' : ['membersTable'],
'where' : [
[“Id”, “=” , req.params.id] , 
[“name”, “=” , “req.params.name”] ,
[“age”, “=” , “req.params.age”]     
],        
'orWhere' : [
[“Id”, “=” , req.params.id] , 
[“name”, “=” , “req.params.name”] ,
[“age”, “=” , “req.params.age”]         
]
}).then(function (q) {
try {

}catch(error){

}
},function(err){
try{

}catch(error){

} 
});
}