@proteinjs/db 1.17.1 → 1.18.1

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.
@@ -33,6 +33,31 @@ class UserTestTable extends Table<User> {
33
33
  ];
34
34
  }
35
35
 
36
+ interface MappedIndexUser extends Record {
37
+ emailAddress: string;
38
+ accountStatus: string;
39
+ createdOn: Date;
40
+ }
41
+
42
+ class MappedIndexUserTable extends Table<MappedIndexUser> {
43
+ name = 'db_test_mapped_index_user';
44
+ columns = withRecordColumns<MappedIndexUser>({
45
+ emailAddress: new StringColumn('email_address'),
46
+ accountStatus: new StringColumn('account_status'),
47
+ createdOn: new DateColumn('created_on'),
48
+ });
49
+ indexes = [
50
+ {
51
+ name: 'db_test_mapped_index_user_email_index',
52
+ columns: ['emailAddress'] as (keyof MappedIndexUser)[],
53
+ },
54
+ {
55
+ name: 'db_test_mapped_index_user_status_email_index',
56
+ columns: ['accountStatus', 'emailAddress'] as (keyof MappedIndexUser)[],
57
+ },
58
+ ];
59
+ }
60
+
36
61
  interface ColumnTypes extends Record {
37
62
  integer: number;
38
63
  bigInteger: number;
@@ -88,6 +113,7 @@ export const tableManagerTests = (
88
113
  afterEach(async () => {
89
114
  await dropTable(new ColumnTypesTable());
90
115
  await dropTable(new UserTestTable());
116
+ await dropTable(new MappedIndexUserTable());
91
117
  });
92
118
 
93
119
  afterAll(async () => {
@@ -527,6 +553,45 @@ export const tableManagerTests = (
527
553
  // expect(foreignKeys[TestColumnTypesTable.columns.text.name]['REFERENCED_COLUMN_NAME']).toBe('id');
528
554
  });
529
555
 
556
+ test('creates index with different column name mapping', async () => {
557
+ const mappedIndexUserTable = new MappedIndexUserTable();
558
+ await tableManager.loadTable(mappedIndexUserTable);
559
+ expect(await tableManager.tableExists(mappedIndexUserTable)).toBeTruthy();
560
+ const indexes = await tableManager.schemaMetadata.getIndexes(mappedIndexUserTable);
561
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_email_index'])).toBe(JSON.stringify(['email_address']));
562
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_status_email_index'])).toBe(
563
+ JSON.stringify(['account_status', 'email_address'])
564
+ );
565
+ });
566
+
567
+ test('alters index with different column name mapping', async () => {
568
+ const mappedIndexUserTable = new MappedIndexUserTable();
569
+ await tableManager.loadTable(mappedIndexUserTable);
570
+ expect(await tableManager.tableExists(mappedIndexUserTable)).toBeTruthy();
571
+ let indexes = await tableManager.schemaMetadata.getIndexes(mappedIndexUserTable);
572
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_email_index'])).toBe(JSON.stringify(['email_address']));
573
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_status_email_index'])).toBe(
574
+ JSON.stringify(['account_status', 'email_address'])
575
+ );
576
+ mappedIndexUserTable.indexes = [
577
+ {
578
+ name: 'db_test_mapped_index_user_email_index',
579
+ columns: ['emailAddress'] as (keyof MappedIndexUser)[],
580
+ },
581
+ {
582
+ name: 'db_test_mapped_index_user_created_email_index',
583
+ columns: ['createdOn', 'emailAddress'] as (keyof MappedIndexUser)[],
584
+ },
585
+ ];
586
+ await tableManager.loadTable(mappedIndexUserTable);
587
+ indexes = await tableManager.schemaMetadata.getIndexes(mappedIndexUserTable);
588
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_email_index'])).toBe(JSON.stringify(['email_address']));
589
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_created_email_index'])).toBe(
590
+ JSON.stringify(['created_on', 'email_address'])
591
+ );
592
+ expect(JSON.stringify(indexes['db_test_mapped_index_user_status_email_index'])).toBeFalsy();
593
+ });
594
+
530
595
  test('create index', async () => {
531
596
  const userTable = new UserTestTable();
532
597
  await tableManager.loadTable(userTable);