Skip to content

Commit

Permalink
0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Nov 22, 2021
1 parent 3d920a0 commit 6f23fd2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
13 changes: 12 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function updateTypes(db, options) {
type += " | null";
}

output.write(` ${x.column}: ${type};\n`);
output.write(` ${sanitize(x.column)}: ${type};\n`);

if (!(columns[i + 1] && columns[i + 1].table === x.table)) {
output.write("};\n\n");
Expand Down Expand Up @@ -180,4 +180,15 @@ function getType(udt, customTypes, defaultValue) {
default:
return (_customTypes$get = customTypes.get(udt)) !== null && _customTypes$get !== void 0 ? _customTypes$get : "unknown";
}
}
/**
* Wraps the target property identifier into quotes in case it contains any
* invalid characters.
*
* @see https://developer.mozilla.org/docs/Glossary/Identifier
*/


function sanitize(name) {
return /^[a-zA-Z$_][a-zA-Z$_0-9]*$/.test(name) ? name : JSON.stringify(name);
}
16 changes: 9 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export async function updateTypes(db: Knex, options: Options): Promise<void> {
type += " | null";
}

output.write(` ${quoteColumnName(x.column)}: ${type};\n`);
output.write(` ${sanitize(x.column)}: ${type};\n`);

if (!(columns[i + 1] && columns[i + 1].table === x.table)) {
output.write("};\n\n");
Expand Down Expand Up @@ -269,10 +269,12 @@ export function getType(
}
}

function quoteColumnName(name: string): string {
// regex is a simplified check for valid IdentifierNames
const isValidIdentifier = /^[a-zA-Z$_][a-zA-Z$_0-9]*$/.test(name);

// also escape any quotes that might be in the name.
return isValidIdentifier ? name : JSON.stringify(name);
/**
* Wraps the target property identifier into quotes in case it contains any
* invalid characters.
*
* @see https://developer.mozilla.org/docs/Glossary/Identifier
*/
function sanitize(name: string): string {
return /^[a-zA-Z$_][a-zA-Z$_0-9]*$/.test(name) ? name : JSON.stringify(name);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knex-types",
"version": "0.3.1",
"version": "0.3.2",
"description": "Generates TypeScript definitions (types) from a (PostgreSQL) database schema.",
"keywords": [
"database",
Expand Down

0 comments on commit 6f23fd2

Please sign in to comment.