-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
174 lines (160 loc) · 5.5 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var insist = require('insist-types');
var r = require('rethinkdb');
var Async = require('async');
/**
* Example config:
* {
* connection: {
* db: "test",
* host: "localhost"
* },
* tables: {
* table0: true,
* table1: "id",
* table2: "primaryKey",
* table3: ["primaryKey", "email"],
* table4: ["id", {name: "location", indexFunction: null, options: {geo: true}}]
* }
* }
*/
var connectAndSetup = function (config, callback) {
insist.args(arguments, Object, Function)
if (!config.connection) {
config.connection = {};
if (config.host) {
config.connection.host = config.host;
}
if (config.db) {
config.connection.db = config.db;
}
}
if (!config.connection.db) {
config.connection.db = 'test';
}
r.connect(config.connection, function (err, connection) {
if (err) return callback(err);
createDatabaseIfNeeded_(connection, function (err) {
setup(connection, config, function (err) {
if (err) return callback(err);
callback(null, connection);
});
});
});
};
var setup = function (connection, config, callback) {
insist.args(arguments, Object, Object, Function)
if (!config.tables) throw Error('Config object requires \'tables\' property');
createDatabaseIfNeeded_(connection, function (err) {
if (err) return callback(err);
r.tableList().run(connection, function (err, tables) {
if (err) return callback(err);
Async.forEachOf(config.tables, function (value, tableName, done) {
if (!value) {
return done()
}
var primaryKey = getPrimaryKeyFromConfig_(value);
var secondaryIndexes = getSecondaryIndexesFromConfig_(value);
if (tables.indexOf(tableName) === -1) {
// Create the table if it does not exist.
r.tableCreate(tableName, {primaryKey: primaryKey}).run(connection, function (err) {
if (err) return done(err);
addSecondaryIndexes_(connection, tableName, secondaryIndexes, done);
});
} else {
addSecondaryIndexes_(connection, tableName, secondaryIndexes, done);
}
}, callback);
});
});
};
var empty = function (connection, config, callback) {
insist.args(arguments, Object, Object, Function)
if (!config.tables) throw Error('Config object requires \'tables\' property');
Async.forEachOf(config.tables, function (value, tableName, done) {
r.table(tableName).delete().run(connection, done)
}, callback);
};
var load = function (connection, tables, callback) {
insist.args(arguments, Object, Object, Function)
Async.forEachOf(tables, function (value, tableName, done) {
r.table(tableName).insert(value).run(connection, done)
}, callback);
}
var addSecondaryIndexes_ = function (conn, tableName, indexes, callback) {
insist.args(arguments, Object, String, insist.arrayOf([String, Object]), Function)
r.table(tableName).indexList().run(conn, function (err, currentIndexes) {
if (err) return callback(err);
Async.each(indexes, function (index, done) {
var key = getSecondaryIndexKeyFromConfig_(index);
if (currentIndexes.indexOf(key) === -1) {
addSecondaryIndex_(conn, tableName, index, done);
} else {
done();
}
}, callback);
});
};
var addSecondaryIndex_ = function (conn, tableName, index, callback) {
insist.args(arguments, Object, String, [String, Object], Function)
var key = getSecondaryIndexKeyFromConfig_(index);
var createIndexQuery = null;
var options = index.options || {};
if (index.indexFunction) {
createIndexQuery = r.table(tableName).indexCreate(key, index.indexFunction, options);
} else {
createIndexQuery = r.table(tableName).indexCreate(key, options);
}
createIndexQuery.run(conn, function () {
r.table(tableName).indexWait(key).run(conn, callback);
});
};
var createDatabaseIfNeeded_ = function (connection, callback) {
insist.args(arguments, Object, Function)
r.dbList().run(connection, function (err, list) {
if (err) return callback(err);
if (list.indexOf(connection.db) === -1) {
r.dbCreate(connection.db).run(connection, function (err, data) {
callback(err);
});
} else {
callback();
}
});
};
var getPrimaryKeyFromConfig_ = function (tableConfig) {
insist.args(arguments, [Boolean, String, Array])
if (tableConfig === true || tableConfig === false) {
return "id";
}
if (typeof tableConfig === "string") {
return tableConfig;
}
// Must be an array of strings at this point.
if (!(tableConfig instanceof Array)) {
throw Error("value for tables must be a string or an array");
}
return tableConfig[0];
};
var getSecondaryIndexesFromConfig_ = function (tableConfig) {
insist.args(arguments, [Boolean, String, Array])
if (!(tableConfig instanceof Array)) {
return [];
}
return tableConfig.splice(1);
}
var getSecondaryIndexKeyFromConfig_ = function (indexConfig) {
insist.args(arguments, [String, Object])
if (typeof indexConfig === "string") {
return indexConfig;
}
if (!indexConfig.name) {
throw Error ("secondary index config missing name property:" + JSON.stringify(indexConfig));
}
return indexConfig.name;
}
module.exports = {
connectAndSetup: connectAndSetup,
setup: setup,
empty: empty,
load: load
};