brackets-prisma-db 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +53 -19
  2. package/dist/prisma-id.d.ts +10 -0
  3. package/dist/prisma-id.js +32 -0
  4. package/dist/storage-handlers/delete-handlers/group.d.ts +1 -1
  5. package/dist/storage-handlers/delete-handlers/group.js +24 -15
  6. package/dist/storage-handlers/delete-handlers/match-game.d.ts +1 -1
  7. package/dist/storage-handlers/delete-handlers/match-game.js +25 -16
  8. package/dist/storage-handlers/delete-handlers/match.d.ts +1 -1
  9. package/dist/storage-handlers/delete-handlers/match.js +26 -17
  10. package/dist/storage-handlers/delete-handlers/participant.d.ts +1 -1
  11. package/dist/storage-handlers/delete-handlers/participant.js +24 -15
  12. package/dist/storage-handlers/delete-handlers/round.d.ts +1 -1
  13. package/dist/storage-handlers/delete-handlers/round.js +25 -16
  14. package/dist/storage-handlers/delete-handlers/stage.d.ts +1 -1
  15. package/dist/storage-handlers/delete-handlers/stage.js +32 -29
  16. package/dist/storage-handlers/delete.d.ts +1 -1
  17. package/dist/storage-handlers/delete.js +1 -2
  18. package/dist/storage-handlers/insert-handlers/group.d.ts +2 -2
  19. package/dist/storage-handlers/insert-handlers/group.js +16 -16
  20. package/dist/storage-handlers/insert-handlers/match-game.d.ts +3 -2
  21. package/dist/storage-handlers/insert-handlers/match-game.js +20 -18
  22. package/dist/storage-handlers/insert-handlers/match.d.ts +3 -2
  23. package/dist/storage-handlers/insert-handlers/match.js +20 -18
  24. package/dist/storage-handlers/insert-handlers/participant.d.ts +2 -2
  25. package/dist/storage-handlers/insert-handlers/participant.js +20 -16
  26. package/dist/storage-handlers/insert-handlers/round.d.ts +2 -2
  27. package/dist/storage-handlers/insert-handlers/round.js +16 -16
  28. package/dist/storage-handlers/insert-handlers/stage.d.ts +2 -2
  29. package/dist/storage-handlers/insert-handlers/stage.js +26 -26
  30. package/dist/storage-handlers/insert.d.ts +1 -1
  31. package/dist/storage-handlers/insert.js +1 -2
  32. package/dist/storage-handlers/select-handlers/group.d.ts +2 -2
  33. package/dist/storage-handlers/select-handlers/group.js +34 -27
  34. package/dist/storage-handlers/select-handlers/match-game.d.ts +3 -2
  35. package/dist/storage-handlers/select-handlers/match-game.js +49 -44
  36. package/dist/storage-handlers/select-handlers/match.d.ts +3 -2
  37. package/dist/storage-handlers/select-handlers/match.js +59 -53
  38. package/dist/storage-handlers/select-handlers/participant.d.ts +2 -2
  39. package/dist/storage-handlers/select-handlers/participant.js +31 -24
  40. package/dist/storage-handlers/select-handlers/round.d.ts +2 -2
  41. package/dist/storage-handlers/select-handlers/round.js +35 -28
  42. package/dist/storage-handlers/select-handlers/stage.d.ts +2 -2
  43. package/dist/storage-handlers/select-handlers/stage.js +54 -47
  44. package/dist/storage-handlers/select.d.ts +2 -2
  45. package/dist/storage-handlers/select.js +1 -2
  46. package/dist/storage-handlers/update-handlers/group.d.ts +2 -2
  47. package/dist/storage-handlers/update-handlers/group.js +31 -25
  48. package/dist/storage-handlers/update-handlers/match-game.d.ts +3 -2
  49. package/dist/storage-handlers/update-handlers/match-game.js +47 -31
  50. package/dist/storage-handlers/update-handlers/match.d.ts +3 -2
  51. package/dist/storage-handlers/update-handlers/match.js +49 -33
  52. package/dist/storage-handlers/update-handlers/participant.d.ts +2 -2
  53. package/dist/storage-handlers/update-handlers/participant.js +34 -25
  54. package/dist/storage-handlers/update-handlers/round.d.ts +2 -2
  55. package/dist/storage-handlers/update-handlers/round.js +34 -28
  56. package/dist/storage-handlers/update-handlers/stage.d.ts +2 -2
  57. package/dist/storage-handlers/update-handlers/stage.js +46 -43
  58. package/dist/storage-handlers/update.d.ts +2 -2
  59. package/dist/storage-handlers/update.js +1 -2
  60. package/dist/storage.d.ts +3 -4
  61. package/dist/transformers/enum/grand-final-type.js +17 -7
  62. package/dist/transformers/enum/match-result.js +17 -7
  63. package/dist/transformers/enum/match-status.d.ts +1 -1
  64. package/dist/transformers/enum/match-status.js +21 -7
  65. package/dist/transformers/enum/round-robin-mode.js +17 -7
  66. package/dist/transformers/enum/seed-ordering.js +17 -7
  67. package/dist/transformers/enum/stage-type.js +17 -7
  68. package/dist/transformers/model/group.d.ts +1 -1
  69. package/dist/transformers/model/group.js +2 -1
  70. package/dist/transformers/model/match-game.d.ts +20 -20
  71. package/dist/transformers/model/match-game.js +64 -2
  72. package/dist/transformers/model/match.d.ts +22 -24
  73. package/dist/transformers/model/match.js +67 -3
  74. package/dist/transformers/model/participant-result.d.ts +1 -1
  75. package/dist/transformers/model/participant.d.ts +6 -2
  76. package/dist/transformers/model/participant.js +22 -2
  77. package/dist/transformers/model/round.d.ts +1 -1
  78. package/dist/transformers/model/round.js +3 -2
  79. package/dist/transformers/model/stage-settings.d.ts +2 -2
  80. package/dist/transformers/model/stage.d.ts +2 -2
  81. package/dist/transformers/model/stage.js +2 -1
  82. package/dist/types.d.ts +10 -0
  83. package/dist/types.js +2 -0
  84. package/package.json +5 -5
package/README.md CHANGED
@@ -1,33 +1,23 @@
1
1
  # brackets-prisma-db
2
2
 
3
3
  This implementation of the [`CrudInterface`](https://drarig29.github.io/brackets-docs/reference/manager/interfaces/CrudInterface.html)
4
- uses [prisma](https://www.prisma.io/) to store the data in an SQL Database.
5
-
6
- > **Warning**
7
- > This implementation has not been full tested yet. Use with caution.
8
-
9
- ## Limitations
10
-
11
- Currently there are some features of the [manager](https://github.com/Drarig29/brackets-manager.js) that can't be used.
12
-
13
- | **Feature** | **Status** |
14
- | ------------------- | --------------- |
15
- | Custom Participants | Implemented |
16
- | Custom Matches | Not Implemented |
4
+ uses [Prisma](https://www.prisma.io/) to store the data in a SQL database.
17
5
 
18
6
  ## Usage
19
7
 
20
- To use this implementation make sure you have [prisma](https://www.prisma.io/) included in your project.
21
- A Guide on how to include prisma in your project can be found on the official documentation page.
8
+ To use this implementation, make sure you have [Prisma](https://www.prisma.io/) included in your project. You may follow their [Getting Started](https://www.prisma.io/docs/getting-started) guide.
22
9
 
23
- Once [prisma](https://www.prisma.io/) is included in your project copy the [`schema.prisma`](./prisma/schema.prisma) into your project.
10
+ Once [Prisma](https://www.prisma.io/) is included in your project, copy the [`schema.prisma`](./prisma/schema.prisma) into your project.
24
11
  If you have an existing `schema.prisma` in your project copy and paste the schema definitions into your existing one.
25
12
 
26
- > **Warning**
13
+ > [!WARNING]
27
14
  > Do not rename any of the models or enums provided! This **will** break the implementation.
28
15
 
29
- Next generate the prisma client using `npx prisma generate`.
30
- Lastly push the definition to your database using `npx prisma db push`.
16
+ Next, generate the Prisma client using `npx prisma generate`.
17
+
18
+ And lastly, push the definition to your database using `npx prisma db push`.
19
+
20
+ ### Minimal example
31
21
 
32
22
  ```typescript
33
23
  import { SqlDatabase } from 'brackets-prisma-db';
@@ -40,3 +30,47 @@ const prisma = new PrismaClient();
40
30
  const storage = new SqlDatabase(prisma);
41
31
  const manager = new BracketsManager(storage);
42
32
  ```
33
+
34
+ ### Example with custom matches and participants
35
+
36
+ ```typescript
37
+ import { SqlDatabase } from 'brackets-prisma-db'
38
+ import { PrismaClient } from '@prisma/client'
39
+ import { BracketsManager } from 'brackets-manager'
40
+ import type { Match } from 'brackets-model'
41
+
42
+ type MatchWithWeather = Match & {
43
+ // The schema defines a JSON column named `extra` in the `Match` and `MatchGame` tables.
44
+ // Anything you put in the `extra` object will be stored in that column.
45
+ extra: {
46
+ weather: 'sunny' | 'rainy' | 'cloudy' | 'snowy'
47
+ }
48
+ }
49
+
50
+ const storage = new SqlDatabase(new PrismaClient())
51
+ const manager = new BracketsManager(storage)
52
+
53
+ const stage = await manager.create.stage({
54
+ tournamentId: 1,
55
+ name: 'Example',
56
+ type: 'single_elimination',
57
+ seeding: [
58
+ // Only `name` is mandatory. All other properties are stored in the `Participant` table rows.
59
+ { name: 'Team 1', color: 'red' },
60
+ { name: 'Team 2', color: 'blue' },
61
+ { name: 'Team 3', color: 'green' },
62
+ { name: 'Team 4', color: 'yellow' },
63
+ ],
64
+ })
65
+
66
+ const currentMatches = await manager.get.currentMatches(stage.id)
67
+
68
+ await manager.update.match<MatchWithWeather>({
69
+ id: currentMatches[0].id,
70
+ opponent1: { score: 6, result: 'win' },
71
+ opponent2: { score: 3, result: 'loss' },
72
+ extra: {
73
+ weather: 'sunny',
74
+ },
75
+ })
76
+ ```
@@ -0,0 +1,10 @@
1
+ import type { Id } from 'brackets-model';
2
+ export declare class InvalidPrismaIdError extends Error {
3
+ constructor(id: Id);
4
+ }
5
+ export declare function isModelId(value: unknown): value is Id;
6
+ export declare function toPrismaId(id: Id): number;
7
+ export declare function toPrismaId(id: null): null;
8
+ export declare function toPrismaId(id: undefined): undefined;
9
+ export declare function toPrismaId(id: Id | null): number | null;
10
+ export declare function toPrismaId(id: Id | undefined): number | undefined;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidPrismaIdError = void 0;
4
+ exports.isModelId = isModelId;
5
+ exports.toPrismaId = toPrismaId;
6
+ class InvalidPrismaIdError extends Error {
7
+ constructor(id) {
8
+ super(`Invalid Prisma integer id: ${JSON.stringify(id)}`);
9
+ }
10
+ }
11
+ exports.InvalidPrismaIdError = InvalidPrismaIdError;
12
+ function isModelId(value) {
13
+ return typeof value === 'number' || typeof value === 'string';
14
+ }
15
+ function toPrismaId(id) {
16
+ if (id === null || id === undefined) {
17
+ return id;
18
+ }
19
+ if (typeof id === 'number') {
20
+ if (Number.isSafeInteger(id)) {
21
+ return id;
22
+ }
23
+ throw new InvalidPrismaIdError(id);
24
+ }
25
+ if (/^-?\d+$/.test(id)) {
26
+ const numericId = Number(id);
27
+ if (Number.isSafeInteger(numericId)) {
28
+ return numericId;
29
+ }
30
+ }
31
+ throw new InvalidPrismaIdError(id);
32
+ }
@@ -1,3 +1,3 @@
1
- import { DataTypes } from 'brackets-manager/dist/types';
1
+ import { DataTypes } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
3
  export declare function handleGroupDelete(prisma: PrismaClient, filter?: Partial<DataTypes['group']>): Promise<boolean>;
@@ -1,23 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleGroupDelete = void 0;
3
+ exports.handleGroupDelete = handleGroupDelete;
4
+ const prisma_id_1 = require("../../prisma-id");
4
5
  async function handleGroupDelete(prisma, filter) {
5
6
  // No filter so delete everything
6
7
  if (!filter) {
7
- return prisma.group
8
- .deleteMany({})
9
- .then(() => true)
10
- .catch(() => false);
8
+ try {
9
+ await prisma.group.deleteMany({});
10
+ return true;
11
+ }
12
+ catch {
13
+ return false;
14
+ }
11
15
  }
12
- return prisma.group
13
- .deleteMany({
14
- where: {
15
- id: filter.id,
16
- stageId: filter.stage_id,
16
+ try {
17
+ const where = {
18
+ id: (0, prisma_id_1.toPrismaId)(filter.id),
19
+ stageId: (0, prisma_id_1.toPrismaId)(filter.stage_id),
17
20
  number: filter.number,
18
- },
19
- })
20
- .then(() => true)
21
- .catch(() => false);
21
+ };
22
+ await prisma.group.deleteMany({ where });
23
+ return true;
24
+ }
25
+ catch (error) {
26
+ // An unsupported storage ID should behave like a filter that matched no rows.
27
+ if (error instanceof prisma_id_1.InvalidPrismaIdError) {
28
+ return true;
29
+ }
30
+ return false;
31
+ }
22
32
  }
23
- exports.handleGroupDelete = handleGroupDelete;
@@ -1,3 +1,3 @@
1
- import { DataTypes } from 'brackets-manager/dist/types';
1
+ import { DataTypes } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
3
  export declare function handleMatchGameDelete(prisma: PrismaClient, filter?: Partial<DataTypes['match_game']>): Promise<boolean>;
@@ -1,28 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleMatchGameDelete = void 0;
3
+ exports.handleMatchGameDelete = handleMatchGameDelete;
4
4
  const transformers_1 = require("../../transformers");
5
+ const prisma_id_1 = require("../../prisma-id");
5
6
  async function handleMatchGameDelete(prisma, filter) {
6
7
  // No filter so delete everything
7
8
  if (!filter) {
8
- return prisma.matchGame
9
- .deleteMany({})
10
- .then(() => true)
11
- .catch(() => false);
9
+ try {
10
+ await prisma.matchGame.deleteMany({});
11
+ return true;
12
+ }
13
+ catch {
14
+ return false;
15
+ }
12
16
  }
13
- return prisma.matchGame
14
- .deleteMany({
15
- where: {
16
- id: filter.id,
17
- stageId: filter.stage_id,
18
- matchId: filter.parent_id,
17
+ try {
18
+ const where = {
19
+ id: (0, prisma_id_1.toPrismaId)(filter.id),
20
+ stageId: (0, prisma_id_1.toPrismaId)(filter.stage_id),
21
+ matchId: (0, prisma_id_1.toPrismaId)(filter.parent_id),
19
22
  number: filter.number,
20
23
  status: filter.status
21
24
  ? transformers_1.MatchStatusTransformer.to(filter.status)
22
25
  : undefined,
23
- },
24
- })
25
- .then(() => true)
26
- .catch(() => false);
26
+ };
27
+ await prisma.matchGame.deleteMany({ where });
28
+ return true;
29
+ }
30
+ catch (error) {
31
+ // An unsupported storage ID should behave like a filter that matched no rows.
32
+ if (error instanceof prisma_id_1.InvalidPrismaIdError) {
33
+ return true;
34
+ }
35
+ return false;
36
+ }
27
37
  }
28
- exports.handleMatchGameDelete = handleMatchGameDelete;
@@ -1,3 +1,3 @@
1
- import { DataTypes } from 'brackets-manager/dist/types';
1
+ import { DataTypes } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
3
  export declare function handleMatchDelete(prisma: PrismaClient, filter?: Partial<DataTypes['match']>): Promise<boolean>;
@@ -1,29 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleMatchDelete = void 0;
3
+ exports.handleMatchDelete = handleMatchDelete;
4
4
  const transformers_1 = require("../../transformers");
5
+ const prisma_id_1 = require("../../prisma-id");
5
6
  async function handleMatchDelete(prisma, filter) {
6
7
  // No filter so delete everything
7
8
  if (!filter) {
8
- return prisma.match
9
- .deleteMany({})
10
- .then(() => true)
11
- .catch(() => false);
9
+ try {
10
+ await prisma.match.deleteMany({});
11
+ return true;
12
+ }
13
+ catch {
14
+ return false;
15
+ }
12
16
  }
13
- return prisma.match
14
- .deleteMany({
15
- where: {
16
- id: filter.id,
17
- stageId: filter.stage_id,
18
- groupId: filter.group_id,
19
- roundId: filter.round_id,
17
+ try {
18
+ const where = {
19
+ id: (0, prisma_id_1.toPrismaId)(filter.id),
20
+ stageId: (0, prisma_id_1.toPrismaId)(filter.stage_id),
21
+ groupId: (0, prisma_id_1.toPrismaId)(filter.group_id),
22
+ roundId: (0, prisma_id_1.toPrismaId)(filter.round_id),
20
23
  number: filter.number,
21
24
  status: filter.status
22
25
  ? transformers_1.MatchStatusTransformer.to(filter.status)
23
26
  : undefined,
24
- },
25
- })
26
- .then(() => true)
27
- .catch(() => false);
27
+ };
28
+ await prisma.match.deleteMany({ where });
29
+ return true;
30
+ }
31
+ catch (error) {
32
+ // An unsupported storage ID should behave like a filter that matched no rows.
33
+ if (error instanceof prisma_id_1.InvalidPrismaIdError) {
34
+ return true;
35
+ }
36
+ return false;
37
+ }
28
38
  }
29
- exports.handleMatchDelete = handleMatchDelete;
@@ -1,3 +1,3 @@
1
1
  import { PrismaClient } from '@prisma/client';
2
- import { DataTypes } from 'brackets-manager/dist/types';
2
+ import { DataTypes } from 'brackets-model';
3
3
  export declare function handleParticipantDelete(prisma: PrismaClient, filter?: Partial<DataTypes['participant']>): Promise<boolean>;
@@ -1,23 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleParticipantDelete = void 0;
3
+ exports.handleParticipantDelete = handleParticipantDelete;
4
+ const prisma_id_1 = require("../../prisma-id");
4
5
  async function handleParticipantDelete(prisma, filter) {
5
6
  // No filter so delete everything
6
7
  if (!filter) {
7
- return prisma.participant
8
- .deleteMany({})
9
- .then(() => true)
10
- .catch(() => false);
8
+ try {
9
+ await prisma.participant.deleteMany({});
10
+ return true;
11
+ }
12
+ catch {
13
+ return false;
14
+ }
11
15
  }
12
- return prisma.participant
13
- .deleteMany({
14
- where: {
15
- id: filter.id,
16
+ try {
17
+ const where = {
18
+ id: (0, prisma_id_1.toPrismaId)(filter.id),
16
19
  name: filter.name,
17
- tournamentId: filter.tournament_id,
18
- },
19
- })
20
- .then(() => true)
21
- .catch(() => false);
20
+ tournamentId: (0, prisma_id_1.toPrismaId)(filter.tournament_id),
21
+ };
22
+ await prisma.participant.deleteMany({ where });
23
+ return true;
24
+ }
25
+ catch (error) {
26
+ // An unsupported storage ID should behave like a filter that matched no rows.
27
+ if (error instanceof prisma_id_1.InvalidPrismaIdError) {
28
+ return true;
29
+ }
30
+ return false;
31
+ }
22
32
  }
23
- exports.handleParticipantDelete = handleParticipantDelete;
@@ -1,3 +1,3 @@
1
- import { DataTypes } from 'brackets-manager/dist/types';
1
+ import { DataTypes } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
3
  export declare function handleRoundDelete(prisma: PrismaClient, filter?: Partial<DataTypes['round']>): Promise<boolean>;
@@ -1,24 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleRoundDelete = void 0;
3
+ exports.handleRoundDelete = handleRoundDelete;
4
+ const prisma_id_1 = require("../../prisma-id");
4
5
  async function handleRoundDelete(prisma, filter) {
5
6
  // No filter so delete everything
6
7
  if (!filter) {
7
- return prisma.round
8
- .deleteMany({})
9
- .then(() => true)
10
- .catch(() => false);
8
+ try {
9
+ await prisma.round.deleteMany({});
10
+ return true;
11
+ }
12
+ catch {
13
+ return false;
14
+ }
11
15
  }
12
- return prisma.round
13
- .deleteMany({
14
- where: {
15
- id: filter.id,
16
- stageId: filter.stage_id,
17
- groupId: filter.group_id,
16
+ try {
17
+ const where = {
18
+ id: (0, prisma_id_1.toPrismaId)(filter.id),
19
+ stageId: (0, prisma_id_1.toPrismaId)(filter.stage_id),
20
+ groupId: (0, prisma_id_1.toPrismaId)(filter.group_id),
18
21
  number: filter.number,
19
- },
20
- })
21
- .then(() => true)
22
- .catch(() => false);
22
+ };
23
+ await prisma.round.deleteMany({ where });
24
+ return true;
25
+ }
26
+ catch (error) {
27
+ // An unsupported storage ID should behave like a filter that matched no rows.
28
+ if (error instanceof prisma_id_1.InvalidPrismaIdError) {
29
+ return true;
30
+ }
31
+ return false;
32
+ }
23
33
  }
24
- exports.handleRoundDelete = handleRoundDelete;
@@ -1,3 +1,3 @@
1
- import { DataTypes } from 'brackets-manager/dist/types';
1
+ import { DataTypes } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
3
  export declare function handleStageDelete(prisma: PrismaClient, filter?: Partial<DataTypes['stage']>): Promise<boolean>;
@@ -1,47 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleStageDelete = void 0;
3
+ exports.handleStageDelete = handleStageDelete;
4
4
  const transformers_1 = require("../../transformers");
5
+ const prisma_id_1 = require("../../prisma-id");
5
6
  async function handleStageDelete(prisma, filter) {
6
- return prisma
7
- .$transaction(async (tx) => {
8
- // Build where clause if a filter is provided
7
+ try {
9
8
  const where = filter
10
9
  ? {
11
- id: filter.id,
10
+ id: (0, prisma_id_1.toPrismaId)(filter.id),
12
11
  name: filter.name,
13
12
  number: filter.number,
14
- tournamentId: filter.tournament_id,
13
+ tournamentId: (0, prisma_id_1.toPrismaId)(filter.tournament_id),
15
14
  type: filter.type
16
15
  ? transformers_1.StageTypeTransformer.to(filter.type)
17
16
  : undefined,
18
17
  }
19
18
  : undefined;
20
- if (!where) {
21
- // No filter: delete in the right order to satisfy FK constraints
22
- await tx.stageSettings.deleteMany({});
23
- await tx.stage.deleteMany({});
24
- return true;
25
- }
26
- // Filtered delete: find matching stages
27
- const stages = await tx.stage.findMany({
28
- where,
29
- select: { id: true },
30
- });
31
- if (stages.length === 0)
19
+ await prisma.$transaction(async (tx) => {
20
+ if (!where) {
21
+ // No filter: delete in the right order to satisfy FK constraints
22
+ await tx.stageSettings.deleteMany({});
23
+ await tx.stage.deleteMany({});
24
+ return true;
25
+ }
26
+ // Filtered delete: find matching stages
27
+ const stages = await tx.stage.findMany({
28
+ where,
29
+ select: { id: true },
30
+ });
31
+ if (stages.length === 0)
32
+ return true;
33
+ // Delete related StageSettings first to satisfy FK constraints
34
+ await tx.stageSettings.deleteMany({
35
+ where: { stageId: { in: stages.map((s) => s.id) } },
36
+ });
37
+ // Then delete the stages
38
+ await tx.stage.deleteMany({ where });
32
39
  return true;
33
- // Delete related StageSettings first to satisfy FK constraints
34
- await tx.stageSettings.deleteMany({
35
- where: { stageId: { in: stages.map((s) => s.id) } },
36
40
  });
37
- // Then delete the stages
38
- await tx.stage.deleteMany({ where });
39
41
  return true;
40
- })
41
- .then(() => true)
42
- .catch((e) => {
43
- console.error(new Error(`Error deleting stages with filter ${JSON.stringify(filter)}`, { cause: e }));
42
+ }
43
+ catch (error) {
44
+ // An unsupported storage ID should behave like a filter that matched no rows.
45
+ if (error instanceof prisma_id_1.InvalidPrismaIdError) {
46
+ return true;
47
+ }
44
48
  return false;
45
- });
49
+ }
46
50
  }
47
- exports.handleStageDelete = handleStageDelete;
@@ -1,3 +1,3 @@
1
- import { DataTypes } from 'brackets-manager/dist/types';
1
+ import { DataTypes } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
3
  export declare function handleDelete<T extends keyof DataTypes>(prisma: PrismaClient, table: T, filter?: Partial<DataTypes[T]>): Promise<boolean>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleDelete = void 0;
3
+ exports.handleDelete = handleDelete;
4
4
  const delete_handlers_1 = require("./delete-handlers");
5
5
  // Can't be named `delete` because its a reserved word...
6
6
  async function handleDelete(prisma, table, filter) {
@@ -21,4 +21,3 @@ async function handleDelete(prisma, table, filter) {
21
21
  return false;
22
22
  }
23
23
  }
24
- exports.handleDelete = handleDelete;
@@ -1,3 +1,3 @@
1
- import { DataTypes, OmitId } from 'brackets-manager/dist/types';
1
+ import { DataTypes, OmitId } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
- export declare function handleGroupInsert(prisma: PrismaClient, values: OmitId<DataTypes['group']> | OmitId<DataTypes['group']>[]): Promise<number> | Promise<boolean>;
3
+ export declare function handleGroupInsert(prisma: PrismaClient, values: OmitId<DataTypes['group']> | OmitId<DataTypes['group']>[]): Promise<number | boolean>;
@@ -1,21 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleGroupInsert = void 0;
3
+ exports.handleGroupInsert = handleGroupInsert;
4
4
  const transformers_1 = require("../../transformers");
5
- function handleGroupInsert(prisma, values) {
6
- if (Array.isArray(values)) {
7
- return prisma.group
8
- .createMany({
9
- data: values.map(transformers_1.GroupTransformer.to),
10
- })
11
- .then(() => true)
12
- .catch(() => false);
5
+ async function handleGroupInsert(prisma, values) {
6
+ try {
7
+ if (Array.isArray(values)) {
8
+ await prisma.group.createMany({
9
+ data: values.map(transformers_1.GroupTransformer.to),
10
+ });
11
+ return true;
12
+ }
13
+ const group = await prisma.group.create({
14
+ data: transformers_1.GroupTransformer.to(values),
15
+ });
16
+ return group.id;
17
+ }
18
+ catch {
19
+ return Array.isArray(values) ? false : -1;
13
20
  }
14
- return prisma.group
15
- .create({
16
- data: transformers_1.GroupTransformer.to(values),
17
- })
18
- .then((v) => v.id)
19
- .catch(() => -1);
20
21
  }
21
- exports.handleGroupInsert = handleGroupInsert;
@@ -1,3 +1,4 @@
1
- import { DataTypes, OmitId } from 'brackets-manager/dist/types';
1
+ import { OmitId } from 'brackets-model';
2
2
  import { PrismaClient } from '@prisma/client';
3
- export declare function handleMatchGameInsert(prisma: PrismaClient, values: OmitId<DataTypes['match_game']> | OmitId<DataTypes['match_game']>[]): Promise<number> | Promise<boolean>;
3
+ import type { MatchGameWithExtra } from '../../types';
4
+ export declare function handleMatchGameInsert(prisma: PrismaClient, values: OmitId<MatchGameWithExtra> | OmitId<MatchGameWithExtra>[]): Promise<number | boolean>;
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleMatchGameInsert = void 0;
3
+ exports.handleMatchGameInsert = handleMatchGameInsert;
4
4
  const transformers_1 = require("../../transformers");
5
+ const prisma_id_1 = require("../../prisma-id");
5
6
  function getCreationData(value) {
6
7
  return {
7
8
  ...transformers_1.MatchGameTransformer.to(value),
9
+ extra: value.extra ?? undefined,
8
10
  opponent1Result: value.opponent1
9
11
  ? {
10
12
  create: {
11
- participantId: value.opponent1.id,
13
+ participantId: (0, prisma_id_1.toPrismaId)(value.opponent1.id),
12
14
  forfeit: value.opponent1.forfeit,
13
15
  position: value.opponent1.position,
14
16
  score: value.opponent1.score,
@@ -21,7 +23,7 @@ function getCreationData(value) {
21
23
  opponent2Result: value.opponent2
22
24
  ? {
23
25
  create: {
24
- participantId: value.opponent2.id,
26
+ participantId: (0, prisma_id_1.toPrismaId)(value.opponent2.id),
25
27
  forfeit: value.opponent2.forfeit,
26
28
  position: value.opponent2.position,
27
29
  score: value.opponent2.score,
@@ -33,20 +35,20 @@ function getCreationData(value) {
33
35
  : undefined,
34
36
  };
35
37
  }
36
- function handleMatchGameInsert(prisma, values) {
37
- if (Array.isArray(values)) {
38
- return prisma.matchGame
39
- .createMany({
40
- data: values.map((v) => getCreationData(v)),
41
- })
42
- .then(() => true)
43
- .catch(() => false);
38
+ async function handleMatchGameInsert(prisma, values) {
39
+ try {
40
+ if (Array.isArray(values)) {
41
+ await prisma.matchGame.createMany({
42
+ data: values.map((v) => getCreationData(v)),
43
+ });
44
+ return true;
45
+ }
46
+ const matchGame = await prisma.matchGame.create({
47
+ data: getCreationData(values),
48
+ });
49
+ return matchGame.id;
50
+ }
51
+ catch {
52
+ return Array.isArray(values) ? false : -1;
44
53
  }
45
- return prisma.matchGame
46
- .create({
47
- data: getCreationData(values),
48
- })
49
- .then((v) => v.id)
50
- .catch(() => -1);
51
54
  }
52
- exports.handleMatchGameInsert = handleMatchGameInsert;