- import {
- application, dataSources,
- resource,
- validation as v,
- } from '../../src';
- import {TEXT_SERIALIZER_PAIR} from './serializers';
- import {autoIncrement} from './data-source';
-
- const Piano = resource(v.object(
- {
- brand: v.string()
- },
- v.never()
- ))
- .name('Piano')
- .route('pianos')
- .canFetchItem()
- .canFetchCollection()
- .canCreate()
- .canEmplace()
- .canPatch()
- .canDelete()
- .id('id', {
- generationStrategy: autoIncrement,
- serialize: (id) => id?.toString() ?? '0',
- deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
- schema: v.number(),
- });
-
- const User = resource(v.object(
- {
- firstName: v.string(),
- middleName: v.string(),
- lastName: v.string(),
- bio: v.string(),
- birthday: v.datelike()
- },
- v.never()
- ))
- .name('User')
- .route('users')
- .fullText('bio')
- .id('id' as const, {
- generationStrategy: autoIncrement,
- serialize: (id) => id?.toString() ?? '0',
- deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
- schema: v.number(),
- });
-
- const app = application({
- name: 'piano-service',
- })
- .mediaType(TEXT_SERIALIZER_PAIR)
- .resource(Piano)
- .resource(User);
-
- const backend = app.createBackend({
- dataSource: new dataSources.jsonlFile.DataSource('examples/basic'),
- });
-
- const server = backend.createHttpServer({
- basePath: '/api'
- });
-
- server.listen(3000);
-
- setTimeout(() => {
- // Allow user operations after 5 seconds from startup
- User
- .canFetchItem()
- .canFetchCollection()
- .canCreate()
- .canPatch();
- }, 5000);
|