-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
101 lines (84 loc) · 2.17 KB
/
index.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
const { Client } = require('ckan-client')
const apiKey = '4ed9c0cc-60c1-4c12-8bb7-fedeb0aba422' // get it from "/user/{your-admin-username}/api-tokens"
const apiUrl = 'http://localhost:5000'
const organizationId = 'organization'
const datasetId = 'dataset'
const client = new Client(apiKey, organizationId, datasetId, apiUrl)
const main = async () => {
// create_package
let package
try {
package = await client.create({
name: 'package', // should be lowercase without symbols
owner_org: 'organization' // should be lowercase without symbols
})
} catch (err) {
console.log(JSON.stringify(err, null, 2))
}
// create_dataset
let actionName = 'resource_create'
let payload = {
package_id: package.id,
name: 'resource',
url: 'url',
}
let resource
try {
resource = await client.action(actionName, payload)
} catch (err) {
console.log(JSON.stringify(err, null, 2))
}
// create datastore
const fields = [
{
"id": "x",
"type": "float",
},
{
"id": "y",
"type": "float",
},
]
records = []
for (let i = 0; i < 10; i++) {
records.push({
"y": i,
"x": Math.random(),
})
}
actionName = 'datastore_create'
payload = {
package_id: package.id,
resource_id: resource.result.id,
force: 'true',
fields,
records,
primary_key: 'y'
}
try {
await client.action(actionName, payload)
} catch (err) {
console.log(JSON.stringify(err, null, 2))
}
// upsert datastore
records = []
for (let i = 0; i < 20; i++) {
records.push({
"y": i,
"x": Math.random(),
})
}
actionName = 'datastore_upsert'
payload = {
resource_id: resource.result.id,
force: 'true',
records,
method: 'upsert' // can be 'insert'
}
try {
await client.action(actionName, payload)
} catch (err) {
console.log(JSON.stringify(err, null, 2))
}
}
main()