-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
indexeddb.test.ts
583 lines (481 loc) · 14.8 KB
/
indexeddb.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import Dexie from 'dexie';
import 'dexie-export-import';
import 'fake-indexeddb/auto';
import * as idb from 'idb';
import { DataStore, SortDirection } from '../src/index';
import { DATASTORE, SYNC, USER } from '../src/util';
import {
Author,
Blog,
BlogOwner,
Comment,
Nested,
Post,
PostAuthorJoin,
PostMetadata,
Person,
} from './model';
let db: idb.IDBPDatabase;
const indexedDB = require('fake-indexeddb');
const IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange');
Dexie.dependencies.indexedDB = indexedDB;
Dexie.dependencies.IDBKeyRange = IDBKeyRange;
describe('Indexed db storage test', () => {
let blog: InstanceType<typeof Blog>,
blog2: InstanceType<typeof Blog>,
blog3: InstanceType<typeof Blog>,
owner: InstanceType<typeof BlogOwner>,
owner2: InstanceType<typeof BlogOwner>;
beforeAll(async () => {
await DataStore.start();
db = await idb.openDB('amplify-datastore', 2);
});
beforeEach(async () => {
owner = new BlogOwner({ name: 'Owner 1' });
owner2 = new BlogOwner({ name: 'Owner 2' });
blog = new Blog({
name: 'Avatar: Last Airbender',
owner,
});
blog2 = new Blog({
name: 'blog2',
owner: owner2,
});
blog3 = new Blog({
name: 'Avatar 101',
owner: new BlogOwner({ name: 'owner 3' }),
});
});
test('setup function', async () => {
const createdObjStores = db.objectStoreNames;
const expectedStores = [
`${DATASTORE}_Setting`,
`${SYNC}_ModelMetadata`,
`${SYNC}_MutationEvent`,
`${USER}_Author`,
`${USER}_Blog`,
`${USER}_BlogOwner`,
`${USER}_Comment`,
`${USER}_Person`,
`${USER}_Post`,
`${USER}_PostAuthorJoin`,
];
expect(createdObjStores).toHaveLength(expectedStores.length);
expect(expectedStores).toMatchObject(createdObjStores);
expect(createdObjStores).not.toContain(`${USER}_blah`);
// TODO: better way to get this
const commentStore = (<any>db)._rawDatabase.rawObjectStores.get(
`${USER}_Comment`
);
const postAuthorStore = (<any>db)._rawDatabase.rawObjectStores.get(
`${USER}_PostAuthorJoin`
);
expect(commentStore.rawIndexes.has('byId')).toBe(true); // checks byIdIndex
expect(postAuthorStore.rawIndexes.has('byId')).toBe(true); // checks byIdIndex
expect(commentStore.rawIndexes.has('commentPostId')).toBe(true); // checks 1:M
expect(postAuthorStore.rawIndexes.has('postId')).toBe(true); // checks M:M
expect(postAuthorStore.rawIndexes.has('authorId')).toBe(true); // checks M:M
});
test('save function 1:1 insert', async () => {
await DataStore.save(blog);
await DataStore.save(owner);
const get1 = await db
.transaction(`${USER}_Blog`, 'readonly')
.objectStore(`${USER}_Blog`)
.index('byId')
.get(blog.id);
expect(get1).toBeDefined();
expect([...Object.keys(blog).sort(), 'blogOwnerId']).toEqual(
expect.arrayContaining(Object.keys(get1).sort())
);
expect(get1['blogOwnerId']).toBe(owner.id);
const get2 = await db
.transaction(`${USER}_BlogOwner`, 'readonly')
.objectStore(`${USER}_BlogOwner`)
.index('byId')
.get(owner.id);
expect([...Object.keys(owner)].sort()).toEqual(
expect.arrayContaining(Object.keys(get2).sort())
);
await DataStore.save(blog2);
const get3 = await db
.transaction(`${USER}_Blog`, 'readonly')
.objectStore(`${USER}_Blog`)
.index('byId')
.get(blog2.id);
expect([...Object.keys(blog2).sort(), 'blogOwnerId']).toEqual(
expect.arrayContaining(Object.keys(get3).sort())
);
});
test('save stores non-model types along the item (including nested)', async () => {
const p = new Post({
title: 'Avatar',
blog,
metadata: new PostMetadata({
rating: 3,
tags: ['a', 'b', 'c'],
nested: new Nested({
aField: 'Some value',
}),
}),
});
await DataStore.save(p);
const postFromDB = await db
.transaction(`${USER}_Post`, 'readonly')
.objectStore(`${USER}_Post`)
.index('byId')
.get(p.id);
expect(postFromDB.metadata).toMatchObject({
rating: 3,
tags: ['a', 'b', 'c'],
nested: new Nested({
aField: 'Some value',
}),
});
});
test('save function 1:M insert', async () => {
// test 1:M
const p = new Post({
title: 'Avatar',
blog,
});
const c1 = new Comment({ content: 'comment 1', post: p });
await DataStore.save(c1);
const getComment = await db
.transaction(`${USER}_Comment`, 'readonly')
.objectStore(`${USER}_Comment`)
.index('byId')
.get(c1.id);
expect([...Object.keys(c1), 'commentPostId'].sort()).toEqual(
expect.arrayContaining(Object.keys(getComment).sort())
);
const checkIndex = await db
.transaction(`${USER}_Comment`, 'readonly')
.objectStore(`${USER}_Comment`)
.index('commentPostId')
.get(p.id);
expect(checkIndex['commentPostId']).toEqual(p.id);
});
test('save function M:M insert', async () => {
const post = new Post({
title: 'Avatar',
blog,
});
await DataStore.save(post);
const getPost = await db
.transaction(`${USER}_Post`, 'readonly')
.objectStore(`${USER}_Post`)
.index('byId')
.get(post.id);
expect(getPost.author).toBeUndefined();
const a1 = new Author({ name: 'author1' });
const a2 = new Author({ name: 'author2' });
await DataStore.save(a1);
await DataStore.save(a2);
const getA1 = await db
.transaction(`${USER}_Author`, 'readonly')
.objectStore(`${USER}_Author`)
.index('byId')
.get(a1.id);
expect(getA1.name).toEqual('author1');
const getA2 = await db
.transaction(`${USER}_Author`, 'readonly')
.objectStore(`${USER}_Author`)
.index('byId')
.get(a2.id);
expect(getA2.name).toEqual('author2');
await DataStore.save(new PostAuthorJoin({ post, author: a1 }));
await DataStore.save(new PostAuthorJoin({ post, author: a2 }));
const p2 = new Post({ title: 'q', blog });
await DataStore.save(new PostAuthorJoin({ post: p2, author: a2 }));
const getAuthors = await db
.transaction(`${USER}_PostAuthorJoin`)
.objectStore(`${USER}_PostAuthorJoin`)
.index('postId')
.getAll(post.id);
expect(getAuthors).toHaveLength(2);
});
test('save update', async () => {
await DataStore.save(blog);
await DataStore.save(owner);
const get1 = await db
.transaction(`${USER}_Blog`, 'readonly')
.objectStore(`${USER}_Blog`)
.index('byId')
.get(blog.id);
expect(get1['blogOwnerId']).toBe(owner.id);
const updated = Blog.copyOf(blog, draft => {
draft.name = 'Avatar: The Last Airbender';
});
await DataStore.save(updated);
const get2 = await db
.transaction(`${USER}_Blog`, 'readonly')
.objectStore(`${USER}_Blog`)
.index('byId')
.get(blog.id);
expect(get2.name).toEqual(updated.name);
});
test('query function 1:1', async () => {
const res = await DataStore.save(blog);
await DataStore.save(owner);
const query = await DataStore.query(Blog, blog.id);
expect(query).toEqual(res);
await DataStore.save(blog3);
const query1 = await DataStore.query(Blog);
query1.forEach(item => {
if (item.owner) {
expect(item.owner).toHaveProperty('name');
}
});
});
test('query M:1 eager load', async () => {
const p = new Post({
title: 'Avatar',
blog,
});
const c1 = new Comment({ content: 'comment 1', post: p });
const c2 = new Comment({ content: 'comment 2', post: p });
await DataStore.save(p);
await DataStore.save(c1);
await DataStore.save(c2);
const q1 = await DataStore.query(Comment, c1.id);
expect(q1.post.id).toEqual(p.id);
});
test('query with sort on a single field', async () => {
const p1 = new Person({
firstName: 'John',
lastName: 'Snow',
});
const p2 = new Person({
firstName: 'Clem',
lastName: 'Fandango',
});
const p3 = new Person({
firstName: 'Beezus',
lastName: 'Fuffoon',
});
const p4 = new Person({
firstName: 'Meow Meow',
lastName: 'Fuzzyface',
});
await DataStore.save(p1);
await DataStore.save(p2);
await DataStore.save(p3);
await DataStore.save(p4);
const sortedPersons = await DataStore.query(Person, null, {
page: 0,
limit: 20,
sort: s => s.firstName(SortDirection.DESCENDING),
});
expect(sortedPersons[0].firstName).toEqual('Meow Meow');
expect(sortedPersons[1].firstName).toEqual('John');
expect(sortedPersons[2].firstName).toEqual('Clem');
expect(sortedPersons[3].firstName).toEqual('Beezus');
});
test('query with sort on multiple fields', async () => {
const p1 = new Person({
firstName: 'John',
lastName: 'Snow',
username: 'johnsnow',
});
const p2 = new Person({
firstName: 'John',
lastName: 'Umber',
username: 'smalljohnumber',
});
const p3 = new Person({
firstName: 'John',
lastName: 'Umber',
username: 'greatjohnumber',
});
await DataStore.save(p1);
await DataStore.save(p2);
await DataStore.save(p3);
const sortedPersons = await DataStore.query(
Person,
c => c.username('ne', undefined),
{
page: 0,
limit: 20,
sort: s =>
s
.firstName(SortDirection.ASCENDING)
.lastName(SortDirection.ASCENDING)
.username(SortDirection.ASCENDING),
}
);
expect(sortedPersons[0].username).toEqual('johnsnow');
expect(sortedPersons[1].username).toEqual('greatjohnumber');
expect(sortedPersons[2].username).toEqual('smalljohnumber');
});
test('delete 1:1 function', async () => {
await DataStore.save(blog);
await DataStore.save(owner);
await DataStore.delete(Blog, blog.id);
await DataStore.delete(BlogOwner, owner.id);
expect(await DataStore.query(BlogOwner, owner.id)).toBeUndefined();
expect(await DataStore.query(Blog, blog.id)).toBeUndefined();
await DataStore.save(owner);
await DataStore.save(owner2);
await DataStore.save(
Blog.copyOf(blog, draft => {
draft;
})
);
await DataStore.save(blog2);
await DataStore.save(blog3);
await DataStore.delete(Blog, c => c.name('beginsWith', 'Avatar'));
expect(await DataStore.query(Blog, blog.id)).toBeUndefined();
expect(await DataStore.query(Blog, blog2.id)).toBeDefined();
expect(await DataStore.query(Blog, blog3.id)).toBeUndefined();
});
test('delete M:1 function', async () => {
const post = new Post({
title: 'Avatar',
blog,
});
const c1 = new Comment({ content: 'c1', post });
const c2 = new Comment({ content: 'c2', post });
await DataStore.save(post);
await DataStore.save(c1);
await DataStore.save(c2);
await DataStore.delete(Comment, c1.id);
expect(await DataStore.query(Comment, c1.id)).toBeUndefined;
expect((await DataStore.query(Comment, c2.id)).id).toEqual(c2.id);
});
test('delete 1:M function', async () => {
const post = new Post({
title: 'Avatar 1',
blog,
});
const post2 = new Post({
title: 'Avatar 2',
blog,
});
await DataStore.save(post);
await DataStore.save(post2);
const c1 = new Comment({ content: 'c1', post });
const c2 = new Comment({ content: 'c2', post });
const c3 = new Comment({ content: 'c3', post: post2 });
await DataStore.save(c1);
await DataStore.save(c2);
await DataStore.save(c3);
await DataStore.delete(Post, post.id);
expect(await DataStore.query(Comment, c1.id)).toBeUndefined();
expect(await DataStore.query(Comment, c2.id)).toBeUndefined();
expect((await DataStore.query(Comment, c3.id)).id).toEqual(c3.id);
expect(await DataStore.query(Post, post.id)).toBeUndefined();
});
test('delete M:M function', async () => {
const a1 = new Author({ name: 'author1' });
const a2 = new Author({ name: 'author2' });
const a3 = new Author({ name: 'author3' });
const blog = new Blog({ name: 'B1', owner: new BlogOwner({ name: 'O1' }) });
const post = new Post({
title: 'Avatar',
blog,
});
const post2 = new Post({
title: 'Avatar 2',
blog,
});
await DataStore.save(post);
await DataStore.save(post2);
await DataStore.delete(Post, post.id);
const res = await db
.transaction('user_PostAuthorJoin', 'readwrite')
.objectStore('user_PostAuthorJoin')
.index('postId')
.getAll(post.id);
expect(res).toHaveLength(0);
await DataStore.delete(Post, c => c);
await DataStore.delete(Author, c => c);
});
test('delete cascade', async () => {
const a1 = await DataStore.save(new Author({ name: 'author1' }));
const a2 = await DataStore.save(new Author({ name: 'author2' }));
const blog = new Blog({
name: 'The Blog',
owner,
});
const p1 = new Post({
title: 'Post 1',
blog,
});
const p2 = new Post({
title: 'Post 2',
blog,
});
const c1 = await DataStore.save(new Comment({ content: 'c1', post: p1 }));
const c2 = await DataStore.save(new Comment({ content: 'c2', post: p1 }));
await DataStore.save(p1);
await DataStore.save(p2);
await DataStore.save(blog);
await DataStore.delete(BlogOwner, owner.id);
expect(await DataStore.query(Blog, blog.id)).toBeUndefined();
expect(await DataStore.query(BlogOwner, owner.id)).toBeUndefined();
expect(await DataStore.query(Post, p1.id)).toBeUndefined();
expect(await DataStore.query(Post, p2.id)).toBeUndefined();
expect(await DataStore.query(Comment, c1.id)).toBeUndefined();
expect(await DataStore.query(Comment, c2.id)).toBeUndefined();
expect(await DataStore.query(Author, a1.id)).toEqual(a1);
expect(await DataStore.query(Author, a2.id)).toEqual(a2);
const refResult = await db
.transaction(`${USER}_PostAuthorJoin`, 'readonly')
.objectStore(`${USER}_PostAuthorJoin`)
.index('postId')
.getAll(p1.id);
expect(refResult).toHaveLength(0);
});
test('delete non existent', async () => {
const author = new Author({ name: 'author1' });
const deleted = await DataStore.delete(author);
expect(deleted).toStrictEqual(author);
const fromDB = await db
.transaction(`${USER}_Author`, 'readonly')
.objectStore(`${USER}_Author`)
.get(author.id);
expect(fromDB).toBeUndefined();
});
});
describe('DB versions migration', () => {
beforeEach(async () => {
db.close();
await DataStore.clear();
});
test('Migration from v1 to v2', async () => {
const v1Data = require('./v1schema.data.json');
const blob = new Blob([JSON.stringify(v1Data)], {
type: 'application/json',
});
// Import V1
(await Dexie.import(blob)).close();
// Migrate to V2
await DataStore.start();
// Open V2
db = await idb.openDB('amplify-datastore', 2);
expect(db.objectStoreNames).toMatchObject(
v1Data.data.tables.map(({ name }) => name)
);
for (const storeName of db.objectStoreNames) {
expect(db.transaction(storeName).store.indexNames).toContain('byId');
}
const dexie = await new Dexie('amplify-datastore').open();
const exportedBlob = await dexie.export();
function readBlob(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onabort = ev => reject(new Error('file read aborted'));
reader.onerror = ev => reject((ev.target as any).error);
reader.onload = ev => resolve((ev.target as any).result);
reader.readAsText(blob);
});
}
const exportedJSON = await readBlob(exportedBlob);
const exported = JSON.parse(exportedJSON);
for (const { schema } of exported.data.tables) {
expect(schema.split(',')).toContain('&id');
}
expect(exported).toMatchSnapshot('v2-schema');
});
});