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.

50 regels
1.2 KiB

  1. import { resource, application, validation as v } from '@modal-sh/yasumi';
  2. import { http } from '@modal-sh/yasumi/backend';
  3. import { DuckDbDataSource, AutoincrementIdConfig } from '@modal-sh/yasumi-data-source-duckdb';
  4. import { constants } from 'http2';
  5. const Post = resource(
  6. v.object({
  7. title: v.string(),
  8. content: v.string(),
  9. })
  10. )
  11. .name('Post')
  12. .route('posts')
  13. .id('id', AutoincrementIdConfig)
  14. .createdAt('createdAt')
  15. .updatedAt('updatedAt')
  16. .canFetchItem()
  17. .canFetchCollection()
  18. .canCreate()
  19. .canEmplace()
  20. .canPatch()
  21. .canDelete();
  22. const app = application({
  23. name: 'duckdb-service'
  24. })
  25. .resource(Post);
  26. const backend = app.createBackend({
  27. dataSource: new DuckDbDataSource('test.db'),
  28. })
  29. .showTotalItemCountOnGetCollection()
  30. .throwsErrorOnDeletingNotFound();
  31. const server = backend.createHttpServer({
  32. basePath: '/api',
  33. })
  34. .defaultErrorHandler((_req, res) => () => {
  35. throw new http.ErrorPlainResponse('urlNotFound', {
  36. statusCode: constants.HTTP_STATUS_NOT_FOUND,
  37. res,
  38. });
  39. // throw new http.ErrorPlainResponse('notImplemented', {
  40. // statusCode: constants.HTTP_STATUS_NOT_IMPLEMENTED,
  41. // res,
  42. // });
  43. });
  44. server.listen(6969);