This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimportGoals.js
63 lines (53 loc) · 1.68 KB
/
importGoals.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
//
// This script imports additional crowdfunding goals
//
// usage
// cf_server cat script/examples/goals.json | node script/importGoals.js
//
const PgDb = require('../lib/pgdb')
const rw = require('rw')
require('dotenv').config()
const CF_NAME = 'REPUBLIK'
PgDb.connect().then(async (pgdb) => {
const input = JSON.parse(rw.readFileSync('/dev/stdin', 'utf8'))
if (!input || !input.goals) {
console.error('input.goals required')
process.exit(1)
}
console.log(`importing new goals...`)
const transaction = await pgdb.transactionBegin()
try {
const crowdfundingId = await transaction.public.crowdfundings.findOneFieldOnly({name: CF_NAME}, 'id')
for (let goal of input.goals) {
const {name, people, money, description} = goal
const existingGoal = await transaction.public.crowdfundingGoals.findOne({name})
if (existingGoal) { // update existing
await transaction.public.crowdfundingGoals.update({id: existingGoal.id}, {
people,
money,
description,
updatedAt: new Date()
}, {skipUndefined: true})
} else {
await transaction.public.crowdfundingGoals.insert({
crowdfundingId,
name,
people,
money,
description
}, {skipUndefined: true})
}
}
await transaction.transactionCommit()
console.log('done! New goals:')
console.log(await pgdb.public.crowdfundingGoals.find({}, {orderBy: ['people asc', 'money asc']}))
} catch (e) {
console.log('error in transaction! rolledback!')
console.log(e)
await transaction.transactionRollback()
}
}).then(() => {
process.exit()
}).catch(e => {
console.log(e)
})