@m1212e/rumble 0.13.1 → 0.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/index.cjs +23 -16
- package/out/index.cjs.map +1 -1
- package/out/index.d.cts +3 -6
- package/out/index.d.cts.map +1 -1
- package/out/index.d.mts +3 -6
- package/out/index.d.mts.map +1 -1
- package/out/index.mjs +23 -16
- package/out/index.mjs.map +1 -1
- package/package.json +1 -1
package/out/index.cjs
CHANGED
|
@@ -1357,12 +1357,18 @@ function isPostgresDB(db) {
|
|
|
1357
1357
|
|
|
1358
1358
|
//#endregion
|
|
1359
1359
|
//#region lib/search.ts
|
|
1360
|
-
async function initSearchIfApplicable(
|
|
1361
|
-
if (!isPostgresDB(db)) {
|
|
1362
|
-
console.info("Database dialect is not compatible with search, skipping search initialization.");
|
|
1360
|
+
async function initSearchIfApplicable(input) {
|
|
1361
|
+
if (!isPostgresDB(input.db)) {
|
|
1362
|
+
console.info("Database dialect is not compatible with search, skipping search initialization. Only PostgreSQL is supported.");
|
|
1363
1363
|
return;
|
|
1364
1364
|
}
|
|
1365
|
-
await db.execute(drizzle_orm.sql`CREATE EXTENSION IF NOT EXISTS pg_trgm;`);
|
|
1365
|
+
await input.db.execute(drizzle_orm.sql`CREATE EXTENSION IF NOT EXISTS pg_trgm;`);
|
|
1366
|
+
if (input.search?.threshold) {
|
|
1367
|
+
const threshold = Number(input.search.threshold);
|
|
1368
|
+
if (threshold < 0 || threshold > 1) throw new Error(`Search threshold must be between 0 and 1`);
|
|
1369
|
+
const dbName = (await input.db.execute(drizzle_orm.sql`SELECT current_database()`)).rows[0].current_database;
|
|
1370
|
+
await input.db.execute(drizzle_orm.sql.raw(`ALTER DATABASE ${dbName} SET pg_trgm.similarity_threshold = ${threshold};`));
|
|
1371
|
+
}
|
|
1366
1372
|
}
|
|
1367
1373
|
/**
|
|
1368
1374
|
* Performs adjustments to the query args to issue a full text search in case the
|
|
@@ -1370,13 +1376,11 @@ async function initSearchIfApplicable(db) {
|
|
|
1370
1376
|
*/
|
|
1371
1377
|
function adjustQueryArgsForSearch({ search, args, tableSchema, abilities }) {
|
|
1372
1378
|
if (search?.enabled && args.search && args.search.length > 0) {
|
|
1373
|
-
const columnsToSearch = abilities.query.many.columns ? Object.entries(tableSchema.columns).filter(([key]) => abilities.query.many.columns[key]) : Object.entries(tableSchema.columns);
|
|
1379
|
+
const columnsToSearch = (abilities.query.many.columns ? Object.entries(tableSchema.columns).filter(([key]) => abilities.query.many.columns[key]) : Object.entries(tableSchema.columns)).filter(([key, col]) => isStringLikeSQLTypeString(col.getSQLType()) || isIDLikeSQLTypeString(col.getSQLType()));
|
|
1374
1380
|
const searchParam = drizzle_orm.sql`${args.search}`;
|
|
1375
|
-
args.extras = {
|
|
1376
|
-
return drizzle_orm.sql`COALESCE(
|
|
1377
|
-
}), drizzle_orm.sql.raw(" + "))}
|
|
1378
|
-
return drizzle_orm.sql`similarity(${table[key]}::TEXT, ${searchParam})`;
|
|
1379
|
-
}), drizzle_orm.sql.raw(", "))})` };
|
|
1381
|
+
args.extras = { search_distance: (table) => drizzle_orm.sql`${drizzle_orm.sql.join(columnsToSearch.map(([key]) => {
|
|
1382
|
+
return drizzle_orm.sql`COALESCE((${table[key]}::TEXT <-> ${searchParam}), 1)`;
|
|
1383
|
+
}), drizzle_orm.sql.raw(" + "))}` };
|
|
1380
1384
|
const originalOrderBy = (0, es_toolkit.cloneDeep)(args.orderBy);
|
|
1381
1385
|
args.orderBy = (table) => {
|
|
1382
1386
|
const argsOrderBySQL = drizzle_orm.sql.join(Object.entries(originalOrderBy ?? {}).map(([key, value]) => {
|
|
@@ -1384,9 +1388,12 @@ function adjustQueryArgsForSearch({ search, args, tableSchema, abilities }) {
|
|
|
1384
1388
|
else if (value === "desc") return drizzle_orm.sql`${table[key]} DESC`;
|
|
1385
1389
|
else throw new Error(`Invalid value ${value} for orderBy`);
|
|
1386
1390
|
}), drizzle_orm.sql.raw(", "));
|
|
1387
|
-
const searchSQL = drizzle_orm.sql`
|
|
1391
|
+
const searchSQL = drizzle_orm.sql`search_distance ASC`;
|
|
1388
1392
|
return originalOrderBy ? drizzle_orm.sql.join([argsOrderBySQL, searchSQL], drizzle_orm.sql.raw(", ")) : searchSQL;
|
|
1389
1393
|
};
|
|
1394
|
+
args.where = { AND: [(0, es_toolkit.cloneDeep)(args.where) ?? {}, { RAW: (table) => drizzle_orm.sql`(${drizzle_orm.sql.join(columnsToSearch.map(([key]) => {
|
|
1395
|
+
return drizzle_orm.sql`${table[key]} % ${searchParam}`;
|
|
1396
|
+
}), drizzle_orm.sql.raw(" OR "))})` }] };
|
|
1390
1397
|
}
|
|
1391
1398
|
}
|
|
1392
1399
|
|
|
@@ -1547,11 +1554,11 @@ const createObjectImplementer = ({ db, search, schemaBuilder, makePubSubInstance
|
|
|
1547
1554
|
return acc;
|
|
1548
1555
|
}, {});
|
|
1549
1556
|
if (search?.enabled) {
|
|
1550
|
-
if (fields.
|
|
1551
|
-
fields.
|
|
1552
|
-
description: "The search
|
|
1557
|
+
if (fields.search_distance) throw new Error("Reserved field name 'search_distance' found on " + tableSchema.tsName + ". If search is enabled, the 'search_distance' field is automatically added and cannot be defined manually.");
|
|
1558
|
+
fields.search_distance = t.float({
|
|
1559
|
+
description: "The search distance of the object. If a search is provided, this field will be populated with the search distance.",
|
|
1553
1560
|
nullable: true,
|
|
1554
|
-
resolve: (parent, args, ctx, info) => parent.
|
|
1561
|
+
resolve: (parent, args, ctx, info) => parent.search_distance
|
|
1555
1562
|
});
|
|
1556
1563
|
}
|
|
1557
1564
|
return {
|
|
@@ -1933,7 +1940,7 @@ export const db = drizzle(
|
|
|
1933
1940
|
"delete"
|
|
1934
1941
|
];
|
|
1935
1942
|
if (rumbleInput.defaultLimit === void 0) rumbleInput.defaultLimit = 100;
|
|
1936
|
-
if (rumbleInput.search?.enabled) initSearchIfApplicable(rumbleInput
|
|
1943
|
+
if (rumbleInput.search?.enabled) initSearchIfApplicable(rumbleInput);
|
|
1937
1944
|
const abilityBuilder = createAbilityBuilder(rumbleInput);
|
|
1938
1945
|
const context = createContextFunction({
|
|
1939
1946
|
...rumbleInput,
|