Skip to content

Commit a80d104

Browse files
committed
Added js-data to rethinkdb filter conversion.
1 parent ee979c4 commit a80d104

File tree

2 files changed

+121
-17
lines changed

2 files changed

+121
-17
lines changed

mocha.start.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ test.globals(testGlobals);
4949

5050
beforeEach(function () {
5151
store = new JSData.DS();
52-
adapter = new DSRethinkDBAdapter({
53-
min: 10,
54-
max: 50,
55-
bufferSize: 10
56-
});
52+
adapter = new DSRethinkDBAdapter();
5753
DSUtils = JSData.DSUtils;
5854
DSErrors = JSData.DSErrors;
5955
globals.User = global.User = User = store.defineResource('user');

src/index.js

Lines changed: 120 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,129 @@ if (!JSData) {
3131
}
3232

3333
var DSUtils = JSData.DSUtils;
34-
var deepMixIn = DSUtils.deepMixIn;
35-
var forEach = DSUtils.forEach;
3634

3735
function Defaults() {
3836

3937
}
4038

39+
Defaults.prototype.host = 'localhost';
40+
Defaults.prototype.port = 28015;
41+
Defaults.prototype.authKey = '';
42+
Defaults.prototype.db = 'test';
43+
Defaults.prototype.min = 10;
44+
Defaults.prototype.max = 50;
45+
Defaults.prototype.bufferSize = 10;
46+
47+
var reserved = [
48+
'orderBy',
49+
'sort',
50+
'limit',
51+
'offset',
52+
'skip',
53+
'where'
54+
];
55+
56+
function filterQuery(r, resourceConfig, params) {
57+
params = params || {};
58+
params.where = params.where || {};
59+
params.orderBy = params.orderBy || params.sort;
60+
params.skip = params.skip || params.offset;
61+
62+
var keys = Object.keys(params);
63+
keys.forEach(function (k) {
64+
var v = params[k];
65+
if (!DSUtils.contains(reserved, k)) {
66+
if (DSUtils.isObject(v)) {
67+
params.where[k] = v;
68+
} else {
69+
params.where[k] = {
70+
'==': v
71+
};
72+
}
73+
delete params[k];
74+
}
75+
});
76+
77+
var query = r.table(resourceConfig.endpoint);
78+
var subQuery;
79+
80+
DSUtils.forOwn(params.where, function (criteria, field) {
81+
if (!DSUtils.isObject(criteria)) {
82+
params.where[field] = {
83+
'==': criteria
84+
};
85+
}
86+
DSUtils.forOwn(criteria, function (v, op) {
87+
if (op === '==' || op === '===') {
88+
subQuery = subQuery ? subQuery.and(r.row(field).eq(v)) : r.row(field).eq(v);
89+
} else if (op === '!=' || op === '!==') {
90+
subQuery = subQuery ? subQuery.and(r.row(field).ne(v)) : r.row(field).ne(v);
91+
} else if (op === '>') {
92+
subQuery = subQuery ? subQuery.and(r.row(field).gt(v)) : r.row(field).gt(v);
93+
} else if (op === '>=') {
94+
subQuery = subQuery ? subQuery.and(r.row(field).ge(v)) : r.row(field).ge(v);
95+
} else if (op === '<') {
96+
subQuery = subQuery ? subQuery.and(r.row(field).lt(v)) : r.row(field).lt(v);
97+
} else if (op === '<=') {
98+
subQuery = subQuery ? subQuery.and(r.row(field).le(v)) : r.row(field).le(v);
99+
} else if (op === 'in') {
100+
subQuery = subQuery ? subQuery.and(r.row(field).contains(v)) : r.row(field).contains(v);
101+
} else if (op === 'notIn') {
102+
subQuery = subQuery ? subQuery.and(r.row(field).contains(v).not()) : r.row(field).contains(v).not();
103+
} else if (op === '|==' || op === '|===') {
104+
subQuery = subQuery ? subQuery.or(r.row(field).eq(v)) : r.row(field).eq(v);
105+
} else if (op === '|!=' || op === '|!==') {
106+
subQuery = subQuery ? subQuery.or(r.row(field).ne(v)) : r.row(field).ne(v);
107+
} else if (op === '|>') {
108+
subQuery = subQuery ? subQuery.or(r.row(field).gt(v)) : r.row(field).gt(v);
109+
} else if (op === '|>=') {
110+
subQuery = subQuery ? subQuery.or(r.row(field).ge(v)) : r.row(field).ge(v);
111+
} else if (op === '|<') {
112+
subQuery = subQuery ? subQuery.or(r.row(field).lt(v)) : r.row(field).lt(v);
113+
} else if (op === '|<=') {
114+
subQuery = subQuery ? subQuery.or(r.row(field).le(v)) : r.row(field).le(v);
115+
} else if (op === '|in') {
116+
subQuery = subQuery ? subQuery.or(r.row(field).contains(v)) : r.row(field).contains(v);
117+
} else if (op === '|notIn') {
118+
subQuery = subQuery ? subQuery.or(r.row(field).contains(v).not()) : r.row(field).contains(v).not();
119+
}
120+
});
121+
});
122+
123+
if (subQuery) {
124+
query = query.filter(subQuery);
125+
}
126+
127+
if (params.orderBy) {
128+
if (utils.isString(params.orderBy)) {
129+
params.orderBy = [
130+
[params.orderBy, 'asc']
131+
];
132+
}
133+
for (var i = 0; i < params.orderBy.length; i++) {
134+
if (utils.isString(params.orderBy[i])) {
135+
params.orderBy[i] = [params.orderBy[i], 'asc'];
136+
}
137+
query = utils.upperCase(params.orderBy[i][1]) === 'DESC' ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]);
138+
}
139+
}
140+
141+
if (params.skip) {
142+
query = query.skip(params.skip);
143+
}
144+
145+
if (params.limit) {
146+
query = query.limit(params.limit);
147+
}
148+
149+
return query;
150+
}
151+
41152
function DSRethinkDBAdapter(options) {
42-
var _this = this;
43153
options = options || {};
44-
_this.defaults = new Defaults();
45-
deepMixIn(_this.defaults, options);
46-
_this.r = rethinkdbdash(options);
154+
this.defaults = new Defaults();
155+
DSUtils.deepMixIn(this.defaults, options);
156+
this.r = rethinkdbdash(this.defaults);
47157
}
48158

49159
var dsRethinkDBAdapterPrototype = DSRethinkDBAdapter.prototype;
@@ -59,8 +169,7 @@ dsRethinkDBAdapterPrototype.find = function find(resourceConfig, id) {
59169
};
60170

61171
dsRethinkDBAdapterPrototype.findAll = function (resourceConfig, params) {
62-
params = params || {};
63-
return this.r.table(resourceConfig.endpoint).filter(params).run();
172+
return filterQuery(this.r, resourceConfig, params).run();
64173
};
65174

66175
dsRethinkDBAdapterPrototype.create = function (resourceConfig, attrs) {
@@ -76,11 +185,10 @@ dsRethinkDBAdapterPrototype.update = function (resourceConfig, id, attrs) {
76185
};
77186

78187
dsRethinkDBAdapterPrototype.updateAll = function (resourceConfig, attrs, params) {
79-
var _this = this;
80188
params = params || {};
81-
return _this.r.table(resourceConfig.endpoint).filter(params).update(attrs, { returnChanges: true }).run().then(function (cursor) {
189+
return filterQuery(this.r, resourceConfig, params).update(attrs, { returnChanges: true }).run().then(function (cursor) {
82190
var items = [];
83-
DSUtils.forEach(cursor.changes, function (change) {
191+
cursor.changes.forEach(function (change) {
84192
items.push(change.new_val);
85193
});
86194
return items;
@@ -95,7 +203,7 @@ dsRethinkDBAdapterPrototype.destroy = function (resourceConfig, id) {
95203

96204
dsRethinkDBAdapterPrototype.destroyAll = function (resourceConfig, params) {
97205
params = params || {};
98-
return this.r.table(resourceConfig.endpoint).filter(params).delete().run().then(function () {
206+
return filterQuery(this.r, resourceConfig, params).delete().run().then(function () {
99207
return undefined;
100208
});
101209
};

0 commit comments

Comments
 (0)