schema.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {expect} from 'chai';
  2. import {Schema} from './schema.js';
  3. import {Repository} from './repository/index.js';
  4. import {DefinitionRegistry} from './definition/index.js';
  5. describe('Schema', function () {
  6. describe('defineDatasource', function () {
  7. it('returns this', function () {
  8. const schema = new Schema();
  9. const res = schema.defineDatasource({
  10. name: 'datasource',
  11. adapter: 'memory',
  12. });
  13. expect(res).to.be.eq(schema);
  14. });
  15. it('sets the datasource definition', function () {
  16. const schema = new Schema();
  17. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  18. const res =
  19. schema.getService(DefinitionRegistry).getDatasource('datasource');
  20. expect(res).to.be.eql({name: 'datasource', adapter: 'memory'});
  21. });
  22. it('throws an error if the datasource name already defined', function () {
  23. const schema = new Schema();
  24. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  25. const throwable =
  26. () => schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  27. expect(throwable).to.throw(
  28. 'The datasource "datasource" is already defined.',
  29. );
  30. });
  31. });
  32. describe('defineModel', function () {
  33. it('returns this', function () {
  34. const schema = new Schema();
  35. const res = schema.defineModel({name: 'model'});
  36. expect(res).to.be.eq(schema);
  37. });
  38. it('sets the model definition', function () {
  39. const schema = new Schema();
  40. schema.defineModel({name: 'model'});
  41. const res = schema.getService(DefinitionRegistry).getModel('model');
  42. expect(res).to.be.eql({name: 'model'});
  43. });
  44. it('throws an error if the model name already defined', function () {
  45. const schema = new Schema();
  46. schema.defineModel({name: 'model'});
  47. const throwable = () => schema.defineModel({name: 'model'});
  48. expect(throwable).to.throw('The model "model" is already defined.');
  49. });
  50. });
  51. describe('getRepository', function () {
  52. it('returns a repository by the model name', function () {
  53. const schema = new Schema();
  54. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  55. schema.defineModel({name: 'model', datasource: 'datasource'});
  56. const res = schema.getRepository('model');
  57. expect(res).to.be.instanceof(Repository);
  58. });
  59. it('throws an error if the model is not defined', function () {
  60. const schema = new Schema();
  61. const throwable = () => schema.getRepository('model');
  62. expect(throwable).to.throw('The model "model" is not defined.');
  63. });
  64. it('uses generic types to define the repository type', function () {
  65. const schema = new Schema();
  66. schema.defineDatasource({name: 'datasource', adapter: 'memory'});
  67. schema.defineModel({name: 'model', datasource: 'datasource'});
  68. interface MyModel {
  69. myId: number;
  70. }
  71. const res1: Repository = schema.getRepository('model');
  72. const res2: Repository<MyModel, number, 'myId'> =
  73. schema.getRepository<MyModel, number, 'myId'>('model');
  74. expect(res1).to.be.eq(res2);
  75. });
  76. });
  77. });