Skip to content

Instantly share code, notes, and snippets.

View vonvick's full-sized avatar

Victor Nwaiwu vonvick

View GitHub Profile
@vonvick
vonvick / raw-queries.example.js
Last active October 10, 2023 10:12
Raw SQL queries in Sequelize ORM for express.js
import db from '../../models';
import moment from 'moment';
const getAllUserEarnings = async (req, res) => {
// get the beginning of the current month
const currentDateMonth = moment().startOf('month').format();
let cars, carIds, totalEarnings, currentMonthEarnings;
// Use raw SQL queries to select all cars which belongs to the user
cars = await db.sequelize.query('SELECT "id" FROM Cars" WHERE "Cars"."ownerId" = (:id)', {
@vonvick
vonvick / raw-queries-2.example.js
Created February 6, 2019 17:27
sequelize option property
// passing the model type to return the result as the instance of the model
currentMonthEarnings = await db.sequelize.query('SELECT SUM("priceEstimate") FROM "Orders" WHERE "assignedCarId" IN(:ids) AND "createdAt" < (:createdDate)', {
replacements: {ids: carIds, createdDate: currentDateMonth},
model: db.Order,
mapToModel: true
});
@vonvick
vonvick / raw-queries-3.example.js
Created February 6, 2019 17:29
another example of raw queries
cars = await db.sequelize.query('SELECT "id" FROM Cars WHERE "Cars"."ownerId" = ?', {
replacements: ['active'], type: sequelize.QueryTypes.SELECT
});
@vonvick
vonvick / index.js
Created February 6, 2019 17:30
Sample model index file
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = require('<path-to-sequelize-config>');
const db = {};
const sequelize = new Sequelize(config.url, config);
@vonvick
vonvick / post-build.js
Created February 6, 2019 17:33
Post Build file for creating hash version
const path = require('path');
const fs = require('fs');
const util = require('util');
// get application version from package.json
const appVersion = require('../package.json').version;
// promisify core APIs
const readDir = util.promisify(fs.readdir);
const writeFile = util.promisify(fs.writeFile);
@vonvick
vonvick / example.package.json
Created February 6, 2019 17:42
Vue.JS auto refresh package.json example file
{
"scripts": {
"build": "node build/build.js && npm run post:build",
"post:build": "node build/post-build.js"
},
}
@vonvick
vonvick / refresh-page.mixin.js
Last active April 19, 2021 20:44
Mixin to be injected into App.vue
import axios from 'axios';
export const refreshPageMixin = {
data() {
return {
currentHash: '{{POST_BUILD_ENTERS_HASH_HERE}}',
token: localStorage.getItem('user-token'),
hashChanged: false,
newHash: ''
}
@vonvick
vonvick / example.app.vue
Last active April 19, 2021 20:44
App.vue example
<template>
<div id="app">
<router-view></router-view>
<div class="refresh-container" v-if="hashChanged && $root.env !== 'development'">
<div class="notification-header">
<button type="button" class="close-refresh-modal" @click="closeModal" aria-label="Close">
<span aria-hidden="true"><i class="fal fa-times fa-sm"></i></span>
</button>
</div>
<div class="notification-body">
@vonvick
vonvick / example.firebase-service-account.json
Created March 27, 2019 12:40
An Example Firebase Service Account json file
{
"type": "service_account",
"project_id": "<project-id>",
"private_key_id": "<private-key-id>",
"private_key": "<private-key>",
"client_email": "<client-email>",
"client_id": "<client-id>",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
require('dotenv').config();
import firebase from 'firebase-admin';
var serviceAccount = require('./firebase-service-account.json');
export default firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL
})