Last active
September 15, 2020 18:03
-
-
Save Ciantic/1a1e5be350fce71fa95f47980527cf0f to your computer and use it in GitHub Desktop.
Safeish SQL field names with typescript
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 fields<T>() { | |
return new Proxy( | |
{}, | |
{ | |
get: function (_target, prop, _receiver) { | |
return prop; | |
}, | |
} | |
) as { | |
[P in keyof T]: P; | |
}; | |
}; | |
interface ResourceRow { | |
id: number; | |
modified_on_disk: Date; | |
local_path: string; | |
server_path: string; | |
} | |
const f = fields<ResourceRow>(); | |
const sql = ` | |
CREATE TABLE IF NOT EXISTS resource ( | |
${f.id} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | |
${f.modified_on_disk} DATETIME NOT NULL, | |
${f.local_path} VARCHAR (2048) NOT NULL UNIQUE, | |
${f.server_path} VARCHAR (2048) NOT NULL UNIQUE | |
); | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment