-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
335 lines (283 loc) · 10.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const createError = require('http-errors')
const _ = require('lodash')
const { raw } = require('objection')
const pg = require('pg')
const { extractDataFromObjectId } = require('stelace-util-keys')
const { getKnex } = require('./util')
const { getPlatformEnvData } = require('../redis')
const { mergeFunctionName } = require('../database')
const models = {
ApiKey: require('./ApiKey'),
Assessment: require('./Assessment'),
Asset: require('./Asset'),
AssetType: require('./AssetType'),
AuthMean: require('./AuthMean'),
AuthToken: require('./AuthToken'),
Availability: require('./Availability'),
Category: require('./Category'),
Config: require('./Config'),
CustomAttribute: require('./CustomAttribute'),
Document: require('./Document'),
Entry: require('./Entry'),
Event: require('./Event'),
Message: require('./Message'),
Order: require('./Order'),
Role: require('./Role'),
SavedSearch: require('./SavedSearch'),
Task: require('./Task'),
Transaction: require('./Transaction'),
User: require('./User'),
Webhook: require('./Webhook'),
WebhookLog: require('./WebhookLog'),
Workflow: require('./Workflow'),
WorkflowLog: require('./WorkflowLog'),
InternalAvailability: require('./InternalAvailability')
}
// map only models that can be exposed via event
const mapIdPrefixToModelNames = {
[models.ApiKey.idPrefix]: 'ApiKey',
[models.Assessment.idPrefix]: 'Assessment',
[models.Asset.idPrefix]: 'Asset',
[models.AssetType.idPrefix]: 'AssetType',
[models.Availability.idPrefix]: 'Availability',
[models.Category.idPrefix]: 'Category',
[models.CustomAttribute.idPrefix]: 'CustomAttribute',
[models.Entry.idPrefix]: 'Entry',
[models.Message.idPrefix]: 'Message',
[models.Order.idPrefix]: 'Order',
[models.Transaction.idPrefix]: 'Transaction',
[models.User.idPrefix]: 'User',
[models.User.organizationIdPrefix]: 'User'
}
const getObjectTypeFromModelName = (modelName) => {
return _.camelCase(modelName)
}
const getModelNameFromObjectType = (objectType) => {
return objectType.charAt(0).toUpperCase() + objectType.slice(1)
}
const cacheModels = {}
// must run before establishing any Knex connections
loadTypeParser()
function loadTypeParser () {
// https://github.com/tgriesser/knex/issues/387
// converts PostgreSQL `bigint` into Javascript integers
pg.types.setTypeParser(20, 'text', (value) => {
if (Number.MAX_SAFE_INTEGER < value) {
throw new Error(`The value ${value} exceeds the JS max integer value ${Number.MAX_SAFE_INTEGER}`)
}
return parseInt(value, 10)
})
}
// SSL options are available since this Knex.js PR: https://github.com/knex/knex/pull/3410
// using the library pg-connection-string
// https://github.com/iceddev/pg-connection-string
function getSSLOptions (configuration) {
const {
ssl,
sslcert,
sslkey,
sslca,
} = configuration
let sslOptions
const useSSLObject = Boolean(sslcert || sslkey || sslca)
const useSSL = Boolean(ssl)
if (useSSLObject) {
sslOptions = {}
if (sslcert) sslOptions.cert = sslcert
if (sslkey) sslOptions.key = sslkey
if (sslca) sslOptions.ca = sslca
} else if (useSSL) {
sslOptions = true
}
return sslOptions
}
async function getConnection ({ platformId, env } = {}) {
if (!platformId) {
throw new Error('Missing platformId when retrieving PostgreSQL connection')
}
if (!env) {
throw new Error('Missing environment when retrieving PostgreSQL connection')
}
let connection
let schema
const useRemoteStore = process.env.REMOTE_STORE === 'true'
let sslOptions
if (useRemoteStore) {
const postgresqlData = await getPlatformEnvData(platformId, env, 'postgresql')
if (!postgresqlData) {
throw createError(500, 'PostgreSQL missing environment variables', { platformId, env })
}
connection = {
host: postgresqlData.host,
user: postgresqlData.user,
password: postgresqlData.password,
database: postgresqlData.database,
port: postgresqlData.port,
schema: postgresqlData.schema
}
sslOptions = getSSLOptions(postgresqlData)
schema = postgresqlData.schema
} else {
connection = {
host: process.env.POSTGRES_HOST,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
port: process.env.POSTGRES_PORT,
schema: 'public'
}
sslOptions = getSSLOptions({
ssl: process.env.POSTGRES_SSL,
sslcert: process.env.POSTGRES_SSL_CERT,
sslkey: process.env.POSTGRES_SSL_KEY,
sslca: process.env.POSTGRES_SSL_CA,
})
schema = 'public'
}
if (sslOptions) connection.ssl = sslOptions
return {
connection,
schema
}
}
async function getModels ({ platformId, env } = {}) {
if (!platformId) {
throw new Error('Missing platformId when getting models')
}
if (!env) {
throw new Error('Missing environment when getting models')
}
const cacheKey = `${platformId}_${env}`
if (cacheModels[cacheKey]) {
return cacheModels[cacheKey]
}
const { schema, connection } = await getConnection({ platformId, env })
const knex = getKnex(connection)
const customModels = Object.keys(models).reduce((memo, key) => {
const Model = models[key]
// Set the schema automatically
// https://github.com/Vincit/objection.js/issues/85
class SchemaModel extends Model {
static get defaultSchema () {
return schema
}
/**
* Returns raw PostgreSQL JSONB merge custom function call.
* Algorithm is similar to a recursive Object.assign(base, changes).
* This ensures we only update appropriate fields,
* rather than passing whole object as in Base.getCustomData,
* which used to break even simple concurrency: concurrent updates of different JSON fields.
* @param {String} baseObjectName - same as column name such as "metadata"
* @param {Object} changesObject - values (incl. `null`) overwrite any baseObject value with same key
* @param {Object} [options]
* @param {String} [options.jsonbAccessor] - using native Postgres operator like `->'field'`
* @return {Object} knex `raw` query object
*/
static rawJsonbMerge (baseObjectName, changesObject, { jsonbAccessor } = {}) {
if (typeof baseObjectName !== 'string') throw new Error('String column name expected')
if (!_.isObjectLike(changesObject)) throw new Error('changesObject expected as second argument')
return raw(`${this.defaultSchema}.${mergeFunctionName}(${jsonbAccessor ? '(' : ''}"${
baseObjectName // We need double quotes for column names with caps like 'platformData'
}"${
jsonbAccessor ? `${jsonbAccessor})::jsonb` : ''
}, ?::jsonb)`, JSON.stringify(changesObject))
}
/**
* Returns the updated model after updating a JSONB column (can replace a property instead of merging)
*
* WARNING: should only be used in specific locations as future implementation of advanced update will
* be implemented in the future via JSON Patch (https://erosb.github.io/post/json-patch-vs-merge-patch/)
* @param {Object} params
* @param {String} params.modelId - model ID to update
* @param {String} params.baseObjectName - same as column name such as "metadata"
* @param {Object} params.changesObject - values (incl. `null`) overwrite any baseObject value with same key
* @param {String[]} params.replacingProperties - array of `changesObject` property names that needs to be replaced
* instead of being merged with previous value
* @param {Object} [params.trx] - Knex transaction object
* @return {Object} updated model
*/
static async updateJsonb ({
modelId,
baseObjectName,
changesObject,
replacingProperties,
trx
}) {
if (!modelId) throw new Error('modelId expected')
if (typeof baseObjectName !== 'string') throw new Error('String column name expected')
if (!_.isObjectLike(changesObject)) throw new Error('changesObject expected as second argument')
if (!trx) throw new Error('Transaction object trx expected')
const beforeReplaceChanges = Object.assign({}, changesObject)
const afterReplaceChanges = {}
if (replacingProperties && Array.isArray(replacingProperties)) {
replacingProperties.forEach(prop => {
if (!_.isUndefined(changesObject[prop])) {
beforeReplaceChanges[prop] = null
afterReplaceChanges[prop] = changesObject[prop]
}
})
}
let updatedModel = await this.query(trx).patchAndFetchById(
modelId,
{ [baseObjectName]: this.rawJsonbMerge(baseObjectName, beforeReplaceChanges) }
)
if (!_.isEmpty(afterReplaceChanges)) {
updatedModel = await this.query(trx).patchAndFetchById(
modelId,
{ [baseObjectName]: this.rawJsonbMerge(baseObjectName, afterReplaceChanges) }
)
}
return updatedModel
}
}
memo[key] = SchemaModel.bindKnex(knex)
return memo
}, {})
cacheModels[cacheKey] = customModels
return customModels
}
/**
* Pass an ID or a prefix ID and retrieve
* @param {Object} params
* @param {String} [params.objectId]
* @param {String} [params.idPrefix]
* @param {String} [params.objectType]
* @param {String} [params.Models] - models from `getModels({ platformId, env })`
* or defaults to models without attached platform DB.
* Passing models from getModels is useful to query database afterwards.
* @return {Object} info
* @return {String} info.idPrefix - can be null if no match
* @return {String} info.objectType - can be null if no match
* @return {Object} info.Model - can be null if no match, return the Objection.js model
* useful to access model class methods or to query database
*/
function getModelInfo ({ objectId, objectType, idPrefix, Models = models }) {
const info = {
idPrefix: null,
objectType: null,
Model: null
}
let modelName
if (objectId) {
try {
const { object: prefix } = extractDataFromObjectId(objectId) // can throw
const modelName = mapIdPrefixToModelNames[prefix]
if (modelName) info.idPrefix = prefix
} catch (err) {}
} else if (idPrefix) info.idPrefix = idPrefix
if (info.idPrefix) {
modelName = mapIdPrefixToModelNames[info.idPrefix]
if (modelName) info.objectType = getObjectTypeFromModelName(modelName)
} else if (objectType) info.objectType = objectType
if (info.objectType) {
if (!modelName) modelName = getModelNameFromObjectType(info.objectType)
info.Model = Models[modelName]
}
return info
}
module.exports = Object.assign({}, models, {
getSSLOptions,
getConnection,
getModels,
getModelInfo
})