database-schema.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {expect} from 'chai';
  2. import {Repository} from './repository/index.js';
  3. import {DatabaseSchema} from './database-schema.js';
  4. import {DefinitionRegistry} from './definition/index.js';
  5. describe('DatabaseSchema', function () {
  6. describe('defineDatasource', function () {
  7. it('returns this', function () {
  8. const dbs = new DatabaseSchema();
  9. const res = dbs.defineDatasource({
  10. name: 'datasource',
  11. adapter: 'memory',
  12. });
  13. expect(res).to.be.eq(dbs);
  14. });
  15. it('sets the datasource definition', function () {
  16. const dbs = new DatabaseSchema();
  17. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  18. const res =
  19. dbs.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 dbs = new DatabaseSchema();
  24. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  25. const throwable =
  26. () => dbs.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 dbs = new DatabaseSchema();
  35. const res = dbs.defineModel({name: 'model'});
  36. expect(res).to.be.eq(dbs);
  37. });
  38. it('sets the model definition', function () {
  39. const dbs = new DatabaseSchema();
  40. dbs.defineModel({name: 'model'});
  41. const res = dbs.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 dbs = new DatabaseSchema();
  46. dbs.defineModel({name: 'model'});
  47. const throwable = () => dbs.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 dbs = new DatabaseSchema();
  54. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  55. dbs.defineModel({name: 'model', datasource: 'datasource'});
  56. const res = dbs.getRepository('model');
  57. expect(res).to.be.instanceof(Repository);
  58. });
  59. it('throws an error if the model is not defined', function () {
  60. const dbs = new DatabaseSchema();
  61. const throwable = () => dbs.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 dbs = new DatabaseSchema();
  66. dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
  67. dbs.defineModel({name: 'model', datasource: 'datasource'});
  68. interface MyModel {
  69. myId: number;
  70. }
  71. const res1: Repository = dbs.getRepository('model');
  72. const res2: Repository<MyModel, number, 'myId'> =
  73. dbs.getRepository<MyModel, number, 'myId'>('model');
  74. expect(res1).to.be.eq(res2);
  75. });
  76. });
  77. });