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 pathseed.js
122 lines (109 loc) · 3.34 KB
/
seed.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// This script imports a json seed file
// params
// truncate: truncate tables before import
// filename: the filenme to import
//
// usage
// cf_server node seeds/seed.js [--truncate] --filename FILENAME
//
require('dotenv').config()
const PgDb = require('../lib/pgdb')
PgDb.connect().then(async (pgdb) => {
const argv = require('minimist')(process.argv.slice(2))
const {filename, truncate} = argv
if (!filename) { throw new Error('filename must be provided as last argument') }
const {rewards, crowdfundings} = require('./' + filename).default
const tableNames = {
Goodie: 'goodies',
MembershipType: 'membershipTypes'
}
const transaction = await pgdb.transactionBegin()
const now = new Date()
try {
if(truncate) {
console.log('TRUNCATING tables')
const tables = [
'pledges',
'packages',
'rewards',
'crowdfundings'
]
for(let table of tables) {
await transaction.query(`TRUNCATE TABLE ${table} CASCADE`)
}
}
console.log('Importing...')
for(let rewardType in rewards) {
for(let reward of rewards[rewardType]) {
const newReward = await transaction.public.rewards.insertAndGet({
type: rewardType,
createdAt: now,
updatedAt: now
})
const table = transaction.public[tableNames[rewardType]]
await table.insert(Object.assign({}, reward, {
rewardId: newReward.id,
rewardType: newReward.type,
createdAt: now,
updatedAt: now
}))
}
}
for(let crowdfunding of crowdfundings) {
const newCrowdfunding = await transaction.public.crowdfundings.insertAndGet({
name: crowdfunding.name,
beginDate: crowdfunding.beginDate,
endDate: crowdfunding.endDate,
createdAt: now,
updatedAt: now
})
for(let goal of crowdfunding.goals) {
await transaction.public.crowdfundingGoals.insert( Object.assign({}, goal, {
crowdfundingId: newCrowdfunding.id,
createdAt: now,
updatedAt: now
}))
}
for(let pkg of crowdfunding.packages) {
const newPackage = await transaction.public.packages.insertAndGet({
name: pkg.name,
crowdfundingId: newCrowdfunding.id,
createdAt: now,
updatedAt: now
})
for(let option of pkg.options) {
let rewardId
if(option.rewardType || option.rewardName) {
const table = transaction.public[tableNames[option.rewardType]]
const rewardEntity = await table.findOne({
name: option.rewardName
})
rewardId = rewardEntity.rewardId
}
await transaction.public.packageOptions.insert({
packageId: newPackage.id,
rewardId,
minAmount: option.minAmount,
maxAmount: option.maxAmount,
defaultAmount: option.defaultAmount,
price: option.price,
userPrice: option.userPrice,
createdAt: now,
updatedAt: now
}, {skipUndefined: true})
}
}
}
console.log('finished.')
await transaction.transactionCommit()
} catch (e) {
await transaction.transactionRollback()
throw e
}
}).then(() => {
process.exit()
}).catch(e => {
console.error(e)
process.exit(1)
})