Skip to content

Instantly share code, notes, and snippets.

View hongphuc5497's full-sized avatar
🐧
Focusing

Hong Phuc hongphuc5497

🐧
Focusing
View GitHub Profile
def reverse_array(arr)
temp = []
lastIndex = arr.length - 1
arr.length.times do
temp << arr[lastIndex]
lastIndex -= 1
end
return temp
@hongphuc5497
hongphuc5497 / drop-multiple-datatbases-with-postgresql.md
Last active June 19, 2024 14:19
How to drop multiple databases in PostgreSQL
  1. Connect to PostgreSQL server via command line: psql -d postgre
  2. At the prompt, type SQL query to construct DROP DATABASE statement then that statement via \gexec meta-command
  3. Replace test with your desired pattern

Some alternative ways :

  • Anonymous code block
  • Using a shell script
SELECT 'DROP DATABASE ' || quote_ident(datname) || ';'
const SamplePromise = (index) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Promise ${index}`);
}, 1000);
});
};
const withoutPromiseAll = () => {
console.time('Without Promise.all');
function slowProduct(a, b) {
console.time('Execution time')
for (let i = 0; i <= 10000000; i++) { }
console.timeEnd('Execution time')
return console.log(a * b);
}
function memoizeValue(fnc, context) {
let res = {};
@hongphuc5497
hongphuc5497 / nginx.conf
Last active July 15, 2022 17:32
Nginx Reusable Conf
user www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 65535;
// Examples of Dependency Injection
// Example 1
class LoggerConstructorInjection {
constructor(config) {
this.config = config;
}
getConfig() {
return this.config;
@hongphuc5497
hongphuc5497 / encryption.js
Created August 16, 2022 08:36 — forked from vlucas/encryption.js
Stronger Encryption and Decryption in Node.js
'use strict';
const crypto = require('crypto');
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bits (32 characters)
const IV_LENGTH = 16; // For AES, this is always 16
function encrypt(text) {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY), iv);
@hongphuc5497
hongphuc5497 / buildAssocOpts.js
Created October 4, 2022 19:49
Reduce dot-delimeter string to level object with reduce
const db = EXAMPLE_DB;
const expand = "group.permissions.entities.nestedEntities.slug(searchValue)"
const buildAssocOptions = (str) => {
return str
.split('.')
.reverse()
.reduce((acc, val, index, arr) => {
let currentObj = {
model:
@hongphuc5497
hongphuc5497 / README.md
Created November 8, 2022 05:53 — forked from twolfson/README.md
Audit logging via sequelize

We prefer to have audit logging in our services that leverage databases. It gives us clarity into sources of where ACL issues might originate as well as gives us a general timeline of activity in our application.

Audit logging is tedious to set up so this gist contains our latest iteration of audit logging support for a sequelize based service.

@hongphuc5497
hongphuc5497 / wrapper.js
Created January 13, 2023 07:37
Build reusable Try Catch Wrapper in ExpressJS
/**
* It takes a function as an argument, and returns a function that will call the original function, and
* if it throws an error, it will call the next function in the middleware chain
* @param req - The request object
* @param res - The response object
* @param next - The next function in the middleware chain.
* @returns A function that takes a function as an argument and returns the result of that function.
*/
const wrapper = (req, res, next) => async (func) => {
try {