Skip to content

Commit 7ecb26f

Browse files
author
Fuss Florian (uid10804)
committed
feat(server): add storage adapter for aws dynamodb
1 parent 06cc18d commit 7ecb26f

File tree

6 files changed

+151
-2
lines changed

6 files changed

+151
-2
lines changed

packages/server/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
},
1111
"scripts": {
1212
"test": "npx jest",
13-
"debug": "tsc-watch --onSuccess \"node --inspect-brk ./lib/example/simple.js\"",
14-
"start": "tsc-watch --onSuccess \"node ./lib/example/simple.js\"",
13+
"debug": "tsc-watch --onSuccess \"node --inspect-brk ./lib/example/simple.s3.js\"",
14+
"start": "tsc-watch --onSuccess \"node ./lib/example/simple.s3.js\"",
15+
"start:dynamodb": "tsc-watch --onSuccess \"node ./lib/example/simple.dynamodb.js\"",
1516
"start:advanced": "tsc-watch --onSuccess \"node ./lib/example/advanced.js\"",
1617
"type-check": "tsc --noEmit",
1718
"type-check:watch": "npm run type-check -- --watch",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import express from 'express';
2+
import {
3+
AppConfig,
4+
CoreApp,
5+
Environment,
6+
PublicStrategy,
7+
Swagger,
8+
SwaggerConfig,
9+
} from '../index';
10+
import { DynamoDBStorageAdapter } from '../storage/dynamodb.storage';
11+
const server = express();
12+
const appConfig = new AppConfig();
13+
14+
const environment = new Environment();
15+
16+
const swagger = new Swagger(
17+
server,
18+
new SwaggerConfig(appConfig.readOnly, appConfig.enableApiKeyAuth),
19+
environment.basePath,
20+
appConfig.routes.apiRoutePath,
21+
'./package.json',
22+
appConfig.routes.swaggerSpecRoutePath
23+
);
24+
25+
const core = new CoreApp(
26+
appConfig,
27+
server,
28+
new DynamoDBStorageAdapter(
29+
'json-serverless-simple-example1',
30+
appConfig.stackName,
31+
appConfig.jsonFile,
32+
'us-east-1'
33+
),
34+
swagger,
35+
environment,
36+
new PublicStrategy()
37+
);
38+
39+
const init = async () => {
40+
await core!.setup().catch(e => {
41+
console.error(e.message);
42+
});
43+
console.log(
44+
'JSON Server is running under port 3000. Use http://localhost:3000/ to access it'
45+
);
46+
47+
server.listen(3000);
48+
};
49+
50+
init();
File renamed without changes.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { DynamoDB } from 'aws-sdk';
2+
3+
export class DynamoDBLowDBAdapter {
4+
dynamo: DynamoDB.DocumentClient;
5+
defaultValue: {};
6+
table: string;
7+
keyId: string;
8+
source = '';
9+
readonly '@@reference': any;
10+
constructor(defaultValue = {}, table: string, keyId: string, region: string) {
11+
this.defaultValue = defaultValue;
12+
this.dynamo = new DynamoDB.DocumentClient({
13+
apiVersion: '2012-08-10',
14+
region,
15+
});
16+
this.table = table;
17+
this.keyId = keyId;
18+
}
19+
20+
async read(): Promise<object> {
21+
let result = null;
22+
try {
23+
const params: DynamoDB.DocumentClient.GetItemInput = {
24+
TableName: this.table,
25+
Key: {
26+
keyId: this.keyId,
27+
},
28+
};
29+
console.log(JSON.stringify(params));
30+
result = await this.dynamo.get(params).promise();
31+
} catch (error) {
32+
console.log(error.message);
33+
throw error;
34+
}
35+
36+
if (result.Item === undefined || result.Item === null) {
37+
this.write(this.defaultValue);
38+
return this.defaultValue;
39+
}
40+
return JSON.parse(result.Item![this.keyId]);
41+
}
42+
43+
async write(data: object) {
44+
try {
45+
const params: DynamoDB.DocumentClient.PutItemInput = {
46+
TableName: this.table,
47+
Item: {
48+
keyId: this.keyId,
49+
jsonsls: JSON.stringify(data),
50+
},
51+
};
52+
await this.dynamo.put(params).promise();
53+
} catch (error) {
54+
if (
55+
error.code === 'ValidationException' &&
56+
error.message === 'Item size has exceeded the maximum allowed size'
57+
) {
58+
throw new Error(
59+
'Item size has exceeded the maximum allowed size (currently 400 kb) - Please use another storage provider e.g. S3 to store large files'
60+
);
61+
} else {
62+
throw new Error(error.message);
63+
}
64+
}
65+
}
66+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { StorageAdapter } from './storage';
2+
import fs from 'fs';
3+
import { DynamoDBLowDBAdapter } from './dynamodb.adapter';
4+
import { AdapterAsync } from 'lowdb';
5+
6+
export class DynamoDBStorageAdapter implements StorageAdapter {
7+
private table: string;
8+
private keyId: string;
9+
private jsonFilePath: string;
10+
private region: string;
11+
constructor(table: string, keyId: string, jsonFilePath: string, region) {
12+
this.table = table;
13+
this.keyId = keyId;
14+
this.jsonFilePath = jsonFilePath;
15+
this.region = region;
16+
}
17+
18+
init(): import('lowdb').AdapterAsync {
19+
console.log('initController');
20+
const db = JSON.parse(fs.readFileSync(this.jsonFilePath, 'UTF-8'));
21+
const storageAdapter = new DynamoDBLowDBAdapter(
22+
db,
23+
this.table,
24+
this.keyId,
25+
this.region
26+
) as unknown;
27+
28+
return storageAdapter as AdapterAsync;
29+
}
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './file.storage';
22
export * from './storage';
33
export * from './s3.storage';
4+
export * from './dynamodb.storage';
5+
export * from './dynamodb.adapter';

0 commit comments

Comments
 (0)