Skip to content

Instantly share code, notes, and snippets.

View DimitrK's full-sized avatar

dimitris kyriazopoulos DimitrK

  • Workable
  • Athens
View GitHub Profile
@DimitrK
DimitrK / Include-in-Sequelize.md
Last active December 17, 2019 10:20 — forked from zcaceres/Include-in-Sequelize.md
using Include in sequelize

'Include' in Sequelize: The One Confusing Query That You Should Memorize

When querying your database in Sequelize, you'll often want data associated with a particular model which isn't in the model's table directly. This data is usually typically associated through join tables (e.g. a 'hasMany' or 'belongsToMany' association), or a foreign key (e.g. a 'hasOne' or 'belongsTo' association).

When you query, you'll receive just the rows you've looked for. With eager loading, you'll also get any associated data. For some reason, I can never remember the proper way to do eager loading when writing my Sequelize queries. I've seen others struggle with the same thing.

Eager loading is confusing because the 'include' that is uses has unfamiliar fields is set in an array rather than just an object.

So let's go through the one query that's worth memorizing to handle your eager loading.

The Basic Query

@DimitrK
DimitrK / expect.js
Last active October 4, 2017 10:06
Validate parameters or variables with a more declarative way.
/**
* Copyright 2017 dimitrk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@DimitrK
DimitrK / carryOperationsCounter.js
Last active November 14, 2021 00:09
Counts the number of carry operation in an addition of two numbers
function numberOfCarryOperations(x,y) {
var xs = x.toString();
var ys = y.toString();
var cary = 0;
var operations=0;
var xCurrent;
var yCurrent;
while (xs.substr(xs.length-1) || ys.substr(ys.length-1) ) {
yCurrent =ys.substr(ys.length-1);
@DimitrK
DimitrK / toBaseConverter.js
Last active September 24, 2018 22:34
Convert to any base from base10. Returns the result into an array which you can later process further or user Array.join in order to get the final based number
function toBase(number, base) {
if (base <2) {
throw new Error("Base has to be greater than 2");
}
var basedNumberArray = [];
var divider = Math.floor(number / base);
var basedNumber = number % base;
var basedCode = getAsciiCap(basedNumber);
basedNumberArray.push(basedCode || basedNumber);
@DimitrK
DimitrK / Readme.md
Last active December 31, 2020 20:41
Similar to URLSearchParams. Manipulate URL GET parameters. On `toString` sorts URL parameters by variable name in order to improve SEO when you use the generated URLs to redirect user in another page. Escapes HTML for parameter values in order to stop XSS attacks based on [unsecured links](https://www.owasp.org/index.php/XSS_(Cross_Site_Scriptin…

UrlSearchParams

Manipulate URL GET parameters and hash.

Similar to URLSearchParams.

In addition:

  • Compatible with >IE5
@DimitrK
DimitrK / expand.js
Created May 25, 2013 13:25
This gist is used to augment object instances with a function called expand. It checks if the object has some property which includes dot characters in its name and translates it to deep level property nesting. e.g: account['user.name'] ==> account.user.name
// Transforms a property of object which includes dots to nested property as follows
// object = {};
// object["firstlevelprop.secondlevelprop"] = "something" =to=> object["firstlevelprop"]["secondlevelprop"] = "something"
(function () {
var enableExpand = function () {
//We dont want the method to appear in the object and mess with the rest of our object's properties
Object.defineProperty(this, "expand", {
enumerable: false, // Not visible
configurable: false, // Not configurable
value: function () {
@DimitrK
DimitrK / BracketsExtesionsUpdater.bat
Last active December 17, 2015 17:49
Brackets extension batch updater for Windows. For more info read the readme.md file.
:: Brackets Extensions Updater
:: Dimitris Kyriazopoulos. jim.feedback at Google mail dot com
:: MIT License. For more info visit http://opensource.org/licenses/MIT
@echo off
call:iterateFolders >> updatelog.xml
echo.&goto:eof
:iterateFolders
set "extensionsDir=%~dp0"