Created
May 27, 2022 04:31
-
-
Save chapmanjacobd/f62cc5353b2f8f5bba98ad9b14f9690e 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
export function obj2schema(obj: object | [], table: string, result = {}) { | |
if (obj) { | |
if (Array.isArray(obj)) return obj2schema(obj[0], table) | |
result[table] = Object.keys(obj).map(k => { | |
const v = obj[k]; | |
if (Array.isArray(v)) obj2schema(v[0], k, result) | |
if (typeof v === 'object') return; | |
return `${k} ${typeof v}`; | |
}).filter(Boolean); | |
return result | |
} | |
/* | |
const schema = obj2schema(data, 'videos') | |
Object.keys(schema).map(k => { | |
const columns = schema[k].join(',') | |
const sql = `CREATE TABLE IF NOT EXISTS ${k} (${columns})` | |
// console.log(sql); | |
console.log(data[k]); | |
alasql(sql); | |
alasql(`INSERT INTO ${k} SELECT * FROM ?`, [data[k]]) | |
}); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment