vona-module-test-vona 5.0.38 → 5.0.41
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.
- package/dist/.metadata/index.d.ts +22 -1
- package/dist/bean/cacheRedis.post.d.ts +7 -0
- package/dist/index.js +499 -268
- package/dist/service/order.d.ts +4 -0
- package/dist/service/post.d.ts +20 -0
- package/package.json +4 -2
- package/src/.metadata/index.ts +1741 -0
- package/src/.metadata/this.ts +2 -0
- package/src/bean/aop.regExp.ts +30 -0
- package/src/bean/aop.simple.ts +58 -0
- package/src/bean/aopMethod.test.ts +42 -0
- package/src/bean/bean.testCtx.ts +55 -0
- package/src/bean/broadcast.test.ts +22 -0
- package/src/bean/cacheMem.test.ts +7 -0
- package/src/bean/cacheRedis.post.ts +10 -0
- package/src/bean/cacheRedis.test.ts +7 -0
- package/src/bean/event.helloEcho.ts +8 -0
- package/src/bean/eventListener.helloEcho.ts +21 -0
- package/src/bean/meta.version.ts +81 -0
- package/src/bean/queue.test.ts +18 -0
- package/src/bean/schedule.test.ts +12 -0
- package/src/bean/schedule.test3.ts +12 -0
- package/src/bean/summerCache.test.ts +29 -0
- package/src/config/config.ts +5 -0
- package/src/config/locale/en-us.ts +19 -0
- package/src/config/locale/zh-cn.ts +17 -0
- package/src/controller/bean.ts +95 -0
- package/src/controller/cacheMem.ts +63 -0
- package/src/controller/cacheRedis.ts +55 -0
- package/src/controller/dtoTest.ts +63 -0
- package/src/controller/guardPassport.ts +25 -0
- package/src/controller/onion.ts +92 -0
- package/src/controller/passport.ts +48 -0
- package/src/controller/performAction.ts +18 -0
- package/src/controller/queue.ts +25 -0
- package/src/controller/summer.ts +125 -0
- package/src/controller/tail.ts +32 -0
- package/src/controller/transaction.ts +29 -0
- package/src/controller/upload.ts +57 -0
- package/src/dto/categoryTree.ts +8 -0
- package/src/dto/orderCreate.ts +10 -0
- package/src/dto/orderResult.ts +9 -0
- package/src/dto/orderUpdate.ts +9 -0
- package/src/dto/postCreate.ts +9 -0
- package/src/dto/profile.ts +14 -0
- package/src/dto/roleLazy.ts +15 -0
- package/src/dto/user.ts +19 -0
- package/src/dto/userCreate.ts +8 -0
- package/src/dto/userLazy.ts +18 -0
- package/src/dto/userUpdate.ts +8 -0
- package/src/entity/category.ts +14 -0
- package/src/entity/order.ts +18 -0
- package/src/entity/post.ts +17 -0
- package/src/entity/postContent.ts +14 -0
- package/src/entity/product.ts +24 -0
- package/src/entity/role.ts +11 -0
- package/src/entity/roleUser.ts +14 -0
- package/src/entity/test.ts +18 -0
- package/src/entity/user.ts +17 -0
- package/src/index.ts +1 -0
- package/src/model/category.ts +17 -0
- package/src/model/categoryChain.ts +16 -0
- package/src/model/order.ts +26 -0
- package/src/model/orderStats.ts +27 -0
- package/src/model/post.ts +16 -0
- package/src/model/postContent.ts +17 -0
- package/src/model/product.ts +8 -0
- package/src/model/role.ts +15 -0
- package/src/model/roleUser.ts +8 -0
- package/src/model/test.ts +8 -0
- package/src/model/testDynamicTable.ts +21 -0
- package/src/model/user.ts +21 -0
- package/src/model/userStats.ts +15 -0
- package/src/model/userStatsGroup.ts +15 -0
- package/src/service/aopMethod.ts +38 -0
- package/src/service/caching.ts +75 -0
- package/src/service/order.ts +65 -0
- package/src/service/post.ts +219 -0
- package/src/service/test.ts +9 -0
- package/src/service/testApp.ts +13 -0
- package/src/service/testClass.ts +15 -0
- package/src/service/testData.ts +67 -0
- package/src/service/transaction.ts +20 -0
- package/test/aopMethod.test.ts +21 -0
- package/test/authSimple.test.ts +45 -0
- package/test/bean.test.ts +16 -0
- package/test/broadcast.test.ts +17 -0
- package/test/cache/cacheMem.test.ts +10 -0
- package/test/cache/cacheRedis.test.ts +10 -0
- package/test/cache/caching.test.ts +58 -0
- package/test/cache/summer.test.ts +10 -0
- package/test/database/database.test.ts +127 -0
- package/test/database/dtoAggregate.test.ts +44 -0
- package/test/database/dtoGet.test.ts +61 -0
- package/test/database/dtoGroup.test.ts +44 -0
- package/test/database/dtoLazy.test.ts +45 -0
- package/test/database/dtoMutate.test.ts +56 -0
- package/test/database/modelAggregate.test.ts +197 -0
- package/test/database/modelCache.test.ts +37 -0
- package/test/database/modelGeneral.test.ts +18 -0
- package/test/database/modelGroup.test.ts +211 -0
- package/test/database/modelRelations.test.ts +203 -0
- package/test/database/modelRelationsJoins.test.ts +39 -0
- package/test/database/modelRelationsMutate.test.ts +354 -0
- package/test/database/modelWhere.test.ts +138 -0
- package/test/database/orm.test.ts +22 -0
- package/test/database/transaction.test.ts +142 -0
- package/test/event.test.ts +14 -0
- package/test/guardPassport.test.ts +44 -0
- package/test/jwt.test.ts +27 -0
- package/test/locale.test.ts +51 -0
- package/test/passport.test.ts +57 -0
- package/test/queue.test.ts +11 -0
- package/test/tail.test.ts +21 -0
- package/test/upload.test.ts +58 -0
- package/test/utils/cabloyUtils.test.ts +22 -0
- package/test/utils/celjs.test.ts +26 -0
- package/test/utils/mappedTypes.test.ts +65 -0
- package/test/utils/performAction.test.ts +28 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
import { getTargetDecoratorRules } from 'vona-module-a-openapi';
|
|
5
|
+
import { $Dto } from 'vona-module-a-orm';
|
|
6
|
+
import { ModelUserStats } from 'vona-module-test-vona';
|
|
7
|
+
|
|
8
|
+
describe('dtoAggregate.test.ts', () => {
|
|
9
|
+
it('action:dtoAggregate', async () => {
|
|
10
|
+
await app.bean.executor.mockCtx(async () => {
|
|
11
|
+
await app.bean.executor.mockCtx(async () => {
|
|
12
|
+
// aggr
|
|
13
|
+
const DtoUserAggr = $Dto.aggregate('test-vona:user', {
|
|
14
|
+
count: ['*', 'age'],
|
|
15
|
+
sum: ['age'],
|
|
16
|
+
avg: 'age',
|
|
17
|
+
// max: 'age',
|
|
18
|
+
// min: 'age',
|
|
19
|
+
});
|
|
20
|
+
let rules: any;
|
|
21
|
+
rules = getTargetDecoratorRules(DtoUserAggr.prototype);
|
|
22
|
+
assert.equal(rules.count_all._def.typeName, 'ZodOptional');
|
|
23
|
+
assert.equal(rules.count_all._def.innerType._def.typeName, 'ZodUnion');
|
|
24
|
+
assert.equal(rules.count_all._def.innerType._def.options[0]._def.typeName, 'ZodString');
|
|
25
|
+
assert.equal(rules.count_all._def.innerType._def.options[1]._def.typeName, 'ZodNumber');
|
|
26
|
+
assert.equal(rules.count_age._def.typeName, 'ZodOptional');
|
|
27
|
+
assert.equal(rules.sum_age._def.typeName, 'ZodOptional');
|
|
28
|
+
assert.equal(rules.avg_age._def.typeName, 'ZodOptional');
|
|
29
|
+
assert.equal(rules.max_age, undefined);
|
|
30
|
+
assert.equal(rules.min_age, undefined);
|
|
31
|
+
// aggr: usersStats: posts: autoload
|
|
32
|
+
const DtoUserStats = $Dto.get(() => ModelUserStats, {
|
|
33
|
+
columns: 'name',
|
|
34
|
+
include: { roles: true },
|
|
35
|
+
});
|
|
36
|
+
rules = getTargetDecoratorRules(DtoUserStats.prototype);
|
|
37
|
+
assert.equal(rules.name._def.typeName, 'ZodString');
|
|
38
|
+
assert.equal(rules.iid, undefined);
|
|
39
|
+
assert.equal(rules.posts._def.typeName, 'ZodOptional');
|
|
40
|
+
assert.equal(rules.roles._def.typeName, 'ZodOptional');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
import { getTargetDecoratorRules } from 'vona-module-a-openapi';
|
|
5
|
+
import { $Dto, $relationDynamic } from 'vona-module-a-orm';
|
|
6
|
+
import { ModelPost, ModelPostContent, ModelRole, ModelRoleUser, ModelUser } from 'vona-module-test-vona';
|
|
7
|
+
|
|
8
|
+
describe('dtoGet.test.ts', () => {
|
|
9
|
+
it('action:dtoGet', async () => {
|
|
10
|
+
await app.bean.executor.mockCtx(async () => {
|
|
11
|
+
await app.bean.executor.mockCtx(async () => {
|
|
12
|
+
const DtoPostNew = $Dto.get(ModelPost, {
|
|
13
|
+
columns: ['id', 'title', 'userId'],
|
|
14
|
+
include: {
|
|
15
|
+
postContent: {
|
|
16
|
+
columns: ['id', 'content'],
|
|
17
|
+
include: {
|
|
18
|
+
post: { include: { user: { columns: ['id'] } } },
|
|
19
|
+
},
|
|
20
|
+
with: {
|
|
21
|
+
post3: $relationDynamic.belongsTo(() => ModelPostContent, () => ModelPost, 'postId', {
|
|
22
|
+
include: {
|
|
23
|
+
postContent: true,
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
with: {
|
|
30
|
+
user3: $relationDynamic.belongsTo(ModelPost, () => ModelUser, 'userId', {
|
|
31
|
+
include: { posts: true },
|
|
32
|
+
with: { roles: $relationDynamic.belongsToMany(() => ModelRoleUser, () => ModelRole, 'userId', 'roleId') },
|
|
33
|
+
columns: ['id', 'name'],
|
|
34
|
+
}),
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
const rules: any = getTargetDecoratorRules(DtoPostNew.prototype);
|
|
38
|
+
assert.equal(['ZodString', 'ZodNumber'].includes(rules.id._def.typeName), true);
|
|
39
|
+
assert.equal(rules.title._def.typeName, 'ZodString');
|
|
40
|
+
assert.equal(['ZodString', 'ZodNumber'].includes(rules.userId._def.typeName), true);
|
|
41
|
+
assert.equal(rules.iid, undefined);
|
|
42
|
+
assert.equal(rules.postContent._def.typeName, 'ZodOptional');
|
|
43
|
+
assert.equal(rules.user._def.typeName, 'ZodOptional');
|
|
44
|
+
assert.equal(rules.user3._def.typeName, 'ZodOptional');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it('action:dtoGet:categoryTree', async () => {
|
|
49
|
+
await app.bean.executor.mockCtx(async () => {
|
|
50
|
+
await app.bean.executor.mockCtx(async () => {
|
|
51
|
+
const DtoCategoryTree = $Dto.get('test-vona:category', { columns: ['id', 'name'], include: { children: { columns: ['id'] } } });
|
|
52
|
+
const rules: any = getTargetDecoratorRules(DtoCategoryTree.prototype);
|
|
53
|
+
assert.equal(rules.children._def.typeName, 'ZodEffects');
|
|
54
|
+
assert.equal(rules.iid, undefined);
|
|
55
|
+
const DtoCategoryChain = $Dto.get('test-vona:categoryChain', { columns: ['id', 'name', 'categoryIdParent'] });
|
|
56
|
+
const _apiJson = await app.bean.scope('a-openapi').service.openapi.generateJsonOfClass(DtoCategoryChain);
|
|
57
|
+
// console.log(JSON.stringify(apiJson.components));
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
import { getTargetDecoratorRules } from 'vona-module-a-openapi';
|
|
5
|
+
import { $Dto } from 'vona-module-a-orm';
|
|
6
|
+
import { ModelUserStatsGroup } from 'vona-module-test-vona';
|
|
7
|
+
|
|
8
|
+
describe('dtoGroup.test.ts', () => {
|
|
9
|
+
it('action:dtoGroup', async () => {
|
|
10
|
+
await app.bean.executor.mockCtx(async () => {
|
|
11
|
+
await app.bean.executor.mockCtx(async () => {
|
|
12
|
+
const DtoUserAggr = $Dto.group('test-vona:user', ['name'], {
|
|
13
|
+
count: ['*', 'age'],
|
|
14
|
+
sum: ['age'],
|
|
15
|
+
avg: 'age',
|
|
16
|
+
// max: 'age',
|
|
17
|
+
// min: 'age',
|
|
18
|
+
});
|
|
19
|
+
let rules: any;
|
|
20
|
+
rules = getTargetDecoratorRules(DtoUserAggr.prototype);
|
|
21
|
+
assert.equal(rules.name._def.typeName, 'ZodString');
|
|
22
|
+
assert.equal(rules.count_all._def.typeName, 'ZodOptional');
|
|
23
|
+
assert.equal(rules.count_all._def.innerType._def.typeName, 'ZodUnion');
|
|
24
|
+
assert.equal(rules.count_all._def.innerType._def.options[0]._def.typeName, 'ZodString');
|
|
25
|
+
assert.equal(rules.count_all._def.innerType._def.options[1]._def.typeName, 'ZodNumber');
|
|
26
|
+
assert.equal(rules.count_age._def.typeName, 'ZodOptional');
|
|
27
|
+
assert.equal(rules.sum_age._def.typeName, 'ZodOptional');
|
|
28
|
+
assert.equal(rules.avg_age._def.typeName, 'ZodOptional');
|
|
29
|
+
assert.equal(rules.max_age, undefined);
|
|
30
|
+
assert.equal(rules.min_age, undefined);
|
|
31
|
+
// group: userStats: posts: autoload
|
|
32
|
+
const DtoUserStats = $Dto.get(() => ModelUserStatsGroup, {
|
|
33
|
+
columns: 'name',
|
|
34
|
+
include: { roles: true },
|
|
35
|
+
});
|
|
36
|
+
rules = getTargetDecoratorRules(DtoUserStats.prototype);
|
|
37
|
+
assert.equal(rules.name._def.typeName, 'ZodString');
|
|
38
|
+
assert.equal(rules.iid, undefined);
|
|
39
|
+
assert.equal(rules.posts._def.typeName, 'ZodEffects');
|
|
40
|
+
assert.equal(rules.roles._def.typeName, 'ZodEffects');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { OpenApiGeneratorV31, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
|
|
4
|
+
import { appResource, cast } from 'vona';
|
|
5
|
+
import { app } from 'vona-mock';
|
|
6
|
+
import { $schema } from 'vona-module-a-openapi';
|
|
7
|
+
import { DtoUserLazy } from 'vona-module-test-vona';
|
|
8
|
+
|
|
9
|
+
describe('dtoLazy.test.ts', () => {
|
|
10
|
+
it('action:dtoLazy', async () => {
|
|
11
|
+
await app.bean.executor.mockCtx(async () => {
|
|
12
|
+
const data = {
|
|
13
|
+
name: 'kevin',
|
|
14
|
+
other: 'other',
|
|
15
|
+
user: {
|
|
16
|
+
id: 1,
|
|
17
|
+
name: 'tom',
|
|
18
|
+
married: 1,
|
|
19
|
+
},
|
|
20
|
+
roles: [{ id: 2, name: 'admin' }],
|
|
21
|
+
};
|
|
22
|
+
const schema = $schema(DtoUserLazy);
|
|
23
|
+
const res = await schema.parseAsync(data);
|
|
24
|
+
assert.equal(res.name, 'kevin');
|
|
25
|
+
assert.equal(cast(res).other, undefined);
|
|
26
|
+
assert.equal(res.user?.name, 'tom');
|
|
27
|
+
assert.equal(cast(res.user)?.married, undefined);
|
|
28
|
+
assert.equal(res.roles?.length, 1);
|
|
29
|
+
assert.equal(res.roles?.[0].name, 'admin');
|
|
30
|
+
assert.equal(cast(res.roles?.[0])?.id, undefined);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
it('action:openapi', async () => {
|
|
34
|
+
await app.bean.executor.mockCtx(async () => {
|
|
35
|
+
const registry = new OpenAPIRegistry();
|
|
36
|
+
const beanOptions = appResource.getBean(DtoUserLazy)!;
|
|
37
|
+
const schema = $schema(beanOptions.beanClass);
|
|
38
|
+
registry.register(beanOptions.beanFullName, schema);
|
|
39
|
+
const generator = new OpenApiGeneratorV31(registry.definitions);
|
|
40
|
+
const apiObj = generator.generateDocument(app.bean.scope('a-openapi').config.generateDocument.V31);
|
|
41
|
+
assert.equal(cast(apiObj).components.schemas['test-vona.dto.userLazy'].properties.user.$ref, '#/components/schemas/test-vona.dto.userLazy');
|
|
42
|
+
assert.equal(cast(apiObj).components.schemas['test-vona.dto.roleLazy'].properties.users.items.$ref, '#/components/schemas/test-vona.dto.userLazy');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { app } from 'vona-mock';
|
|
4
|
+
import { getTargetDecoratorRules } from 'vona-module-a-openapi';
|
|
5
|
+
import { $Dto, $relationDynamic } from 'vona-module-a-orm';
|
|
6
|
+
import { ModelPost } from 'vona-module-test-vona';
|
|
7
|
+
|
|
8
|
+
describe('dtoMutate.test.ts', () => {
|
|
9
|
+
it('action:dtoMutate', async () => {
|
|
10
|
+
await app.bean.executor.mockCtx(async () => {
|
|
11
|
+
await app.bean.executor.mockCtx(async () => {
|
|
12
|
+
// create
|
|
13
|
+
const DtoUserCreate = $Dto.create('test-vona:user', {
|
|
14
|
+
include: { posts: true },
|
|
15
|
+
});
|
|
16
|
+
let rules: any;
|
|
17
|
+
rules = getTargetDecoratorRules(DtoUserCreate.prototype);
|
|
18
|
+
assert.equal(rules.name._def.typeName, 'ZodString');
|
|
19
|
+
assert.equal(rules.id, undefined);
|
|
20
|
+
assert.equal(rules.iid, undefined);
|
|
21
|
+
assert.equal(rules.deleted, undefined);
|
|
22
|
+
assert.equal(rules.createdAt, undefined);
|
|
23
|
+
assert.equal(rules.updatedAt, undefined);
|
|
24
|
+
assert.equal(rules.posts._def.typeName, 'ZodOptional');
|
|
25
|
+
// update
|
|
26
|
+
const DtoUserUpdate = $Dto.update('test-vona:user', {
|
|
27
|
+
with: {
|
|
28
|
+
posts: $relationDynamic.hasMany(() => ModelPost, 'userId', { columns: ['id', 'title'] }),
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
rules = getTargetDecoratorRules(DtoUserUpdate.prototype);
|
|
32
|
+
assert.equal(rules.name._def.typeName, 'ZodString'); // ZodOptional
|
|
33
|
+
assert.equal(rules.id, undefined);
|
|
34
|
+
assert.equal(rules.iid, undefined);
|
|
35
|
+
assert.equal(rules.deleted, undefined);
|
|
36
|
+
assert.equal(rules.createdAt, undefined);
|
|
37
|
+
assert.equal(rules.updatedAt, undefined);
|
|
38
|
+
assert.equal(rules.posts._def.typeName, 'ZodOptional');
|
|
39
|
+
// create: not mutate post(belongsTo)
|
|
40
|
+
const DtoPostCreate = $Dto.create('test-vona:post', {
|
|
41
|
+
include: {
|
|
42
|
+
postContent: true,
|
|
43
|
+
user: true,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
rules = getTargetDecoratorRules(DtoPostCreate.prototype);
|
|
47
|
+
assert.equal(rules.title._def.typeName, 'ZodString'); // ZodOptional
|
|
48
|
+
assert.equal(rules.stars._def.typeName, 'ZodOptional');
|
|
49
|
+
assert.equal(['ZodString', 'ZodNumber'].includes(rules.userId._def.typeName), true);
|
|
50
|
+
assert.equal(rules.postContent._def.typeName, 'ZodOptional');
|
|
51
|
+
assert.equal(rules.user, undefined);
|
|
52
|
+
assert.equal(rules.id, undefined);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
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('modelAggregate.test.ts', () => {
|
|
9
|
+
it('action:modelAggregate', async () => {
|
|
10
|
+
await app.bean.executor.mockCtx(async () => {
|
|
11
|
+
const prefix = 'action:modelAggregate';
|
|
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
|
+
// count: user
|
|
65
|
+
const userCount = await scopeTest.model.user.count({
|
|
66
|
+
column: '*',
|
|
67
|
+
where: {
|
|
68
|
+
name: { _startsWith_: `${prefix}:` },
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
assert.equal(userCount, 3);
|
|
72
|
+
// aggr: user
|
|
73
|
+
const userStats = await scopeTest.model.user.aggregate({
|
|
74
|
+
aggrs: {
|
|
75
|
+
count: ['*', 'age'],
|
|
76
|
+
sum: ['age'],
|
|
77
|
+
avg: 'age',
|
|
78
|
+
max: 'age',
|
|
79
|
+
},
|
|
80
|
+
where: {
|
|
81
|
+
name: { _startsWith_: `${prefix}:` },
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
assert.equal(userStats.count_all, 3);
|
|
85
|
+
assert.equal(userStats.count_age, 2);
|
|
86
|
+
assert.equal(userStats.sum_age, 8);
|
|
87
|
+
assert.equal(userStats.avg_age, 4);
|
|
88
|
+
assert.equal(userStats.max_age, 5);
|
|
89
|
+
// aggr: usersStats: posts: autoload
|
|
90
|
+
const usersStats = await scopeTest.model.userStats.select({
|
|
91
|
+
where: {
|
|
92
|
+
id: users.map(item => item.id),
|
|
93
|
+
},
|
|
94
|
+
orders: [['id', 'asc']],
|
|
95
|
+
include: {
|
|
96
|
+
roles: true,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
assert.equal(usersStats.length, 3);
|
|
100
|
+
assert.equal(usersStats[0].posts?.count_all, 2);
|
|
101
|
+
assert.equal(usersStats[0].posts?.count_title, 2);
|
|
102
|
+
assert.equal(usersStats[0].posts?.sum_stars, 5);
|
|
103
|
+
assert.equal(usersStats[0].roles?.count_all, 1);
|
|
104
|
+
// aggr: usersStats: posts: mixed
|
|
105
|
+
const usersStats2 = await scopeTest.model.userStats.select({
|
|
106
|
+
where: {
|
|
107
|
+
id: users.map(item => item.id),
|
|
108
|
+
},
|
|
109
|
+
orders: [['id', 'asc']],
|
|
110
|
+
include: {
|
|
111
|
+
posts: { aggrs: { count: '*' } },
|
|
112
|
+
roles: true,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
assert.equal(usersStats2.length, 3);
|
|
116
|
+
assert.equal(usersStats2[0].posts?.count_all, 2);
|
|
117
|
+
assert.equal(cast(usersStats2[0].posts).count_title, undefined);
|
|
118
|
+
assert.equal(usersStats2[0].posts?.sum_stars, 5);
|
|
119
|
+
assert.equal(usersStats2[0].roles?.count_all, 1);
|
|
120
|
+
// aggr: usersStats: posts: disable
|
|
121
|
+
const usersStats3 = await scopeTest.model.userStats.select({
|
|
122
|
+
where: {
|
|
123
|
+
id: users.map(item => item.id),
|
|
124
|
+
},
|
|
125
|
+
orders: [['id', 'asc']],
|
|
126
|
+
include: {
|
|
127
|
+
posts: {
|
|
128
|
+
aggrs: {
|
|
129
|
+
count: ['*'],
|
|
130
|
+
sum: [],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
roles: true,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
assert.equal(usersStats3.length, 3);
|
|
137
|
+
assert.equal(usersStats3[0].posts?.count_all, 2);
|
|
138
|
+
assert.equal(cast(usersStats3[0].posts).count_title, undefined);
|
|
139
|
+
assert.equal(cast(usersStats3[0].posts).sum_stars, undefined);
|
|
140
|
+
assert.equal(usersStats3[0].roles?.count_all, 1);
|
|
141
|
+
// aggr: usersStats: with
|
|
142
|
+
const usersStats4 = await scopeTest.model.userStats.select({
|
|
143
|
+
where: {
|
|
144
|
+
id: users.map(item => item.id),
|
|
145
|
+
},
|
|
146
|
+
orders: [['id', 'asc']],
|
|
147
|
+
include: {
|
|
148
|
+
posts: false,
|
|
149
|
+
roles: false,
|
|
150
|
+
},
|
|
151
|
+
with: {
|
|
152
|
+
posts: $relationDynamic.hasMany(() => ModelPost, 'userId', {
|
|
153
|
+
aggrs: { count: '*' },
|
|
154
|
+
}),
|
|
155
|
+
roles: $relationDynamic.belongsToMany(() => ModelRoleUser, () => ModelRole, 'userId', 'roleId', {
|
|
156
|
+
aggrs: { count: '*' },
|
|
157
|
+
}),
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
assert.equal(usersStats4.length, 3);
|
|
161
|
+
assert.equal(usersStats4[0].posts?.count_all, 2);
|
|
162
|
+
assert.equal(cast(usersStats4[0].posts).count_title, undefined);
|
|
163
|
+
assert.equal(cast(usersStats4[0].posts).sum_stars, undefined);
|
|
164
|
+
assert.equal(usersStats4[0].roles?.count_all, 1);
|
|
165
|
+
// aggr: get
|
|
166
|
+
const userStats4 = await scopeTest.model.userStats.get({
|
|
167
|
+
id: users[0].id,
|
|
168
|
+
}, {
|
|
169
|
+
include: {
|
|
170
|
+
posts: false,
|
|
171
|
+
roles: false,
|
|
172
|
+
},
|
|
173
|
+
with: {
|
|
174
|
+
posts: $relationDynamic.hasMany(() => ModelPost, 'userId', {
|
|
175
|
+
aggrs: { count: '*' },
|
|
176
|
+
}),
|
|
177
|
+
roles: $relationDynamic.belongsToMany(() => ModelRoleUser, () => ModelRole, 'userId', 'roleId', {
|
|
178
|
+
aggrs: { count: '*' },
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
assert.equal(userStats4?.posts?.count_all, 2);
|
|
183
|
+
assert.equal(cast(userStats4?.posts).count_title, undefined);
|
|
184
|
+
assert.equal(cast(userStats4?.posts).sum_stars, undefined);
|
|
185
|
+
assert.equal(userStats4?.roles?.count_all, 1);
|
|
186
|
+
// delete: users
|
|
187
|
+
await scopeTest.model.user.deleteBulk(users.map(item => item.id), {
|
|
188
|
+
include: {
|
|
189
|
+
posts: { include: { postContent: true } },
|
|
190
|
+
roles: true,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
// delete: roles
|
|
194
|
+
await scopeTest.model.role.deleteBulk(roles.map(item => item.id));
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -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
|
+
});
|