HATEOAS-first backend framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.5 KiB

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