HATEOAS-first backend framework.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.ts 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {
  2. application, dataSources,
  3. resource,
  4. validation as v,
  5. } from '../../src';
  6. import {TEXT_SERIALIZER_PAIR} from './serializers';
  7. import {autoIncrement} from './data-source';
  8. const Piano = resource(v.object(
  9. {
  10. brand: v.string()
  11. },
  12. v.never()
  13. ))
  14. .name('Piano')
  15. .route('pianos')
  16. .canFetchItem()
  17. .canFetchCollection()
  18. .canCreate()
  19. .canEmplace()
  20. .canPatch()
  21. .canDelete()
  22. .id('id', {
  23. generationStrategy: autoIncrement,
  24. serialize: (id) => id?.toString() ?? '0',
  25. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  26. schema: v.number(),
  27. });
  28. const User = resource(v.object(
  29. {
  30. firstName: v.string(),
  31. middleName: v.string(),
  32. lastName: v.string(),
  33. bio: v.string(),
  34. birthday: v.datelike()
  35. },
  36. v.never()
  37. ))
  38. .name('User')
  39. .route('users')
  40. .fullText('bio')
  41. .id('id' as const, {
  42. generationStrategy: autoIncrement,
  43. serialize: (id) => id?.toString() ?? '0',
  44. deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0,
  45. schema: v.number(),
  46. });
  47. const app = application({
  48. name: 'piano-service',
  49. })
  50. .mediaType(TEXT_SERIALIZER_PAIR)
  51. .resource(Piano)
  52. .resource(User);
  53. const backend = app.createBackend({
  54. dataSource: new dataSources.jsonlFile.DataSource('examples/basic'),
  55. });
  56. const server = backend.createHttpServer({
  57. basePath: '/api'
  58. });
  59. server.listen(3000);
  60. setTimeout(() => {
  61. // Allow user operations after 5 seconds from startup
  62. User
  63. .canFetchItem()
  64. .canFetchCollection()
  65. .canCreate()
  66. .canPatch();
  67. }, 5000);