Complete feathers in console

Nov 21, 05:25 AM

Instructions to run feathers completely from console, no coding!

IMPORTANT: open up a new tab “about:blank”


Click to open a new about:blank page.

Load the libraries

/* load in libraries */
[ 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'
, 'https://unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js'
].forEach( (src) => {
    let script = document.createElement('script'); 
            // script.setAttribute('crossOrigin',  'anonymous' );  /* optional  */
    script.src =  src;
            // script.type = 'javascript';   /*optional  */
    script.src = src; 
    script.async = false;
    document.head.appendChild(script); // or  document.getElementsByTagName('script')[0];
});
alternative method:
/usr/bin/cat <<END  | ncat --listen 80 ;
<!DOCTYPE html>
<html lang="en-us">
<head>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js'></script>
  <script src='https://unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js'></script>
</head>
<body>
  fake webserver page.  include extra jScript code in the console.
</body>
</html>
END

initialize constants

/* initialize constants */
const app = feathers();
const socket = io();   /*  defaults to:    const socket = io('ws://localhost:3030');    */
const socket = io('ws://###.###.###.###:3030');   /*if we need to connect to different host */
app.configure(feathers.socketio(socket));     /* PLEASE dont skip this one!!  */
/*   -- or --   */
app.configure(feathers.socketio(io('ws://###.###.###.###:3030')));  /* just combine the last two lines */

create(POST)

/* create(POST) */
async function createTiny(name, address) {
let createResult
      = await app.service(‘tiny-connect’).create({ name: name, address: address });
    console.log(‘createTiny function result: ‘ + JSON.stringify(createResult) );
    return createResult;
};

createTiny(‘Mark Edwards’, ’123 Swallow Lane’).then((value) => { /* create(POST) */
    console.log(‘createTiny: ‘ + JSON.stringify(value));
})

get(GET) just one using the primary index

/* get(GET) (just one by index) */ 

async function getTiny(id) {   return await app.service(‘tiny-connect’).get(id);   };
—- or —-
async function getTiny(id) {      
  let getResult = await app.service(‘tiny-connect’).get(id); 
  console.log(‘getTiny function: ‘ + JSON.stringify(getResult) ); 
  return getResult; 
}; 
let getTinyResult = null; let key = 1;   // assuming your row ID is one
getTiny(key).then( value => {      /* get(GET) */ 
  getTinyResult = value; 
  console.log(‘getTiny(’ + key + ‘) : ‘ + JSON.stringify(getTinyResult) ); 
}); 
/* one line test:  */
(async () => { try { let result = await app.service(‘stimword’).get(1) ; console.log(‘the result is: ‘ + JSON.stringify(result)) } catch (e) { console.log(e); }  } )() ;
/* or:  */
(async () => { let logoutResult = await app.service(‘stimword’).get(2); console.log(logoutResult); })().catch(e => { console.log(e) });

find(GET) all or by query

 /* find(GET) (get all or by query) */ 

async function findTiny(query) { return await app.service(‘tiny-connect’).find(query);
};
async function findTiny(query) { console.log(‘Query function using: ‘ + JSON.stringify(query) ); let findResult = await app.service(‘tiny-connect’).find(query); console.log(‘findTiny function: ‘ + JSON.stringify(findResult) ); return findResult;
};
let findTinyResultAll;
findTiny(null).then((value) => { /* find(GET) (find all) */ findTinyResultAll = value; console.log(‘findTiny without query: ‘ + JSON.stringify(findTinyResultAll) );
});

let findTinyResultOne; let findDataObject = { ‘query’ :{ id: 0}}; // again, assuming your id is zero! findTiny(findDataObject).then((value) => { /* find(GET) (with data ) */ findTinyResultOne = value; console.log(‘findTiny with query: ‘ + JSON.stringify(findTinyResultOne) ); }); /* shortcut examples: */ (async () => { try { let result = await app.service(‘stimword’).find() ; console.log(‘the result is: ‘ + JSON.stringify(result)) } catch (e) { console.log(e); } } ) () ; (async () => { try { let result = await app.service(‘stimword’).find({‘query’:{‘stimwordWord’:‘horse’}}) ; console.log(‘the result is: ‘ + JSON.stringify(result)) } catch (e) { console.log(e); } } ) () ;

Accessing a KNEX RAW selecxt

     SELECT    JSON_ARRAYAGG(
                  JSON_OBJECT(
                      ..........
                  )
                )  'JSON_ARRAYAGG'
  .....................................
const result = await knex.raw( sqlStatement, sqlQuery);
return result[0][0].JSON_ARRAYAGG   ;
...................................
var newResult;
(async () => { try { let result = await  app.service('raw-service').find({query: sqlQuery}) ; newResult = result; } catch (e) { console.log(e); }  } )() ;
console.log(JSON.parse(newResult));

patch(PATCH) to update row(s)

/* patch(PATCH) */ 

async function patchTiny(id, data, params) { /* patch(PATCH) */ let patchResult = await app.service(‘tiny-connect’).patch(id, data, params); console.log(‘patchTiny function: ‘ + JSON.stringify(patchResult) ); return patchResult;
};

let patchResult = null; let patchKey = 0; let patchData = {address: ’5678 There Street!’ }; patchTiny(patchKey, patchData ).then( value => { /* patch(PATCH) */ patchResult = value; console.log(‘patchResult: ‘ + JSON.stringify(patchResult)); });

combine create and insert into one promise

async function clientMasterCreate(query) {    
  return await app.service(‘client-master’).create(query); 
};

async function clientMasterFind(query) { return await app.service(‘client-master’).find( { ‘query’ : query } );
};
clientMasterObj = { ‘layoutName’ : ‘PESL’ , ‘teacherEmail’ : ‘info@englishwithoutaccent.com’ , ‘teacherAutoIncr’ : 385 , ‘clientMasterEmail’ : ‘12yukos@gmail.comX’ };
clientMasterFind(clientMasterObj) .then( (clientMasterRow) => { return clientMasterRow }) .then( (clientMasterRow) => { if ( clientMasterRow.data.length == 0 ) { return clientMasterCreate(clientMasterObj) .then( (clientMasterRow) => { console.log(‘inserting: ‘ + clientMasterRow.clientMasterAutoIncr ); return clientMasterRow.clientMasterAutoIncr; }) } else { return clientMasterRow.data[clientMasterRow.data.length-1].clientMasterAutoIncr; } }) .then ((result) => { console.log(‘clientMasterAutoIncr: ‘ + result)

});
Mark Edwards

,

---

Commenting is closed for this article.

---