Skip to content

Instantly share code, notes, and snippets.

@roycechua
roycechua / nodeExportSample.js
Created July 19, 2022 17:57
Node API code snippet for exporting data as .csv
export const exportStudents = async (req, res, next) => {
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const studentRepository = AppDataSource.getRepository(Student);
const students = await studentRepository.find();
const csvWriter = createCsvWriter({
path: 'out.csv',
header: [
{id: 'id', title: 'id'},
{id: 'name', title: 'name'},
{id: 'birthday', title: 'birthday'},
@roycechua
roycechua / sample_controller.js
Last active July 19, 2022 17:56
Node API code snippet for saving data to database with Cloudinary image uploading
export const addStudent = async (req, res, next) => {
const form = formidable({});
form.parse(req, async (err, fields, files) => {
if (err) {
next(err);
return;
}
const newStudent = new Student()
newStudent.name = fields.name
newStudent.birthday = fields.birthday
@roycechua
roycechua / react-native+0.63.0.patch
Created September 28, 2021 15:38
Patch for React Native 0.63 and below for iOS 14 devices not showing images
diff --git a/node_modules/react-native/Libraries/Image/RCTUIImageViewAnimated.m b/node_modules/react-native/Libraries/Image/RCTUIImageViewAnimated.m
index 21f1a06..2444713 100644
--- a/node_modules/react-native/Libraries/Image/RCTUIImageViewAnimated.m
+++ b/node_modules/react-native/Libraries/Image/RCTUIImageViewAnimated.m
@@ -272,6 +272,9 @@ - (void)displayDidRefresh:(CADisplayLink *)displayLink
- (void)displayLayer:(CALayer *)layer
{
+ if (!_currentFrame) {
+ _currentFrame = self.image;
@roycechua
roycechua / websocketClientSample.js
Created April 15, 2021 10:05
Websocket Client Example using ws
const ws = require('ws');
const client = new ws('ws://localhost:81');
client.on('open', () => {
// Causes the server to print "Hello"
console.log('connection opened')
client.send('Hello');
});
@roycechua
roycechua / checkAxiosLatency.js
Created March 3, 2021 02:25
Check Axios Latency
// CHECKING LATENCY OR SPEED OF POST REQUEST TO SERVER
const axiosTiming = (instance) => {
instance.interceptors.request.use((request) => {
request.ts = Date.now();
return request;
});
instance.interceptors.response.use((response) => {
const timeInMs = `${Number(Date.now() - response.config.ts).toFixed()}ms`;
response.latency = timeInMs;
return response;
@roycechua
roycechua / RenderWebImageSample.java
Last active December 14, 2020 01:59
Java Render/Read an image from the web
BufferedImage img = ImageIO.read(new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Microsoft_Account.svg/1024px-Microsoft_Account.svg.png"));
@roycechua
roycechua / append_pdfs.py
Last active October 22, 2019 06:48
PyPDF2 code to append/Stitch two PDF files into one single file. This is just a proof of the idea but the more optimal thing to do would be to determine the total number of pages per file.
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import PageObject
writer = PdfFileWriter()
reader = PdfFileReader(open("1.2.1.13 Lab - Research Computer Components.pdf",'rb'))
for i in range(3):
page = reader.getPage(i)
writer.addPage(page)
sup_reader = PdfFileReader(open("Week1_Introduction to Machine Learning and Toolkit.pdf",'rb'))
@roycechua
roycechua / pymysql_sampleprogram.py
Last active August 3, 2019 02:53
A sample SQL Controller program using Python and PyMySQL
"""
These codes demonstrate an implementation of a class-based python sql controller object
Note: You need to have a MySQL Server installed either the stand-alone MySQL Server or through XAMPP to test these programs
You also need to run pip install pymysql if you don't have the module installed yet
There is no error handling in this sample.
"""
from pymysql_sqlcontroller import *
controller = SQLController(host="localhost",port=3306,db="mydb",user="root",password="")
@roycechua
roycechua / pymysql_insert.py
Last active August 3, 2019 03:51
Inserting records to a Python MySQL Database using Python PyMySQL
"""
These codes demonstrate how to perform a simple INSERT SQL Command on a Python program using PyMySQL
Note: You need to have a MySQL Server installed either the stand-alone MySQL Server or through XAMPP to test these programs
You also need to run pip install pymysql if you don't have the module installed yet
"""
import pymysql
# Connection
conn = pymysql.connect(host="localhost",port=3306,db="mydb",user="root",password="")
print("Connection established sucessfully")
@roycechua
roycechua / pymysql_update.py
Last active August 3, 2019 03:52
Updating records in MySQL database using Python PyMySQL
"""
These codes demonstrate how to perform a simple UPDATE/DELETE SQL Command on a Python program using PyMySQL
Note: You need to have a MySQL Server installed either the stand-alone MySQL Server or through XAMPP to test these programs
You also need to run pip install pymysql if you don't have the module installed yet
"""
import pymysql
# Connection
conn = pymysql.connect(host="localhost",port=3306,db="mydb",user="root",password="")
print("Connection established sucessfully")