Skip to content

Instantly share code, notes, and snippets.

View willikay11's full-sized avatar

William Kamuyu willikay11

View GitHub Profile
@willikay11
willikay11 / paginate.js
Created July 30, 2020 04:53
Paginate the data from any mode
export const paginate = async (model, pageSize, pageLimit, search = {}, order = [], transform) => {
try {
const limit = parseInt(pageLimit, 10) || 10;
const page = parseInt(pageSize, 10) || 1;
// create an options object
let options = {
offset: getOffset(page, limit),
limit: limit,
};
@willikay11
willikay11 / product.controller.js
Last active July 30, 2020 04:39
List products method in product.controller.js
import { Op } from 'sequelize';
import ProductModel from './product.model';
/*
* List products
*/
export const listProducts = async(req, res) => {
try {
// get the query params
const { q, page, limit, order_by, order_direction } = req.query;
@willikay11
willikay11 / server.js
Last active July 29, 2020 12:07
call connect sequelize orm to db method when the server starts
import express from 'express';
import { json, urlencoded } from 'body-parser';
import { connect } from './utils/database';
export const app = express();
app.use(json());
app.use(urlencoded({ extended: true }));
export const start = async () => {
try {
import { Sequelize } from 'sequelize';
import { config } from '../config/local';
export const sequelize = new Sequelize(config.database, config.username, config.password, {
host: config.host,
dialect: 'mysql'
});
export const connect = async () => {
try {
@willikay11
willikay11 / product.model.js
Last active July 30, 2020 04:45
Sequelize ORM product model
import { DataTypes, Model } from 'sequelize';
import { sequelize } from '../../utils/database';
class ProductModel extends Model {}
const Product = Product.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
@willikay11
willikay11 / SignOut.js
Created July 12, 2020 07:32
Sign out the user upon button click.
/**
* Sign out the user upon button click.
*/
const handleSignOutClick = (event) => {
setListDocumentsVisibility(false);
gapi.auth2.getAuthInstance().signOut();
};
@willikay11
willikay11 / ListFiles.js
Last active July 12, 2020 06:44
Fetch files from google drive api
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
const updateSigninStatus = (isSignedIn) => {
if (isSignedIn) {
// Set the signed in user
setSignedInUser(gapi.auth2.getAuthInstance().currentUser.je.Qt);
setIsLoadingGoogleDriveApi(false);
// list files if user is authenticated
@willikay11
willikay11 / SelectSource.js
Last active July 12, 2020 08:08
Initializes the API client library and sets up sign-in state listeners.
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
const initClient = () => {
setIsLoadingGoogleDriveApi(true);
gapi.client
.init({
apiKey: API_KEY,
clientId: CLIENT_ID,