-
Notifications
You must be signed in to change notification settings - Fork 2
/
Controller.js
77 lines (65 loc) · 1.87 KB
/
Controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
OneTable Migration controller
*/
import {DynamoDBClient} from '@aws-sdk/client-dynamodb'
import SenseLogs from 'senselogs'
import {Migrate} from 'onetable-migrate'
import {Tables} from './tables/index.js'
/*
One-time global config
*/
const profile = 'prod'
const client = new DynamoDBClient()
let log = new SenseLogs()
/*
Lambda entry point
*/
exports.handler = async (event) => {
let {cmd, args, config, dry} = event
let module = Tables[config.name]
if (!module) {
throw new Error(`No migrations defined for ${config.name}`)
}
let migrations = `./tables/${module.migrations}`
if (!migrations) {
throw new Error(`No migrations directory configured for table ${config.name}`)
}
config.client = client
config.senselogs = log
// config.crypto = {crypto settings for onetable encrypted attributes}
config.partial = true
let params = {dir: migrations, dry, log, profile}
let migrate = new Migrate(config, params)
await migrate.init()
let data
switch (cmd) {
case 'apply':
let {action, version} = args
data = await migrate.apply(action, version, params)
break
case 'getCurrentVersion':
data = await migrate.getCurrentVersion()
break
case 'getNamedVersions':
data = await migrate.getNamedVersions()
break
case 'getOutstandingVersions':
data = await migrate.getOutstandingVersions()
break
case 'getOutstandingMigrations':
data = await migrate.getOutstandingMigrations()
break
case 'getPastMigrations':
data = await migrate.getPastMigrations()
break
case 'getPastVersions':
data = await migrate.getPastVersions()
break
default:
throw new Error(`Unknown migration action ${action}`)
}
return {
body: data,
statusCode: 200,
}
}