@travetto/model 8.0.0-alpha.2 → 8.0.0-alpha.21

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,190 +0,0 @@
1
- import assert from 'node:assert';
2
-
3
- import { Suite, Test } from '@travetto/test';
4
- import { Schema } from '@travetto/schema';
5
- import { TimeUtil } from '@travetto/runtime';
6
-
7
- import { Index, Model } from '../../src/registry/decorator.ts';
8
- import type { ModelIndexedSupport } from '../../src/types/indexed.ts';
9
- import { NotFoundError } from '../../src/error/not-found.ts';
10
- import { IndexNotSupported } from '../../src/error/invalid-index.ts';
11
-
12
- import { BaseModelSuite } from './base.ts';
13
-
14
- @Model('index_user')
15
- @Index({
16
- name: 'userName',
17
- type: 'unsorted',
18
- fields: [{ name: 1 }]
19
- })
20
- class User {
21
- id: string;
22
- name: string;
23
- }
24
-
25
- @Model('index_user_2')
26
- class User2 {
27
- id: string;
28
- name: string;
29
- }
30
-
31
- @Model()
32
- @Index({ type: 'sorted', name: 'userAge', fields: [{ name: 1 }, { age: 1 }] })
33
- class User3 {
34
- id: string;
35
- name: string;
36
- age: number;
37
- color?: string;
38
- }
39
-
40
- @Schema()
41
- class Child {
42
- name: string;
43
- age: number;
44
- }
45
-
46
- @Model()
47
- @Index({ type: 'sorted', name: 'childAge', fields: [{ child: { name: 1 } }, { child: { age: 1 } }] })
48
- @Index({ type: 'sorted', name: 'nameCreated', fields: [{ child: { name: 1 } }, { createdDate: 1 }] })
49
- class User4 {
50
- id: string;
51
- createdDate?: Date = new Date();
52
- color: string;
53
- child: Child;
54
- }
55
-
56
- @Suite()
57
- export abstract class ModelIndexedSuite extends BaseModelSuite<ModelIndexedSupport> {
58
- @Test()
59
- async writeAndRead() {
60
- const service = await this.service;
61
-
62
- await service.create(User, User.from({ name: 'bob1' }));
63
- await service.create(User, User.from({ name: 'bob2' }));
64
-
65
- const found1 = await service.getByIndex(User, 'userName', {
66
- name: 'bob1'
67
- });
68
-
69
- assert(found1.name === 'bob1');
70
-
71
- const found2 = await service.getByIndex(User, 'userName', {
72
- name: 'bob2'
73
- });
74
-
75
- assert(found2.name === 'bob2');
76
- }
77
-
78
- @Test()
79
- async readMissingIndex() {
80
- const service = await this.service;
81
- await assert.rejects(() => service.getByIndex(User, 'missing', {}), NotFoundError);
82
- }
83
-
84
- @Test()
85
- async readMissingValue() {
86
- const service = await this.service;
87
- await assert.rejects(() => service.getByIndex(User, 'userName', { name: 'jim' }), NotFoundError);
88
- }
89
-
90
- @Test()
91
- async readDifferentType() {
92
- const service = await this.service;
93
- await assert.rejects(() => service.getByIndex(User2, 'userName', { name: 'jim' }), NotFoundError);
94
- }
95
-
96
- @Test()
97
- async queryMultiple() {
98
- const service = await this.service;
99
-
100
- await service.create(User3, User3.from({ name: 'bob', age: 20 }));
101
- await service.create(User3, User3.from({ name: 'bob', age: 30, color: 'green' }));
102
-
103
- const found = await service.getByIndex(User3, 'userAge', { name: 'bob', age: 30 });
104
-
105
- assert(found.color === 'green');
106
-
107
- const found2 = await service.getByIndex(User3, 'userAge', { name: 'bob', age: 20 });
108
-
109
- assert(!found2.color);
110
-
111
- await assert.rejects(() => service.getByIndex(User3, 'userAge', { name: 'bob' }));
112
- }
113
-
114
- @Test()
115
- async queryList() {
116
- const service = await this.service;
117
-
118
- await service.create(User3, User3.from({ name: 'bob', age: 40, color: 'blue' }));
119
- await service.create(User3, User3.from({ name: 'bob', age: 30, color: 'red' }));
120
- await service.create(User3, User3.from({ name: 'bob', age: 50, color: 'green' }));
121
-
122
- const arr = await this.toArray(service.listByIndex(User3, 'userAge', { name: 'bob' }));
123
-
124
- assert(arr[0].color === 'red' && arr[0].name === 'bob');
125
- assert(arr[1].color === 'blue' && arr[1].name === 'bob');
126
- assert(arr[2].color === 'green' && arr[2].name === 'bob');
127
-
128
- await assert.rejects(() => this.toArray(service.listByIndex(User3, 'userAge', {})), IndexNotSupported);
129
- }
130
-
131
- @Test()
132
- async queryDeepList() {
133
- const service = await this.service;
134
-
135
- await service.create(User4, User4.from({ child: { name: 'bob', age: 40 }, color: 'blue' }));
136
- await service.create(User4, User4.from({ child: { name: 'bob', age: 30 }, color: 'red' }));
137
- await service.create(User4, User4.from({ child: { name: 'bob', age: 50 }, color: 'green' }));
138
-
139
- const arr = await this.toArray(service.listByIndex(User4, 'childAge', User4.from({ child: { name: 'bob' } })));
140
- assert(arr[0].color === 'red' && arr[0].child.name === 'bob' && arr[0].child.age === 30);
141
- assert(arr[1].color === 'blue' && arr[1].child.name === 'bob' && arr[1].child.age === 40);
142
- assert(arr[2].color === 'green' && arr[2].child.name === 'bob' && arr[2].child.age === 50);
143
-
144
- await assert.rejects(() => this.toArray(service.listByIndex(User4, 'childAge', {})), IndexNotSupported);
145
- }
146
-
147
- @Test()
148
- async queryComplexDateList() {
149
- const service = await this.service;
150
-
151
- await service.create(User4, User4.from({ child: { name: 'bob', age: 40 }, createdDate: TimeUtil.fromNow('3d'), color: 'blue' }));
152
- await service.create(User4, User4.from({ child: { name: 'bob', age: 30 }, createdDate: TimeUtil.fromNow('2d'), color: 'red' }));
153
- await service.create(User4, User4.from({ child: { name: 'bob', age: 50 }, createdDate: TimeUtil.fromNow('-1d'), color: 'green' }));
154
-
155
- const arr = await this.toArray(service.listByIndex(User4, 'nameCreated', { child: { name: 'bob' } }));
156
-
157
- assert(arr[0].color === 'green' && arr[0].child.name === 'bob' && arr[0].child.age === 50);
158
- assert(arr[1].color === 'red' && arr[1].child.name === 'bob' && arr[1].child.age === 30);
159
- assert(arr[2].color === 'blue' && arr[2].child.name === 'bob' && arr[2].child.age === 40);
160
-
161
- await assert.rejects(() => this.toArray(service.listByIndex(User4, 'nameCreated', {})), IndexNotSupported);
162
- }
163
-
164
- @Test()
165
- async upsertByIndex() {
166
- const service = await this.service;
167
-
168
- const user1 = await service.upsertByIndex(User4, 'childAge', { child: { name: 'bob', age: 40 }, color: 'blue' });
169
- const user2 = await service.upsertByIndex(User4, 'childAge', { child: { name: 'bob', age: 40 }, color: 'green' });
170
- const user3 = await service.upsertByIndex(User4, 'childAge', { child: { name: 'bob', age: 40 }, color: 'red' });
171
-
172
- const arr = await this.toArray(service.listByIndex(User4, 'childAge', { child: { name: 'bob' } }));
173
- assert(arr.length === 1);
174
-
175
- assert(user1.id === user2.id);
176
- assert(user2.id === user3.id);
177
- assert(user1.color === 'blue');
178
- assert(user3.color === 'red');
179
-
180
- const user4 = await service.upsertByIndex(User4, 'childAge', { child: { name: 'bob', age: 30 }, color: 'red' });
181
- const arr2 = await this.toArray(service.listByIndex(User4, 'childAge', { child: { name: 'bob' } }));
182
- assert(arr2.length === 2);
183
-
184
- await service.deleteByIndex(User4, 'childAge', user1);
185
-
186
- const arr3 = await this.toArray(service.listByIndex(User4, 'childAge', { child: { name: 'bob' } }));
187
- assert(arr3.length === 1);
188
- assert(arr3[0].id === user4.id);
189
- }
190
- }