@ditojs/server 2.100.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,177 +0,0 @@
1
- import {
2
- BelongsToOneRelation,
3
- HasOneRelation,
4
- HasOneThroughRelation,
5
- HasManyRelation,
6
- ManyToManyRelation
7
- } from 'objection'
8
- import { Model } from '../models/index.js'
9
- import {
10
- getRelationClass,
11
- convertRelation,
12
- addRelationSchemas
13
- } from './relations.js'
14
-
15
- describe('getRelationClass()', () => {
16
- it('returns the corresponding relation classes', () => {
17
- expect(getRelationClass('belongs-to')).toBe(BelongsToOneRelation)
18
- expect(getRelationClass('has-one')).toBe(HasOneRelation)
19
- expect(getRelationClass('has-one-through')).toBe(HasOneThroughRelation)
20
- expect(getRelationClass('has-many')).toBe(HasManyRelation)
21
- expect(getRelationClass('many-to-many')).toBe(ManyToManyRelation)
22
-
23
- expect(getRelationClass('belongsTo')).toBe(BelongsToOneRelation)
24
- expect(getRelationClass('hasOne')).toBe(HasOneRelation)
25
- expect(getRelationClass('hasOneThrough')).toBe(HasOneThroughRelation)
26
- expect(getRelationClass('hasMany')).toBe(HasManyRelation)
27
- expect(getRelationClass('manyToMany')).toBe(ManyToManyRelation)
28
-
29
- expect(getRelationClass('BelongsToOneRelation')).toBe(BelongsToOneRelation)
30
- expect(getRelationClass('HasOneRelation')).toBe(HasOneRelation)
31
- expect(getRelationClass('HasOneThroughRelation'))
32
- .toBe(HasOneThroughRelation)
33
- expect(getRelationClass('HasManyRelation')).toBe(HasManyRelation)
34
- expect(getRelationClass('ManyToManyRelation')).toBe(ManyToManyRelation)
35
- })
36
- })
37
-
38
- describe('convertRelation()', () => {
39
- class ModelOne extends Model {}
40
- class ModelTwo extends Model {}
41
-
42
- const models = { ModelOne, ModelTwo }
43
-
44
- it('converts a belongs-to relation to Objection.js format', () => {
45
- expect(
46
- convertRelation(
47
- {
48
- relation: 'belongs-to',
49
- from: 'ModelOne.modelTwoId',
50
- to: 'ModelTwo.id',
51
- modify: 'myScope'
52
- },
53
- models
54
- )
55
- ).toEqual({
56
- relation: BelongsToOneRelation,
57
- modelClass: ModelTwo,
58
- join: {
59
- from: 'ModelOne.modelTwoId',
60
- to: 'ModelTwo.id'
61
- },
62
- modify: 'myScope'
63
- })
64
- })
65
-
66
- it('converts a many-to-many relation to Objection.js format', () => {
67
- expect(
68
- convertRelation(
69
- {
70
- relation: 'many-to-many',
71
- from: 'ModelOne.id',
72
- to: 'ModelTwo.id',
73
- inverse: false
74
- },
75
- models
76
- )
77
- ).toEqual({
78
- relation: ManyToManyRelation,
79
- modelClass: ModelTwo,
80
- join: {
81
- from: 'ModelOne.id',
82
- through: {
83
- from: 'ModelOneModelTwo.modelOneId',
84
- to: 'ModelOneModelTwo.modelTwoId'
85
- },
86
- to: 'ModelTwo.id'
87
- }
88
- })
89
- })
90
-
91
- it('converts an inverse many-to-many relation to Objection.js format', () => {
92
- expect(
93
- convertRelation(
94
- {
95
- relation: 'many-to-many',
96
- from: 'ModelTwo.id',
97
- to: 'ModelOne.id',
98
- inverse: true
99
- },
100
- models
101
- )
102
- ).toEqual({
103
- relation: ManyToManyRelation,
104
- modelClass: ModelOne,
105
- join: {
106
- from: 'ModelTwo.id',
107
- through: {
108
- from: 'ModelOneModelTwo.modelTwoId',
109
- to: 'ModelOneModelTwo.modelOneId'
110
- },
111
- to: 'ModelOne.id'
112
- }
113
- })
114
- })
115
-
116
- it('preserves Objection.js relation format', () => {
117
- const relation = {
118
- relation: BelongsToOneRelation,
119
- modelClass: ModelTwo,
120
- join: {
121
- from: 'ModelOne.modelTwoId',
122
- to: 'ModelTwo.id'
123
- },
124
- modify: 'myScope'
125
- }
126
- expect(convertRelation(relation, models)).toEqual(relation)
127
- })
128
- })
129
-
130
- describe('addRelationSchemas()', () => {
131
- class ModelOne extends Model {
132
- static relations = {
133
- modelTwo: {
134
- relation: 'belongs-to',
135
- from: 'ModelOne.modelTwoId',
136
- to: 'ModelTwo.id'
137
- }
138
- }
139
- }
140
-
141
- class ModelTwo extends Model {
142
- static relations = {
143
- modelOnes: {
144
- relation: 'has-many',
145
- from: 'ModelTwo.id',
146
- to: 'ModelOne.modelTwoId',
147
- owner: true
148
- }
149
- }
150
- }
151
-
152
- // Add a mock app to both, to expose models:
153
- ModelOne.app = ModelTwo.app = {
154
- models: { ModelOne, ModelTwo }
155
- }
156
-
157
- it('adds correct property schema for a belongs-to relation', () => {
158
- expect(addRelationSchemas(ModelOne, {})).toEqual({
159
- modelTwo: {
160
- anyOf: [
161
- { type: 'null' },
162
- { relate: 'ModelTwo' },
163
- { $ref: 'ModelTwo' }
164
- ]
165
- }
166
- })
167
- })
168
-
169
- it('adds correct property schema for a has-many owner relation', () => {
170
- expect(addRelationSchemas(ModelTwo, {})).toEqual({
171
- modelOnes: {
172
- type: 'array',
173
- items: { $ref: 'ModelOne' }
174
- }
175
- })
176
- })
177
- })