Skip to main content

· One min read
Hreniuc Cristian-Alexandru

When you want to test a function that uses a method from an object, you can mock the method and check if it was called with the right parameters. For example, this is how to mock for sendNotification method from firebaseService object:

import { firebaseService } from '../../../firebase/firebase';

let mockSendNotification: jest.Mock;

describe('Tests suit', () => {

test('Test1', async () => {
mockSendNotification = jest.fn();
firebaseService.sendNotification = mockSendNotification;

// call the function that uses firebaseService.sendNotification

expect(mockSendNotification).toHaveBeenCalledWith(
C1.notifications.N1U4S1.contents,
C1.notifications.N1U4S1.subscriptions,
);
});
});

And if you want to check if the method was called multiple times with different parameters, you can use toHaveBeenNthCalledWith:

expect(mockSendNotification).toHaveBeenNthCalledWith(
1,
C1.notifications.N4U1S2E1.contents,
C1.notifications.N4U1S2E1.subscriptions,
);
expect(mockSendNotification).toHaveBeenNthCalledWith(
2,
C1.notifications.N5U1S2E2.contents,
C1.notifications.N5U1S2E2.subscriptions,
);

· One min read
Hreniuc Cristian-Alexandru

When you have two tables and you want to create an inner join without having a relation in the entities, you can use the innerJoin method from the createQueryBuilder method like this:

.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)

This works even if you don't have a relation between the two entities.

const notifForCompanyManagers = await dbConnection.manager
.getRepository(NotificationSubscription)
.createQueryBuilder('notification_subscription')
.select('notification_subscription.token')
.addSelect('notification_subscription.userId')
.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)
.where(
'(user_company.manager = true or user_company.owner = true) and user_company.companyId = :companyId',
{ companyId: companyId },
)
.getMany();

· One min read
Hreniuc Cristian-Alexandru

I was using composition API and I had an object/store that I wanted to reset on client logout. I found that I can do it like this:

const company = reactive(new Company());

// Used when we re-login or on logout.
export class CompanyUtil {
public static clear() {
//https://stackoverflow.com/a/61509432
// https://github.com/vuejs/core/issues/1081
Object.assign(company, new Company());
}
}

export default company;

· 3 min read
Hreniuc Cristian-Alexandru

MariaDB container

I started the mariadb container like this:

version: "3"
name: "mariadb"
services:
mariadb:
image: mariadb:10.10.2
container_name: mariadb_prod_1
ports:
- "3306:3306"
volumes:
- ./fs/mariadb/volume:/var/lib/mysql:rw
- ./fs/mariadb/mysqld:/var/run/mysqld:rw
- ./fs/mariadb/log:/var/log/mysql:rw
- ./fs/mariadb/conf.d:/etc/mysql/conf.d:ro
environment:
MARIADB_MYSQL_LOCALHOST_USER: 1
MARIADB_MYSQL_LOCALHOST_GRANTS: "RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR"
MARIADB_ROOT_PASSWORD: password
restart: always

Those folders are empty at startup, I used them to make the server data persistent and also because some were required by mariabackup.

Afterwards I created a script which is ran by a cronjob, the script creates a full backup using mariabackup and uploads the backup to S3 or back blaze using restic(restic detects which files are already up and it won't upload them again).

· One min read
Hreniuc Cristian-Alexandru

I needed this when I switched from kubernetes to docker compose(Kubernetes was too much to maintain and I only had one small project).

To load balance I used the following config for nginx:

upstream pool-of-services {
# Resolved by docker dns, so they must exist at nginx startup, othewise this fails
server container_1:6070;
server container_2:6070;
}

server {
# Listen to port 443 on both IPv4 and IPv6.
listen 443 ssl;
listen [::]:443 ssl;

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://pool-of-services;
}
}

Source

· One min read
Hreniuc Cristian-Alexandru

To do so, I used jonasal/nginx-certbot docker image.

My docker compose file looked like this:

version: "3"
name: "nginx"
services:
nginx:
image: jonasal/nginx-certbot:3.3.1
container_name: nginx_prod_1
ports:
- 80:80
- 443:443
volumes:
- ./fs/nginx/secrets:/etc/letsencrypt:rw
- ./fs/nginx/log:/var/log/nginx:rw
- ./fs/nginx/user_conf.d:/etc/nginx/user_conf.d:ro
environment:
DEBUG: 1
CERTBOT_EMAIL: email
#STAGING: 1
restart: always

· One min read
Hreniuc Cristian-Alexandru

If you have a PWA, you might want to make sure that the client is using the latest version of it. Here are the steps needed to do this using quasar framework:

1. Make sure that the service-worker.js is not cached by nginx

Add this configuration in your nginx:

location ~ service-worker.js$ {
#add_header X-debug-whats-going-on 3;
add_header Cache-Control "no-cache, no-store";
expires -1;
try_files $uri $uri/ =404;
#/service-worker.js; -> https://serverfault.com/a/505099
}

· One min read
Hreniuc Cristian-Alexandru

This is a tutorial on how to set a git config for a specific folder where you have work related projects and a folder where you have personal projects.

Create a gitconfig file for your personal projects

Contents of th:

# In ~/data/personal/configs/git/.personal_gitconfig
[user]
name = Hreniuc Cristian-Alexandru
email = email@gmail.com

Add the path to that config in the global git config:

[includeIf "gitdir:~/data/personal/"]
path = ~/data/personal/configs/git/.personal_gitconfig

Make sure you add it at the end of .gitconfig and make sure the last \ from the If exists, otherwise, it won't work.

Source