-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from plezanje-net/crags-updates
Crags updates
- Loading branch information
Showing
14 changed files
with
354 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import DataLoader from 'dataloader'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { NestDataLoader } from '../../core/interceptors/data-loader.interceptor'; | ||
import { Area } from '../entities/area.entity'; | ||
import { AreasService } from '../services/areas.service'; | ||
|
||
@Injectable() | ||
export class AreaLoader implements NestDataLoader<string, Area> { | ||
constructor(private readonly areasService: AreasService) {} | ||
|
||
generateDataLoader(): DataLoader<string, Area> { | ||
return new DataLoader<string, Area>(async (keys) => { | ||
const areas = await this.areasService.findByIds(keys.map((k) => k)); | ||
|
||
const areasMap: { [key: string]: Area } = {}; | ||
|
||
areas.forEach((area) => { | ||
areasMap[area.id] = area; | ||
}); | ||
|
||
return keys.map((areaId) => areasMap[areaId] ?? null); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import slugify from 'slugify'; | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class areaSlugNonNullable1703958939270 implements MigrationInterface { | ||
name = 'areaSlugNonNullable1703958939270'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// Some areas that were added by users have slugs missing. Add slugs before making slug non-nullable. | ||
const missingSlugAreas = await queryRunner.query( | ||
`SELECT a.name, a.id FROM "area" a WHERE a.slug IS NULL`, | ||
); | ||
|
||
for (let i = 0; i < missingSlugAreas.length; i++) { | ||
const area = missingSlugAreas[i]; | ||
const slug = await this.generateAreaSlug(area.name, queryRunner); | ||
await queryRunner.query( | ||
`UPDATE "area" a SET "slug" = '${slug}' WHERE a.id = '${area.id}'`, | ||
); | ||
} | ||
|
||
await queryRunner.query( | ||
`ALTER TABLE "area" ALTER COLUMN "slug" SET NOT NULL`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "area" ALTER COLUMN "slug" DROP NOT NULL`, | ||
); | ||
} | ||
|
||
private async generateAreaSlug(areaName: string, queryRunner: QueryRunner) { | ||
let slug = slugify(areaName, { lower: true }); | ||
let suffixCounter = 0; | ||
let suffix = ''; | ||
|
||
while ( | ||
( | ||
await queryRunner.query( | ||
`SELECT * FROM "area" a WHERE a.slug = '${slug}${suffix}'`, | ||
) | ||
).length > 0 | ||
) { | ||
suffixCounter++; | ||
suffix = '-' + suffixCounter; | ||
} | ||
slug += suffix; | ||
|
||
return slug; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class fixRainproofFieldName1705000587590 implements MigrationInterface { | ||
name = 'fixRainproofFieldName1705000587590'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "crag" RENAME COLUMN "rain_proof" TO "rainproof"`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "crag" RENAME COLUMN "rainproof" TO "rain_proof"`, | ||
); | ||
} | ||
} |
Oops, something went wrong.