Skip to content

Instantly share code, notes, and snippets.

View junibrosas's full-sized avatar
🎯
Create! create! create!

Juni Brosas junibrosas

🎯
Create! create! create!
View GitHub Profile
@junibrosas
junibrosas / server.js
Created April 17, 2019 06:16 — forked from glemiere/server.js
Simple file written in ES6 to serve a ExpressJS/NextJS Server.
/**********
server.ts
---------------
Simple file written in ES6 to serve a ExpressJS/NextJS Server.
Uses SSL, Compression, redirection and service worker routing. See server config file at :
https://github.com/glemiere/nextjs-starter/tree/master/config
---------------
Requires Let's Encrypt SSL Certificate :
https://letsencrypt.org/
**********/
@junibrosas
junibrosas / next.config.js
Created April 22, 2019 05:19
Next.js with polyfills
module.exports = {
webpack: function (cfg) {
const originalEntry = cfg.entry
cfg.entry = async () => {
const entries = await originalEntry()
if (
entries['main.js'] &&
!entries['main.js'].includes('./client/polyfills.js')
) {
@junibrosas
junibrosas / redis-cache-service.js
Created April 22, 2019 11:01 — forked from StarpTech/redis-cache-service.js
Simple Redis Cache Client for Node.js
'use strict';
const assert = require('assert');
/**
* The redis client is https://github.com/luin/ioredis
*/
/*
const redisClient = new Redis({
@junibrosas
junibrosas / moment.js
Last active November 22, 2019 08:21
Anything momentJS
// a few seconds ago
moment.fn.fromNowOrNow = function (a) {
if (Math.abs(moment().diff(this)) < 25000) { // 25 seconds before or after now
return 'just now';
}
return this.fromNow(a);
}
// check expiry
const isExpired = moment().isAfter(activity.endDate);
@junibrosas
junibrosas / crud.js
Created May 13, 2019 08:30
Mongoose CRUD sample codes
// Find one and remove entity afterwards
Charity.findOne({ cuid: userCuid });
.exec()
.then(entity => {
if (!entity) res.status(404).end();
else {
entity.remove()
.then(() => res.status(204).end());
}
});
@junibrosas
junibrosas / lookup.js
Last active July 16, 2019 05:23
MongoDB Aggregation
export const populateUserActions = (userCuid, period = { from: new Date(), to: null }) => {
const aggregation = [
{ $match: { 'cuid': userCuid }},
{ $lookup: CompanyLookup },
{ $lookup: FundraisingLookup },
{ $lookup: MissionLookup },
{ $lookup: CreatedExperienceLookup },
{ $lookup: SubscribedExperienceLookup },
{ $project: {
...simpleUserProjection,
@junibrosas
junibrosas / .babelrc
Created September 4, 2019 05:30
Transpile async await
{
"presets": [
["@babel/preset-env", {
"modules": false,
"useBuiltIns": "entry",
"targets": {
"browsers": [
"> 0.25%",
"not dead"
],
@junibrosas
junibrosas / promise.js
Last active June 3, 2020 06:05
Async Await
let characterResponse = await fetch('http://swapi.co/api/people/2/')
let characterResponseJson = await characterResponse.json()
let films = await Promise.all(
characterResponseJson.films.map(async filmUrl => {
let filmResponse = await fetch(filmUrl)
return filmResponse.json()
})
)
console.log(films)
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx}": "eslint --cache --fix"
},
/**