Skip to content

Commit 5b55894

Browse files
committed
added migrate command
1 parent 305a115 commit 5b55894

File tree

4 files changed

+172
-5
lines changed

4 files changed

+172
-5
lines changed

package-lock.json

Lines changed: 89 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-fastcli",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"main": "index.js",
55
"author": "Robert Zibert",
66
"license": "MIT",
@@ -15,12 +15,16 @@
1515
"quasar": "quasar dev"
1616
},
1717
"dependencies": {
18+
"await-to-js": "^2.1.1",
1819
"axios": "^0.18.0",
1920
"babel-core": "^6.26.3",
2021
"babel-plugin-source-map-support": "^2.0.1",
22+
"babel-polyfill": "^6.26.0",
2123
"chokidar": "^2.0.4",
2224
"colors": "^1.2.1",
2325
"commander": "^2.15.1",
26+
"fast-fluent": "^0.1.18",
27+
"fluent-formio": "^0.1.3",
2428
"globby": "^8.0.1",
2529
"hygen": "^1.6.2",
2630
"lodash": "^4.17.5",

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import program from 'commander'
33
import init from './init'
44
import pull from './pull'
55
import runServer from './run'
6+
import migrate from './migrate'
67
// import build from './build'
78
import colors from 'colors/safe'
89
import './helpers/fastLogo'
@@ -49,9 +50,20 @@ program
4950

5051
program
5152
.command('run')
52-
.description('Runs dev build for the current Fast project.')
53+
.description('Runs dev build for the current FAST project.')
5354
.action(run(runServer))
5455

56+
program
57+
.command('migrate <file>')
58+
.option('-u, --url [URL]', 'Existing project url')
59+
.option('-p, --path [Path to form]', 'Path where data should be imported')
60+
.option('-k, --dst-key [API key]', 'Form.io API key')
61+
.description('Migrate data from a JSON file to an existing FAST project')
62+
// eslint-disable-next-line
63+
.action((file, cmd) => {
64+
migrate(file, cmd)
65+
})
66+
5567
// program
5668
// .command('build')
5769
// .description('Compiles an Orionjs app and exports it to a simple nodejs app')

src/migrate/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'babel-polyfill'
2+
import fs from 'fs'
3+
import { Fluent } from 'fast-fluent'
4+
import formio from 'fluent-formio'
5+
import to from 'await-to-js'
6+
7+
export default async function(file, options) {
8+
console.log(file, options.url, options.path, options.dstKey)
9+
10+
if (!options.url) {
11+
console.error('No url specified.')
12+
return
13+
}
14+
15+
if (!options.path) {
16+
console.error('No form or resource path specified.')
17+
return
18+
}
19+
20+
if (!options.dstKey) {
21+
console.error('No API key provided.')
22+
return
23+
}
24+
25+
const data = JSON.parse(fs.readFileSync(file, 'utf-8'))
26+
27+
await Fluent.config({
28+
REMOTE_CONNECTORS: [{
29+
name: 'formio',
30+
baseUrl: options.url,
31+
connector: formio
32+
}]
33+
})
34+
35+
const model = Fluent.model({
36+
properties: {
37+
name: 'submission',
38+
config: {
39+
remote: {
40+
path: options.path,
41+
token: options.dstKey
42+
}
43+
}
44+
}
45+
})()
46+
47+
const collection = Fluent.collect(data)
48+
const errors = []
49+
50+
try {
51+
console.time('saveChunk')
52+
await collection.chunkApply(200, async (d) => {
53+
const [error, result] = await to(model.remote().insert(d))
54+
55+
if (error) {
56+
errors.push({ original: d, error })
57+
}
58+
})
59+
const time = console.timeEnd('saveChunk')
60+
61+
console.log(`Took ${time} seconds, with ${errors.length} errors.`)
62+
} catch (e) {
63+
console.error(e)
64+
}
65+
}

0 commit comments

Comments
 (0)