Skip to content

Commit

Permalink
*rds + dynamo - partial
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Zuev committed Sep 20, 2020
1 parent 9749a02 commit 795f8da
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 69 deletions.
13 changes: 13 additions & 0 deletions rds_dynamo/dynamo/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rds_dynamo/dynamo/app/package.json
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"apollo-server-express": "2.9.1",
"aws-sdk": "^2.756.0",
"axios": "^0.20.0",
"body-parser": "1.18.3",
"bunyan": "1.8.12",
"class-validator": "^0.12.2",
Expand Down
18 changes: 0 additions & 18 deletions rds_dynamo/dynamo/app/src/models/dynamo/artist/ArtistSchema.ts
Expand Up @@ -19,24 +19,6 @@ export default {
KeyType: 'RANGE'
}
],
/* LocalSecondaryIndexes: [
{
IndexName: 'index_symbol_createdAt',
KeySchema: [
{
AttributeName: 'symbol',
KeyType: 'HASH'
},
{
AttributeName: 'createdAt',
KeyType: 'RANGE'
}
],
Projection: {
ProjectionType: 'ALL'
}
}
],*/
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
Expand Down
32 changes: 3 additions & 29 deletions rds_dynamo/dynamo/app/src/models/dynamo/song/SongSchema.ts
@@ -1,11 +1,7 @@
export default {
AttributeDefinitions: [
{
AttributeName: 'name',
AttributeType: 'S'
},
{
AttributeName: 'releaseDate',
AttributeName: 'id', // where id is genreType#releaseDate
AttributeType: 'S'
},
{
Expand All @@ -15,36 +11,14 @@ export default {
],
KeySchema: [
{
AttributeName: 'name',
AttributeName: 'artistId',
KeyType: 'HASH'
},
{
AttributeName: 'releaseDate',
AttributeName: 'id',
KeyType: 'RANGE'
}
],
GlobalSecondaryIndexes: [
{
IndexName: 'index_artistId_releaseDate',
KeySchema: [
{
AttributeName: 'artistId',
KeyType: 'HASH'
},
{
AttributeName: 'releaseDate',
KeyType: 'RANGE'
}
],
Projection: {
ProjectionType: 'ALL'
},
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
Expand Down
2 changes: 1 addition & 1 deletion rds_dynamo/dynamo/app/src/models/gql/artist/Artist.ts
Expand Up @@ -5,7 +5,7 @@ export class Artist {

@Field(()=> String)
public id() {
return `${ this.name }:${ this.createdAt }`
return `${ this.name }#${ this.createdAt }`
}

@Field()
Expand Down
3 changes: 3 additions & 0 deletions rds_dynamo/dynamo/app/src/models/gql/song/Song.ts
Expand Up @@ -9,6 +9,9 @@ export class Song {
@Field()
public genreType: number;

@Field()
public id: string;

@Field()
public releaseDate: number;

Expand Down
12 changes: 9 additions & 3 deletions rds_dynamo/dynamo/app/src/resolvers/gql/artist/ArtistMutation.ts
Expand Up @@ -4,13 +4,17 @@ import { Inject } from 'typedi';
import { DI } from '../../../constants/DI';
import * as AWS from 'aws-sdk';
import ArtistModel from '../../../models/dynamo/artist/ArtistSchema';
import * as bunyan from 'bunyan';

@Resolver()
export default class ArtistMutation {

@Inject(DI.dynamodb)
private readonly dynamodb: AWS.DynamoDB;

@Inject(DI.logger)
private readonly logger: bunyan;

@Mutation(returnType => Artist)
public async artistAdd(
@Arg('name') name: string
Expand All @@ -32,7 +36,8 @@ export default class ArtistMutation {
ReturnConsumedCapacity: 'TOTAL'
};

await this.dynamodb.putItem(params).promise();
const { ConsumedCapacity } = await this.dynamodb.putItem(params).promise();
this.logger.info(`consumed ${ ConsumedCapacity.CapacityUnits } units for adding artist`);
const artist: Artist = new Artist();
artist.name = params.Item.name.S;
artist.createdAt = parseInt(params.Item.createdAt.S, 10);
Expand All @@ -43,17 +48,18 @@ export default class ArtistMutation {
public async artistRemove(
@Arg('id') id: string
): Promise<boolean> {
await this.dynamodb.deleteItem({
const { ConsumedCapacity } = await this.dynamodb.deleteItem({
TableName: ArtistModel.TableName,
Key: {
symbol: {
S: id.substr(0, 1).toUpperCase()
},
createdAt: {
S: id.split(':')[1]
S: id.split('#')[1]
}
}
}).promise();
this.logger.info(`consumed ${ ConsumedCapacity.CapacityUnits } units for removing artist`);
return true;
}

Expand Down
10 changes: 8 additions & 2 deletions rds_dynamo/dynamo/app/src/resolvers/gql/artist/ArtistResolver.ts
Expand Up @@ -4,13 +4,17 @@ import ArtistModel from '../../../models/dynamo/artist/ArtistSchema';
import { Inject } from 'typedi';
import { DI } from '../../../constants/DI';
import * as AWS from 'aws-sdk';
import * as bunyan from 'bunyan';

@Resolver()
export default class ArtistResolver {

@Inject(DI.dynamodb)
private readonly dynamodb: AWS.DynamoDB;

@Inject(DI.logger)
private readonly logger: bunyan;

@Query(returns => [Artist])
public async artistList(
@Arg('symbol', type => String, { nullable: true }) symbol: string,
Expand All @@ -32,10 +36,12 @@ export default class ArtistResolver {
':date': { S: date.toString() }
},
KeyConditionExpression: 'symbol = :symbol AND createdAt < :date',
ScanIndexForward: false
ScanIndexForward: false,
ReturnConsumedCapacity: 'TOTAL'
};

const { Items } = await this.dynamodb.query(params).promise();
const { Items, ConsumedCapacity, ScannedCount } = await this.dynamodb.query(params).promise();
this.logger.info(`consumed ${ ConsumedCapacity.CapacityUnits } units for reading songs list with scanned count ${ ScannedCount }`);
const result = [];

for (const item of Items) {
Expand Down
28 changes: 24 additions & 4 deletions rds_dynamo/dynamo/app/src/resolvers/gql/song/SongMutation.ts
Expand Up @@ -4,15 +4,18 @@ import { Inject } from 'typedi';
import { DI } from '../../../constants/DI';
import * as AWS from 'aws-sdk';
import SongModel from '../../../models/dynamo/song/SongSchema';
import { Artist } from '../../../models/gql/artist/Artist';
import { GenreType } from '../../../../../../rds/app/src/constants/GenreType';
import * as bunyan from 'bunyan';

@Resolver()
export default class SongMutation {

@Inject(DI.dynamodb)
private readonly dynamodb: AWS.DynamoDB;

@Inject(DI.logger)
private readonly logger: bunyan;

@Mutation(returnType => Song)
public async songAdd(
@Arg('name') name: string,
Expand All @@ -35,25 +38,42 @@ export default class SongMutation {
},
artistId: {
S: artistId
},
id: {
S: `${genreType}#${releaseDate}`
}
},
ReturnConsumedCapacity: 'TOTAL'
};

await this.dynamodb.putItem(params).promise();
const {ConsumedCapacity } = await this.dynamodb.putItem(params).promise();
this.logger.info(`consumed ${ ConsumedCapacity.CapacityUnits } units for adding song`);
const song: Song = new Song();
song.name = params.Item.name.S;
song.genreType = parseInt(params.Item.genreType.N, 10);
song.releaseDate = parseInt(params.Item.releaseDate.S, 10);
song.artistId = params.Item.artistId.S;
song.id = params.Item.id.S;
return song;
}

@Mutation(returnType => Boolean)
public async songRemove(
@Arg('id') id: number
@Arg('artistId') artistId: string,
@Arg('id') id: string
): Promise<boolean> {
//await this.songRepository.delete({ id });
const { ConsumedCapacity } = await this.dynamodb.deleteItem({
TableName: SongModel.TableName,
Key: {
id: {
S: id
},
artistId: {
S: artistId
}
}
}).promise();
this.logger.info(`consumed ${ ConsumedCapacity.CapacityUnits } units for removing song`);
return true;
}

Expand Down
30 changes: 18 additions & 12 deletions rds_dynamo/dynamo/app/src/resolvers/gql/song/SongResolver.ts
Expand Up @@ -4,47 +4,53 @@ import { Inject } from 'typedi';
import { DI } from '../../../constants/DI';
import * as AWS from 'aws-sdk';
import SongModel from '../../../models/dynamo/song/SongSchema';
import * as bunyan from 'bunyan';

@Resolver()
export default class SongResolver {

@Inject(DI.dynamodb)
private readonly dynamodb: AWS.DynamoDB;

@Inject(DI.logger)
private readonly logger: bunyan;

@Query(returns => [Song])
public async songsListByArtist(
@Arg('artistId', type => String, { nullable: true }) artistId: string,
@Arg('upperDate', type => Number, { nullable: true }) date: number,
@Arg('lastId', type => String, { nullable: true }) lastId: string,
@Arg('limit', type => Number, { nullable: true, defaultValue: 10 }) limit: number
): Promise<Song[]> {

if (!date) {
date = Date.now();
}

const params = {
TableName: SongModel.TableName,
Select: 'ALL_ATTRIBUTES',
Limit: limit,
ConsistentRead: false,
ExpressionAttributeValues: {
':artistId': { S: artistId },
':releaseDate': { S: date.toString() }
':artistId': { S: artistId }
},
KeyConditionExpression: 'artistId = :artistId AND releaseDate < :releaseDate',
IndexName: SongModel.GlobalSecondaryIndexes[0].IndexName,
ScanIndexForward: false
KeyConditionExpression: 'artistId = :artistId',
ScanIndexForward: false,
ReturnConsumedCapacity: 'TOTAL'
};

const { Items } = await this.dynamodb.query(params).promise();
if (lastId) {
params.ExpressionAttributeValues[':lastId'] = { S: lastId };
params.KeyConditionExpression = 'artistId = :artistId AND id < :lastId'
}

const { Items, ScannedCount, ConsumedCapacity } = await this.dynamodb.query(params).promise();
this.logger.info(`consumed ${ ConsumedCapacity.CapacityUnits } units for reading songs list with scanned count ${ ScannedCount }`);
const result = [];

for (const item of Items) {
result.push({
name: item.name.S,
genreType: parseInt(item.genreType.N, 10),
artistId: item.artistId.S,
releaseDate: item.releaseDate.S
releaseDate: item.releaseDate.S,
id: item.id.S
})
}

Expand Down

0 comments on commit 795f8da

Please sign in to comment.