Skip to main content

Leverage a passthrough and condition

Commit Message

Leverage a passthrough and condition

to create a HasOne currentLocalizedText
association for displaying localized
text to Guests

```console
pnpm psy sync
pnpm uspec spec/unit/models/Host.spec.ts
pnpm uspec spec/unit/models/Place.spec.ts
pnpm uspec spec/unit/models/Room.spec.ts
pnpm uspec
```

Changes

diff --git a/api/spec/unit/models/Host.spec.ts b/api/spec/unit/models/Host.spec.ts
index cdcb8d6..79b1487 100644
--- a/api/spec/unit/models/Host.spec.ts
+++ b/api/spec/unit/models/Host.spec.ts
@@ -28,4 +28,13 @@ describe('Host', () => {
expect(localizedText.locale).toEqual('en-US')
})
})
+
+ it('has one currentLocalizedText', async () => {
+ let host = await createHost()
+ const esLocalizedText = await createLocalizedText({ localizable: host, locale: 'es-ES' })
+
+ host = await host.passthrough({ locale: 'es-ES' }).load('currentLocalizedText').execute()
+
+ expect(host.currentLocalizedText).toMatchDreamModel(esLocalizedText)
+ })
})
diff --git a/api/spec/unit/models/Place.spec.ts b/api/spec/unit/models/Place.spec.ts
index 7399652..c4f54f5 100644
--- a/api/spec/unit/models/Place.spec.ts
+++ b/api/spec/unit/models/Place.spec.ts
@@ -29,4 +29,13 @@ describe('Place', () => {
expect(localizedText.title).toEqual('My cottage')
})
})
+
+ it('has one currentLocalizedText', async () => {
+ let place = await createPlace()
+ const esLocalizedText = await createLocalizedText({ localizable: place, locale: 'es-ES' })
+
+ place = await place.passthrough({ locale: 'es-ES' }).load('currentLocalizedText').execute()
+
+ expect(place.currentLocalizedText).toMatchDreamModel(esLocalizedText)
+ })
})
diff --git a/api/spec/unit/models/Room.spec.ts b/api/spec/unit/models/Room.spec.ts
index d888641..2e966a8 100644
--- a/api/spec/unit/models/Room.spec.ts
+++ b/api/spec/unit/models/Room.spec.ts
@@ -18,4 +18,13 @@ describe('Room', () => {
expect(localizedText.locale).toEqual('en-US')
})
})
+
+ it('has one currentLocalizedText', async () => {
+ let room = await createDen()
+ const esLocalizedText = await createLocalizedText({ localizable: room, locale: 'es-ES' })
+
+ room = await room.passthrough({ locale: 'es-ES' }).load('currentLocalizedText').execute()
+
+ expect(room.currentLocalizedText).toMatchDreamModel(esLocalizedText)
+ })
})
diff --git a/api/src/app/models/Host.ts b/api/src/app/models/Host.ts
index 7e4b6b4..075e112 100644
--- a/api/src/app/models/Host.ts
+++ b/api/src/app/models/Host.ts
@@ -1,4 +1,4 @@
-import { Decorators, SoftDelete } from '@rvoh/dream'
+import { Decorators, DreamConst, SoftDelete } from '@rvoh/dream'
import { DreamColumn, DreamSerializers } from '@rvoh/dream/types'
import ApplicationModel from '@models/ApplicationModel.js'
import HostPlace from '@models/HostPlace.js'
@@ -43,4 +43,11 @@ export default class Host extends ApplicationModel {
public async createDefaultLocalizedText(this: Host) {
await this.createAssociation('localizedTexts', { locale: 'en-US' })
}
+
+ @deco.HasOne('LocalizedText', {
+ polymorphic: true,
+ on: 'localizableId',
+ and: { locale: DreamConst.passthrough },
+ })
+ public currentLocalizedText: LocalizedText
}
diff --git a/api/src/app/models/Place.ts b/api/src/app/models/Place.ts
index 0715f2c..a1be52d 100644
--- a/api/src/app/models/Place.ts
+++ b/api/src/app/models/Place.ts
@@ -1,4 +1,4 @@
-import { Decorators, SoftDelete } from '@rvoh/dream'
+import { Decorators, DreamConst, SoftDelete } from '@rvoh/dream'
import { DreamColumn, DreamSerializers } from '@rvoh/dream/types'
import ApplicationModel from '@models/ApplicationModel.js'
import Host from '@models/Host.js'
@@ -45,4 +45,11 @@ export default class Place extends ApplicationModel {
public async createDefaultLocalizedText(this: Place) {
await this.createAssociation('localizedTexts', { locale: 'en-US', title: `My ${this.style}` })
}
+
+ @deco.HasOne('LocalizedText', {
+ polymorphic: true,
+ on: 'localizableId',
+ and: { locale: DreamConst.passthrough },
+ })
+ public currentLocalizedText: LocalizedText
}
diff --git a/api/src/app/models/Room.ts b/api/src/app/models/Room.ts
index 9e90a30..2b15ff5 100644
--- a/api/src/app/models/Room.ts
+++ b/api/src/app/models/Room.ts
@@ -1,4 +1,4 @@
-import { Decorators, SoftDelete } from '@rvoh/dream'
+import { Decorators, DreamConst, SoftDelete } from '@rvoh/dream'
import { DreamColumn, DreamSerializers } from '@rvoh/dream/types'
import ApplicationModel from '@models/ApplicationModel.js'
import LocalizedText from '@models/LocalizedText.js'
@@ -37,4 +37,11 @@ export default class Room extends ApplicationModel {
public async createDefaultLocalizedText(this: Room) {
await this.createAssociation('localizedTexts', { locale: 'en-US' })
}
+
+ @deco.HasOne('LocalizedText', {
+ polymorphic: true,
+ on: 'localizableId',
+ and: { locale: DreamConst.passthrough },
+ })
+ public currentLocalizedText: LocalizedText
}
diff --git a/api/src/types/dream.ts b/api/src/types/dream.ts
index 188991c..d73f541 100644
--- a/api/src/types/dream.ts
+++ b/api/src/types/dream.ts
@@ -295,6 +295,15 @@ export const schema = {
},
virtualColumns: [],
associations: {
+ currentLocalizedText: {
+ type: 'HasOne',
+ foreignKey: 'localizableId',
+ foreignKeyTypeColumn: 'localizableType',
+ tables: ['localized_texts'],
+ optional: null,
+ requiredAndClauses: null,
+ passthroughAndClauses: ['locale'],
+ },
hostPlaces: {
type: 'HasMany',
foreignKey: 'hostId',
@@ -528,6 +537,15 @@ export const schema = {
},
virtualColumns: [],
associations: {
+ currentLocalizedText: {
+ type: 'HasOne',
+ foreignKey: 'localizableId',
+ foreignKeyTypeColumn: 'localizableType',
+ tables: ['localized_texts'],
+ optional: null,
+ requiredAndClauses: null,
+ passthroughAndClauses: ['locale'],
+ },
hostPlaces: {
type: 'HasMany',
foreignKey: 'placeId',
@@ -678,6 +696,15 @@ export const schema = {
},
virtualColumns: [],
associations: {
+ currentLocalizedText: {
+ type: 'HasOne',
+ foreignKey: 'localizableId',
+ foreignKeyTypeColumn: 'localizableType',
+ tables: ['localized_texts'],
+ optional: null,
+ requiredAndClauses: null,
+ passthroughAndClauses: ['locale'],
+ },
localizedTexts: {
type: 'HasMany',
foreignKey: 'localizableId',
@@ -793,7 +820,7 @@ export const schema = {
} as const

export const connectionTypeConfig = {
- passthroughColumns: [],
+ passthroughColumns: ['locale'],
allDefaultScopeNames: ['dream:STI', 'dream:SoftDelete'],
globalNames: {
models: {