73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
'use strict'
|
|
|
|
const fs = require( 'fs' )
|
|
, path = require( 'path' )
|
|
, URL = require( 'url' ).URL
|
|
, request = require( 'request' )
|
|
|
|
, Now = require( './readable-timestamp.js' )
|
|
, Config = require( '../config.js' )
|
|
|
|
const req
|
|
= ( http_method, sql_params ) =>
|
|
new Promise( ( resolve, reject ) =>
|
|
{ let req_url = new URL( Config.SQL.BaseURL )
|
|
|
|
for( const k in sql_params )
|
|
{ req_url.searchParams.set( k, sql_params[ k ] ) }
|
|
|
|
// console.log
|
|
// ({ url : req_url.toString()
|
|
// , method : http_method
|
|
// , auth : Config.SQL.auth
|
|
// })
|
|
|
|
request( { url: req_url.toString()
|
|
, method: http_method
|
|
, auth: Config.SQL.auth
|
|
}
|
|
, ( err, res, body ) =>
|
|
{ // console.log({ err, res, body })
|
|
|
|
if( err || !res )
|
|
{ reject( err )
|
|
return false
|
|
}
|
|
if( 400 <= res.statusCode )
|
|
{ resolve( false ) }
|
|
|
|
let _res = ''
|
|
|
|
try
|
|
{ _res = JSON.parse( body ) }
|
|
catch( err )
|
|
{ if( err )
|
|
{ console.error( Now(), '\n', JSON.stringify(
|
|
{ http_method
|
|
, sql_params
|
|
, err
|
|
, _res
|
|
}, false, 2 ) )
|
|
resolve( [] )
|
|
}
|
|
}
|
|
resolve( _res )
|
|
})
|
|
|
|
}).catch( err => console.error
|
|
( Now()
|
|
, 'cant make request to mysql,'
|
|
, err
|
|
, http_method
|
|
, sql_params
|
|
) )
|
|
|
|
|
|
module.exports = exports =
|
|
{ create: ( _ ) => req( 'POST' , _ )
|
|
, read: ( _ ) => req( 'GET' , _ )
|
|
, update: ( _ ) => req( 'PUT' , _ )
|
|
, delete: ( _ ) => req( 'DELETE' , _ )
|
|
, raw: ( _ ) => req( 'PATCH' , _ )
|
|
}
|