Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Last active September 15, 2020 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/1a1e5be350fce71fa95f47980527cf0f to your computer and use it in GitHub Desktop.
Save Ciantic/1a1e5be350fce71fa95f47980527cf0f to your computer and use it in GitHub Desktop.
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