Created
August 29, 2022 18:09
-
-
Save alobato/c360aa085f49438679d92614d1654016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import client from './mongodb.js' | |
import { transformObject, convertToDate } from './utils.js' | |
;(async () => { | |
const nfeDB = client.db('nfeServiceDB') | |
const nfeCollection = nfeDB.collection('nfe') | |
const count = await nfeCollection.countDocuments({ vs: '1.0', 'NFe.infNFe.ide.dhEmi': { $type: 'string' } }) | |
// eslint-disable-next-line no-console | |
console.log('count', count) | |
// https://stackoverflow.com/questions/16357448/mongo-how-to-check-if-field-is-a-number | |
const nfes = await nfeCollection.find({ vs: '1.0', 'NFe.infNFe.ide.dhEmi': { $type: 'string' } }).sort({ dataColeta: -1 }).limit(10000).toArray() | |
let i = 0 | |
for (const nfe of nfes) { | |
i++ | |
// eslint-disable-next-line no-console | |
console.log('nfe._id', nfe._id) | |
const transformMap = new Map([ | |
['dhEmi', convertToDate] | |
]) | |
const transformedObj = transformObject(nfe.NFe.infNFe.ide, transformMap) | |
// eslint-disable-next-line no-console | |
console.log('transformedObj', transformedObj) | |
const result = await nfeCollection.findOneAndUpdate({ _id: nfe._id }, { $set: { 'NFe.infNFe.ide': transformedObj } }) | |
// eslint-disable-next-line no-console | |
console.log('result', result) | |
} | |
// eslint-disable-next-line no-console | |
console.log('i', i) | |
// process.exit() | |
})().catch((error) => { | |
// eslint-disable-next-line no-console | |
console.error(error) | |
// process.exit(1) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setNestedProperty(object, stringPath, value) { | |
let objectReference = object | |
const arrayPath = stringPath.split('.') | |
const pathAmount = arrayPath.length | |
for (let pathIndex = 0; pathIndex < pathAmount - 1; pathIndex += 1) { | |
const property = arrayPath[pathIndex] | |
if (!objectReference[property]) objectReference[property] = {} | |
objectReference = objectReference[property] | |
} | |
objectReference[arrayPath[pathAmount - 1]] = value | |
return object | |
} | |
function getNestedProperty(object, stringPath) { | |
const arrayPath = stringPath.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '').split('.') | |
const pathAmount = arrayPath.length | |
let propertyValue = object | |
for (let pathIndex = 0; pathIndex < pathAmount; pathIndex += 1) { | |
const property = arrayPath[pathIndex] | |
if (propertyValue && typeof propertyValue === 'object' && property in propertyValue) propertyValue = propertyValue[property] | |
else { | |
propertyValue = undefined | |
break | |
} | |
} | |
return propertyValue | |
} | |
export function propertyTransformer(map, object, ignoreEmpty = true) { | |
Array.from(map, ([stringPath, typeTransformerFunction]) => { | |
const value = getNestedProperty(object, stringPath) | |
if (typeof value === 'undefined') { | |
if (ignoreEmpty === true) return false | |
throw new Error('propertyTransformer found a undefined property.') | |
} | |
setNestedProperty(object, stringPath, typeTransformerFunction(value)) | |
return [stringPath, typeTransformerFunction] | |
}) | |
return object | |
} | |
export function transformKeysToNumber(minus = []) { | |
return (data) => { | |
const clonedData = { ...data } | |
Object.keys(clonedData).map((key) => { | |
if (typeof clonedData[key] === 'string' && minus.indexOf(key) === -1) { | |
clonedData[key] = Number(clonedData[key]) | |
} | |
return key | |
}) | |
return clonedData | |
} | |
} | |
export function cloneArray(array) { | |
return array.map((item) => { | |
if (Array.isArray(item)) return cloneArray(item) | |
if (item !== null && typeof item === 'object') return cloneObject(item) | |
return item | |
}) | |
} | |
export function cloneObject(object) { | |
const clonedObject = {} | |
Object.keys(object).map((key) => { | |
if (Array.isArray(object[key])) clonedObject[key] = cloneArray(object[key]) | |
else if (object[key] !== null && typeof object[key] === 'object') { | |
clonedObject[key] = cloneObject(object[key]) | |
} else clonedObject[key] = object[key] | |
return key | |
}) | |
return clonedObject | |
} | |
export function transformObject(data, transformMap) { | |
return propertyTransformer(transformMap, data, true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment