-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-db-state-1.js
More file actions
58 lines (51 loc) · 1.55 KB
/
create-db-state-1.js
File metadata and controls
58 lines (51 loc) · 1.55 KB
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
/**
* Test script for STATE 1 of data migration testing.
* Create a simple model, jam a lot of data in.
*/
import ottoman from 'ottoman';
import connection from './connection';
import randomstring from 'randomstring';
import Promise from 'bluebird';
ottoman.bucket = connection.bucket;
const Model = ottoman.model('Test', {
oid: { type: 'string', auto: 'uuid', readonly: true },
number: { type: 'number' },
name: { type: 'string' },
renameMe: { type: 'string' },
removeMe: { type: 'string' },
},
{
id: 'oid',
index: {
findByOid: {
by: 'oid',
type: 'n1ql',
},
},
});
const promises = [];
ottoman.ensureIndices(err => {
if (err) {
console.error(`Failed to create ottoman indices: ${err}`);
throw new Error('Nope.');
}
for (let i = 0; i < 100; i++) {
const number = Math.random();
const name = randomstring.generate({ length: 12, charset: 'alphabetic', });
const renameMe = randomstring.generate({ length: 12, charset: 'alphabetic' });
const removeMe = randomstring.generate({ length: 12, charset: 'alphabetic' });
promises.push(new Promise((resolve, reject) => {
new Model({ name, number, renameMe, removeMe }).save(err2 => {
if (err2) { return reject(err2); }
console.log('Saving record...');
return resolve(true);
});
}));
}
return Promise.all(promises)
.then(() => setTimeout(() => process.exit(0), 1000))
.catch(err2 => {
console.error(`Failed to insert records: ${err2}`);
setTimeout(() => process.exit(1), 1000);
});
});