Skip to content

Instantly share code, notes, and snippets.

View pulkitsinghal's full-sized avatar

Pulkit Singhal pulkitsinghal

View GitHub Profile
@pulkitsinghal
pulkitsinghal / readme.md
Created April 28, 2016 14:01 — forked from felippenardi/readme.md
Benchmarking AngularJS performance

Benchmarking AngularJS performance

Things to optmize

  • Use one-time-bind on expressions ( {{::value}} )
  • Replace $scope.$apply() with $scope.$digest() whenever possible
  • Move filters to controllers

Measuring Watch list

To get the total watchers on the current page, you can run this script on the browser console:

@pulkitsinghal
pulkitsinghal / findCallback.js
Last active June 12, 2016 15:59
nodejs mongo workers
var dbUrl = 'mongodb://username:[email protected]:port/database-name';
MongoClient.connect(
dbUrl,
function(err, db) {
if(err) throw err;
db.collection('ItemModel').find({}).toArray(function(err, docs) {
console.dir(docs);
console.dir(docs.length);
db.close();
});
@pulkitsinghal
pulkitsinghal / Dockerfile
Last active April 21, 2016 03:16
How to write a multiline command in Dockerfile
# https://github.com/docker/docker/issues/1799
RUN echo "module.exports={\n\
x:{\n\
arg1:'${arg1}'\n\
},\n\
a:{\n\
b:{\n\
c:{\n\
d:{\n\
arg2:'${arg2}'\n\
@pulkitsinghal
pulkitsinghal / app.js
Last active March 12, 2016 03:17
LoopBackAuth and AngularJS
angular
.module('myApp', [
'ui.router',
'lbServices',
'ngStorage'
])
.run([
'$rootScope', '$state', 'LoopBackAuth',
function ($rootScope, $state, LoopBackAuth) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
@pulkitsinghal
pulkitsinghal / MyModel.js
Last active December 22, 2015 21:41
LoopBack: How to change a remote method to handle both callbacks and promise based invocations?
var loopbackUtils = require('loopback/lib/utils'); // handle both callbacks and promise based invocations
module.exports = function(MyModel) {
MyModel.remoteMethod('generateToken', {
accepts: [
{arg: 'id', type: 'string', required: true}
],
http: {path: '/:id/generateToken', verb: 'get'}
});
@pulkitsinghal
pulkitsinghal / 01-add-custom-role-resolver.js
Last active December 8, 2015 22:47
How to set user in loopback context for use by remote methods when an arbitrary app token is used by incoming requests?
// it resides in `/server/boot/` directory
'use strict';
var path = require('path');
var fileName = path.basename(__filename, '.js'); // gives the filename without the .js extension
var log = require('debug')('boot:'+fileName);
module.exports = function(app) {
var Role = app.models.Role;
@pulkitsinghal
pulkitsinghal / seller-model.js
Last active March 4, 2017 15:44
How to create an AccessToken in LoopBack for a user
/** A quick github search for sample code:
* > https://github.com/search?utf8=%E2%9C%93&q=-1+AccessToken.create+path%3A%2Fcommon%2Fmodels&type=Code&ref=searchresults
* Yields two similar result:
* > https://github.com/patriciamolina/test/blob/90a8829728e4a404109e1eafa0a1075687042792/common/models/customer.js#L421
* > https://github.com/stormpath/loopback-stormpath/blob/e1cb4d98eacfe36ade2d58e1a48e6cfd590308ff/common/models/stormpath-user.js#L458
*/
module.exports = function(SellerModel) {
SellerModel.observe('after save', function(ctx, next) {
console.log('`after save` supports isNewInstance?', ctx.isNewInstance !== undefined);
@pulkitsinghal
pulkitsinghal / 01_notes.md
Last active June 4, 2016 02:56
Loopback: Alternative to `bunyan` logging via `debug` module
//require('log-prefix')(function() { return '[' + Date.now() + '] %s'; });
// prints: [1465008912650] ...

//require('log-prefix')(function() { return '[' + new Date().toISOString() + '] %s'; });
// prints: [2016-06-04T02:54:36.488Z] ...

//require('log-prefix')(function() { return '[' + new Date().toUTCString() + '] %s'; });
// prints: [Sat, 04 Jun 2016 02:53:26 GMT] ...
@pulkitsinghal
pulkitsinghal / 01_chat.md
Last active November 28, 2016 03:36 — forked from doublemarked/1:middleware.json
Bunyan Loopback logging
doublemarked 09:50
@pulkitsinghal here’s a gist of the core of it all:
https://gist.github.com/doublemarked/e4bf329d11c55baf468f

@pulkitsinghal this will log access tokens with all log messages.
Bunyan produces JSON, so you’re essentially going to need to parse
the log to fish out things, but JSON makes it easy to do that.

For simple things (like access tokens) you can also use grep
@pulkitsinghal
pulkitsinghal / utils.js
Created September 15, 2015 06:29
Logging Permutations - Path to insanity or Enlightenment?
'use strict';
var loopback = require('loopback');
var Promise = require('bluebird');
var path = require('path');
var fileName = path.basename(__filename, '.js'); // gives the filename without the .js extension
var log = require('debug')('common:models:'+fileName);
//var logger = require('./../lib/logger')('common:models:'+fileName);