json-schema-to-sql
Version:
Convert a JSON schema into SQL DDL (create table) statements.
83 lines • 3 kB
JavaScript
function visitColumn(parent, visitedTables, visitedTable, columnName, column, path = [], schemaPath = [], oneToMany = false) {
if ('$ref' in column) {
visitedTable.unresolvedReferences.push({
name: columnName,
reference: column.$ref,
schemaPath,
oneToMany,
});
}
const parentPrimaryKeyRef = parent['x-primaryKey']
? {
[[...path, parent['x-primaryKey']].join('_')]: {
$ref: ['#', ...path, parent['x-primaryKey']].join('/'),
},
}
: {
[[...path, 'id'].join('_')]: {
$ref: ['#', ...path, 'id'].join('/'),
},
};
if ('type' in column) {
if (column.type === 'object') {
visitTable(visitedTables, columnName, {
...column,
properties: {
...column.properties,
...parentPrimaryKeyRef,
},
}, path, schemaPath);
}
else if (column.type === 'array') {
if ('type' in column.items) {
if (column.items.type === 'object') {
visitTable(visitedTables, columnName, {
...column.items,
properties: {
...column.items.properties,
...parentPrimaryKeyRef,
},
}, path, schemaPath, true);
}
else {
visitTable(visitedTables, columnName, {
type: 'object',
properties: {
value: column.items,
...parentPrimaryKeyRef,
},
}, path, schemaPath, true);
}
}
}
else {
if ('format' in column && column.format) {
visitedTable.visitedColumns.push({
name: columnName,
...column,
type: column.format,
});
}
else {
visitedTable.visitedColumns.push({
name: columnName,
...column,
});
}
}
}
}
export function visitTable(visitedTables, tableName, table, path = [], schemaPath = [], fromArray = false) {
const visitedTable = {
name: [...path, tableName].join('_'),
required: table.required,
primaryKey: table['x-primaryKey'],
visitedColumns: [],
unresolvedReferences: [],
};
for (const [columnName, column] of Object.entries(table.properties)) {
visitColumn(table, visitedTables, visitedTable, columnName, column, [...path, tableName], [...schemaPath, tableName, ...(fromArray ? ['items'] : []), 'properties'], fromArray);
}
visitedTables.push(visitedTable);
}
//# sourceMappingURL=visitor.js.map