-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·501 lines (472 loc) · 17.2 KB
/
index.js
File metadata and controls
executable file
·501 lines (472 loc) · 17.2 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env node
const net = require('net')
const BSON = require('bson-ext')
const mongoToPostgres = require('mongo-query-to-postgres-jsonb')
const debug = require('debug')
const _ = require('lodash')
const util = require('./util')
const wire = require('./wireprotocol')
const adminReplies = require('./admin')
const args = process.argv.slice(2)
if (args.length < 1) {
console.error('node index.js [<pghost>] [<mongoport>]')
return
}
const createMissingDatabases = true
const pgHost = args[0] || 'localhost'
const mongoPort = parseInt(args[1] || '27018')
const { Client } = require('pg')
// database -> client
const clients = {}
let lastResult = {
n: 0,
connectionId : 1789,
updatedExisting: true,
errMsg: '',
syncMillis: 0,
writtenTo: null,
ok: 1
}
async function createDatabase(databaseName) {
await doQuery('postgres', 'CREATE DATABASE ' + databaseName)
}
async function doQuery(database, pgQuery) {
if (!clients[database]) {
try {
const client = new Client({ database, host: pgHost })
clients[database] = client
await client.connect()
} catch (e2) {
if (database !== 'postgres' && createMissingDatabases) {
await createDatabase(database);
const client = new Client({ database, host: pgHost })
clients[database] = client
await client.connect()
}
}
}
debug('pgmongo:pgquery')(pgQuery)
return await clients[database].query(pgQuery)
}
async function createTable(databaseName, collectionName) {
await doQuery(databaseName, `CREATE TABLE IF NOT EXISTS ${collectionName} (data jsonb)`)
}
async function tryOrCreateTable(action, databaseName, collectionName) {
try {
return await action()
} catch (e) {
if (e.message.includes('does not exist')) {
await createTable(collectionName)
return await action()
} else {
throw e
}
}
}
var server = net.createServer(function (socket) {
socket.on('data', (data) => processRecord(socket, data))
})
const convertToBSON = function(doc) {
if (doc && doc._id && doc._id.length === 24) {
doc._id = BSON.ObjectID(doc._id)
}
return doc
}
let commands = ['find', 'count', 'update', 'insert', 'create', 'delete', 'drop', 'validate',
'listIndexes', 'createIndexes', 'deleteIndexes', 'renameCollection']
const arrayPathsMap = {}
function getArrayPaths(collectionName) {
debug('pgmongo:arraypaths')(collectionName + ' ' + arrayPathsMap[collectionName])
return arrayPathsMap[collectionName] || []
}
function safeWhereConversion(filter, collectionName) {
try {
return mongoToPostgres('data', filter, getArrayPaths(collectionName))
} catch (err) {
const data = {
errmsg: 'in must be an array',
code: 2,
codeName: 'BadValue',
ok: 0
}
console.error('query error')
console.error(err)
throw data
}
}
async function crud(socket, reqId, databaseName, commandName, doc, build) {
const normalizedCommandName = commandName.toLowerCase()
let rawCollectionName = doc[commandName] || doc[normalizedCommandName]
let collectionName = '"' + rawCollectionName + '"'
if (commandName === 'distinct') {
if ((doc.query && typeof doc.query !== 'object') || typeof doc.key !== 'string') {
socket.write(build(reqId, { ok: 0, errmsg: '"query" had the wrong type. Expected object or null,', code: 14 }, {}))
//throw new Error('\\"query\\" had the wrong type. Expected object or null, found ' + typeof doc.query)
return true
}
const filter = doc.query || {}
let where = safeWhereConversion(filter, rawCollectionName)
const distinctField = mongoToPostgres.pathToText(['data'].concat(doc.key.split('.')), false)
const arrayCondition = `jsonb_typeof(${distinctField})='array'`
const query1 = `SELECT DISTINCT ${distinctField} AS data FROM ${collectionName} WHERE ${where} AND NOT ${arrayCondition}`
const query2 = `SELECT DISTINCT jsonb_array_elements(${distinctField}) AS data FROM ${collectionName} WHERE ${where} AND ${arrayCondition}`
const query = `${query1} UNION ${query2}`
const res = await tryOrCreateTable(async () => await doQuery(databaseName, query), databaseName, collectionName)
const rows = res.rows.map((row) => convertToBSON(row.data))
debug('pgmongo:rows')(rows)
const data = { values: rows, ok: 1 }
socket.write(build(reqId, data, {}))
return true
}
if (commandName === 'find') {
const filter = doc.filter
const where = safeWhereConversion(filter, rawCollectionName)
let select = mongoToPostgres.convertSelect('data', doc.projection)
let query = `SELECT ${select} FROM ${collectionName}`
if (where !== 'TRUE') {
query += ` WHERE ${where}`
}
if (doc.sort) {
query += ' ORDER BY ' + mongoToPostgres.convertSort('data', doc.sort, doc.collation && doc.collation.numericOrdering)
}
if (doc.limit) {
query += ' LIMIT ' + Math.abs(doc.limit)
}
if (doc.skip) {
if (doc.skip < 0) {
socket.write(build(reqId, { ok: 0, errmsg: 'negative skip not allowed' }, {}))
return true
}
query += ' OFFSET ' + doc.skip
}
let res
try {
res = await tryOrCreateTable(async () => await doQuery(databaseName, query), databaseName, collectionName)
} catch (e) {
console.error(e)
res = { rows: [] }
}
const rows = res.rows.map((row) => convertToBSON(row.data))
debug('pgmongo:rows')(rows)
const data = util.createCursor(databaseName + '.' + collectionName, rows)
socket.write(build(reqId, data, {}))
return true
}
if (commandName === 'count') {
if (doc.skip && doc.skip < 0) {
socket.write(build(reqId, { ok: 0, errmsg: 'negative skip not allowed' }, {}))
return true
}
const where = mongoToPostgres('data', doc.query, getArrayPaths(rawCollectionName))
const data = { n: 0, ok: 1 }
try {
const res = await doQuery(databaseName, `SELECT COUNT(*) FROM ${collectionName} WHERE ${where}`)
data.n = res.rows[0].count
if (doc.skip) {
data.n = Math.max(0, data.n - doc.skip)
}
if (doc.limit) {
data.n = Math.min(Math.abs(doc.limit), data.n)
}
} catch (e) {
console.error(e)
}
socket.write(build(reqId, data, data))
return true
}
if (commandName === 'update') {
const update = doc.updates[0]
const where = mongoToPostgres('data', update.q, getArrayPaths(rawCollectionName))
let res
try {
const newValue = mongoToPostgres.convertUpdate('data', update.u)
// TODO (handle multi)
await createTable(collectionName)
res = await doQuery(databaseName, `UPDATE ${collectionName} SET data = ${newValue} WHERE ${where}`)
if (res.rowCount === 0 && update.upsert) {
const changes = update.u || {}
if (mongoToPostgres.countUpdateSpecialKeys(changes) === 0) {
// TODO: expand dot notation
_.assign(changes, update.q)
} else {
changes['$set'] = changes['$set'] || {}
_.assign(changes['$set'], update.q)
}
const newValue = mongoToPostgres.convertUpdate('data', changes, true)
async function insert() {
const res = await doQuery(databaseName, `INSERT INTO ${collectionName} VALUES (${newValue})`)
const data = { n: res.rowCount, nInserted: res.rowCount, updatedExisting: false, ok: 1 }
socket.write(build(reqId, data, {}))
}
await tryOrCreateTable(insert, databaseName, collectionName)
return true
} else {
lastResult.n = res.rowCount
socket.write(build(reqId, lastResult, lastResult))
return true
}
} catch (e) {
console.error(e)
if (e.message.includes('_id')) {
const data = {
errmsg: e.message,
ok: 0
}
socket.write(build(reqId, data, data))
return true
}
// Can also upsert, fallback
const isChangingId = typeof update.u._id !== 'undefined'
if (isChangingId) {
console.error('failed to update, falling back to insert')
commandName = 'insert'
doc.documents = [update.u]
}
}
}
if (commandName === 'insert') {
arrayPathsMap[rawCollectionName] = _.union(arrayPathsMap[rawCollectionName], util.getArrayPaths(doc.documents[0]))
const newValues = doc.documents.map((values) => '(' + mongoToPostgres.convertUpdate('data', values) + ')')
async function insert() {
const res = await doQuery(databaseName, `INSERT INTO ${collectionName} VALUES ${newValues.join(',')}`)
const data = { n: res.rowCount, nInserted: res.rowCount, updatedExisting: false, ok: 1 }
socket.write(build(reqId, data, {}))
}
await tryOrCreateTable(insert, databaseName, collectionName)
return true
}
if (commandName === 'create') {
lastResult.ok = 1
await createTable(collectionName)
}
if (commandName === 'delete') {
async function del() {
const where = mongoToPostgres('data', doc.deletes[0].q, getArrayPaths(rawCollectionName))
// TODO (handle multi)
let query = `DELETE FROM ${collectionName} WHERE ${where}`
if (doc.deletes[0].limit) {
// TODO: handle limits on deletion
//query += ' LIMIT ' + doc.deletes[0].limit
}
const res = await doQuery(databaseName, query)
lastResult.n = res.rowCount
return socket.write(build(reqId, lastResult, lastResult))
}
await tryOrCreateTable(del, databaseName, collectionName)
return true
}
if (commandName === 'drop') {
try {
await doQuery(databaseName, `DROP TABLE ${collectionName}`)
} catch (e) {
// often not an error if already doesn't exist
}
socket.write(build(reqId, lastResult, lastResult))
return true
}
if (commandName === 'validate') {
const countRes = await doQuery(`SELECT COUNT(*) FROM ${collectionName}`)
const data = {
'ns' : databaseName + '.' + collectionName,
'nrecords' : countRes.rows[0].count,
'nIndexes' : 0,
'keysPerIndex' : {},
'valid' : true,
'warnings' : ['Some checks omitted for speed. use {full:true} option to do more thorough scan.'],
'errors' : [ ],
'ok' : 1
}
return socket.write(build(reqId, data, data))
}
if (normalizedCommandName === 'listindexes') {
const listIQuery = util.listIndicesQuery('data', rawCollectionName)
const indexRes = await doQuery(databaseName, listIQuery)
const indices = indexRes.rows.map((row) => ({
v: 2,
key: { _id: 1 },
name: row.index_name,
ns: databaseName + '.' + collectionName
}))
const data = util.createCursor(databaseName + '.' + collectionName, indices)
socket.write(build(reqId, data, data))
return true
}
if (normalizedCommandName === 'createindexes') {
rawCollectionName = rawCollectionName || doc['createIndexes']
collectionName = '"' + rawCollectionName + '"'
const data = {
createIndexes: {
numIndexesBefore: 0,
numIndexesAfter: 0,
ok: 1
},
ok: 1
}
const listIQuery = util.listIndicesQuery('data', rawCollectionName)
const indexRes = await doQuery(databaseName, listIQuery)
const indices = indexRes.rows.map((row) => row.index_name)
data.createIndexes.numIndexesBefore = indices.length
for (const index of doc.indexes) {
if (indices.includes(index.name)) {
continue
}
const keys = Object.keys(index.key)
/*if (keys.length > 1) {
console.error(keys)
throw new Error('compound indices not supported');
}*/
const pgPath = keys.map(function(key) {
const path = ['data'].concat(key.split('.'))
return mongoToPostgres.pathToText(path, false)
}).join(', ')
let indexQuery = `CREATE INDEX "${index.name}" ON ${collectionName} USING gin ((${pgPath}));`
try {
await doQuery(databaseName, indexQuery)
} catch (e) {
console.error('failed to create index')
//console.error(e)
}
data.createIndexes.numIndexesAfter++
}
if (normalizedCommandName === 'deleteindexes') {
// Todo
// Also handle case where index is "*" and all need to be dropped.
}
socket.write(build(reqId, data, data))
return true
}
if (normalizedCommandName === 'renameCollection') {
const data = { ok: 1 }
collectionName = doc[commandName].split('.', 2)[1]
const newName = doc.to.split('.', 2)[1]
let query = `ALTER TABLE ${collectionName} RENAME TO "${newName}"`
query = doc.dropTarget ? `DROP TABLE IF EXISTS ${newName}; ${query}` : query
try {
await doQuery(databaseName, query)
} catch (e) {
data.ok = 0
console.error(e)
}
socket.write(build(reqId, data, data))
return true
}
}
async function processAdmin(databaseName, socket, reqId, commandName, doc) {
let res
let reply
switch (commandName) {
case 'listdatabases':
try {
res = await doQuery('postgres', 'SELECT datname AS name FROM pg_database WHERE datistemplate = false')
} catch (e) {
console.error(e)
res = { rows: [] }
}
reply = { 'databases': res.rows, 'ok': 1 }
socket.write(wire.createCommandReply(reqId, reply, reply))
break
case 'whatsmyuri':
socket.write(wire.createCommandReply(reqId, {}, { 'you': '127.0.0.1:56709', 'ok': 1 }))
break
case 'getlog':
if (doc.getLog === 'startupWarnings') {
const log = {
'totalLinesWritten': 1,
'log': ['This is a Postgres database using the Mongo wrapper.'],
'ok': 1
}
socket.write(wire.createCommandReply(reqId, log, log))
} else {
return false
}
break
case 'replsetgetstatus':
socket.write(wire.createCommandReply(reqId, {}, adminReplies.replSetGetStatus()))
break
case 'serverstatus':
socket.write(wire.createCommandReply(reqId, adminReplies.getServerStatus(), {}))
break
case 'currentop':
reply = {
inprog: [],
ok: 1
}
socket.write(wire.createCommandReply(reqId, reply, reply))
break
default:
return false
}
return true
}
let previousData
async function processRecord(socket, data) {
if (previousData) {
data = Buffer.concat([previousData, data])
previousData = null
}
const { msgLength, reqId, opCode } = wire.parseHeader(data)
if (data.length < msgLength - 1) {
previousData = data
return console.log('partial data received')
}
try {
await handleRecord(socket, data, opCode, reqId)
} catch (e) {
console.error(e)
if (e.code) {
return socket.write(wire.createCommandReply(reqId, e, {}))
}
return socket.write(wire.createCommandReply(reqId, { errmsg: e.message, ok: 0 }, { ok: 0 }))
}
}
async function handleRecord(socket, data, opCode, reqId) {
if (opCode === wire.OP_QUERY) {
const { doc, collectionName } = wire.parseQuery(data)
if (collectionName === 'admin.$cmd' && (doc.isMaster || doc.ismaster)) {
return socket.write(wire.createResponse(reqId, adminReplies.getIsMasterReply()))
}
const parts = collectionName.split('.')
const databaseName = parts[0]
for (const command of commands) {
if (doc[command] || doc[command.toLowerCase()]) {
if (await crud(socket, reqId, databaseName, command, doc, wire.createResponse))
return
}
}
console.dir({ err: 'UNHANDLED REQUEST OP_QUERY', collectionName, doc })
return socket.write(wire.createCommandReply(reqId, { ok: 0 }, { ok: 0 }))
} else if (opCode === wire.OP_COMMAND) {
const { databaseName, commandName, doc } = wire.parseCommand(data)
if (await crud(socket, reqId, databaseName, commandName, doc, wire.createCommandReply))
return
if (databaseName === 'admin' || databaseName === 'db') {
if (await processAdmin(databaseName, socket, reqId, commandName, doc))
return
}
let res
switch (commandName) {
case 'ismaster':
return socket.write(wire.createCommandReply(reqId, adminReplies.getIsMasterReply(), adminReplies.getIsMasterReply()))
case 'buildinfo':
return socket.write(wire.createCommandReply(reqId, adminReplies.getBuildInfo(), {}))
case 'listcollections':
res = await doQuery(databaseName, 'SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\' AND table_type=\'BASE TABLE\';')
const tables = res.rows.map((row) => ({ name: row.table_name, type: 'collection', options: {}, info: { readOnly: false } }))
const data = util.createCursor(`${databaseName}.$cmd.listCollections`, tables)
return socket.write(wire.createCommandReply(reqId, data, data))
case 'ping':
return socket.write(wire.createCommandReply(reqId, {}, { 'ok': 1 }))
case 'getlasterror':
return socket.write(wire.createCommandReply(reqId, lastResult, lastResult))
case 'dropdatabase':
res = await doQuery(databaseName, 'select string_agg(\'drop table "\' || tablename || \'" cascade\', \'; \') from pg_tables where schemaname = \'public\'')
await doQuery(databaseName, res.rows[0].string_agg)
return socket.write(wire.createCommandReply(reqId, {}, { 'ok': 1 }))
}
console.dir({ err: 'UNHANDLED REQUEST', commandName, databaseName, doc })
return socket.write(wire.createCommandReply(reqId, { ok: 0 }, { ok: 0 }))
}
}
server.listen(mongoPort, '127.0.0.1')
console.log(`Connecting to postgres at ${pgHost}:5432`)
console.log(`Serving Mongo on port ${mongoPort}`)