Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Safeish SQL field names with typescript
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