forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.js
More file actions
148 lines (134 loc) · 4.42 KB
/
Copy pathQuery.js
File metadata and controls
148 lines (134 loc) · 4.42 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
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
var db = require('../lib/db.js')
var config = require('../lib/config.js')
var Joi = require('joi')
var request = require('request')
/*
"chartConfiguration": {
"chartType": "line",
"fields": {
"x": "created_month",
"y": "package_count",
"split": "keyword",
"xFacet": "",
"yFacet": "keyword",
"trendline": "true"
}
}
*/
var schema = {
_id: Joi.string().optional(), // generated by nedb
name: Joi.string().required(),
tags: Joi.array().items(Joi.string().empty('')).sparse().optional(),
connectionId: Joi.string().optional().empty(''),
queryText: Joi.string().optional().empty(''),
chartConfiguration: Joi.object({
chartType: Joi.string().optional().empty(''),
// key value pairings. key=chart property, value=field mapped to property
fields: Joi.object().unknown(true).optional()
}).optional(),
createdDate: Joi.date().default(new Date(), 'time of creation'),
modifiedDate: Joi.date().default(new Date(), 'time of modification'),
createdBy: Joi.string().required(),
modifiedBy: Joi.string().required(),
lastAccessDate: Joi.date().default(new Date(), 'time of last access')
}
var Query = function (data) {
this._id = data._id
this.name = data.name
this.tags = data.tags
this.connectionId = data.connectionId
this.queryText = data.queryText
this.chartConfiguration = data.chartConfiguration
this.createdDate = data.createdDate
this.createdBy = data.createdBy
this.modifiedDate = data.modifiedDate
this.modifiedBy = data.modifiedBy
this.lastAccessDate = data.lastAccessedDate
}
Query.prototype.save = function QuerySave (callback) {
var self = this
this.modifiedDate = new Date()
this.lastAccessDate = new Date()
// clean tags if present
// sqlpad v1 saved a lot of bad inputs
if (Array.isArray(self.tags)) {
self.tags = self.tags.filter(tag => {
return typeof tag === 'string' && tag.trim() !== ''
}).map(tag => {
return tag.trim()
})
}
var joiResult = Joi.validate(self, schema)
if (joiResult.error) return callback(joiResult.error)
if (self._id) {
db.queries.update({_id: self._id}, joiResult.value, {upsert: true}, function (err) {
if (err) return callback(err)
Query.findOneById(self._id, callback)
})
} else {
db.queries.insert(joiResult.value, function (err, newDoc) {
if (err) return callback(err)
return callback(null, new Query(newDoc))
})
}
}
Query.prototype.pushQueryToSlackIfSetup = function () {
const SLACK_WEBHOOK = config.get('slackWebhook')
if (SLACK_WEBHOOK) {
const PUBLIC_URL = config.get('publicUrl')
const BASE_URL = config.get('baseUrl')
var options = {
method: 'post',
body: {'text': 'New Query <' + PUBLIC_URL + BASE_URL +
'/queries/' + this._id + '|' + this.name +
'> saved by ' + this.modifiedBy + ' on SqlPad ```' +
this.queryText + '```'},
json: true,
url: SLACK_WEBHOOK
}
request(options, function (err, httpResponse, body) {
if (err) {
console.error('Something went wrong while sending to Slack.')
console.error(err)
}
})
}
}
/* Query methods
============================================================================== */
Query.findOneById = function QueryFindOneById (id, callback) {
db.queries.findOne({_id: id}).exec(function (err, doc) {
if (err) return callback(err)
if (!doc) return callback()
return callback(null, new Query(doc))
})
}
Query.findAll = function QueryFindAll (callback) {
db.queries.find({}).exec(function (err, docs) {
if (err) return callback(err)
var queries = docs.map(function (doc) {
return new Query(doc)
})
return callback(null, queries)
})
}
Query.findByFilter = function QueryFindByFilter (filter, callback) {
db.queries.find(filter).exec(function (err, docs) {
if (err) return callback(err)
var queries = docs.map(function (doc) {
return new Query(doc)
})
return callback(null, queries)
})
}
Query.prototype.logAccess = function QueryLogAccess (callback) {
var self = this
db.queries.update({_id: self._id}, {$set: {lastAccessedDate: new Date()}}, {}, callback)
}
Query.removeOneById = function QueryRemoveOneById (id, callback) {
db.queries.remove({_id: id}, callback)
}
Query._removeAll = function QueryRemoveAll (callback) {
db.queries.remove({}, {multi: true}, callback)
}
module.exports = Query