vona-module-test-vona 5.0.39 → 5.0.42

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.
Files changed (123) hide show
  1. package/dist/.metadata/index.d.ts +62 -29
  2. package/dist/controller/dtoTest.d.ts +1 -1
  3. package/dist/index.js +401 -158
  4. package/dist/service/category.d.ts +5 -0
  5. package/dist/service/order.d.ts +4 -0
  6. package/dist/service/post.d.ts +4 -2
  7. package/dist/service/user.d.ts +4 -0
  8. package/package.json +4 -2
  9. package/src/.metadata/index.ts +1771 -0
  10. package/src/.metadata/this.ts +2 -0
  11. package/src/bean/aop.regExp.ts +30 -0
  12. package/src/bean/aop.simple.ts +58 -0
  13. package/src/bean/aopMethod.test.ts +42 -0
  14. package/src/bean/bean.testCtx.ts +55 -0
  15. package/src/bean/broadcast.test.ts +22 -0
  16. package/src/bean/cacheMem.test.ts +7 -0
  17. package/src/bean/cacheRedis.post.ts +10 -0
  18. package/src/bean/cacheRedis.test.ts +7 -0
  19. package/src/bean/event.helloEcho.ts +8 -0
  20. package/src/bean/eventListener.helloEcho.ts +21 -0
  21. package/src/bean/meta.version.ts +81 -0
  22. package/src/bean/queue.test.ts +18 -0
  23. package/src/bean/schedule.test.ts +12 -0
  24. package/src/bean/schedule.test3.ts +12 -0
  25. package/src/bean/summerCache.test.ts +29 -0
  26. package/src/config/config.ts +5 -0
  27. package/src/config/locale/en-us.ts +19 -0
  28. package/src/config/locale/zh-cn.ts +17 -0
  29. package/src/controller/bean.ts +95 -0
  30. package/src/controller/cacheMem.ts +63 -0
  31. package/src/controller/cacheRedis.ts +55 -0
  32. package/src/controller/dtoTest.ts +63 -0
  33. package/src/controller/guardPassport.ts +25 -0
  34. package/src/controller/onion.ts +92 -0
  35. package/src/controller/passport.ts +48 -0
  36. package/src/controller/performAction.ts +18 -0
  37. package/src/controller/queue.ts +25 -0
  38. package/src/controller/summer.ts +125 -0
  39. package/src/controller/tail.ts +32 -0
  40. package/src/controller/transaction.ts +29 -0
  41. package/src/controller/upload.ts +57 -0
  42. package/src/dto/categoryTree.ts +8 -0
  43. package/src/dto/orderCreate.ts +10 -0
  44. package/src/dto/orderResult.ts +9 -0
  45. package/src/dto/orderUpdate.ts +9 -0
  46. package/src/dto/postCreate.ts +9 -0
  47. package/src/dto/profile.ts +14 -0
  48. package/src/dto/roleLazy.ts +15 -0
  49. package/src/dto/user.ts +19 -0
  50. package/src/dto/userCreate.ts +8 -0
  51. package/src/dto/userLazy.ts +18 -0
  52. package/src/dto/userUpdate.ts +8 -0
  53. package/src/entity/category.ts +14 -0
  54. package/src/entity/order.ts +18 -0
  55. package/src/entity/post.ts +17 -0
  56. package/src/entity/postContent.ts +14 -0
  57. package/src/entity/product.ts +24 -0
  58. package/src/entity/role.ts +11 -0
  59. package/src/entity/roleUser.ts +14 -0
  60. package/src/entity/test.ts +18 -0
  61. package/src/entity/user.ts +17 -0
  62. package/src/index.ts +1 -0
  63. package/src/model/category.ts +20 -0
  64. package/src/model/categoryChain.ts +16 -0
  65. package/src/model/order.ts +26 -0
  66. package/src/model/orderStats.ts +27 -0
  67. package/src/model/post.ts +16 -0
  68. package/src/model/postContent.ts +17 -0
  69. package/src/model/product.ts +8 -0
  70. package/src/model/role.ts +15 -0
  71. package/src/model/roleUser.ts +8 -0
  72. package/src/model/test.ts +8 -0
  73. package/src/model/testDynamicTable.ts +21 -0
  74. package/src/model/user.ts +21 -0
  75. package/src/model/userStats.ts +15 -0
  76. package/src/model/userStatsGroup.ts +15 -0
  77. package/src/service/aopMethod.ts +38 -0
  78. package/src/service/caching.ts +75 -0
  79. package/src/service/category.ts +72 -0
  80. package/src/service/order.ts +65 -0
  81. package/src/service/post.ts +219 -0
  82. package/src/service/test.ts +9 -0
  83. package/src/service/testApp.ts +13 -0
  84. package/src/service/testClass.ts +15 -0
  85. package/src/service/testData.ts +67 -0
  86. package/src/service/transaction.ts +20 -0
  87. package/src/service/user.ts +68 -0
  88. package/test/aopMethod.test.ts +21 -0
  89. package/test/authSimple.test.ts +45 -0
  90. package/test/bean.test.ts +16 -0
  91. package/test/broadcast.test.ts +17 -0
  92. package/test/cache/cacheMem.test.ts +10 -0
  93. package/test/cache/cacheRedis.test.ts +10 -0
  94. package/test/cache/caching.test.ts +58 -0
  95. package/test/cache/summer.test.ts +10 -0
  96. package/test/database/database.test.ts +127 -0
  97. package/test/database/dtoAggregate.test.ts +44 -0
  98. package/test/database/dtoGet.test.ts +61 -0
  99. package/test/database/dtoGroup.test.ts +44 -0
  100. package/test/database/dtoLazy.test.ts +45 -0
  101. package/test/database/dtoMutate.test.ts +56 -0
  102. package/test/database/modelAggregate.test.ts +197 -0
  103. package/test/database/modelCache.test.ts +37 -0
  104. package/test/database/modelGeneral.test.ts +18 -0
  105. package/test/database/modelGroup.test.ts +211 -0
  106. package/test/database/modelRelations.test.ts +203 -0
  107. package/test/database/modelRelationsJoins.test.ts +39 -0
  108. package/test/database/modelRelationsMutate.test.ts +354 -0
  109. package/test/database/modelWhere.test.ts +138 -0
  110. package/test/database/orm.test.ts +22 -0
  111. package/test/database/transaction.test.ts +142 -0
  112. package/test/event.test.ts +14 -0
  113. package/test/guardPassport.test.ts +44 -0
  114. package/test/jwt.test.ts +27 -0
  115. package/test/locale.test.ts +51 -0
  116. package/test/passport.test.ts +57 -0
  117. package/test/queue.test.ts +11 -0
  118. package/test/tail.test.ts +21 -0
  119. package/test/upload.test.ts +58 -0
  120. package/test/utils/cabloyUtils.test.ts +22 -0
  121. package/test/utils/celjs.test.ts +26 -0
  122. package/test/utils/mappedTypes.test.ts +65 -0
  123. package/test/utils/performAction.test.ts +28 -0
@@ -0,0 +1,37 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { app } from 'vona-mock';
4
+
5
+ describe('modelCache.test.ts', () => {
6
+ it('action:modelCache:cacheQuery', async () => {
7
+ await app.bean.executor.mockCtx(async () => {
8
+ const prefix = 'action:modelCache:cacheQuery';
9
+ // scope
10
+ const scopeTest = app.bean.scope('test-vona');
11
+ // create
12
+ const user = await scopeTest.model.user.insert({ name: `${prefix}:1` });
13
+ // select
14
+ let items = await scopeTest.model.user.select({ where: { name: `${prefix}:1` } });
15
+ assert.equal(items.length, 1);
16
+ // select again
17
+ items = await scopeTest.model.user.select({ where: { name: `${prefix}:1` } });
18
+ assert.equal(items.length, 1);
19
+ // get
20
+ const user2 = await scopeTest.model.user.get({ id: user.id });
21
+ const user3 = await scopeTest.model.user.get({ name: `${prefix}:1` });
22
+ const user4 = await scopeTest.model.user.get({ name: `${prefix}:1` });
23
+ assert.equal(user2?.id, user3?.id);
24
+ assert.equal(user3?.id, user4?.id);
25
+ // update
26
+ await scopeTest.model.user.update({ id: user.id, name: `${prefix}:2` });
27
+ // select
28
+ items = await scopeTest.model.user.select({ where: { name: `${prefix}:2` } });
29
+ assert.equal(items.length, 1);
30
+ // delete
31
+ await scopeTest.model.user.delete({ name: `${prefix}:2` });
32
+ // select
33
+ items = await scopeTest.model.user.select({ where: { name: `${prefix}:2` } });
34
+ assert.equal(items.length, 0);
35
+ });
36
+ });
37
+ });
@@ -0,0 +1,18 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { isNil } from '@cabloy/utils';
4
+ import { app } from 'vona-mock';
5
+
6
+ describe('model.test.ts', () => {
7
+ it('action:model:defaultValue', async () => {
8
+ await app.bean.executor.mockCtx(async () => {
9
+ // scope
10
+ const scopeTest = app.bean.scope('test-vona');
11
+ // create
12
+ const item = await scopeTest.model.test.insert();
13
+ assert.equal(isNil(item.testDate), false);
14
+ // delete
15
+ await scopeTest.model.test.delete({ id: item.id });
16
+ });
17
+ });
18
+ });
@@ -0,0 +1,211 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { cast } from 'vona';
4
+ import { app } from 'vona-mock';
5
+ import { $relationDynamic } from 'vona-module-a-orm';
6
+ import { ModelPost, ModelRole, ModelRoleUser } from 'vona-module-test-vona';
7
+
8
+ describe('modelGroup.test.ts', () => {
9
+ it('action:modelGroup', async () => {
10
+ await app.bean.executor.mockCtx(async () => {
11
+ const prefix = 'action:modelGroup';
12
+ // scope
13
+ const scopeTest = app.bean.scope('test-vona');
14
+ // insert: roles
15
+ const roles = await scopeTest.model.role.insertBulk([
16
+ { name: `${prefix}:family` },
17
+ { name: `${prefix}:friend` },
18
+ ]);
19
+ assert.equal(roles.length, 2);
20
+ assert.equal(roles[0].id !== undefined, true);
21
+ // create: users
22
+ const users = await scopeTest.model.user.insertBulk([
23
+ {
24
+ name: `${prefix}:tom`,
25
+ age: 3,
26
+ posts: [
27
+ {
28
+ title: `${prefix}:postApple`,
29
+ stars: 2,
30
+ postContent: {
31
+ content: `${prefix}:postContentApple`,
32
+ },
33
+ },
34
+ {
35
+ title: `${prefix}:postApple2`,
36
+ stars: 3,
37
+ postContent: {
38
+ content: `${prefix}:postContentApple2`,
39
+ },
40
+ },
41
+ ],
42
+ roles: [{
43
+ id: roles[0].id,
44
+ }],
45
+ },
46
+ {
47
+ name: `${prefix}:jimmy`,
48
+ age: 5,
49
+ posts: [{
50
+ title: `${prefix}:postPear`,
51
+ stars: 4,
52
+ postContent: {
53
+ content: `${prefix}:postContentPear`,
54
+ },
55
+ }],
56
+ },
57
+ { name: `${prefix}:mike` },
58
+ ], {
59
+ include: {
60
+ posts: { include: { postContent: true } },
61
+ roles: true,
62
+ },
63
+ });
64
+ // group: user
65
+ const usersGroup = await scopeTest.model.user.group({
66
+ groups: ['name'],
67
+ aggrs: {
68
+ count: ['*', 'age'],
69
+ sum: ['age'],
70
+ avg: 'age',
71
+ max: 'age',
72
+ },
73
+ where: {
74
+ name: { _startsWith_: `${prefix}:` },
75
+ },
76
+ having: {
77
+ _and_: {
78
+ name: { _startsWith_: `${prefix}:` },
79
+ max_age: { _isNot_: null },
80
+ },
81
+ count_all: { _gt_: 0 },
82
+ },
83
+ orders: [['max_age', 'desc']],
84
+ });
85
+ assert.equal(usersGroup.length, 2);
86
+ assert.equal(usersGroup[0].name, `${prefix}:jimmy`);
87
+ assert.equal(usersGroup[0].count_all, 1);
88
+ assert.equal(usersGroup[0].count_age, 1);
89
+ assert.equal(usersGroup[0].sum_age, 5);
90
+ assert.equal(usersGroup[0].avg_age, 5);
91
+ assert.equal(usersGroup[0].max_age, 5);
92
+ // group: usersStats: posts: autoload
93
+ const usersStats = await scopeTest.model.userStatsGroup.select({
94
+ where: {
95
+ id: users.map(item => item.id),
96
+ },
97
+ orders: [['id', 'asc']],
98
+ include: {
99
+ roles: true,
100
+ },
101
+ });
102
+ assert.equal(usersStats.length, 3);
103
+ assert.equal(usersStats[0].posts[0].title, `${prefix}:postApple2`);
104
+ assert.equal(usersStats[0].posts[0].count_all, 1);
105
+ assert.equal(usersStats[0].posts[0].count_title, 1);
106
+ assert.equal(usersStats[0].posts[0].sum_stars, 3);
107
+ assert.equal(usersStats[0].roles[0].count_all, 1);
108
+ // group: usersStats: posts: mixed
109
+ const usersStats2 = await scopeTest.model.userStatsGroup.select({
110
+ where: {
111
+ id: users.map(item => item.id),
112
+ },
113
+ orders: [['id', 'asc']],
114
+ include: {
115
+ posts: { aggrs: { count: '*' } },
116
+ roles: true,
117
+ },
118
+ });
119
+ assert.equal(usersStats2.length, 3);
120
+ assert.equal(usersStats2[0].posts[0].title, `${prefix}:postApple2`);
121
+ assert.equal(usersStats2[0].posts[0].count_all, 1);
122
+ assert.equal(cast(usersStats2[0].posts[0]).count_title, undefined);
123
+ assert.equal(usersStats2[0].posts[0].sum_stars, 3);
124
+ assert.equal(usersStats2[0].roles[0].count_all, 1);
125
+ // group: usersStats: posts: disable
126
+ const usersStats3 = await scopeTest.model.userStatsGroup.select({
127
+ where: {
128
+ id: users.map(item => item.id),
129
+ },
130
+ orders: [['id', 'asc']],
131
+ include: {
132
+ posts: {
133
+ aggrs: {
134
+ count: ['*'],
135
+ sum: [],
136
+ },
137
+ },
138
+ roles: true,
139
+ },
140
+ });
141
+ assert.equal(usersStats3.length, 3);
142
+ assert.equal(usersStats3[0].posts[0].title, `${prefix}:postApple2`);
143
+ assert.equal(usersStats3[0].posts[0].count_all, 1);
144
+ assert.equal(cast(usersStats3[0].posts[0]).count_title, undefined);
145
+ assert.equal(cast(usersStats3[0].posts[0]).sum_stars, undefined);
146
+ assert.equal(usersStats3[0].roles[0].count_all, 1);
147
+ // group: usersStats: with
148
+ const usersStats4 = await scopeTest.model.userStatsGroup.select({
149
+ where: {
150
+ id: users.map(item => item.id),
151
+ },
152
+ orders: [['id', 'asc']],
153
+ include: {
154
+ posts: false,
155
+ roles: false,
156
+ },
157
+ with: {
158
+ posts: $relationDynamic.hasMany(() => ModelPost, 'userId', {
159
+ groups: ['title'],
160
+ aggrs: { count: '*' },
161
+ orders: [['title', 'desc']],
162
+ }, undefined, true),
163
+ roles: $relationDynamic.belongsToMany(() => ModelRoleUser, () => ModelRole, 'userId', 'roleId', {
164
+ groups: 'name',
165
+ aggrs: { count: '*' },
166
+ }),
167
+ },
168
+ });
169
+ assert.equal(usersStats4.length, 3);
170
+ assert.equal(usersStats4[0].posts[0].title, `${prefix}:postApple2`);
171
+ assert.equal(usersStats4[0].posts[0].count_all, 1);
172
+ assert.equal(cast(usersStats4[0].posts[0]).count_title, undefined);
173
+ assert.equal(cast(usersStats4[0].posts[0]).sum_stars, undefined);
174
+ assert.equal(usersStats4[0].roles[0].count_all, 1);
175
+ // group: get
176
+ const userStats4 = await scopeTest.model.userStatsGroup.get({
177
+ id: users[0].id,
178
+ }, {
179
+ include: {
180
+ posts: false,
181
+ roles: false,
182
+ },
183
+ with: {
184
+ posts: $relationDynamic.hasMany(() => ModelPost, 'userId', {
185
+ groups: ['title'],
186
+ aggrs: { count: '*' },
187
+ orders: [['title', 'desc']],
188
+ }, undefined, true),
189
+ roles: $relationDynamic.belongsToMany(() => ModelRoleUser, () => ModelRole, 'userId', 'roleId', {
190
+ groups: 'name',
191
+ aggrs: { count: '*' },
192
+ }),
193
+ },
194
+ });
195
+ assert.equal(userStats4?.posts[0].title, `${prefix}:postApple2`);
196
+ assert.equal(userStats4?.posts[0].count_all, 1);
197
+ assert.equal(cast(userStats4?.posts[0]).count_title, undefined);
198
+ assert.equal(cast(userStats4?.posts[0]).sum_stars, undefined);
199
+ assert.equal(userStats4?.roles[0].count_all, 1);
200
+ // delete: users
201
+ await scopeTest.model.user.deleteBulk(users.map(item => item.id), {
202
+ include: {
203
+ posts: { include: { postContent: true } },
204
+ roles: true,
205
+ },
206
+ });
207
+ // delete: roles
208
+ await scopeTest.model.role.deleteBulk(roles.map(item => item.id));
209
+ });
210
+ });
211
+ });
@@ -0,0 +1,203 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { cast } from 'vona';
4
+ import { app } from 'vona-mock';
5
+ import { $relationDynamic } from 'vona-module-a-orm';
6
+ import { ModelPost, ModelPostContent, ModelUser } from 'vona-module-test-vona';
7
+
8
+ describe('modelRelations.test.ts', () => {
9
+ it('action:modelRelations', async () => {
10
+ await app.bean.executor.mockCtx(async () => {
11
+ const prefix = 'action:modelRelations';
12
+ // scope
13
+ const scopeTest = app.bean.scope('test-vona');
14
+ // test data: create
15
+ const testData = await scopeTest.service.testData.create(prefix);
16
+ const {
17
+ userTom,
18
+ userJimmy,
19
+ roleFamily,
20
+ roleFriend,
21
+ postApple,
22
+ postPear,
23
+ postContentApple,
24
+ } = testData;
25
+ // relation: hasOne
26
+ const posts = await scopeTest.model.post.select({
27
+ columns: ['id', 'title', 'userId'],
28
+ where: {
29
+ id: [postApple.id, postPear.id],
30
+ },
31
+ orders: [['id', 'asc']],
32
+ include: { postContent: true },
33
+ });
34
+ assert.equal(posts.length, 2);
35
+ assert.equal(posts[0].postContent?.content, 'action:modelRelations:postContentApple');
36
+ assert.equal(cast(posts[0].postContent)?.iid, undefined);
37
+ assert.equal(posts[1].postContent, undefined);
38
+ // relation: hasOne: get
39
+ const postGet = await scopeTest.model.post.get(
40
+ { id: postApple.id },
41
+ { columns: 'id', include: { postContent: true } },
42
+ );
43
+ assert.equal(cast(postGet)?.iid, undefined);
44
+ assert.equal(postGet?.postContent?.content, 'action:modelRelations:postContentApple');
45
+ // relation: hasOne: columns
46
+ const postGetColumns = await scopeTest.model.post.get(
47
+ { id: postApple.id },
48
+ { columns: ['id'], include: { postContent: { columns: ['id'] } } },
49
+ );
50
+ assert.equal(cast(postGetColumns?.postContent)?.content, undefined);
51
+ assert.equal(Object.keys(postGetColumns!.postContent!).length, 1);
52
+ assert.equal(Object.keys(postGetColumns!).length, 1 + 2);// .user + .postContent
53
+ // relation: belongsTo
54
+ const postContents = await scopeTest.model.postContent.select({
55
+ where: {
56
+ id: postContentApple.id,
57
+ },
58
+ include: { post: true },
59
+ });
60
+ assert.equal(postContents.length, 1);
61
+ assert.equal(postContents[0].post?.title, 'action:modelRelations:postApple');
62
+ // relation: belongsTo: get
63
+ const postContentGet = await scopeTest.model.postContent.get(
64
+ { id: postContentApple.id },
65
+ { include: { post: true } },
66
+ );
67
+ assert.equal(postContentGet?.post?.title, 'action:modelRelations:postApple');
68
+ // relation: belongsTo: autoload
69
+ const postAutoload = await scopeTest.model.post.get(
70
+ { id: postApple.id },
71
+ );
72
+ assert.equal(postAutoload!.user?.id, postAutoload!.userId);
73
+ assert.equal(cast(postAutoload?.user)?.iid, undefined);
74
+ assert.equal(Object.keys(postAutoload!.user!).length, 2); // id + name
75
+ // relation: hasMany
76
+ const users = await scopeTest.model.user.select({
77
+ where: {
78
+ id: [userTom.id, userJimmy.id],
79
+ },
80
+ orders: [['id', 'asc']],
81
+ include: { posts: true },
82
+ });
83
+ assert.equal(users.length, 2);
84
+ assert.equal(users[0].posts.length, 2);
85
+ assert.equal(users[1].posts.length, 0);
86
+ // relation: hasMany: options.where/orders
87
+ const userOptions = await scopeTest.model.user.select({
88
+ where: {
89
+ id: [userTom.id, userJimmy.id],
90
+ },
91
+ orders: [['id', 'asc']],
92
+ include: { posts: {
93
+ where: {
94
+ id: { _in_: [postApple.id, postPear.id] },
95
+ },
96
+ orders: [['id', 'desc']],
97
+ } },
98
+ });
99
+ assert.equal(userOptions.length, 2);
100
+ assert.equal(userOptions[0].posts.length, 2);
101
+ assert.equal(userOptions[1].posts.length, 0);
102
+ assert.equal(cast(userOptions[0].posts[0]).iid, undefined);
103
+ assert.equal(Number.parseInt(userOptions[0].posts[0].id as any) > Number.parseInt(userOptions[0].posts[1].id as any), true);
104
+ // relation: hasMany: options.where/orders
105
+ const userOptions2 = await scopeTest.model.user.select({
106
+ where: {
107
+ id: [userTom.id, userJimmy.id],
108
+ },
109
+ orders: [['id', 'asc']],
110
+ include: {
111
+ posts: {
112
+ joins: [['innerJoin', 'testVonaPostContent', ['testVonaPost.id', 'testVonaPostContent.postId']]],
113
+ where: {
114
+ 'testVonaPost.id': { _in_: [postApple.id, postPear.id] },
115
+ '_and_': { 'testVonaPost.id': { _notIn_: [postApple.id, postPear.id] } },
116
+ 'testVonaPostContent.id': [postContentApple.id],
117
+ },
118
+ orders: [['testVonaPost.id', 'desc']],
119
+ },
120
+ roles: true,
121
+ },
122
+ });
123
+ assert.equal(userOptions2.length, 2);
124
+ assert.equal(userOptions2[0].posts.length, 0);
125
+ assert.equal(userOptions2[1].posts.length, 0);
126
+ // relation: hasMany: get
127
+ const userGet = await scopeTest.model.user.get(
128
+ { id: userTom.id },
129
+ { include: { posts: true } },
130
+ );
131
+ assert.equal(userGet?.posts.length, 2);
132
+ // relation: belongsToMany
133
+ const roles = await scopeTest.model.role.select({
134
+ where: {
135
+ id: [roleFamily.id, roleFriend.id],
136
+ },
137
+ orders: [['id', 'asc']],
138
+ include: { users: true },
139
+ });
140
+ assert.equal(roles.length, 2);
141
+ assert.equal(roles[0].users.length, 2);
142
+ assert.equal(roles[1].users.length, 1);
143
+ // relation: belongsToMany: get
144
+ const roleGet = await scopeTest.model.role.get(
145
+ { id: roleFamily.id },
146
+ { include: { users: true } },
147
+ );
148
+ assert.equal(roleGet?.users.length, 2);
149
+ assert.equal(cast(roleGet?.users[0]).iid, undefined);
150
+ // relation: belongsToMany: mget
151
+ const roles2 = await scopeTest.model.role.mget([roleFamily.id, roleFriend.id], { columns: ['id', 'name'], include: { users: true } });
152
+ assert.equal(roles2.length, 2);
153
+ assert.equal(cast(roles2[0]).iid, undefined);
154
+ assert.equal(roles2[0].users.length, 2);
155
+ assert.equal(roles2[1].users.length, 1);
156
+ // relation: include + with
157
+ const items = await scopeTest.model.post.select(
158
+ {
159
+ where: {
160
+ id: postApple.id,
161
+ },
162
+ include: {
163
+ postContent: {
164
+ include: {
165
+ post: { include: { user: { columns: ['id'] } }, meta: { client: '_auto_' } },
166
+ },
167
+ with: {
168
+ post3: $relationDynamic.belongsTo(() => ModelPostContent, () => ModelPost, 'postId', {
169
+ include: {
170
+ postContent: true,
171
+ },
172
+ }),
173
+ },
174
+ },
175
+ },
176
+ with: {
177
+ user3: $relationDynamic.belongsTo(ModelPost, () => ModelUser, 'userId', {
178
+ include: { posts: true },
179
+ with: {
180
+ roles: $relationDynamic.belongsToMany(
181
+ 'test-vona:roleUser',
182
+ 'test-vona:role',
183
+ 'userId',
184
+ 'roleId',
185
+ { columns: ['id', 'name'], meta: { client: '_auto_' } },
186
+ ),
187
+ },
188
+ columns: ['id', 'name'],
189
+ }),
190
+ },
191
+ },
192
+ );
193
+ assert.equal(Object.keys(items[0].postContent!.post!.user!).length, 1);
194
+ assert.equal(items[0].postContent?.post3?.postContent?.content !== undefined, true);
195
+ assert.equal(items[0].user3!.posts.length > 0, true);
196
+ assert.equal(items[0].user3!.roles.length > 0, true);
197
+ assert.equal(cast(items[0].user3!.roles[0]).iid, undefined);
198
+ assert.equal(cast(items[0].user3)?.iid, undefined);
199
+ // test data: delete
200
+ await scopeTest.service.testData.drop(testData);
201
+ });
202
+ });
203
+ });
@@ -0,0 +1,39 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { app } from 'vona-mock';
4
+
5
+ describe('modelRelationsJoins.test.ts', () => {
6
+ it('action:modelRelationsJoins', async () => {
7
+ await app.bean.executor.mockCtx(async () => {
8
+ const prefix = 'action:modelRelationsJoins';
9
+ // scope
10
+ const scopeTest = app.bean.scope('test-vona');
11
+ // test data: create
12
+ const testData = await scopeTest.service.testData.create(prefix);
13
+ const {
14
+ userTom,
15
+ userJimmy,
16
+ } = testData;
17
+ // joins: auto
18
+ const itemsJoins = await scopeTest.model.post.select({
19
+ joins: [['innerJoin', 'testVonaUser', ['testVonaPost.userId', 'testVonaUser.id']]],
20
+ where: {
21
+ 'testVonaUser.id': userTom.id,
22
+ },
23
+ orders: [['testVonaUser.id', 'asc']],
24
+ });
25
+ assert.equal(itemsJoins.length, 2);
26
+ // joins: manual
27
+ const itemsJoins2 = await scopeTest.model.post.select({
28
+ joins: [['innerJoin', 'testVonaUser', ['testVonaPost.userId', 'testVonaUser.id']]],
29
+ where: {
30
+ 'testVonaUser.id': userJimmy.id,
31
+ },
32
+ orders: [['testVonaUser.id', 'asc']],
33
+ }, {}, ['test-vona:user']);
34
+ assert.equal(itemsJoins2.length, 0);
35
+ // test data: delete
36
+ await scopeTest.service.testData.drop(testData);
37
+ });
38
+ });
39
+ });