-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsgrid-sequelize.bsh
751 lines (590 loc) · 25.2 KB
/
jsgrid-sequelize.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#! /bin/bash
## jsgrid-sequelize.bsh last update: 2019-02-13
## Bash-shell script to create a Google-jsgrid (mariadb or cockroachdb) example entirely from scratch.
## 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 the feathers install gist first!
MariaDB='MariaDB' ;
CockroachDB='CockroachDB' ;
TYPE=CockroachDB ; ## SET THIS LINE FOR YOUR DATABASE CHOICE!
## vars for ./config/default.json changes -- you may need to double-check the values here.
## google credentials
HOST='fastfeathers.site' ;
GOOGLE_KEY='81XXXXXXX56-dXXXXXXXXXXXXXXXXXXXXXXaii7rjqp9.apps.googleusercontent.com' ;
GOOGLE_SECRET='BLXXXXXXXXXXXXXXXXXXXX7g' ;
### get your credentials here: https://console.developers.google.com/apis/credentials/
## database credentials
DB_USER='database-username' ;
DB_PASS='database-password' ;
DB_NAME='database-name' ;
DB_SERVER_IP='XXX.XXX.XXX.XXX' ;
## 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.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 HOST or blank to use '$HOST': " tmpHost;
if [ ! -z "$tmpHost" ]; then
HOST=$tmpHost;
fi;
read -p "Enter GOOGLE_KEYor blank to use '$GOOGLE_KEY': " tmpGOOGLE_KEY;
if [ ! -z "$tmpGOOGLE_KEY" ]; then
GOOGLE_KEY=$tmpGOOGLE_KEY;
fi;
read -p "Enter GOOGLE_SECRET or blank to use '$GOOGLE_SECRET': " tmpGOOGLE_SECRET;
if [ ! -z "$tmpGOOGLE_SECRET" ]; then
GOOGLE_SECRET=$tmpGOOGLE_SECRET;
fi;
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:aaaaaa@35.196.161.183:26257/bank
## mysql example: mysql://feathersuser:aaaaaa@167.99.13.123:3306/bank
THIS_IPADDR=$(dig +short myip.opendns.com @resolver1.opendns.com. ;);
export HOST;
DOMAIN_IP=$(dig ${HOST} | grep "$(printf '\t')IN$(printf '\t')A$(printf '\t')" | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])');
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 CONNECT_STRING;
## feathers generate authentication;
expect <(cat <<'END_OF_GENERATE_AUTHENTICATION'
# generates an authentication for Google
# written from https://docs.feathersjs.com/guides/chat/authentication.html
set timeout -1
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
spawn feathers generate authentication ;
expect -re ".*What authentication strategies do you want to use\?.*"
##send -- "${RETURN}"
send -- " ${DOWNARROW}${DOWNARROW}${SPACE}${RETURN}"
## return defaults to Local Storage
## SPACE, downarrow (twice) SPACE to select Google
expect -re ".*What is the name of the user \\\(entity\\\) service.*"
send -- "${RETURN}"
## default answer is 'users'
expect -re ".*What kind of service is it\?.*"
send -- "${DOWNARROW}${DOWNARROW}${DOWNARROW}${RETURN}"
## downarrow(3) for sequelize
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_AUTHENTICATION
) ## end of feathers generate authentication
## feathers generate hook;
expect <(cat <<'END_OF_GENERATE_USER_HOOK'
# generates a "BEFORE--CREATE/UPDATE" hook named "user-hook" for for the "users" service for using Google
# written from https://docs.feathersjs.com/api/authentication/oauth2.html#customizing-the-oauth-response
set timeout -1
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
spawn feathers generate hook ;
expect -re ".*What is the name of the hook\?.*"
send -- "user-hook${RETURN}"
expect -re "What kind of hook should it be\?"
send -- "${DOWNARROW}${RETURN}"
## downarrow return selects 'before'
expect -re "What service\\\(s\\\) should this hook be for \\\(select none to add it yourself\\\)\?"
send -- "${DOWNARROW}${SPACE}${RETURN}"
## downarrow space return selects 'users'
expect -re "What methods should the hook be for \\\(select none to add it yourself\\\)\?"
send -- "${DOWNARROW}${DOWNARROW}${DOWNARROW}${SPACE}${DOWNARROW}${SPACE}${RETURN}"
## downarrow (3) space downarrow space selects 'create' and 'update'
expect eof
END_OF_GENERATE_USER_HOOK
) ## end of feathers generate hook user-hook
## edit the user-hook.js to extract the custom login stuff, like username, first-name, etc
sed --in-place --file=- ./src/hooks/user-hook.js <<END_OF_SED_USER_HOOK ;
3i\
\\
\\
\/* code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
const logger = require('winston');\\
const _ = require('lodash');\\
\/* end of code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
/^ return async context => {$/a\
\/* code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
if ( _.has(context, 'data.google.profile') ){\\
if ( _.has(context.data.google.profile, 'displayName' ) ) {\\
context.data.displayName = context.data.google.profile.displayName;\\
logger.info('displayName: ' + context.data.displayName); \\
}\\
if ( _.has(context.data.google.profile, 'emails' ) ) {\\
context.data.email = context.data.google.profile.emails.find(email => email.value).value;\\
logger.info('email: ' + context.data.email);\\
}\\
if ( _.has(context.data.google.profile, 'name' ) ) {\\
['familyName', 'givenName'].forEach( (val) => {\\
if ( _.has(context.data.google.profile.name, val ) ) {\\
context.data[val] = context.data.google.profile.name[val];\\
logger.info(val + ': ' + context.data[val]);\\
}\\
})\\
}\\
} // end if has data.google.profile\\
\/* end of code added by ${FileNameWithExtension} ${TimeStamp} *\/\\
END_OF_SED_USER_HOOK
sed --in-place --file=- ./src/models/users.model.js <<END_OF_SED_USER_MODEL ;
/ googleId: { type: Sequelize.STRING },/ a\
\\
\/* data items added by ${FileNameWithExtension} ${TimeStamp} *\/\\
email: { type: DataTypes.STRING }, \\
displayName: { type: DataTypes.STRING },\\
familyName: { type: DataTypes.STRING },\\
givenName: { type: DataTypes.STRING },\\
\/* end of data items added by ${FileNameWithExtension} ${TimeStamp} *\/\\
END_OF_SED_USER_MODEL
## 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 timeout -1
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
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 -- "testtable${RETURN}"
## testtable
expect -re ".*Which path should the service be registered on\?.*"
send -- "${RETURN}"
## defaults to /testtable
expect -re ".*Does the service require authentication\?.*"
send -- "y${RETURN}"
## y for 'yes' and return
expect -re ".*Which database are you connecting to\?.*"
send -- "$env(ARROW_DIRECTION)${RETURN}"
## downarrow for postgres(cockroachdb), uparrow for mariadb
expect eof
END_OF_GENERATE_SERVICE
) ## end of feathers generate service
sed --in-place --file=- ./src/models/testtable.model.js <<END_OF_SED_TESTTABLE_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 }, \\
addr : { type: DataTypes.STRING, allowNull: true }, \\
updatedBy: { type: DataTypes.STRING, allowNull: false }, \\
\/* end of new items added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
}
}
}
}
END_OF_SED_TESTTABLE_MODEL
## feathers generate hook;
expect <(cat <<'END_OF_GENERATE_TESTTABLE_HOOK'
# generates a "BEFORE--CREATE/UPDATE" hook named "testtable-hook" for the "testtable" service for Google
# written from https://docs.feathersjs.com/api/authentication/oauth2.html#customizing-the-oauth-response
set timeout -1
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
spawn feathers generate hook ;
expect -re ".*What is the name of the hook\?.*"
send -- "testtable-hook${RETURN}"
expect -re "What kind of hook should it be\?"
send -- "${DOWNARROW}${RETURN}"
## downarrow return selects 'before'
expect -re "What service\\\(s\\\) should this hook be for \\\(select none to add it yourself\\\)\?"
send -- "${DOWNARROW}${SPACE}${RETURN}"
## downarrow space return selects 'testtable'
expect -re "What methods should the hook be for \\\(select none to add it yourself\\\)\?"
send -- "${DOWNARROW}${DOWNARROW}${DOWNARROW}${SPACE}${DOWNARROW}${SPACE}${RETURN}"
## downarrow (3) space downarrow space selects 'create' & 'update'
expect eof
END_OF_GENERATE_TESTTABLE_HOOK
) ## end of feathers generate hook 'testtable-hook'
## install the feathers-hooks-common npm module
npm install feathers-hooks-common --save ;
## add in a "custom hook" to remove the id during create (db handles the id for us)
## https://auk.docs.feathersjs.com/api/hooks-common.html#discard
sed --in-place --file=- ./src/services/testtable/testtable.hooks.js <<END_OF_SED_TESTTABLE_HOOK_COMMON ;
3s?^?\\nconst { discard } = require('feathers-hooks-common'); // added by bash script ${FileNameWithExtension} ${TimeStamp}\\n\\n?;
s?create: \[testtableHook()],?create: [ discard('id'), testtableHook()], // "added discard('id')" by bash script ${FileNameWithExtension} ${TimeStamp}?;
END_OF_SED_TESTTABLE_HOOK_COMMON
## modify hook to add in the user email into the context variable
sed --in-place --file=- ./src/hooks/testtable-hook.js <<END_OF_SED_TESTTABLE_HOOK ;
3s?^?\\nconst logger = require('winston'); // added by bash script ${FileNameWithExtension} ${TimeStamp}\\n\\n?;
/^ return async context => {$/a\
\/* code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
//logger.info(JSON.stringify(context));\\
context.data.updatedBy = context.params.user.email; // save the user email into the context object ${FileNameWithExtension} ${TimeStamp} \\
\/* end of code added by ${FileNameWithExtension} ${TimeStamp} *\/\\
END_OF_SED_TESTTABLE_HOOK
## 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
## modify the default.json file if you are using google (you will have to change these values!)
## values came from https://console.developers.google.com/apis/api under credidentials
sed --in-place --file=- ./config/default.json << END_OF_SED_CONFIG_DEFAULT ;
s/"host": "localhost",/"host": "${HOST}",/;
s/<google oauth key>/${GOOGLE_KEY}/;
s/"secret": "<google oauth secret>"/"secret": "${GOOGLE_SECRET}",\n" scope": [ "profile openid email"]/;
END_OF_SED_CONFIG_DEFAULT
cat > ./public/index.html << END_OF_CAT_INDEX_HTML ;
<html>
<head>
<link rel="stylesheet" type="text/css" href="demos.css" />
<link href='//fonts.googleapis.com/css?family=Open+Sans:300,600,400' rel='stylesheet' type='text/css' />
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
</head>
<body>
<main class="container">
<h2 class="center-text">A REST and realtime API layer for modern applications. - using <a href="${DB_URL}" target="_blank">${TYPE}</a></h2>
<p class="center-text"><br/>
<span id='login'><a href="/oauth/connect/google">Login With Google</a></span>
<span id='logout'><a href="javascript: feathersClient.logout(); Cookies.remove('feathers-jwt'); window.location.reload(true);">Log Out Of Google</a></span>
Adapted from <a href='http://js-grid.com/demos/' target='_blank'>js-grid</a>
</p>
</div>
<div id="jsGrid"></div>
<script src="//unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js"></script>
<script src="//unpkg.com/socket.io-client@1.7.3/dist/socket.io.js"></script>
<script src="app.js"></script>
<footer>
<p class="center-text">For more information on Feathers see <a href="https://docs.feathersjs.com" title="Feathers Documentation" target="blank">docs.feathersjs.com</a>.</p>
</footer>
</main>
</html>
END_OF_CAT_INDEX_HTML
## css is identical to the js-grid example
cat > ./public/demos.css <<END_OF_CAT_DEMOS_CSS ;
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
}
body {
height: 100%;
padding: 10px;
color: #262626;
font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
font-size: 14px;
font-weight: 300;
}
h1 {
margin: 0 0 8px 0;
font-size: 24px;
font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
font-weight: 300;
}
h2 {
margin: 16px 0 8px 0;
font-size: 18px;
font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
font-weight: 300;
}
ul {
list-style: none;
}
a {
color: #2ba6cb;
text-decoration: none;
}
a:hover {
text-decoration: underline;
color: #258faf;
}
input, button, select {
font-family: 'Helvetica Neue Light', 'Open Sans', Helvetica;
font-weight: 300;
font-size: 14px;
padding: 2px;
}
.navigation {
width: 200px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
padding: 10px;
border-right: 1px solid #e9e9e9;
}
.navigation li {
margin: 10px 0;
}
.demo-frame {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 200px;
}
iframe[name='demo'] {
display: block;
width: 100%;
height: 100%;
border: none;
}
END_OF_CAT_DEMOS_CSS
cat > ./public/app.js << END_OF_CAT_APP_JS ;
'use strict'
// Socket.io is exposed as the \`io\` global.
var socket = io(':3030', {transports: ['websocket']});
// feathers-client is exposed as the \`feathers\` global.
var feathersClient = feathers()
.configure(feathers.socketio(socket))
.configure(feathers.authentication({
cookie: 'feathers-jwt'
}));
jQuery('span#login').show(); jQuery('span#logout').hide();
feathersClient.authenticate()
.then(response => {
console.info('Feathers Client has Authenticated with the JWT access token!');
console.log(response);
jQuery('span#login').hide(); jQuery('span#logout').show();
createAndPopulateGrid();
})
.catch(error => {
console.info("We have not logged in with OAuth, yet. This means there's no cookie storing the accessToken. As a result, feathersClient.authenticate() failed.");
console.log(error);
});
class timingSwitch {
constructor (switchName) {
this.thisKey = null ;
}
setKeyValue (key) {
this.thisKey = key;
}
getKeyValue () {
return this.thisKey;
}
}
var insertObj = new timingSwitch() ;
var updateObj = new timingSwitch() ;
var deleteObj = new timingSwitch() ;
feathersClient.service('testtable').on('created', (message, context) => {
if ( message.id !== insertObj.getKeyValue() ) {
insertObj.setKeyValue(message.id);
jQuery('div#jsGrid').jsGrid
( 'insertItem'
, message
).done( () => {
console.log("feathers created event - insert completed");
});
} else {
console.log('feathers created event id keys equal - skipping.');
}
})
feathersClient.service('testtable').on('updated', (message, context) => {
for ( var _gridRow=0; _gridRow < jQuery('div#jsGrid').jsGrid('option', 'data').length ; _gridRow++ )
{
if ( jQuery('div#jsGrid').jsGrid('option', 'data')[_gridRow]['id'].toString() === message.id.toString() ) {
// is this the correct jsgrid row?
if ( message.id !== updateObj.getKeyValue() ) { // is this update from some other browser window?
updateObj.setKeyValue(message.id); // save this message.id so we dont process it again.
jQuery('div#jsGrid').jsGrid
( 'updateItem'
, jQuery("div#jsGrid").jsGrid('option', 'data')[_gridRow]
, message
).done( () => {
console.log("update completed");
});
break;
} // end if this update is from some other browser window
} // end if does this row match what we are looking for?
} // end for loop of all grid items
})
feathersClient.service('testtable').on('removed', (message, context) => {
for ( var _gridRow=0; _gridRow < jQuery('div#jsGrid').jsGrid('option', 'data').length ; _gridRow++ )
{
if ( jQuery('div#jsGrid').jsGrid('option', 'data')[_gridRow]['id'].toString() === message.id.toString() ) {
// is this the correct jsgrid row?
if ( message.id !== deleteObj.getKeyValue() ) { // is this delete from some other browser window?
deleteObj.setKeyValue(message.id); // save this message.id so we dont try to delete it again
jQuery('div#jsGrid').jsGrid({confirmDeleting:false}); // temporarily turn OFF prompting
jQuery('div#jsGrid').jsGrid
( 'deleteItem'
, jQuery("div#jsGrid").jsGrid('option', 'data')[_gridRow]
).done( () => {
console.log("delete completed");
jQuery('div#jsGrid').jsGrid({confirmDeleting:true}); // turn prompting back on
});
break;
} // end if this delete is from some other browser window
}
}
})
async function createAndPopulateGrid() {
jQuery("div#jsGrid").jsGrid({
height: "70%",
width: "100%",
filtering: true,
editing: true,
inserting: true,
sorting: true,
paging: true,
autoload: true,
pageSize: 15,
pageButtonCount: 5,
deleteConfirm: "Do you really want to delete?",
fields: [
{ name: "name" , type: "text", width: 150, validate: "required" },
{ name: "addr" , type: "text", width: 200, validate: "required" },
{ name: "updatedBy" , type: "text", width: 200, title: 'Updated By (ReadOnly)' , readOnly: true },
{ name: "id" , type: "text", width: 100, title: 'Id (ReadOnly)' , readOnly: true },
{ type: "control" }
],
controller:
{ insertItem: async (item) => {
/* when an item is inserted, we need to determine if this browser window is inserting,
* or a feathers create event is doing the inserting. if feathers-create-event is
* inserting, we let this jsGrid-controller-insertItem work normally
*/
if ( item.id !== insertObj.getKeyValue() ) { // feathers-create-event sets this flag
/* This means that our browser window is doing the inserting, NOT
* feathers-create-event. In this case, we tell feathers to first insert
* it into the back-end, and skip inserting it for now.
* A microsecond or two later, the feathers-create-event will fire
* and handle the insert. This way, the insert also includes the
* id number.
*/
feathersClient.service('testtable').create(item).then( ()=> {
// clear out the jsGrid insert row
jQuery('div#jsGrid').jsGrid('clearInsert');
})
/* Prevent js-grid from doing any actual updating.
* Remember, feathers will tell js-grid to update once feathers has completed updating.
*/
jQuery("div#jsGrid").jsGrid('insertFailed') = true;
} else {
console.log('tripped by jsgrid-controller-insertItem since the two key values are equal.');
return item;
}
},
updateItem: async (item) => {
if ( item.id !== updateObj.getKeyValue() ) {
feathersClient.service('testtable').update(item.id, item).then( () => {
return false;
});
} else {
console.log('in jsgrid controller updateItem: two key values are equal, skipping.');
updateObj.setKeyValue(null); // reset since feathers since feathers has completed updating.
return item;
}
},
deleteItem: async (item) => {
if ( item.id !== deleteObj.getKeyValue() ) {
feathersClient.service('testtable').remove(item.id).then( () => {
return false;
});
} else {
console.log('in jsgrid controller deleteItem: two key values are equal, skipping.');
}
},
},
});
await feathersClient.service('testtable').find()
.then ( (serviceRow) => {
console.log(serviceRow);
if ( serviceRow.hasOwnProperty('data') && serviceRow.data.length ) {
serviceRow.data.forEach( (element) => {
insertObj.setKeyValue(element.id); // block the feathers event from firing
jQuery('div#jsGrid').jsGrid
( 'insertItem'
, element
).done( () => {
console.log("jsGrid insertItem - initial insert completed");
});
})
}
});
}
END_OF_CAT_APP_JS
dig ${HOST} | grep 'A';
cat <<END_OF_SCRIPT;
Host: ${HOST} points to ${DOMAIN_IP}
Does ${HOST} resolve to ${THIS_IPADDR} ?
Please dont forget to change/check the IP ## in both the ./config/default.json
*AND* the google-applications page (make sure they match)
Feathers google app should now be built. Please confirm your ./config/default.json values.
Client id in your ./config/default.json values file: ${GOOGLE_KEY}
Client secret in your ./config/default.json values file: ${GOOGLE_SECRET}
Connect string in your ./config/default.json values file:
$CONNECT_STRING
Now run ' cd ./${FileNameWithoutExtension}/ ; npm start; ' to try it out.
END_OF_SCRIPT
##
##