-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
263 lines (215 loc) · 6.24 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var parallel = require('async/parallel');
var cassandraDriver = require('cassandra-driver');
var Schema = require('./lib/schema');
var Connection = require('./lib/connection');
var utils = require('./lib/utils');
var Promise = require('./lib/promise');
var Model = require('./lib/model');
var error = require('./lib/error');
var log = require('./lib/log');
var types = cassandraDriver.types;
var format = utils.toCollectionName;
var KEYSPACE_DOES_NOT_EXIST_ERROR = 8704;
function Cassandrom() {
this.connections = [];
this.models = {};
this.modelSchemas = {};
this.uuid = types.uuid;
this.Types = require('./lib/types');
this.UUIDType = require('./lib/schema/uuid');
this.ListId = require('./lib/schema/listid');
this.options = {
};
}
Cassandrom.prototype.Schema = Schema;
Cassandrom.prototype.Promise = Promise;
Cassandrom.prototype.connect = Cassandrom.prototype.createConnection = function (options, fn) {
var keyspace = options.keyspace;
delete options.keyspace;
var conn = new Connection(this, options);
var index = this.connections.length;
this.connections.push(conn);
conn._events = {
error: function() { },
open: function() { },
};
conn.once = function(name, fn) {
conn._events[name] = fn;
return this;
};
var self = this;
if(fn && options.createTables) {
var done = fn;
fn = function(error, conn) {
if(error) {
done(error);
} else {
self._createTables(conn, done);
}
};
}
fn = fn || function() {};
var connectToKeyspace = function(error) {
if(error) {
fn(error, conn);
} else {
conn.useKeyspace(keyspace, function(err, result) {
if(err) {
if(options.createKeyspace) {
log.info('Create keyspace ' + keyspace);
conn.createKeyspace(keyspace, 'SimpleStrategy', 1, function(error) {
if(error) {
log.info('Create keyspace Error: ' + error);
conn._events['error'](error);
fn(error, conn);
} else {
conn.useKeyspace(keyspace, function(err, result) {
if(err) {
log.info('Could not use ' + keyspace + ' ' + err);
conn._events['error'](error);
} else {
self._setupConnection(conn);
conn._events['open']();
}
fn(error, conn);
});
}
});
} else {
conn._events['error'](error);
fn(error, conn);
}
} else {
self._setupConnection(conn);
conn._events['open']();
fn(error, conn);
}
});
}
};
conn.connect(function(error) {
if(error) {
log.info('Connection Error: ' + error);
conn._events['error'](error);
} else {
log.info('Successfully connected...');
}
connectToKeyspace(error);
});
conn.onClose(function() {
self.connections.splice(index, 1);
});
this.connection = conn;
return conn;
};
Cassandrom.prototype.model = function (name, schema, collection, skipInit, cb) {
if ('string' == typeof schema) {
collection = schema;
schema = false;
}
if ('boolean' === typeof collection) {
skipInit = collection;
collection = null;
}
// handle internal options from connection.model()
var options;
if (skipInit && utils.isObject(skipInit)) {
options = skipInit;
skipInit = true;
} else {
options = {};
}
// look up schema for the collection. this might be a
// default schema like system.indexes stored in SchemaDefaults.
if (!this.modelSchemas[name]) {
if (!schema && name in SchemaDefaults) {
schema = SchemaDefaults[name];
}
if (schema) {
// cache it so we only apply plugins once
this.modelSchemas[name] = schema;
// this._applyPlugins(schema);
} else {
throw new Error.MissingSchemaError(name);
}
}
var model;
var sub;
// connection.model() may be passing a different schema for
// an existing model name. in this case don't read from cache.
if (this.models[name] && false !== options.cache) {
if (schema instanceof Schema && schema != this.models[name].schema) {
throw new error.OverwriteModelError(name);
}
if (collection) {
// subclass current model with alternate collection
model = this.models[name];
schema = model.prototype.schema;
sub = model.__subclass(this.connection, schema, collection);
// do not cache the sub model
return sub;
}
return this.models[name];
}
// ensure a schema exists
if (!schema) {
schema = this.modelSchemas[name];
if (!schema) {
throw new Error.MissingSchemaError(name);
}
}
if(!schema.primaryKey) {
throw new Error('Primary Key must be provided');
}
if (!collection) {
collection = schema.get('collection') || name;
}
model = Model.compile(name, schema, collection, null);
if (!skipInit) {
model.init();
}
if(!model.base && this.connection) {
model.base = model.prototype.base = this.connection;
}
if(this.connection._options.createTables) {
this.connection.createTableIfNotExists(name, model.schema, function(error) {
if(error) {
console.error('[cassandrom] Failed create table ' + name + ' ' + error);
}
cb && cb(error);
});
}
cb && cb(error);
if (false === options.cache) {
return model;
}
return this.models[name] = model;
};
Cassandrom.prototype._createTables = function(conn, done) {
var toExecute = [];
for(var name in this.models) {
var schema = this.models[name].schema;
toExecute.push(function(callback) {
conn.createTableIfNotExists(name, schema, callback);
});
}
if(toExecute.length > 0) {
parallel(toExecute, function(error, savedDocs) {
if(error) {
console.error('[cassandrom] Could not create table ' + error);
}
done(error, conn);
});
} else {
done(null, conn);
}
};
Cassandrom.prototype._setupConnection = function(connection) {
for(var name in this.models) {
var model = this.models[name];
if(!model.base) {
model.base = model.prototype.base = connection;
}
}
};
var cassandra = module.exports = exports = new Cassandrom;