-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-tiny.bsh
514 lines (423 loc) · 35.6 KB
/
sql-tiny.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#! /bin/bash
## sql-tiny.bsh last update: 2019-02-13
## Bash-shell script to create a TINY sql example entirely from scratch.
## https://www.geocerts.com/ssl-checker - use this to check the cert
## 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.
## Be sure to run init-feathers-setup.bsh first!
## note: its always best to create the table yourself, rather than relying on sequelize to do it:
## CREATE TABLE tinyConnect
## ( name VARCHAR(30) NOT NULL
## , address VARCHAR(30) NOT NULL
## , createdAt DATETIME NOT NULL
## , updatedAt DATETIME NOT NULL
## , id TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT
## , PRIMARY KEY (`id`)
## );
MariaDB='MariaDB' ;
CockroachDB='CockroachDB' ;
TYPE=MariaDB ; ## SET THIS LINE FOR YOUR DATABASE CHOICE!
TableName='tinyConnect' ; ## name of sql table
ServiceName='tiny-connect'; ## service name (same one that feathers will probably convert to
## if a CAPPED letter was used.
## vars for ./config/default.json changes -- you may need to double-check the values here.
## database credentials
DB_USER='database-username' ;
DB_PASS='database-password' ;
DB_NAME='database-name' ;
DB_SERVER_IP='XXX.XXX.XXX.XXX' ;
FEATHERS_PORT='3030' ;
## check all versions
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 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
sudo yum --assumeyes install expect ;
sudo yum --assumeyes install bind-utils ; ## for the 'dig' command
THIS_IPADDR=$(dig +short myip.opendns.com @resolver1.opendns.com. ;);
## see if this is going to be a SSL job or not
read -p "Enter SSL Domain Name (blank for ${THIS_IPADDR}: " DOMAIN_NAME;
if [ -z "${DOMAIN_NAME}" ]; then
PROTOCOL='http';
else
PROTOCOL='https' ;
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
## originally: postgres": "postgres://postgres:@localhost:5432/google_jsgrid_pgsql
## password example: postgres://feathersuser:[email protected]:26257/bank
## mysql example: mysql://feathersuser:[email protected]:3306/bank
FileNameWithExtension=${0##*/} ;
FileNameWithoutExtension=${FileNameWithExtension%.*} ;
TimeStamp=$(date "+%Y-%m-%d %r") ;
if [ ! -z "${DOMAIN_NAME}" ]; then
### https://certbot.eff.org/lets-encrypt/centosrhel7-other
sudo yum --assumeyes install yum-utils ;
sudo yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional;
sudo yum --assumeyes install certbot;
fi
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 TableName;
export ServiceName;
export CONNECT_STRING;
## 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(TableName)${RETURN}"
## testtable
expect -re ".*Which path should the service be registered on\?.*"
send -- "$env(ServiceName)${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
if [ -z "${DOMAIN_NAME}" ]; then
LOCALHOST_REPLACEMENT=${THIS_IPADDR} ;
else
LOCALHOST_REPLACEMENT=${DOMAIN_NAME} ;
fi
sed --in-place --file=- ./config/default.json <<END_OF_DEFAULT_JSON ;
s?"host": "localhost",?"host": "${LOCALHOST_REPLACEMENT}",?;
s?"port": 3030,?"port": ${FEATHERS_PORT},?;
END_OF_DEFAULT_JSON
if [ ! -z "${DOMAIN_NAME}" ]; then
sed --in-place --file=- ./config/default.json <<END_OF_SSL_JSON ;
/"port": ${FEATHERS_PORT},/a\\
/* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
"privkey_pem" : "/etc/letsencrypt/live/${DOMAIN_NAME}/privkey.pem", \\
"cert_pem" : "/etc/letsencrypt/live/${DOMAIN_NAME}/cert.pem", \\
"chain_pem" : "/etc/letsencrypt/live/${DOMAIN_NAME}/chain.pem", \\
/* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
END_OF_SSL_JSON
fi
## modify the ./src/index.js file for SSL usage, if specified
if [ ! -z "${DOMAIN_NAME}" ]; then
## feathers by default uses http, not https, so this will modify feathers to only use https
##
sed --in-place --file=- ./src/index.js <<END_OF_INDEX_JS ;
s?const server = app.listen(port);?// &?;
s?'Feathers application started on http?&s?;
/const server = app.listen(port);/i\
\\
const https = require('https'); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
const fs = require('fs'); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
\\
/* define these three ssl items in your ./config/default.json file ${FileNameWithExtension} ${TimeStamp} */ \\
const privkey_pem = app.get('privkey_pem'); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
const cert_pem = app.get('cert_pem'); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
const chain_pem = app.get('chain_pem'); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
\\
// derived from: https://docs.feathersjs.com/api/express.html#https \\
const server = https.createServer({ /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
key: fs.readFileSync(privkey_pem), /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
cert: fs.readFileSync(cert_pem), /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
ca: fs.readFileSync(chain_pem), /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
}, app).listen(port); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
\\
app.setup(server); /* added by ${FileNameWithExtension} ${TimeStamp}*/ \\
END_OF_INDEX_JS
fi ## skip this if we are not doing a SSL install
## we need to modify channels.js **ONLY** because this lazy example skips using login authentication,
## otherwise the registered event listeners do not work! notice we are not changing the channel names in the login
## section, since login is not being used here.
## see difference between https://docs.feathersjs.com/guides/basics/real-time.html#channels
## and https://docs.feathersjs.com/api/channels.html#example
sed --in-place --file=- ./src/channels.js <<END_OF_SED_CHANNELS_JS ;
s?app.channel('anonymous').join(connection);?app.channel('everybody').join(connection); /* changed from app.channel('anonymous').join(connection); by ${FileNameWithExtension} ${TimeStamp}*/?;
s?return app.channel('authenticated');?return app.channel('everybody');/* changed from return app.channel('authenticated'); by ${FileNameWithExtension} ${TimeStamp}*/?;
END_OF_SED_CHANNELS_JS
## this does not work, unfortunately... ;-(
## document.write(\\"< script src='//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'></script>\\"); <br />\\
## document.write(\\"< script src='//unpkg.com/@feathersjs/client\@\^3.0.0/dist/feathers.js'></script>\\"); <br />\\
if [ -z "${DOMAIN_NAME}" ]; then
THIS_DOMAIN=${THIS_IPADDR} ;
else
THIS_DOMAIN=${DOMAIN_NAME} ;
fi
## reduce the size of the feathers logo to make more room for code
sed --in-place --file=- ./public/index.html <<END_OF_SED_PUBLIC_HTML ;
s/class="logo"/& width='120' height='20'/; ## shrink the logo size
s/font-size: 2em;/font-size: 1em;/; ## reduce the H2 tag font size at the top
s/font-size: 1.0em;/font-size: 12px;/; ## reduce the "p" font size
s/margin-top: 100px;/margin-top: 10px; margin-left: 10px/; ## reduce the "main" tag from 100 pixels to 10 pixels
s/padding: 20px;/padding: 10px;/; ## reduce the "main" tag from 20 pixels to 10 pixels
s/footer p {/p, td {/ ## replace the "footer p" selector with "p,td"
s/margin-bottom: 30px;/margin-bottom: 10px/; ## reduce the "img.logo" selector from 30 pixels to 10 pixels
/<footer>/d; ## remove the footer tag line
/<\/footer>/d;
/For more information on Feathers/d; ## remove the last line
/<\/style>/i\
table { border-collapse: collapse; margin-left : 20px ; } table, th, td { border: 1px solid black; } td { padding: 5px; }
/<\/main>/i\
<hr /> \\
<table border='0' cellpadding='5'> \\
<tr> \\
<th colspan='2'> \\
Go into your console, and try cut-paste entering the following commands at the command prompt: \\
</th> \\
</tr> \\
<tr> \\
<td colspan='2'> \\
/* This command should be ran <strong>separately</strong> *\/ <br />\\
\[ '//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js' , '//unpkg.com/@feathersjs/client@\^3.0.0/dist/feathers.js' <br />\\
\].forEach( (src) => { <br />\\
let script = document.createElement('script'); script.src = src; script.async = false; <br />\\
document.head.appendChild(script); <br />\\
}); <br />\\
</td> \\
</tr> \\
<tr> \\
<td> \\
/* be sure to run the command ABOVE before attempting to run this one */ <br />\\
const app = feathers(); <br />\\
const socket = io('${PROTOCOL}://${THIS_DOMAIN}:${FEATHERS_PORT}') ; /* or just const socket=io(); */ <br />\\
app.configure(feathers.socketio(socket)); <br />\\
<td> \\
/* optionally, establish a listener for create(POST), patched(PATCH), update(PUT), and remove(DELETE) */ <br />\\
app.service('${ServiceName}').on('created', createMessage => { console.log('create(POST) : ', JSON.stringify(createMessage))});<br />\\
app.service('${ServiceName}').on('updated', updateMessage => { console.log('update(PUT) : ', JSON.stringify(updateMessage))});<br />\\
app.service('${ServiceName}').on('patched', patchMessage => { console.log('patch(PATCH) : ', JSON.stringify(patchMessage ))});<br />\\
app.service('${ServiceName}').on('removed', removeMessage => { console.log('removed(DELETE): ', JSON.stringify(removeMessage))});<br />\\ <br />\\
</td> \\
</tr> \\
</table> \\
<br />\\
<br />\\
<table border='0' cellpadding='5'> \\
<tr> \\
<th colspan='2' align='center'> \\
CRUD callable async functions <span style='font-size: 11px'>Note: update(PUT) is not referenced in this example</span> \\
</th> \\
</tr> \\
<tr> \\
<th> \\
create(POST), patch(PATCH), and remove(DELETE) \\
</th> \\
<th> \\
Get/Find(GET) \\
</th> \\
</tr> \\
<tr> \\
<td> \\
async function createTiny(data) { /* create(POST) */ <br />\\
let createResult = await app.service('${ServiceName}').create(data); <br />\\
console.log('createTiny function result: ' + JSON.stringify(createResult) ); <br />\\
return createResult; <br />\\
}; <br />\\
</td> \\
<td> \\
async function getTiny(id) { /* get(GET) (just one by index) */ <br />\\
let getResult = await app.service('${ServiceName}').get(id); <br />\\
console.log('getTiny function: ' + JSON.stringify(getResult) ); <br />\\
return getResult; <br />\\
}; <br />\\
</td> \\
</tr> \\
<tr> \\
<td> \\
async function patchTiny(id, data, params) { /* patch(PATCH) */ <br />\\
let patchResult = await app.service('${ServiceName}').patch(id, data, params); <br />\\
console.log('patchTiny function: ' + JSON.stringify(patchResult) ); <br />\\
return patchResult; <br />\\
}; <br />\\
</td> \\
<td> \\
async function findTiny(query) { /* find(GET) (get all or by query) */ <br />\\
console.log('Query function using: ' + JSON.stringify(query) ); <br />\\
let findResult = await app.service('${ServiceName}').find(query); <br />\\
console.log('findTiny function: ' + JSON.stringify(findResult) ); <br />\\
return findResult; <br />\\
}; <br />\\
</td> \\
</tr> \\
<tr> \\
<td colspan='2'> \\
async function removeTiny(id, params) { /* remove(DELETE) */ <br />\\
let removeResult = await app.service('${ServiceName}').remove(id, params); <br />\\
console.log('removeTiny function: ' + JSON.stringify(removeResult) ); <br />\\
return removeResult; <br />\\
}; <br />\\
</td> \\
</tr> \\
</table> \\
<br /><br /> \\
<table border='0' cellpadding='5'> \\
<tr> \\
<th colspan='2'> \\
Database Manipulation and Query Statements \\
</th> \\
</tr> \\
<tr> \\
<th> \\
Manipulation Statements \\
</th> \\
<th> \\
Query Statements \\
</th> \\
</tr> \\
<tr> \\
<td> \\
let createTinyData = { name: 'mark edwards', address: '123 Swallow Lane' } ; <br />\\
createTiny(createTinyData).then((value) => { /* create(POST) */ <br />\\
console.log('createTiny: ' + JSON.stringify(value)); <br />\\
}); <br />\\
</td> \\
<td> \\
let findTinyResultAll; <br />\\
findTiny(null).then((value) => { /* find(GET) (find all) */ <br />\\
findTinyResultAll = value; <br />\\
console.log('findTiny without query: ' + JSON.stringify(findTinyResultAll) ); <br />\\
}); <br />\\
</td> \\
</tr> \\
<tr> \\
<td> \\
let patchResult = null; let patchKey = 0; let patchData = {address: '5678 There Street!' }; <br />\\
patchTiny(patchKey, patchData ).then( value => { /* patch(PATCH) */ <br />\\
patchResult = value; <br />\\
console.log('patchResult: ' + JSON.stringify(patchResult)); <br />\\
}); <br />\\
</td> \\
<td> \\
let findTinyResultOne; let findTinyData = {query:{ id: 1}}; <br />\\
findTiny(findTinyData).then((value) => { /* find(GET) (with data) */ <br />\\
findTinyResultOne = value; <br />\\
console.log('findTiny with ' + findTinyData + ' query: ' + JSON.stringify(findTinyResultOne) ); <br />\\
}); <br />\\
</td> \\
</tr> \\
<tr> \\
<td> \\
let removeResult = null; let removeKey = 0; <br />\\
removeTiny(removeKey).then( value => { /* remove(DELETE) */ <br />\\
removeResult = value; <br />\\
console.log('removeResult(' + removeKey + ': ' + JSON.stringify(removeResult)); <br />\\
}); <br />\\
</td> \\
<td> \\
let getTinyResult = null; let getKey = 0; <br />\\
getTiny(getKey).then( value => { /* get(GET) */ <br />\\
getTinyResult = value; <br />\\
console.log('getTiny(' + getKey + ') : ' + JSON.stringify(getTinyResult) ); <br />\\
}); <br />\\
</td> \\
</tr> \\
</table> \\
</div> <br />\\
END_OF_SED_PUBLIC_HTML
sed --in-place --file=- ./src/models/${ServiceName}.model.js <<END_OF_SED_MODEL ;
/text: {/ {
N; /type: DataTypes.STRING,/ {
N; /allowNull: false/ {
N; /}/ {
s/text: {//; s/type: DataTypes.STRING,//; s/allowNull: false//; s/}//;a \
\\
\/* new items added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
name : { type: DataTypes.STRING, allowNull: false }, \\
address : { type: DataTypes.STRING, allowNull: true }, \\
\/* end of new items added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
}
}
}
}
/ }, {/a \
\\
\\
\/* added tableName by ${FileNameWithExtension} ${TimeStamp} *\/ \\
tableName : '${TableName}' , \\
\/* end of new items added by ${FileNameWithExtension} ${TimeStamp} *\/\\
\\
END_OF_SED_MODEL
## modify the sequelize.js to logging, and add a ssl-dialect true-false flag option
## note that mariadb(mysql) does not require a dialect change, and will be ignored for mariadb
sed --in-place --file=- ./src/sequelize.js <<END_OF_SED_SEQUELIZE ;
s?logging: false,?/*logging: false,*/ /* uncomment out 'logging:false in production -- added by ${FileNameWithExtension} ${TimeStamp} */?;
s?dialect: 'postgres',?dialect: 'postgres', dialectOptions: { ssl: ${PG_SSL_FLAG} }, /* added dialectOptions:{} ${FileNameWithExtension} ${TimeStamp} */?;
END_OF_SED_SEQUELIZE
if [ -z "${DOMAIN_NAME}" ]; then
URL_LINK=${PROTOCOL}://${THIS_IPADDR}:${FEATHERS_PORT} ;
SSL_NOTE='' ;
else
URL_LINK=${PROTOCOL}://${DOMAIN_NAME}:${FEATHERS_PORT} ;
SSL_NOTE="Please don't forget to generate your SSL by entering: sudo certbot certonly ; for domain: ${DOMAIN_NAME} " ;
fi
cat <<END_OF_SCRIPT;
Feathers google app should now be built. Please confirm your ./config/default.json values.
Connect string in your ./config/default.json values file:
$CONNECT_STRING
Now run ' cd ./${FileNameWithoutExtension}/ ; npm start; ' to try it out.
${SSL_NOTE}
then open ${URL_LINK} in your browser.
END_OF_SCRIPT
##