Commit 39413db6 authored by Alessandro CERIONI's avatar Alessandro CERIONI
Browse files

Generating UUIDs as primary keys straight from NestJS/TypeORM, instead of...

Generating UUIDs as primary keys straight from NestJS/TypeORM, instead of relying on PostgreSQL's uuid-ossp extension
parent 1d33ca9d
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { Entity, PrimaryColumn, Column, ManyToOne, JoinColumn, BeforeInsert } from 'typeorm';
import { IsString, IsOptional } from 'class-validator';
import { Organization } from '../organizations/organization.entity';
import { ApiModelProperty } from '@nestjs/swagger';
import * as uuidv4 from 'uuid/v4';
@Entity('link')
export class LinkEntity {
@PrimaryGeneratedColumn()
@PrimaryColumn('text')
@IsString()
@ApiModelProperty()
id: number;
id: string;
@IsString()
@IsOptional()
......@@ -31,6 +33,13 @@ export class LinkEntity {
@ApiModelProperty()
organization: Organization;
@BeforeInsert()
private beforeInsert() {
if (!this.id) {
this.id = uuidv4();
}
}
}
export interface LinksRO {
......
......@@ -80,7 +80,10 @@ export class LinksService {
try {
this.logger.log('Entering function', `${LinksService.name} - ${this.create.name}`);
return await this.linkRepository.save(link).catch((error) => {
// cf https://github.com/typeorm/typeorm/issues/2924#issuecomment-430265476
const linkEntity = Object.assign( new LinkEntity(), link );
return await this.linkRepository.save(linkEntity).catch((error) => {
this.logger.error(`Error saving the link into the database.`, `${error}`, `${LinksService.name} - ${this.create.name}`);
throw new InternalServerErrorException({ error, message: 'Error saving the link into the database.' });
});
......
import { Entity, PrimaryGeneratedColumn, Column, OneToMany, Generated } from 'typeorm';
import { Entity, PrimaryColumn, Column, OneToMany, Generated, BeforeInsert } from 'typeorm';
import { LinkEntity } from '../links/link.entity';
import { ApiModelProperty } from '@nestjs/swagger';
import * as uuidv4 from 'uuid/v4';
@Entity('organization')
export class Organization {
@PrimaryGeneratedColumn('uuid')
@PrimaryColumn('text')
@ApiModelProperty()
id: string;
......@@ -31,4 +32,22 @@ export class Organization {
@Column('text', { nullable: true })
@ApiModelProperty()
logo: string;
@BeforeInsert()
private beforeInsert() {
if (!this.id) {
this.id = uuidv4();
}
// UUIDs are not automatically created for related entities
// cf. https://github.com/typeorm/typeorm/issues/4209
if (this.links) {
this.links.forEach((link) => {
if (!link.id) {
link.id = uuidv4();
}
})
}
}
}
......@@ -93,14 +93,17 @@ export class OrganizationsService {
}
}
async create(organization) {
async create(organization: OrganizationDTO) {
try {
this.logger.log('Entering function', `${OrganizationsService.name} - ${this.create.name}`);
// Only save the file name and not the full url of the logo
// organization.logo = await this._fileManagementService.upload('local-actors-logo', logoImage);
const organizationCreated = await this.organizationRepository.save(organization).catch((error) => {
// cf https://github.com/typeorm/typeorm/issues/2924#issuecomment-430265476
const organizationEntity = Object.assign(new Organization(), organization);
const organizationCreated = await this.organizationRepository.save(organizationEntity).catch((error) => {
this.logger.error(`Error saving the organization into the database.`, `${error}`, `${OrganizationsService.name} - ${this.create.name}`);
throw new InternalServerErrorException({ error, message: 'Error saving the organization into the database.' });
});
......@@ -108,6 +111,7 @@ export class OrganizationsService {
this.logger.log('Organization created:', `${OrganizationsService.name} - ${this.findAll.name}`);
this.logger.log(organizationCreated, `${OrganizationsService.name} - ${this.findAll.name}`);
console.log(organizationCreated);
return organizationCreated;
} catch (error) {
this.logger.error('Failed to create the organization.', `${error}`, `${OrganizationsService.name} - ${this.create.name}`);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment