Skip to content

Commit

Permalink
Formating
Browse files Browse the repository at this point in the history
  • Loading branch information
acrodrig committed Apr 13, 2024
1 parent 127fa75 commit 81843a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/ddl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ export class DDL {
static createFullTextIndex(dbType: string, columns: string[], padWidth = 4, table: string, name = "fulltext"): string {
const pad = "".padEnd(padWidth);

if (dbType === DB.Provider.MYSQL) return `${pad}CREATE FULLTEXT INDEX ${table.toLowerCase()}_${name} ON ${table} (${columns.join(",")});\n`;
if (dbType === DB.Provider.POSTGRES) return `${pad}CREATE INDEX ${table.toLowerCase()}_${name} ON ${table} USING GIN (TO_TSVECTOR('english', ${columns.map(c => "COALESCE("+c+",'')").join("||' '||")}));`;
table = table.toLowerCase();
const wrapper = (columns: string[], s = ",", w = false) => columns.map((c) => w ? "COALESCE(" + c + "'')" : c).join(s);
if (dbType === DB.Provider.MYSQL) return `${pad}CREATE FULLTEXT INDEX ${table}_${name} ON ${table} (${wrapper(columns, ",")});\n`;
if (dbType === DB.Provider.POSTGRES) return `${pad}CREATE INDEX ${table}_${name} ON ${table} USING GIN (TO_TSVECTOR('english', ${wrapper(columns, "||' '||", true)}));`;

return "";
}
Expand Down Expand Up @@ -151,7 +153,7 @@ export class DDL {
if (schema.indices) sql += "\n" + schema.indices?.map((i) => this.createIndex(dbType, i, 0, table)).join("");

// Full text index
const fullTextColumns = Object.entries(schema.properties).filter(([n,c]) => c.fullText).map(([n,_]) => n);
const fullTextColumns = Object.entries(schema.properties).filter(([n, c]) => c.fullText).map(([n, _]) => n);
if (fullTextColumns.length) sql += this.createFullTextIndex(dbType, fullTextColumns, 0, table);

const fixDanglingComma = (sql: string) => sql.replace(/,\n\)/, "\n);");
Expand Down
10 changes: 5 additions & 5 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class Repository<T extends Identifiable> extends EventTarget {

// This is the only method where full text search is permitted (it could be disastrous in DELETE for example)
const properties = this.schema?.properties ?? {};
const fullTextColumns = Object.entries(properties).filter(([n,p]) => p.fullText).map(([n,_]) => n);
const fullTextColumns = Object.entries(properties).filter(([n, p]) => p.fullText).map(([n, _]) => n);
if (DB.type === DB.Provider.SQLITE) fullTextColumns.length = 0;

// Build SQL (if there is a select, clean it to prevent SQL injection)
Expand Down Expand Up @@ -327,11 +327,11 @@ export class Repository<T extends Identifiable> extends EventTarget {
tree.push(fullTextColumns?.length ? value[key] + (DB.type === DB.Provider.POSTGRES ? "" : "*") : "%" + value[key] + "%");

// Regardless of what we are looking for, MySQL wants us to enter every column here
const wrapper = (columns: string[], s = ",", w = false) => columns.map((c) => w ? "COALESCE(" + c + "'')" : c).join(s);
if (fullTextColumns?.length) {
if (DB.type === DB.Provider.MYSQL) expressions.push("MATCH (" + fullTextColumns.join(",") + ") AGAINST (? IN BOOLEAN MODE)");
if (DB.type === DB.Provider.POSTGRES) expressions.push("TO_TSVECTOR('english', " + fullTextColumns.map(c => "COALESCE("+c+",'')").join("||' '||") + ") @@ TO_TSQUERY(?)");
}
else expressions.push(column + " LIKE ?");
if (DB.type === DB.Provider.MYSQL) expressions.push("MATCH (" + wrapper(fullTextColumns) + ") AGAINST (? IN BOOLEAN MODE)");
if (DB.type === DB.Provider.POSTGRES) expressions.push("TO_TSVECTOR('english', " + wrapper(fullTextColumns) + ") @@ TO_TSQUERY(?)");
} else expressions.push(column + " LIKE ?");
} else if (column === "$sql") {
// Special case for $sql
expressions.push(value);
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function dbInit(type: string, schemas?: Schema[]) {

try {
await DB.connect({ type, hostname, database, username: "dbx", quiet: true }, schemas);
if (sqlite) await dbExec(Deno.readTextFileSync(import.meta.dirname+"/helpers.sql"));
if (sqlite) await dbExec(Deno.readTextFileSync(import.meta.dirname + "/helpers.sql"));
if (schemas) await createTables(schemas);
} catch (ex) {
console.error("\n❌ Could not connect to DB '" + type + "' using 'dbx@" + hostname + "' or could not execute SQL!\n");
Expand All @@ -46,7 +46,7 @@ export async function createTables(schemas: Schema[]) {
}
}

export const getProvider = function(): DB.Provider {
export const getProvider = function (): DB.Provider {
const provider = PROVIDER?.toLowerCase() as DB.Provider;
if (provider && !Object.values(DB.Provider).includes(provider)) {
console.error("\n❌ DB provider '" + provider + "' does not exist!\n");
Expand Down

0 comments on commit 81843a4

Please sign in to comment.