|
- import {
- application,
- resource,
- valibot as v,
- serializers,
- encodings,
- } from '../../src';
- import {TEXT_SERIALIZER_PAIR} from './serializers';
- import {autoIncrement, dataSource} from './data-source';
-
- const Piano = resource(v.object(
- {
- brand: v.string()
- },
- v.never()
- ))
- .name('Piano')
- .id('id', {
- generationStrategy: autoIncrement,
- serialize: (id) => id?.toString() ?? '0',
- deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
- });
-
- // TODO implement authentication and RBAC on each resource
-
- const User = resource(v.object(
- {
- firstName: v.string(),
- middleName: v.string(),
- lastName: v.string(),
- bio: v.string(),
- birthday: v.datelike()
- },
- v.never()
- ))
- .name('User')
- .fullText('bio')
- .id('id', {
- generationStrategy: autoIncrement,
- serialize: (id) => id?.toString() ?? '0',
- deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
- });
-
- const app = application({
- name: 'piano-service',
- dataSource,
- })
- .contentType('application/json', serializers.applicationJson)
- .contentType('text/json', serializers.textJson)
- .contentType('text/plain', TEXT_SERIALIZER_PAIR)
- .encoding('utf-8', encodings.utf8)
- .resource(Piano)
- .resource(User);
-
- const server = app.createServer({
- baseUrl: '/api'
- });
-
- server.listen(3000);
|