Created
April 13, 2024 10:55
-
-
Save fl0wo/c382d7874c4b3e107a3180fd680fac12 to your computer and use it in GitHub Desktop.
Regexps to check SQL queries patterns for turso hooks
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
export function isUpdateForTable(query: any, tableName: string): boolean { | |
const escapedTableName = tableName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
const regexp = new RegExp(`update "?${escapedTableName}"? set`, 'i'); | |
return regexp.test(typeof query === "string" ? query : query.sql); | |
} | |
export function isInsertForTable(query: any, tableName: string): boolean { | |
const escapedTableName = tableName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
const regexp = new RegExp(`insert into "?${escapedTableName}"?`, 'i'); | |
return regexp.test(typeof query === "string" ? query : query.sql); | |
} | |
export function isDeleteForTable(query: any, tableName: string): boolean { | |
const escapedTableName = tableName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
const regexp = new RegExp(`delete from "?${escapedTableName}"?`, 'i'); | |
return regexp.test(typeof query === "string" ? query : query.sql); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment