UNPKG

2.11 kBJavaScriptView Raw
1'use strict';
2
3const uuid = require('node-uuid');
4const MigrationTableSchema = require('./MigrationTableSchema');
5const _createDynamoTable = require('./util/createDynamoTable');
6const _putAttrItem = require('./util/putAttrItem');
7const _queryFileByName = require('./util/queryFileByName');
8
9class MigrationTable {
10 constructor(dynamodb, migrationTableName) {
11 this.dynamodb = dynamodb;
12 this.migrationTableName = migrationTableName;
13 }
14
15 /**
16 * Insert new migration record
17 */
18 insert(fileName) {
19 let id = uuid.v4();
20 return _putAttrItem(this.dynamodb, this.migrationTableName, {
21 id: id,
22 filename: fileName,
23 pending: true
24 }).then(() => id);
25 }
26
27 updatePending(fileId, pending) {
28 return new Promise((resolve, reject) => {
29 this.dynamodb.updateItem({
30 TableName: this.migrationTableName,
31 Key: {
32 id: {
33 'S': fileId
34 }
35 },
36 UpdateExpression: 'set pending = :pending',
37 ExpressionAttributeValues: {
38 ':pending': {
39 'BOOL': pending
40 },
41 }
42 }, (err, data) => {
43 return err ? reject(err) : resolve();
44 });
45 });
46 }
47
48 alreadyRan(fileName) {
49 return _queryFileByName(this.dynamodb, this.migrationTableName, fileName)
50 .then(data => data.Items.length > 0);
51 }
52
53 static create(dynamodb, migrationTableName) {
54 return new Promise((resolve, reject) => {
55 let migrationTable = new MigrationTable(dynamodb, migrationTableName);
56
57 dynamodb.describeTable({
58 TableName: migrationTableName
59 }, (err, data) => {
60 if (err && err.code == 'ResourceNotFoundException') {
61 let migrationTableSchema = MigrationTableSchema;
62 migrationTableSchema.TableName = migrationTableName;
63
64 _createDynamoTable(dynamodb, migrationTableSchema)
65 .then(() => resolve(migrationTable))
66 .catch(err => reject(err));
67 } else if (err) {
68 reject(err);
69 } else {
70 resolve(migrationTable);
71 }
72 });
73 });
74 }
75}
76
77module.exports = MigrationTable;