Skip to content

Commit 40c07c0

Browse files
committed
Refactor the remaining db labels to pull from EnhancedResource
For the record, I'm not sure that I like the `"ScriptureRange" -> ScriptureRange.dbLabel` change. Fewer magic strings seem better, but maybe this is more cognitive overhead or layers of indirection?
1 parent 2c021c4 commit 40c07c0

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/components/product-progress/product-progress.repository.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { stripIndent } from 'common-tags';
33
import { node, type Query, relation } from 'cypher-query-builder';
44
import { DateTime } from 'luxon';
55
import {
6+
EnhancedResource,
67
generateId,
7-
getDbClassLabels,
88
type ID,
99
NotFoundException,
1010
type Session,
@@ -25,15 +25,18 @@ import { PeriodicReportService } from '../periodic-report';
2525
import { ReportType } from '../periodic-report/dto';
2626
import { type ProductStep } from '../product/dto';
2727
import {
28-
ProductProgress,
2928
type ProductProgressInput,
3029
type ProgressVariant,
3130
type ProgressVariantByProductInput,
3231
type ProgressVariantByReportInput,
33-
StepProgress,
32+
ProductProgress as RawProductProgress,
33+
StepProgress as RawStepProgress,
3434
type UnsecuredProductProgress,
3535
} from './dto';
3636

37+
const ProductProgress = EnhancedResource.of(RawProductProgress);
38+
const StepProgress = EnhancedResource.of(RawStepProgress);
39+
3740
@Injectable()
3841
export class ProductProgressRepository {
3942
constructor(
@@ -169,7 +172,7 @@ export class ProductProgressRepository {
169172
.optionalMatch([
170173
node('report'),
171174
relation('out', '', 'progress', ACTIVE),
172-
node('progress', 'ProductProgress', {
175+
node('progress', ProductProgress.dbLabel, {
173176
variant: variable('variant'),
174177
}),
175178
relation('in', '', 'progress', ACTIVE),
@@ -191,7 +194,7 @@ export class ProductProgressRepository {
191194
.match([
192195
node('progress'),
193196
relation('out', '', 'step', ACTIVE),
194-
node('stepNode', 'StepProgress'),
197+
node('stepNode', StepProgress.dbLabel),
195198
])
196199
.apply(matchProps({ nodeName: 'stepNode', outputVar: 'step' }))
197200
.return(collect('step').as('steps')),
@@ -249,7 +252,7 @@ export class ProductProgressRepository {
249252
.merge([
250253
node('product'),
251254
relation('out', 'productProgressRel', 'progress', ACTIVE),
252-
node('progress', 'ProductProgress', {
255+
node('progress', ProductProgress.dbLabel, {
253256
variant: variable('variant'),
254257
}),
255258
relation('in', 'reportProgressRel', 'progress', ACTIVE),
@@ -258,7 +261,7 @@ export class ProductProgressRepository {
258261
.onCreate.set(
259262
{
260263
labels: {
261-
progress: getDbClassLabels(ProductProgress),
264+
progress: ProductProgress.dbLabels,
262265
},
263266
values: {
264267
progress: { id: tempProgressId, createdAt },
@@ -282,14 +285,14 @@ export class ProductProgressRepository {
282285
.merge([
283286
node('progress'),
284287
relation('out', 'progressStepRel', 'step', ACTIVE),
285-
node('stepNode', 'StepProgress', {
288+
node('stepNode', StepProgress.dbLabel, {
286289
step: variable('stepInput.step'),
287290
}),
288291
])
289292
.onCreate.set(
290293
{
291294
labels: {
292-
stepNode: getDbClassLabels(StepProgress),
295+
stepNode: StepProgress.dbLabels,
293296
},
294297
values: {
295298
stepNode: { createdAt },

src/components/product/product.repository.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { DateTime } from 'luxon';
1212
import { type Except, type Merge } from 'type-fest';
1313
import {
1414
CreationFailed,
15-
getDbClassLabels,
15+
EnhancedResource,
1616
type ID,
1717
type Range,
1818
type Session,
@@ -36,7 +36,7 @@ import {
3636
} from '~/core/database/query';
3737
import { ScriptureReferenceRepository } from '../scripture';
3838
import {
39-
ScriptureRange,
39+
ScriptureRange as RawScriptureRange,
4040
type ScriptureRangeInput,
4141
type UnspecifiedScripturePortion,
4242
type UnspecifiedScripturePortionInput,
@@ -60,6 +60,8 @@ import {
6060
type UpdateDirectScriptureProduct,
6161
} from './dto';
6262

63+
const ScriptureRange = EnhancedResource.of(RawScriptureRange);
64+
6365
export type HydratedProductRow = Merge<
6466
Omit<
6567
DbTypeOf<
@@ -109,7 +111,7 @@ export class ProductRepository extends CommonRepository {
109111
.match([
110112
node('node'),
111113
relation('out', '', 'scriptureReferences', ACTIVE),
112-
node('scriptureRanges', 'ScriptureRange'),
114+
node('scriptureRanges', ScriptureRange.dbLabel),
113115
])
114116
.return(
115117
collect('scriptureRanges { .start, .end }').as('scriptureRanges'),
@@ -332,8 +334,8 @@ export class ProductRepository extends CommonRepository {
332334
[
333335
node('node'),
334336
relation('out', '', label, ACTIVE),
335-
node('', getDbClassLabels(ScriptureRange), {
336-
...ScriptureRange.fromReferences(range),
337+
node('', ScriptureRange.dbLabels, {
338+
...ScriptureRange.type.fromReferences(range),
337339
createdAt,
338340
}),
339341
];

src/core/database/common.repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { DateTime } from 'luxon';
55
import {
66
DbUnique,
77
EnhancedResource,
8-
getDbClassLabels,
98
type ID,
109
InputException,
1110
isIdLike,
@@ -170,9 +169,10 @@ export class CommonRepository {
170169
}
171170

172171
protected getConstraintsFor(resource: ResourceShape<any>) {
172+
const { dbLabel } = EnhancedResource.of(resource);
173173
return [
174174
...(resource.Props.includes('id')
175-
? [createUniqueConstraint(getDbClassLabels(resource)[0], 'id')]
175+
? [createUniqueConstraint(dbLabel, 'id')]
176176
: []),
177177
...resource.Props.flatMap((prop) => {
178178
const label = DbUnique.get(resource, prop);

0 commit comments

Comments
 (0)