Bordeaux JS edition
🤮Webpack, les ORMs
😍JOOQ, Mapstruct
function handleLang(lang) {
// later...
const country = lang
// ⬆️ Seems legit with "fr" doesn't it ? 😱
}
handleLang("fr")
// Better...
type LanguageCode = "fr"|"en"|"sp"
type CountryCode = "fr"|"uk"|"sp"
function handleLang(lang: LanguageCode) {
// later...
const country: CountryCode = lang
// ⬆️ Doesn't compile
// Type 'LanguageCode' is not assignable to type 'CountryCode'.
// Type '"en"' is not assignable to type 'CountryCode'.
}
handleLang("fr")
À quoi ressemblerait une application fullstack "monoglote" ?
@Controller("/checkins")
export class CheckinsController {
constructor(private readonly appService: AppService) {}
@Post("/:year")
async createThenGetAllCheckins(
@Param("year", ParseIntPipe) year: number,
@Body() checkins: Checkin[]
): Promise<Checkin[]> {
const persistedCheckins = await this.appService.createThenGetAllCheckins(year, checkins);
return persistedCheckins;
}
}
Quick setup for Frontend+Backend
// Consuming
import {varName} from "module-name"
// Consuming
const moduleName_1 = require("module-name")
const varName = moduleName_1.varName
// Exporting
export const varName = "Hello world"
// Exporting
module.exports = {
varName: "Hello world"
}
// package.json du *module mjs* produit
{
"type": "module"
...
}
// package.json du *module cjs* produit
{
"type": "commonjs"
...
}
// tsconfig.json
{
...
"compilerOptions": {
"module": "ES2015",
"outdir": "dist/mjs"
...
}
}
// package.json
{
"name": "my-shared-module",
"main": "dist/cjs/domains.js",
"module": "dist/mjs/domains.js",
"types": "dist/mjs/domains.d.ts",
"exports": {
".": {
"require": "./dist/cjs/domains.js",
"import": "./dist/mjs/domains.js"
}
},
"scripts": {
"clean": "rm -fr dist/* && mkdir -p dist/cjs dist/mjs",
"build": "npm run clean && concurrently \"npm run build:mjs\" \"npm run build:cjs\"",
"build:mjs": "tsc -p src/ && ./scripts/fixup-dist-mjs-module",
"build:cjs": "tsc -p src/ --module commonjs --outdir dist/cjs && ./scripts/fixup-dist-cjs-module"
},
...
}
// tsconfig.json from shared module
{
"compilerOptions": {
...
"composite": true
...
}
}
// tsconfig.json from UI|SRV modules
{
"compilerOptions": {
...
"moduleResolution": "node",
"paths": {
"@shared/*": ["../shared/dist/mjs/*"]
}
...
},
"references": [
{ "path": "../shared/src/tsconfig.json" }
]
}
import {Hello} from '@shared/domains'
const hello: Hello = new Hello();
Setup d'un shared module
En parcourant le code de kermesse-tracker...
@Injectable()
export class FamiliesDAO {
constructor() {}
async countYearCount(year: number): Promise<number> {
return Number((
await kyselyDb.selectFrom('families')
.select(kyselyDb.fn.count<string>("id").as("cpt"))
.where("year", "=", year)
.executeTakeFirst()
).cpt);
}
async createFamiliesEntry(year: number, families: Family[]) {
const uuid = Db.newUUID();
await kyselyDb.insertInto('families').values({
id: uuid,
year,
content: JSON.stringify(families)
}).executeTakeFirstOrThrow()
}
}
Le source des live-coding :
https://github.com/fcamblor/kermesse-tracker
https://github.com/fcamblor/fullstack-ts-demo
Fullstack Typescript - BordeauxJS Edition
Frédéric CAMBLOR
@fcamblor
fcamblor
4SH