В mongodb содержатся данные с полями [_id, parentId]
.
Как преобразовать эту структуру в формат с children ids
, используемый firebase:
{
123: {
_id: 123,
children: [
325: true,
155: true,
// ...
]
},
// ...
}
В mongodb содержатся данные с полями [_id, parentId]
.
Как преобразовать эту структуру в формат с children ids
, используемый firebase:
{
123: {
_id: 123,
children: [
325: true,
155: true,
// ...
]
},
// ...
}
Моё решение (coffeescript, mongodb, nodejs, asyncjs):
async = require 'async'
fs = require 'fs'
class FirebaseConverter
constructor: (@_collection) ->
convert: (done) =>
async.waterfall [
(cb) => @_extractAll cb
(d, cb) => async.map d, @_cleanUp, cb
(d, cb) => async.map d, @_down, cb
(d, cb) => async.map d, @_up, cb
(d, cb) => async.reduce d, {}, @_appendKeys, cb
(d, cb) => fs.writeFile '../data/firebase.json', JSON.stringify(d), console.log; cb null, d
], (e, d) => done null, d
_appendKeys: (memo, item, cb) ->
delete item.CategoryParentID
memo[item.CategoryID] = item
cb null, memo
_extractAll: (cb) =>
@_collection.find().limit(1e5).toArray (e, d) ->
throw e if e
cb null, d
_cleanUp: (item, cb) ->
console.log "item", item.CategoryID
result = {}
result["CategoryID"] = item.CategoryID
result["CategoryName"] = item.CategoryName
result["CategoryParentID"] = item.CategoryParentID
cb null, result
_up: (item, cb, parentId) =>
if 'up' not of item
item["up"] = []
query = { CategoryID: parentId or item.CategoryParentID }
@_collection.find(query).toArray (error, parent) =>
parent = parent[0]
if item.CategoryID isnt parent.CategoryID # not self reference
parentObject = { "CategoryID": parent.CategoryID, "CategoryName": parent.CategoryName }
# console.log parentObject, parent
item["up"].unshift parentObject
if parent.CategoryID isnt parent.CategoryParentID # not top level
@_up item, cb, parent.CategoryParentID
else
cb null, item
_down: (item, cb) =>
query = { CategoryParentID: item.CategoryID }
@_collection.find(query).toArray (error, subQuery) ->
item["down"] = []
for child in subQuery
continue if child.CategoryID is child.CategoryParentID
childObject = { "CategoryID": child.CategoryID, "CategoryName": child.CategoryName }
item["down"].push childObject
cb null, item
Результат:
{"CategoryID":"99","CategoryName":"Everything Else","down":[{"CategoryID":"319","CategoryName":"Adult Only"},{"CategoryID":"3143","CategoryName":"Career Development & Education"},{"CategoryID":"177600","CategoryName":"eBay Special Offers"},{"CategoryID":"20924","CategoryName":"eBay User Tools"},{"CategoryID":"88739","CategoryName":"Funeral & Cemetery"},{"CategoryID":"20925","CategoryName":"Genealogy"},{"CategoryID":"102480","CategoryName":"Information Products"},{"CategoryID":"19266","CategoryName":"Metaphysical"},{"CategoryID":"102329","CategoryName":"Personal Development"},{"CategoryID":"102535","CategoryName":"Personal Security"},{"CategoryID":"102553","CategoryName":"Reward Points & Incentives"},{"CategoryID":"88433","CategoryName":"Every Other Thing"},{"CategoryID":"102545","CategoryName":"Religious Products & Supplies"},{"CategoryID":"1466","CategoryName":"Weird Stuff"},{"CategoryID":"14112","CategoryName":"Test Auctions"}]}