-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw-sql.bsh
241 lines (182 loc) · 9.04 KB
/
raw-sql.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /bin/bash
## raw-sql.bsh last update: 2019-05-04
## Bash-shell script to create a raw sql service.
## the only code changed is in the ./src/services/SERVICENAME/SERVICENAME.class.js script.
## written from https://docs.feathersjs.com/guides/basics/services.html
## https://docs.feathersjs.com/api/services.html#setupapp-path
## Note: expect-tcl is used to run the feathers-cli steps.
## Also, there is a bit of a debate if sudo is required for the feathers installation. I
## am using sudo to make the command global in nature.
## this writeup was written with the assistance and incredible patience of the
## following feathers-slack members:
## Beau Shaw
## German
## Karolis
## Ryan Wheale
## i am very grateful for them in lending their expertise and tolerating my silly questions.
## !!! Be sure to run the feathers install gist first !!!
sudo yum --assumeyes install expect bind-utils ; ## this might need to be in another module
MariaDB='MariaDB' ;
CockroachDB='CockroachDB' ;
TYPE=MariaDB ; ## SET THIS LINE FOR YOUR DATABASE CHOICE!
SERVICE_NAME='rawsql';
FAKE_SERVICE_NAME='fakeservicename'; ## we need one "unused" sql service.
FAKE_TABLE_NAME='faketablename' ; ## name of sql table
FEATHERS_PORT=3030;
SQL_STATEMENT='SELECT * FROM tester2'; ## must be a valid sql statement
echo -n 'node version: ' ; node --version ; ## 8.15 at the time of this writing
if [ $? -ne 0 ]; then
echo 'Node has not yet been installed!';
exit 1;
fi
echo -n 'npm version: ' ; npm --version ; ## 6.7.0 at the time of this writing
if [ $? -ne 0 ]; then
echo 'NPM has not yet been installed!';
exit 1;
fi
echo -n 'feathers version: ' ; feathers --version ; ## 3.9 at the time of this writing
if [ $? -ne 0 ]; then
echo 'Feathers has not yet been installed!';
exit 1;
fi
## prompt for any/all constant-variable changes
read -p "Enter DB_USER or blank to use '$DB_USER': " tmpDB_USER;
if [ ! -z "$tmpDB_USER" ]; then
DB_USER=$tmpDB_USER;
fi;
read -p "Enter DB_PASS or blank to use '$DB_PASS': " tmpDB_PASS;
if [ ! -z "$tmpDB_PASS" ]; then
DB_PASS=$tmpDB_PASS;
fi;
read -p "Enter DB_NAME or blank to use '$DB_NAME': " tmpDB_NAME;
if [ ! -z "$tmpDB_NAME" ]; then
DB_NAME=$tmpDB_NAME;
fi;
read -p "Enter DB_SERVER_IP or blank to use '$DB_SERVER_IP': " tmpDB_SERVER_IP;
if [ ! -z "$tmpDB_SERVER_IP" ]; then
DB_SERVER_IP=$tmpDB_SERVER_IP;
fi;
# build database string
if [ "${TYPE}" = "${MariaDB}" ]; then
SERVER_PORT='3306' ;
CONNECT_STRING="mysql://${DB_USER}:${DB_PASS}@${DB_SERVER_IP}:${SERVER_PORT}/${DB_NAME}" ;
DB_URL="https://mariadb.com/" ;
elif [ "${TYPE}" = "${CockroachDB}" ]; then
SERVER_PORT='26257' ;
PG_SSL_FLAG='true' ;
CONNECT_STRING="postgres://${DB_USER}:${DB_PASS}@${DB_SERVER_IP}:${SERVER_PORT}/${DB_NAME}?ssl=${PG_SSL_FLAG}" ;
DB_URL="https://cockroachlabs.com/" ;
fi
THIS_IPADDR=$(dig +short myip.opendns.com @resolver1.opendns.com. ;);
FileNameWithExtension=${0##*/} ;
FileNameWithoutExtension=${FileNameWithExtension%.*} ;
TimeStamp=$(date "+%Y-%m-%d %r") ;
commonPath="$(readlink -f $0 | xargs dirname)"/common;
rm -Rf ./${FileNameWithoutExtension}/ ; ## just in case one already exists.
mkdir ./${FileNameWithoutExtension}/ && cd ./${FileNameWithoutExtension}/ ;
. ${commonPath}/createApp.bsh;
if [ "${TYPE}" = "${MariaDB}" ]; then
echo "Setting one uparrow for ${MariaDB}";
export ARROW_DIRECTION=$'\x1b\x5b\x41'; ## up-arrow for MariadbDB
elif [ "${TYPE}" = "${CockroachDB}" ]; then
echo "Setting one downarrow for ${CockroachDB}";
export ARROW_DIRECTION=$'\x1b\x5b\x42'; ## down-arrow for CockroachDB
fi
export FAKE_SERVICE_NAME;
export FAKE_TABLE_NAME;
export CONNECT_STRING;
## database credentials
DB_USER='database-username' ;
DB_PASS='database-password' ;
DB_NAME='database-name' ;
DB_SERVER_IP='XXX.XXX.XXX.XXX' ;
## feathers generate service; ## all defaults except for the service name - "testtable"
expect <(cat <<'END_OF_GENERATE_SERVICE'
# creates a service: testtable (all the defaults)
# written from: https://docs.feathersjs.com/guides/chat/service.html
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
set timeout -1
spawn feathers generate service;
expect -re ".*What kind of service is it\?.*"
send -- "${DOWNARROW}${DOWNARROW}${DOWNARROW}${RETURN}"
## defaults to NeDB
## one downarrow selects "MongoDB"
## 2 - Mongoose
## 3 - Sequelize
## 4 - KnexJS
## 5 - RethinDB
## 6 - custom service
expect -re ".*What is the name of the service\?.*"
send -- "$env(FAKE_TABLE_NAME)${RETURN}"
## testtable
expect -re ".*Which path should the service be registered on\?.*"
send -- "$env(FAKE_SERVICE_NAME)${RETURN}"
## defaults to something something like /tiny-connect
expect -re ".*Which database are you connecting to\?.*"
send -- "$env(ARROW_DIRECTION)${RETURN}"
## downarrow for postgres(cockroachdb), uparrow for mariadb
expect -re ".*What is the database connection string\?.*"
send -- "$env(CONNECT_STRING)${RETURN}"
## default answer is nedb://../data
expect eof
END_OF_GENERATE_SERVICE
) ## end of feathers generate service
export SERVICE_NAME;
## feathers generate service; ## all defaults except for the service name - "testtable"
expect <(cat <<'END_OF_GENERATE_SERVICE'
# creates a service: reverse (all the defaults)
# written from: https://docs.feathersjs.com/guides/chat/service.html
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
set timeout -1
spawn feathers generate service;
expect -re ".*What kind of service is it\?.*"
send -- "${UPARROW}${UPARROW}${RETURN}"
## defaults to NeDB
## one downarrow selects "MongoDB"
## 2 - Mongoose
## 3 - Sequelize
## 4 - KnexJS
## 5 - RethinDB
## 6 - custom service (also two up-arrows)
expect -re ".*What is the name of the service\?.*"
send -- "$env(SERVICE_NAME)${RETURN}"
expect -re ".*Which path should the service be registered on\?.*"
send -- "${RETURN}"
## defaults to /SERVICE_NAME
expect eof
END_OF_GENERATE_SERVICE
) ## end of feathers generate service
sed --in-place --file=- ./src/services/${SERVICE_NAME}/${SERVICE_NAME}.class.js <<END_OF_SED_SERVICE_CLASS ;
s|return \\[];|// return[];|;
/ async find (params) {/i\\
setup(app) { // added by ${FileNameWithExtension} reference https://docs.feathersjs.com/api/services.html#setupapp-path \\
this.app = app; // added by ${FileNameWithExtension} \\
} // added by ${FileNameWithExtension} \\
/ async find (params) {/a\\
// -- lines added by ${FileNameWithExtension} \\
const sequelize = this.app.get('sequelizeClient'); \\
const results = await sequelize.query('${SQL_STATEMENT}', { type: sequelize.QueryTypes.SELECT}); \\
/* \\
const sqlString = sequelize.escape(params.query.sqlString).replace(/(^')|('$)/g, ""); \\
console.log(sqlString) ; \\
const results = await sequelize.query(sqlString, { type: sequelize.QueryTypes.SELECT}); \\
*/ \\
return results;
END_OF_SED_SERVICE_CLASS
sed --in-place --file=- ./config/default.json << END_OF_SED_CONFIG_DEFAULT ;
s/"host": "localhost",/"host": "${THIS_IPADDR}",/;
END_OF_SED_CONFIG_DEFAULT
cat <<END_OF_SCRIPT;
Only a few lines of code were changed in ./src/services/${SERVICE_NAME}/${SERVICE_NAME}.class.js
Now run ' cd ./${FileNameWithoutExtension}/ ; npm start; ' to try it out.
click on this to test FIND: (note that both of these could just as easily be done using Postman)
http://${THIS_IPADDR}:${FEATHERS_PORT}/${SERVICE_NAME}
http://${THIS_IPADDR}:${FEATHERS_PORT}/${SERVICE_NAME}/?sqlString=${SQL_STATEMENT}
END_OF_SCRIPT
##