Skip to content

Instantly share code, notes, and snippets.

View mknparreira's full-sized avatar

Maikon Parreira mknparreira

View GitHub Profile
@mknparreira
mknparreira / postegre-auto-incremeting-primary-key-existing-table.md
Last active August 12, 2021 22:00
PostgreSQL | Add an auto-incrementing primary key to an existing table

MORDERN VERSIONS

   ALTER TABLE test1 ADD COLUMN id SERIAL PRIMARY KEY;

OLD VERSIONS

 ALTER TABLE test1 ADD COLUMN id INTEGER;
  CREATE SEQUENCE test_id_seq OWNED BY test1.id;
 ALTER TABLE test ALTER COLUMN id SET DEFAULT nextval('test_id_seq');
@mknparreira
mknparreira / terminal-discover-what-is-running-on-some-port.md
Last active August 12, 2021 22:01
Terminal | How to discover what is running on the some port

If you want to check if something is running at some port, you may run the following command::

Get-Process -Id (Get-NetTCPConnection -LocalPort 3306).OwningProcess

If an error is returned, then, there is nothing to run on the given port. Otherwise, it displays what the program/service is running.

@mknparreira
mknparreira / golang-understanding-pointers-once-and-for-all.md
Last active August 12, 2021 22:05
Golang | Understanding pointers once and for all
package main

import (
	"fmt"
)

func main() {
	
@mknparreira
mknparreira / golang-problem.md
Last active August 12, 2021 22:05
Golang | This is an simple exemple of Golang problem
package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, playground")
@mknparreira
mknparreira / php-array-pagination.md
Last active August 12, 2021 22:06
PHP | Emulate an pagination using arrays
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $yourDataArray ); //total items in array    
$limit = 20; //per page    
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset &lt; 0 ) $offset = 0;
@mknparreira
mknparreira / laravel-check-if-update-or-create-perfomed-update.md
Last active August 12, 2021 22:07
Laravel | check if updateOrCreate performed update
$tourist = Tourist::updateOrCreate([...]);

if(!$tourist->wasRecentlyCreated && $tourist->wasChanged()){
    // updateOrCreate performed an update
}

if(!$tourist->wasRecentlyCreated && !$tourist->wasChanged()){
    // updateOrCreate performed nothing, row did not change
}
@mknparreira
mknparreira / postegre-row-to-json-example.md
Last active August 12, 2021 22:08
PostegreSQL | row_to_json example
SELECT json_agg(row_to_json(ROW)) AS treeeeeee 
FROM (
   SELECT * FROM table_name					
) row
@mknparreira
mknparreira / postegre-row-to-json-with-nested-joins.md
Last active August 12, 2021 22:09
PostgreSQL | 9.2 row_to_json() with nested joins
SELECT
        json_build_object(
                'id', u.id,
                'name', u.name,
                'email', u.email,
                'user_role_id', u.user_role_id,
                'user_role', json_build_object(
 'id', ur.id,
@mknparreira
mknparreira / typescript-create-decorators.md
Last active August 12, 2021 22:10
Typescript | Simple way to create decorators with TypeScript

Don´t forget to set up experimentalDecorators as a true value into the compileOptions attribute in the tsconfig.json file.

Call this decorator before method as @decoratorName()

export function decoratorName(paramName : boolean = false) {

    return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {

        const originalMethod = descriptor.value;
 
@mknparreira
mknparreira / javascript-remove-object-array-of-objects.md
Last active August 12, 2021 22:12
Javascript | If you want to remove one object using only the id property

If you want to remove one object using only the id property

## I need to replace objects in arr1 with items from arr2 with same id


const apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];

var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37);