-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.js
More file actions
74 lines (59 loc) · 1.75 KB
/
Copy pathplugin.js
File metadata and controls
74 lines (59 loc) · 1.75 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module.exports = Plugin;
function Plugin(db) {
if (!(new RegExp('^mysql', 'i')).test(db.driver.config.protocol)) {
throw new Error("ORM MySQL FTS plugin does not support drivers other than MySQL");
}
db.tools.match = function () {
var props = Array.prototype.slice.apply(arguments);
if (props.length === 0) {
throw new Error("No properties defined for Full-Text Search matching");
}
return {
against: function (expr, column) {
column = column || "score";
var obj = {
// MATCH (?:id, ?:id, ...) AGAINST (?:value) AS score
select : {
str : "MATCH (?:id" + (new Array(props.length)).join(", ?:id") + ") AGAINST (?:value) AS ?:id",
escapes : props.concat([ expr, column ])
},
// HAVING score > 0
having : {
str : "?:id > 0",
escapes : [ column ]
}
};
Object.defineProperty(obj, "sql_comparator", {
configurable : false,
enumerable : false,
value : function () { return "sql"; } // generic SQL
});
return obj;
}
};
};
return {
define: function (Model) {
var fields = Object.keys(Model.properties);
for (var i = 0; i < Model.keys.length; i++) {
if (fields.indexOf(Model.keys[i]) == -1) {
fields.unshift(Model.keys[i]);
}
}
Model.match = function () {
var props = Array.prototype.slice.apply(arguments);
if (props.length === 0) {
throw new Error("No properties defined for Full-Text Search matching");
}
return {
against: function (expr, column) {
var chain = Model.find();
column = column || "score";
chain.only.apply(chain, fields.concat([ db.tools.match.apply(null, props).against(expr, column) ]));
return chain.order("-" + column);
}
};
};
}
};
}