load jquery (or any library) using chrome browser

7 days ago

// from https://releases.jquery.com/ ?? do we really need the https: part ???

[ ‘https://code.jquery.com/jquery-3.6.0.js’
].forEach( (src) => { let script = document.createElement(‘script’); script.src = src; script.async = false; document.head.appendChild(script);
});

Mark Edwards

,

---

manipulate css :root variable

14 days ago

to remove rule:

stylesheet.deleteRule(index),

(loop to find the right value)

==========================================================

// https://webplatform.github.io/docs/tutorials/Dynamic_style_-_manipulating_CSS_with_JavaScript/
// https://stackoverflow.com/questions/70251599/how-to-use-cssstylesheet-insertrule-to-change-a-root-property/71875353

to manipulate the “—” rules such as “—marksVariableName, it has to be done in the” selector and use insertRule:”

this should turn the background color from red to whatever value is in the hash.
Mark Edwards

,

---

javascript destructuring parameters

51 days ago

2022-04-01:

function tryMe({one, two, three}) { console.log(one, two,three) ; }

const tmp = { one: 1, two: 2, three: 3 } ;

tryMe(tmp) ;

=========================================

https://www.sitepoint.com/community/t/promises-feedback/384246/3

consider passing an JSON object for parms in the future:

function tester({param}) { console.log(JSON.stringify(param));
}
tester({param: { ‘parmOne’: 123, ‘paramTwo’: “two”}}) ;

===========

REPLACE:

function desiredCar(eachCar) { const make = eachCar.make; const model = eachCar.model; const hp = eachCar.hp; // then do stuff with make, model, and hp
}

WITH:

function desiredCar({make, model, hp}) {

}

=======

Mark Edwards

,

---

knex examples

153 days ago

knex examples to replace sequelize.

first create knex user:
DROP USER IF EXISTS ‘knexUser’@‘localhost’ ;
CREATE USER ‘knexUser’@‘localhost’ IDENTIFIED BY ‘knexPassword’;

GRANT ALL ON `comptonTransAnlys`.* TO ‘knexUser’@‘localhost’
IDENTIFIED BY ‘knexPassword’ WITH MAX_QUERIES_PER_HOUR 0
MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

GRANT ALL PRIVILEGES ON `comptonTransAnlys`.* TO ‘knexUser’@‘localhost’ ;

  1. mariadb —host=localhost —user=knexUser —password=knexPassword comptonTransAnlys ;

// https://zetcode.com/javascript/knex/

// mariadb —host=localhost —user=knexUser —password=knexPassword knexDb ;

const knexConnectOptions = { client: ‘mysql’, debug: true, connection: { host : ‘localhost’, port : 3306, user : ‘knexUser’, password : ‘knexPassword’, database : ‘knexDb’ }
};

const knex = require(‘knex’)(knexConnectOptions);

knex.raw(“SELECT VERSION”) .then ( (version) => console.log((version00)) ) .catch((err) => { console.log( err); throw err }) .finally(() => { knex.destroy(); });

knex.schema .createTable(‘cars’, (table) => { table.increments(‘id’) ; table.string(‘name’) ; table.integer(‘price’) ; }) .then(() => console.log(“table created”)) .catch((err) => { console.log(err); throw err }) .finally(() => { knex.destroy(); process.exit(); });

const cars = [ { name: ‘Audi’, price: 52642 }, { name: ‘Mercedes’, price: 57127 }, { name: ‘Skoda’, price: 9000 }, { name: ‘Volvo’, price: 29000 }, { name: ‘Bentley’, price: 350000 }, { name: ‘Citroen’, price: 21000 }, { name: ‘Hummer’, price: 41400 }, { name: ‘Volkswagen’, price: 21600 },
]

knex.from(‘cars’) // or just knex(‘cars’) .insert(cars) .then((id) => console.log(“data inserted: “ + id ) /* note display of returned ID! */ .catch((err) => { console.log(err); throw err }) .finally(() => { knex.destroy(); process.exit(); });

knex.from(‘cars’) // or just knex(‘cars’) .select(“*”) .where (true) // optional! .then((rows) => { for (row of rows) { console.log(`${row[‘id’]} ${row[‘name’]} ${row[‘price’]}`); } }).catch((err) => { console.log( err); throw err }) .finally(() => { knex.destroy(); process.exit(); });

knex.from(‘cars’) // or just knex(‘cars’) .select(“name”, “price”).where(‘price’, ‘>’, ‘50000’) .where (true) .then((rows) => { for (row of rows) { console.log(`${row[‘name’]} ${row[‘price’]}`); } }) .catch((err) => { console.log( err); throw err }) .finally(() => { knex.destroy(); process.exit(); });

// mariadb —host=localhost —user=knexUser —password=knexPassword knexDb ;

const knexConnectOptions = { client: ‘mysql’, debug: true, connection: ‘mysql://knexUser:knexPassword@localhost:3306/knexDb’
};

const knex = require(‘knex’)(knexConnectOptions);

knex(‘cars’) .where ({‘id’: 6 }) // OR .where (‘id’ , ‘=’, 6) .update({ name : ‘Toyota’}) /* notice {} */ .catch((err) => { console.log( err); throw err }) .finally(() => { knex.destroy(); process.exit(); });
// delete!
knex(‘cars’) .where (‘id’ , ‘=’, 6) .del() .catch((err) => { console.log( err); throw err }) .finally(() => { knex.destroy(); process.exit(); });

// short connection string:
const knexConnectOptions = { client: ‘mysql’, connection: ‘mysql://knexUser:knexPassword@localhost:3306/knexDb’
};

const knex = require(‘knex’)(knexConnectOptions);

knex.from(‘cars’) .select(“name”, “price”) .where(‘id’, ‘=’, ‘6’) .then((rows) => { for (row of rows) { console.log(`${row[‘name’]} ${row[‘price’]}`); } }) .catch((err) => { console.log( err); throw err }) .finally(() => { knex.destroy(); process.exit(); });

Mark Edwards

,

---

feathersjs SQL Raw Query

222 days ago

## K N E X:

1) npm install knex
2) create custom service “my-custom-service” type knex
3) change: src/services/my-custom-service/my-custom-service.class.js :
4) run DEBUG=* npm start ;

/* eslint-disable no-unused-vars */
const { QueryTypes } = require(‘feathers-knex’);

exports.MyCustomService = class MyCustomService { constructor (options, app) { this.options = options || {}; this.app = app; }

async find (key) { /* Postman GET: http://123.123.123.123:3030/my-custom-service/?one=twooo notice /?one=twoooo – last slash between service name and question mark is OPTIONAL! */ console.log(JSON.stringify(key.query) + ‘ in find routine!’); const knexClient = this.app.get(‘knexClient’); const rows = await knexClient.raw(“SELECT VERSION;”); return `${JSON.stringify(key.query) + JSON.stringify(rows)} find routine!`; }

async get (key) { /* Postman GET: http://123.123.123.123:3030/my-custom-service/123456789 notice /123456789 – one parameter as a suffix */ console.log(JSON.stringify(key) + ‘ in get routine!’); return `${JSON.stringify(key)} get routine!`; }
};

http://123.123.123.123:3030/my-custom-service/1234 # get
http://123.123.123.123:3030/my-custom-service/?one=twoooooo # find

## S E Q U E L I Z E:

0) npm install sequelize

1) create sequelize CUSTOM SERVICE named: “my-custom-service”

2) edit ./src/sequelize.js change logging false to logging true

3) change: src/services/my-custom-service/my-custom-service.class.js :

/* eslint-disable no-unused-vars */
const { QueryTypes } = require(‘sequelize’);

exports.MyCustomService = class MyCustomService { constructor (options, app) { this.options = options || {}; this.app = app; }

async find (key) { const sequelize = this.app.get(‘sequelizeClient’); const stimwordPositionSelectFind = ‘SELECT * FROM stimwordPosition WHERE 1 AND stimwordPositionWord = $stimwordPositionWord;’ ; const stimwordPositionSelectParms = { ‘stimwordPositionWord’ : JSON.parse(key.query).stimwordPositionWord } ; const rows = await sequelize.query ( stimwordPositionSelectFind , { ‘bind’ : stimwordPositionSelectParms } ); return rows; }
};

################################ browser-console:

in browser console (common initial setup for all examples)

first visit http://123.123.123.123:3030/ to “initialize” chrome

[ ‘//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js’
, ‘//unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js’
].forEach( (src) => { let script = document.createElement(‘script’); script.src = src; script.async = false; document.head.appendChild(script);
});
const app = feathers(); const socket = io(); app.configure(feathers.socketio(socket));

// optional if trying to run on another http[s] webpage rather than at http://123.123.123.123:3030:

const socket = io.connect ( ’35.192.14.166:3030’ , { ‘path’ : ‘/socket.io/’ , ‘port’ : ‘3030’ , ‘hostname’ : ’35.192.14.166’ , ‘secure’ : false } );

// socket.io.engine.transport

async function stimwordPositionFind(param) { console.log( ‘param: ‘ + param); let getResult = await app.service(‘my-custom-service’).find(param); console.log(‘stimwordPosition function: ‘ + JSON.stringify(getResult) ); return getResult;
};
let stimwordPositionResult = null;
let key = { ‘query’ : JSON.stringify( { ‘stimwordPositionWord’ : ‘horse’, ‘parm2’ : ‘answer2’ } ) } ;
stimwordPositionFind(key).then( value => { /* get(GET) */ stimwordPositionResult = value; console.log(‘stimwordPosition(’ + key + ‘) : ‘ + JSON.stringify(stimwordPositionResult) );
});

async function stimwordPosition(id) { /* get(GET) (just one by index) */ console.log( ‘id: ‘ + id); let getResult = await app.service(‘my-custom-service’).find(id); console.log(‘stimwordPosition function: ‘ + JSON.stringify(getResult) ); return getResult;
};

############ GET GET GET GET GET GET

async get (id) { const sequelize = this.app.get(‘sequelizeClient’); const stimwordPositionSelectGet = `SELECT * FROM stimwordPosition WHERE 1 AND stimwordPositionAutoIncr = ${id};`; const rows = await sequelize.query(stimwordPositionSelectGet); return rows; }

async function stimwordPosition(id) { /* get(GET) (just one by index) */ console.log( ‘id: ‘ + id); let getResult = await app.service(‘my-custom-service’).get(id); console.log(‘stimwordPosition function: ‘ + JSON.stringify(getResult) ); return getResult;
};
let stimwordPositionResult = null; let key = 1; // assuming your row ID is one
stimwordPosition(key).then( value => { /* get(GET) */ stimwordPositionResult = value; console.log(‘stimwordPosition(’ + key + ‘) : ‘ + JSON.stringify(stimwordPositionResult) );
});
#####################################################

let stimwordPositionResult = null; let key = 1; // assuming your row ID is one
stimwordPosition(key).then( value => { /* get(GET) */ stimwordPositionResult = value; console.log(‘stimwordPosition(’ + key + ‘) : ‘ + JSON.stringify(stimwordPositionResult) );
});

/notice object starting with “query” for the find: let stimwordPositionResult = null; let key = { ‘query’ : ‘horse’ } ; // assuming your row ID is one stimwordPositionFind(key).then( value => { /* get(GET) */ stimwordPositionResult = value; console.log(‘stimwordPosition(’ + key + ‘) : ‘ + JSON.stringify(stimwordPositionResult) ); });
Mark Edwards

,

---

« Older

Manage