@pwrdrvr/microapps-datalib 0.0.18 → 0.0.19

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,22 +1,28 @@
1
- import { expect } from 'chai';
2
- import { describe, it } from 'mocha';
3
- import { dynamoClient, InitializeTable, DropTable, TEST_TABLE_NAME } from '../../../../fixtures';
4
- import Manager from '../index';
5
- import Version from './version';
1
+ import 'jest-dynalite/withDb';
2
+ import * as dynamodb from '@aws-sdk/client-dynamodb';
3
+ import { DBManager } from '../manager';
4
+ import { Version } from './version';
5
+
6
+ let dynamoClient: dynamodb.DynamoDBClient;
7
+ let dbManager: DBManager;
8
+
9
+ const TEST_TABLE_NAME = 'microapps';
6
10
 
7
11
  describe('version records', () => {
8
- before(async () => {
9
- new Manager({ dynamoDB: dynamoClient.client, tableName: TEST_TABLE_NAME });
10
- });
12
+ beforeAll(() => {
13
+ dynamoClient = new dynamodb.DynamoDBClient({
14
+ endpoint: process.env.MOCK_DYNAMODB_ENDPOINT,
15
+ tls: false,
16
+ region: 'local',
17
+ });
11
18
 
12
- beforeEach(async () => {
13
- // Create the table
14
- await InitializeTable();
19
+ // Init the DB manager to point it at the right table
20
+ dbManager = new DBManager({ dynamoClient, tableName: TEST_TABLE_NAME });
15
21
  });
16
22
 
17
- afterEach(async () => {
18
- await DropTable();
19
- });
23
+ afterAll(() => {
24
+ dynamoClient.destroy();
25
+ }, 20000);
20
26
 
21
27
  it('saving a version should create one record', async () => {
22
28
  const version = new Version();
@@ -27,20 +33,21 @@ describe('version records', () => {
27
33
  version.DefaultFile = 'index.html';
28
34
  version.IntegrationID = 'abcd';
29
35
 
30
- await version.SaveAsync(dynamoClient.ddbDocClient);
36
+ await version.Save(dbManager);
31
37
 
32
- const { Item } = await dynamoClient.ddbDocClient.get({
33
- TableName: Manager.TableName,
38
+ const { Item } = await dbManager.ddbDocClient.get({
39
+ TableName: dbManager.tableName,
34
40
  Key: { PK: 'appname#cat', SK: 'version#1.2.3-beta4' },
35
41
  });
36
- expect(Item.PK).equal('appname#cat');
37
- expect(Item.SK).equal('version#1.2.3-beta4');
38
- expect(Item.AppName).equal('cat');
39
- expect(Item.SemVer).equal('1.2.3-Beta4');
40
- expect(Item.Status).equal('pending');
41
- expect(Item.Type).equal('type');
42
- expect(Item.DefaultFile).equal('index.html');
43
- expect(Item.IntegrationID).equal('abcd');
42
+ expect(Item).toBeDefined();
43
+ expect(Item?.PK).toBe('appname#cat');
44
+ expect(Item?.SK).toBe('version#1.2.3-beta4');
45
+ expect(Item?.AppName).toBe('cat');
46
+ expect(Item?.SemVer).toBe('1.2.3-Beta4');
47
+ expect(Item?.Status).toBe('pending');
48
+ expect(Item?.Type).toBe('type');
49
+ expect(Item?.DefaultFile).toBe('index.html');
50
+ expect(Item?.IntegrationID).toBe('abcd');
44
51
  });
45
52
 
46
53
  it('load 1 version should load 1 version', async () => {
@@ -52,7 +59,7 @@ describe('version records', () => {
52
59
  version.DefaultFile = 'index.html';
53
60
  version.IntegrationID = 'abcd';
54
61
 
55
- await version.SaveAsync(dynamoClient.ddbDocClient);
62
+ await version.Save(dbManager);
56
63
 
57
64
  version = new Version();
58
65
  version.AppName = 'Dog';
@@ -62,27 +69,28 @@ describe('version records', () => {
62
69
  version.DefaultFile = 'index.html';
63
70
  version.IntegrationID = 'abcd';
64
71
 
65
- await version.SaveAsync(dynamoClient.ddbDocClient);
72
+ await version.Save(dbManager);
66
73
 
67
- const version1 = await Version.LoadVersionAsync(
68
- dynamoClient.ddbDocClient,
69
- 'Dog',
70
- '1.2.3-Beta5',
71
- );
74
+ const version1 = await Version.LoadVersion({
75
+ dbManager,
76
+ key: {
77
+ AppName: 'Dog',
78
+ SemVer: '1.2.3-Beta5',
79
+ },
80
+ });
72
81
 
73
- expect(version1.AppName).to.equal('dog');
74
- expect(version1.SK).to.equal('version#1.2.3-beta5');
75
- expect(version1.SemVer).to.equal('1.2.3-Beta5');
82
+ expect(version1.AppName).toBe('dog');
83
+ expect(version1.SK).toBe('version#1.2.3-beta5');
84
+ expect(version1.SemVer).toBe('1.2.3-Beta5');
76
85
 
77
- const version2 = await Version.LoadVersionAsync(
78
- dynamoClient.ddbDocClient,
79
- 'Dog',
80
- '1.2.3-Beta6',
81
- );
86
+ const version2 = await Version.LoadVersion({
87
+ dbManager,
88
+ key: { AppName: 'Dog', SemVer: '1.2.3-Beta6' },
89
+ });
82
90
 
83
- expect(version2.AppName).to.equal('dog');
84
- expect(version2.SK).to.equal('version#1.2.3-beta6');
85
- expect(version2.SemVer).to.equal('1.2.3-Beta6');
91
+ expect(version2.AppName).toBe('dog');
92
+ expect(version2.SK).toBe('version#1.2.3-beta6');
93
+ expect(version2.SemVer).toBe('1.2.3-Beta6');
86
94
  });
87
95
 
88
96
  it('load all app versions should load all versions for 1 app', async () => {
@@ -94,7 +102,7 @@ describe('version records', () => {
94
102
  version.DefaultFile = 'index.html';
95
103
  version.IntegrationID = 'abcd';
96
104
 
97
- await version.SaveAsync(dynamoClient.ddbDocClient);
105
+ await version.Save(dbManager);
98
106
 
99
107
  version = new Version();
100
108
  version.AppName = 'Frog';
@@ -104,17 +112,17 @@ describe('version records', () => {
104
112
  version.DefaultFile = 'index.html';
105
113
  version.IntegrationID = 'abcd';
106
114
 
107
- await version.SaveAsync(dynamoClient.ddbDocClient);
115
+ await version.Save(dbManager);
108
116
 
109
- const versions = await Version.LoadVersionsAsync(dynamoClient.ddbDocClient, 'Frog');
117
+ const versions = await Version.LoadVersions({ dbManager, key: { AppName: 'Frog' } });
110
118
 
111
- expect(versions.length).to.equal(2);
119
+ expect(versions.length).toBe(2);
112
120
 
113
- expect(versions[0].AppName).to.equal('frog');
114
- expect(versions[0].SK).to.equal('version#2.2.3-beta5');
115
- expect(versions[0].SemVer).to.equal('2.2.3-Beta5');
116
- expect(versions[1].AppName).to.equal('frog');
117
- expect(versions[1].SK).to.equal('version#2.2.3-beta6');
118
- expect(versions[1].SemVer).to.equal('2.2.3-Beta6');
121
+ expect(versions[0].AppName).toBe('frog');
122
+ expect(versions[0].SK).toBe('version#2.2.3-beta5');
123
+ expect(versions[0].SemVer).toBe('2.2.3-Beta5');
124
+ expect(versions[1].AppName).toBe('frog');
125
+ expect(versions[1].SK).toBe('version#2.2.3-beta6');
126
+ expect(versions[1].SemVer).toBe('2.2.3-Beta6');
119
127
  });
120
128
  });
@@ -1,6 +1,5 @@
1
- import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
2
1
  import { plainToClass } from 'class-transformer';
3
- import { Config } from '../config';
2
+ import { DBManager } from '..';
4
3
 
5
4
  enum SaveBy {
6
5
  AppName,
@@ -25,16 +24,23 @@ export interface IVersionRecord {
25
24
  IntegrationID: string;
26
25
  }
27
26
 
28
- export default class Version implements IVersionRecord {
29
- public static async LoadVersionsAsync(
30
- ddbDocClient: DynamoDBDocument,
31
- appName: string,
32
- ): Promise<Version[]> {
33
- const { Items } = await ddbDocClient.query({
34
- TableName: Config.TableName,
27
+ export type IVersionRecordNoKeysLoose = Partial<
28
+ Omit<IVersionRecord, 'PK' | 'SK' | 'AppName' | 'SemVer'>
29
+ > &
30
+ Pick<IVersionRecord, 'AppName' | 'SemVer'>;
31
+
32
+ export class Version implements IVersionRecord {
33
+ public static async LoadVersions(opts: {
34
+ dbManager: DBManager;
35
+ key: Pick<IVersionRecord, 'AppName'>;
36
+ }): Promise<Version[]> {
37
+ const { dbManager, key } = opts;
38
+
39
+ const { Items } = await dbManager.ddbDocClient.query({
40
+ TableName: dbManager.tableName,
35
41
  KeyConditionExpression: 'PK = :pkval and begins_with(SK, :skval)',
36
42
  ExpressionAttributeValues: {
37
- ':pkval': `appName#${appName}`.toLowerCase(),
43
+ ':pkval': `appName#${key.AppName}`.toLowerCase(),
38
44
  ':skval': 'version',
39
45
  },
40
46
  });
@@ -49,16 +55,17 @@ export default class Version implements IVersionRecord {
49
55
  return records;
50
56
  }
51
57
 
52
- public static async LoadVersionAsync(
53
- ddbDocClient: DynamoDBDocument,
54
- appName: string,
55
- semVer: string,
56
- ): Promise<Version> {
57
- const { Item } = await ddbDocClient.get({
58
- TableName: Config.TableName,
58
+ public static async LoadVersion(opts: {
59
+ dbManager: DBManager;
60
+ key: Pick<IVersionRecord, 'AppName' | 'SemVer'>;
61
+ }): Promise<Version> {
62
+ const { dbManager, key } = opts;
63
+
64
+ const { Item } = await dbManager.ddbDocClient.get({
65
+ TableName: dbManager.tableName,
59
66
  Key: {
60
- PK: `appName#${appName}`.toLowerCase(),
61
- SK: `version#${semVer}`.toLowerCase(),
67
+ PK: `appName#${key.AppName}`.toLowerCase(),
68
+ SK: `version#${key.SemVer}`.toLowerCase(),
62
69
  },
63
70
  });
64
71
  const record = plainToClass<Version, unknown>(Version, Item);
@@ -78,13 +85,18 @@ export default class Version implements IVersionRecord {
78
85
  private _appName: string | undefined;
79
86
  private _semVer: string | undefined;
80
87
  private _type: string | undefined;
81
- private _status: VersionStatus | undefined;
82
- private _defaultFile: string | undefined;
83
- private _integrationID: string | undefined;
88
+ private _status: VersionStatus;
89
+ private _defaultFile: string;
90
+ private _integrationID: string;
84
91
 
85
- public constructor(init?: Partial<IVersionRecord>) {
86
- Object.assign(this, init);
92
+ public constructor(init?: Partial<IVersionRecordNoKeysLoose>) {
87
93
  this._keyBy = SaveBy.AppName;
94
+ this._status = 'pending';
95
+ this._defaultFile = '';
96
+ this._integrationID = '';
97
+
98
+ // Save any passed in values over the defaults
99
+ Object.assign(this, init);
88
100
  }
89
101
 
90
102
  public get DbStruct(): IVersionRecord {
@@ -100,13 +112,13 @@ export default class Version implements IVersionRecord {
100
112
  };
101
113
  }
102
114
 
103
- public async SaveAsync(ddbDocClient: DynamoDBDocument): Promise<void> {
115
+ public async Save(dbManager: DBManager): Promise<void> {
104
116
  // TODO: Validate that all the fields needed are present
105
117
 
106
118
  // Save under specific AppName key
107
119
  this._keyBy = SaveBy.AppName;
108
- await ddbDocClient.put({
109
- TableName: Config.TableName,
120
+ await dbManager.ddbDocClient.put({
121
+ TableName: dbManager.tableName,
110
122
  Item: this.DbStruct,
111
123
  });
112
124
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 PwrDrvr LLC
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/src/index.spec.ts DELETED
@@ -1,89 +0,0 @@
1
- import { expect } from 'chai';
2
- import { describe, it } from 'mocha';
3
- import { dynamoClient, InitializeTable, DropTable, TEST_TABLE_NAME } from '../../../fixtures';
4
- import Manager, { Application, Version, Rules } from './index';
5
-
6
- describe('database manager', () => {
7
- before(async () => {
8
- new Manager({ dynamoDB: dynamoClient.client, tableName: TEST_TABLE_NAME });
9
- });
10
-
11
- beforeEach(async () => {
12
- // Create the table
13
- await InitializeTable();
14
- });
15
-
16
- afterEach(async () => {
17
- await DropTable();
18
- });
19
-
20
- it('should get versions and rules when asked', async () => {
21
- const app = new Application({
22
- AppName: 'Bat',
23
- DisplayName: 'Bat App',
24
- });
25
- await app.SaveAsync(dynamoClient.ddbDocClient);
26
-
27
- const version = new Version({
28
- AppName: 'Bat',
29
- DefaultFile: 'bat.html',
30
- IntegrationID: 'abcd',
31
- SemVer: '3.2.1-beta0',
32
- Status: 'deployed',
33
- Type: 'next.js',
34
- });
35
- await version.SaveAsync(dynamoClient.ddbDocClient);
36
-
37
- const rules = new Rules({
38
- AppName: 'Bat',
39
- Version: 0,
40
- RuleSet: { default: { SemVer: '3.2.1-beta0', AttributeName: '', AttributeValue: '' } },
41
- });
42
- await rules.SaveAsync(dynamoClient.ddbDocClient);
43
-
44
- const versAndRules = await Manager.GetVersionsAndRules('bat');
45
-
46
- expect(versAndRules).to.have.property('Versions');
47
- expect(versAndRules).to.have.property('Rules');
48
- expect(versAndRules.Rules).to.have.property('RuleSet');
49
- expect(versAndRules.Rules.RuleSet).to.have.property('default');
50
- expect(versAndRules.Rules.RuleSet.default).to.have.property('SemVer');
51
- expect(versAndRules.Rules.RuleSet.default.SemVer).to.equal('3.2.1-beta0');
52
- expect(versAndRules.Versions.length).to.equal(1);
53
- expect(versAndRules.Versions[0].SemVer).to.equal('3.2.1-beta0');
54
- });
55
-
56
- it('should update default rule', async () => {
57
- const app = new Application({
58
- AppName: 'Bat2',
59
- DisplayName: 'Bat App',
60
- });
61
- await app.SaveAsync(dynamoClient.ddbDocClient);
62
-
63
- const version = new Version({
64
- AppName: 'Bat2',
65
- DefaultFile: 'bat.html',
66
- IntegrationID: 'abcd',
67
- SemVer: '3.2.1-beta0',
68
- Status: 'deployed',
69
- Type: 'next.js',
70
- });
71
- await version.SaveAsync(dynamoClient.ddbDocClient);
72
-
73
- const rules = new Rules({
74
- AppName: 'Bat2',
75
- Version: 0,
76
- RuleSet: { default: { SemVer: '3.2.1-beta0', AttributeName: '', AttributeValue: '' } },
77
- });
78
- await rules.SaveAsync(dynamoClient.ddbDocClient);
79
-
80
- // Check version before update
81
- let versAndRules = await Manager.GetVersionsAndRules('bat2');
82
- expect(versAndRules.Rules.RuleSet.default.SemVer).to.equal('3.2.1-beta0');
83
-
84
- // Update default version
85
- await Manager.UpdateDefaultRule('bat2', '3.2.2');
86
- versAndRules = await Manager.GetVersionsAndRules('bat2');
87
- expect(versAndRules.Rules.RuleSet.default.SemVer).to.equal('3.2.2');
88
- });
89
- });