Skip to content

Commit

Permalink
Adds explainations about DB relations and polymorphism to the contrib…
Browse files Browse the repository at this point in the history
…uting document + fix the Country enum to restore country filtering
  • Loading branch information
Grouloo committed Feb 3, 2025
1 parent e5dacf5 commit e4fa369
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
70 changes: 70 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,78 @@ const divisionResult = divide(6, 3).val // <-- Will be of type 'Error | number'

### Polymorphism and state management

We use tagged unions and pattern matching to handle some objects, such as the Hypermedia elements:

```ts
import { union, type InferUnion, match } from "shulk"

const Hypermedia = union<{
Text: { label: string; value: string }
Number: { label: string; value: number; unit?: string }
}>()
type Hypermedia = InferUnion<typeof Hypermedia>["any"]

function displayHypermedia(element: Hypermedia) {
const serialized = match(element).case({
Text: (element) => element.value,
Number: (element) => element.value + " " + element.unit,
})

console
log(serialized)
}

const textElement = Hypermedia.Text({ label: "My label", value: "My value" })
const numbeElement = Hypermedia.Number({
label: "My label",
value: 5,
unit: "kg",
})

displayHypermedia(textElement) // -> "My value"
displayHypermedia(numberElement) // -> "5 kg"
```

[Tagged unions documentation](https://shulk.org/docs/tagged-unions/)

### DB relations

Relations are handled, but in a clunky way for now. The fields of both tables are merged in a single object.

You'll have to declare the relation in the table definition.

Example with CAP registered parcels, which need a one-to-one with CAP codes:

```ts
interface CapParcel {
// Parcel properties
id: string
cap_crop_code: CapCode
city_name: string
shape: string
centroid: string
// CAP code property
cap_code: string
cap_label: string
production: string
cap_precision?: string
cap_category?: string
is_seed: boolean
year: number
}

const CapParcelTable = Table<CapParcel>({
table: "registered_graphic_parcels",
primaryKey: "id",
oneToOne: {
cap_crop_code: {
table: "master_crop_production_cap_codes",
primaryKey: "cap_code",
},
},
})
```

### Translations

Translations are stored in the file `src/assets/translations.csv`
Expand Down
2 changes: 1 addition & 1 deletion src/types/Country.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export enum Country {
FR = "fr",
FR = "FR",
}

0 comments on commit e4fa369

Please sign in to comment.