Skip to content

Instantly share code, notes, and snippets.

View arifmahmudrana's full-sized avatar

Arif Mahmud Rana arifmahmudrana

View GitHub Profile
@arifmahmudrana
arifmahmudrana / mongo.js
Last active December 30, 2020 06:36
MongoDB sample queries
db.getCollection('search_params')
.find({ propertyCode: /R.+/ })
.sort({ propertyCode: -1 })
db.getCollection('search_params').aggregate([
{
$match: {
propertyCode: /^R.+$/,
},
},
@arifmahmudrana
arifmahmudrana / axios.proxy.ts
Created January 26, 2020 11:28
Axios proxy request with express
axios({
method: 'get',
url: 'URL',
params: {
keyword: 'keyword'
},
responseType: 'stream'
})
.then(response => {
for (const key in response.headers) {
@arifmahmudrana
arifmahmudrana / promise-all.ts
Created January 22, 2020 11:12
Promise all
/*
* If used Promise all first error will go to catch block
*
* If no error occurs then the resolved values will be an array
with exact sequence of promise here e.g if run Promise.all([p1, p2, p3])
output: [ 'one', 'two', 'three' ]
*
* If there is a setTimeout after all setTimeout execution will be done
*
* If there is a setTimeout, setImmediate or setTimeout 0 thoose will be executed after next tick
@arifmahmudrana
arifmahmudrana / joi-locale.ts
Created January 21, 2020 13:05
joi localization internationalization error messages
import { object, string, number, ValidationError } from '@hapi/joi';
const schema = object({
searchCode: string()
.required()
.label('Search code'),
id: number().required()
}),
messages = {
'alternatives.all': '{{#label}} does not match all of the required types',
@arifmahmudrana
arifmahmudrana / error.ts
Last active January 21, 2020 07:44
Typescript error handling
abstract class BaseError extends Error {
abstract status: number;
}
class ValidationError extends BaseError {
status: number = 422;
constructor(message?: string) {
super(message);
this.name = this.constructor.name;
}
@arifmahmudrana
arifmahmudrana / array-flat-map-alternative.js
Created January 8, 2020 05:27
example of javascript array flat map alternative for typescript
// example of array flat map alternative
const data = [
{
city: [
{
country: "Bhutan"
},
{
country: "Bhutan"
@arifmahmudrana
arifmahmudrana / elastic.txt
Last active July 21, 2020 06:29
Elastic search queries
POST tasks/_doc
{
"title": "Goto ICCDRB",
"description": "I will goto ICCDRB with my mother for her urine test & ultrasonogram test",
"user": {
"id": 1,
"name": "Arif Mahmud Rana",
"email": "arif_mahmud_rana@hotmail.com"
}
}
@arifmahmudrana
arifmahmudrana / validation-example.ts
Created January 7, 2020 12:33
JOI validation JavaScript example
import {array, object, string, validate} from "joi";
const schema = object({
searchCode: string().required().label('Search code'),
primaryContact: object({
mobileNumber: string()
.regex(/^\+?\d{7,13}$/)
.error(() => 'You must provide a valid Primary contact mobile number.')
.required()
.label('Primary contact mobile number')
}).required(),
@arifmahmudrana
arifmahmudrana / toJSON.js
Created January 6, 2020 13:47
custom toJSON example
// custom toJSON example object
const a = {
id: 1,
name: 'ARIF',
toJSON() {
return JSON.stringify({id: this.id})
}
}
const a = new A();
const b = {...a}
@arifmahmudrana
arifmahmudrana / joi.js
Created December 17, 2019 11:20
Joi conditional validation
const joi = require("joi");
// const schema = joi.object({
// roomIds: joi.array()
// .required()
// .min(1)
// .max(6)
// .sparse()
// .items(joi.string())
// }).keys({