@@ -25,14 +25,14 @@ var _header = require('utilise/header');
2525
2626var _header2 = _interopRequireDefault ( _header ) ;
2727
28- var _client = require ( 'utilise/client' ) ;
29-
30- var _client2 = _interopRequireDefault ( _client ) ;
31-
3228var _keys = require ( 'utilise/keys' ) ;
3329
3430var _keys2 = _interopRequireDefault ( _keys ) ;
3531
32+ var _noop = require ( 'utilise/noop' ) ;
33+
34+ var _noop2 = _interopRequireDefault ( _noop ) ;
35+
3636var _key = require ( 'utilise/key' ) ;
3737
3838var _key2 = _interopRequireDefault ( _key ) ;
@@ -52,9 +52,6 @@ var _is2 = _interopRequireDefault(_is);
5252/* istanbul ignore next */
5353function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
5454
55- // -------------------------------------------
56- // Loads resources from the /resources folder
57- // -------------------------------------------
5855function mysql ( ripple ) {
5956 log ( 'creating' ) ;
6057 var type = ripple . types [ 'application/data' ] ;
@@ -69,71 +66,74 @@ var init = function init(ripple) {
6966 return function ( config ) {
7067 var con = require ( 'mysql' ) . createPool ( config ) ;
7168 escape = con . escape . bind ( con ) ;
72-
7369 return {
74- add : exec ( 'add' ) ( con ) ,
75- update : exec ( 'update' ) ( con ) ,
76- remove : exec ( 'remove' ) ( con )
70+ update : crud ( ripple , con ) ( 'update' ) ,
71+ remove : crud ( ripple , con ) ( 'remove' ) ,
72+ add : crud ( ripple , con ) ( 'add' ) ,
73+ load : load ( ripple , con )
74+ // change: change(ripple, con)
7775 } ;
7876 } ;
7977} ;
8078
81- var exec = function exec ( type ) {
82- return function ( con ) {
83- return function ( ripple ) {
84- return function ( res , index , value ) {
85- var table = ( 0 , _header2 . default ) ( 'mysql.table' ) ( res ) ,
86- xto = ( 0 , _key2 . default ) ( 'headers.mysql.to' ) ( res ) ,
87- p = ( 0 , _promise2 . default ) ( ) ;
88-
89- if ( xto && ! xto ( res , { key : index , value : value , type : type } ) ) return ;
90- if ( ! index ) return load ( con ) ( ripple ) ( res ) ;
91- if ( ! table ) return ;
92-
93- var levels = index . split ( '.' ) ,
94- record = levels . length === 1 ? ( levels . shift ( ) , value ) : res . body [ levels . shift ( ) ] ,
95- field = levels . shift ( ) ;
96-
97- if ( field ) record = ( 0 , _key2 . default ) ( [ 'id' , field ] ) ( record ) ;
98- if ( ! _is2 . default . obj ( record ) || levels . length || field && ! _is2 . default . in ( res . headers . mysql . fields ) ( field ) ) return log ( 'cannot generate SQL for' , res . name , index ) ;
99-
100- var sql = sqls [ type ] ( table , ( 0 , _key2 . default ) ( res . headers . mysql . fields ) ( record ) ) ;
101- log ( 'SQL' , sql . grey ) ;
102- con . query ( sql , function ( e , rows ) {
103- if ( e ) return err ( type , table , 'failed' , e ) ;
104- log ( type . green . bold , table , 'done' , rows . insertId ? ( 0 , _str2 . default ) ( rows . insertId ) . grey : '' ) ;
105-
106- rows . insertId ? p . resolve ( value . id = rows . insertId ) : p . resolve ( ) ;
107- } ) ;
79+ var crud = function crud ( ripple , con ) {
80+ return function ( type ) {
81+ return function ( name , record ) {
82+ var res = ripple . resources [ name ] ,
83+ table = ( 0 , _header2 . default ) ( 'mysql.table' ) ( res ) ,
84+ fields = ( 0 , _header2 . default ) ( 'mysql.fields' ) ( res ) ,
85+ p = ( 0 , _promise2 . default ) ( ) ,
86+ sql = void 0 ;
87+
88+ if ( ! table ) return deb ( 'no table' , name ) ;
89+ if ( ! ( sql = sqls [ type ] ( table , ( 0 , _key2 . default ) ( fields ) ( record ) ) ) ) return deb ( 'no sql' , name ) ;
90+ log ( 'SQL' , sql . grey ) ;
91+
92+ con . query ( sql , function ( e , rows ) {
93+ if ( e ) return err ( type , table , 'failed' , e ) ;
94+ log ( type . green . bold , table , 'done' , rows . insertId ? ( 0 , _str2 . default ) ( rows . insertId ) . grey : '' ) ;
95+ p . resolve ( rows . insertId || record . id ) ;
96+ } ) ;
10897
109- return p ;
110- } ;
98+ return p ;
11199 } ;
112100 } ;
113101} ;
114102
115- var load = function load ( con ) {
116- return function ( ripple ) {
117- return function ( res ) {
118- var table = ( 0 , _header2 . default ) ( 'mysql.table' ) ( res ) || res . name ,
119- p = ( 0 , _promise2 . default ) ( ) ;
120-
121- if ( ( 0 , _key2 . default ) ( loaded ) ( res ) ) return ;
122- ( 0 , _key2 . default ) ( loaded , true ) ( res ) ;
123-
124- con . query ( 'SHOW COLUMNS FROM ' + table , function ( e , rows ) {
125- if ( e && e . code == 'ER_NO_SUCH_TABLE' ) return log ( 'no table' , table ) , ( 0 , _key2 . default ) ( 'headers.mysql.table' , '' ) ( res ) ;
103+ // const change = (ripple, con) => type => (res, change) => {
104+ // let levels = (change.key || '').split('.')
105+ // , xto = header('mysql.xto')(res)
106+ // , index = levels[0]
107+ // , field = levels[1]
108+ // , record = change.value
109+
110+ // if (!change.key) return load(ripple, con)(res)
111+ // if (!levels.length || levels.length > 2 || (field && !is.in(fields)(field))) return deb('cannot update', name, key)
112+ // if (xto && !xto(res, change)) return deb('skipping update', name)
113+ // if (field) record = key(['id', field])(res.body[index])
114+ // crud(ripple, con)(type)(res.name, record)
115+ // }
116+
117+ var load = function load ( ripple , con ) {
118+ return function ( name ) {
119+ var res = ripple . resources [ name ] ,
120+ table = ( 0 , _header2 . default ) ( 'mysql.table' ) ( res ) || res . name ,
121+ p = ( 0 , _promise2 . default ) ( ) ;
122+
123+ con . query ( 'SHOW COLUMNS FROM ' + table , function ( e , rows ) {
124+ if ( e && e . code == 'ER_NO_SUCH_TABLE' ) return log ( 'no table' , table ) , ( 0 , _key2 . default ) ( 'headers.mysql.table' , '' ) ( res ) ;
125+ if ( e ) return err ( table , e ) ;
126+ ( 0 , _key2 . default ) ( 'headers.mysql.fields' , rows . map ( ( 0 , _key2 . default ) ( 'Field' ) ) ) ( res ) ;
127+ ( 0 , _key2 . default ) ( 'headers.mysql.table' , table ) ( res ) ;
128+
129+ con . query ( 'SELECT * FROM ' + table , function ( e , rows ) {
126130 if ( e ) return err ( table , e ) ;
127- ( 0 , _key2 . default ) ( 'headers.mysql.fields' , rows . map ( ( 0 , _key2 . default ) ( 'Field' ) ) ) ( res ) ;
128- ( 0 , _key2 . default ) ( 'headers.mysql.table' , table ) ( res ) ;
129-
130- con . query ( 'SELECT * FROM ' + table , function ( e , rows ) {
131- if ( e ) return err ( table , e ) ;
132- log ( 'got' , table , rows . length ) ;
133- ripple ( { name : res . name , body : rows } ) ;
134- } ) ;
131+ log ( 'got' . green , table , ( 0 , _str2 . default ) ( rows . length ) . grey ) ;
132+ p . resolve ( rows ) ;
135133 } ) ;
136- } ;
134+ } ) ;
135+
136+ return p ;
137137 } ;
138138} ;
139139
@@ -142,7 +142,7 @@ var sqls = {
142142 return 'INSERT INTO {table} ({keys}) VALUES ({values});' . replace ( '{table}' , name ) . replace ( '{keys}' , ( 0 , _keys2 . default ) ( body ) . filter ( ( 0 , _not2 . default ) ( ( 0 , _is2 . default ) ( 'id' ) ) ) . map ( ( 0 , _prepend2 . default ) ( '`' ) ) . map ( ( 0 , _append2 . default ) ( '`' ) ) . join ( ',' ) ) . replace ( '{values}' , ( 0 , _keys2 . default ) ( body ) . filter ( ( 0 , _not2 . default ) ( ( 0 , _is2 . default ) ( 'id' ) ) ) . map ( ( 0 , _from2 . default ) ( body ) ) . map ( escape ) . join ( ',' ) ) ;
143143 } ,
144144 update : function update ( name , body ) {
145- return 'UPDATE {table} SET {kvpairs} WHERE id = {id};' . replace ( '{table}' , name ) . replace ( '{id}' , body [ 'id' ] ) . replace ( '{kvpairs}' , ( 0 , _keys2 . default ) ( body ) . filter ( ( 0 , _not2 . default ) ( ( 0 , _is2 . default ) ( 'id' ) ) ) . map ( kvpair ( body ) ) . join ( ',' ) ) ;
145+ return ( 0 , _keys2 . default ) ( body ) . length == 1 && 'id' in body ? '' : 'UPDATE {table} SET {kvpairs} WHERE id = {id};' . replace ( '{table}' , name ) . replace ( '{id}' , body [ 'id' ] ) . replace ( '{kvpairs}' , ( 0 , _keys2 . default ) ( body ) . filter ( ( 0 , _not2 . default ) ( ( 0 , _is2 . default ) ( 'id' ) ) ) . map ( kvpair ( body ) ) . join ( ',' ) ) ;
146146 } ,
147147 remove : function remove ( name , body ) {
148148 return 'DELETE FROM {table} WHERE id = {id};' . replace ( '{table}' , name ) . replace ( '{id}' , body [ 'id' ] ) ;
@@ -168,7 +168,7 @@ var strip = function strip(next) {
168168 } ;
169169} ;
170170
171- var loaded = 'headers. mysql.loaded' ,
172- log = require ( 'utilise/log ' ) ( '[ri/mysql]' ) ,
173- err = require ( 'utilise/err ' ) ( '[ri/mysql]' ) ;
171+ var log = require ( 'utilise/log' ) ( '[ri/ mysql]' ) ,
172+ err = require ( 'utilise/err ' ) ( '[ri/mysql]' ) ,
173+ deb = _noop2 . default ; // require('utilise/log ')('[ri/mysql]')
174174var escape ;
0 commit comments