Skip to content

Instantly share code, notes, and snippets.

View warrenlalata's full-sized avatar
🏠
Working from home

Warren Lalata warrenlalata

🏠
Working from home
View GitHub Profile
numeric only
/^\d+$/
alphanumeric, dash and underscore with space
/^[a-z\d\-_\s]+$/i
detect non alphanumeric chars
/[^a-zA-Z\d]/
@warrenlalata
warrenlalata / App.js
Created July 19, 2020 12:43
focus on next rn input
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
Platform,
Button,
ScrollView,
InputAccessoryView,
KeyboardAvoidingView,
@warrenlalata
warrenlalata / App.js
Created July 19, 2020 12:40
KeyboardAvoidingView + React Navigation v5
import {
HeaderHeightContext
} from "@react-navigation/stack"; <
HeaderHeightContext.Consumer > {
headerHeight => ( <
KeyboardAvoidingView {
...(Platform.OS === "ios" ? {
behavior: "padding"
} : {})
}
gem "devise_custom_authenticatable"
@warrenlalata
warrenlalata / index.js
Created February 6, 2020 03:57
Mask String with Thousand Separator
/**
* Masks Input with thousand separator
*
* @param {string} amount - Amount
* @return {string} Masked Amount
*
* @example
* formatAmount("1000") // 1,000
*/
export function formatAmount(amount) {
Open psql terminal
psql -U postgres -h localhost
Create database
create database dbname;
List databases
\l
Create user to work with db create
@warrenlalata
warrenlalata / CheckboxGroup.js
Last active June 11, 2022 22:50
React Checkbox group with MUI
import React, { Component } from "react";
import { Field } from "redux-form";
import PropTypes from "prop-types";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Checkbox from "@material-ui/core/Checkbox";
export default class CheckboxGroup extends Component {
static propTypes = {
options: PropTypes.arrayOf(
PropTypes.shape({
knex init
edit knexfile.js
in migration object, add directory property:
with 'db/migrations'
@warrenlalata
warrenlalata / .env.sample
Last active June 3, 2019 09:14
Steps to configure Sequelize ORM to work with Node Backend
# Development
DEV_DB_USERNAME=root
DEV_DB_PASSWORD=yourdbpassword
DEV_DB_NAME=apollo_development
DEV_DB_HOSTNAME=localhost
# Test
TEST_DB_USERNAME=root
TEST_DB_PASSWORD=yourdbpassword

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example