@axinom/mosaic-db-common 0.64.0-rc.0 → 0.64.0-rc.10
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.
|
@@ -503,6 +503,84 @@ BEGIN
|
|
|
503
503
|
END;
|
|
504
504
|
$$;
|
|
505
505
|
|
|
506
|
+
/*-snippet
|
|
507
|
+
{
|
|
508
|
+
"body": [
|
|
509
|
+
"SELECT ax_define.drop_indexed_search('${1:column_name}', '${2:table_name}');"
|
|
510
|
+
],
|
|
511
|
+
"description": [
|
|
512
|
+
"Drops the candidate function that was created with 'define_indexed_search'."
|
|
513
|
+
]
|
|
514
|
+
}
|
|
515
|
+
snippet-*/
|
|
516
|
+
CREATE OR REPLACE FUNCTION ax_define.drop_indexed_search(fieldName text, tableName text) RETURNS void
|
|
517
|
+
LANGUAGE plpgsql
|
|
518
|
+
AS $$
|
|
519
|
+
BEGIN
|
|
520
|
+
EXECUTE 'DROP FUNCTION IF EXISTS app_hidden.' || tableName || '_' || fieldName || '_candidates(text);';
|
|
521
|
+
END;
|
|
522
|
+
$$;
|
|
523
|
+
|
|
524
|
+
/*-snippet
|
|
525
|
+
{
|
|
526
|
+
"body": [
|
|
527
|
+
"SELECT ax_define.define_indexed_search('${1:column_name}', '${2:table_name}', '${3:app_public}', '${4:READ_PERMISSIONS}', '${5::DATABASE_GQL_ROLE}');"
|
|
528
|
+
],
|
|
529
|
+
"description": [
|
|
530
|
+
"Lets 'includesInsensitive' filters on a column use its 'define_like_index' trigram index under row level security.\n",
|
|
531
|
+
"PostgreSQL cannot use the index for ILIKE in a row level security protected query unless the ILIKE function is marked as LEAKPROOF, which requires superuser rights.",
|
|
532
|
+
"This creates 'app_hidden.<table_name>_<column_name>_candidates(pattern)', which runs with the owner's rights and returns the primary keys of the matching rows.",
|
|
533
|
+
"Register the column with the 'IndexedSearchPluginFactory' from '@axinom/mosaic-graphql-common' so that the GraphQL filter uses it. The final rows are still filtered by row level security.\n",
|
|
534
|
+
"Fourth parameter: the read permissions of the table, as used for 'define_authentication'.",
|
|
535
|
+
"Fifth parameter: the role that executes GraphQL queries.",
|
|
536
|
+
"Sixth (optional) parameter: the additional RLS check of the table, as used for 'define_authentication'."
|
|
537
|
+
]
|
|
538
|
+
}
|
|
539
|
+
snippet-*/
|
|
540
|
+
CREATE OR REPLACE FUNCTION ax_define.define_indexed_search(fieldName text, tableName text, schemaName text, readPermissions text, gqlRole text, additionalRls text default '1=1') RETURNS void
|
|
541
|
+
LANGUAGE plpgsql
|
|
542
|
+
AS $$
|
|
543
|
+
DECLARE
|
|
544
|
+
functionName text := 'app_hidden.' || tableName || '_' || fieldName || '_candidates';
|
|
545
|
+
keyColumns text;
|
|
546
|
+
keyDefinitions text;
|
|
547
|
+
BEGIN
|
|
548
|
+
PERFORM ax_utils.validate_identifier_length(tableName || '_' || fieldName || '_candidates', 'The table and column names are too long for the generated candidate function name.');
|
|
549
|
+
SELECT string_agg(quote_ident(a.attname), ', ' ORDER BY k.ord),
|
|
550
|
+
string_agg(quote_ident(a.attname) || ' ' || format_type(a.atttypid, a.atttypmod), ', ' ORDER BY k.ord)
|
|
551
|
+
INTO keyColumns, keyDefinitions
|
|
552
|
+
FROM pg_index i
|
|
553
|
+
CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord)
|
|
554
|
+
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum
|
|
555
|
+
WHERE i.indrelid = (schemaName || '.' || tableName)::regclass AND i.indisprimary AND k.ord <= i.indnkeyatts;
|
|
556
|
+
IF keyColumns IS NULL THEN
|
|
557
|
+
PERFORM ax_utils.raise_error('The table "%s.%s" must have a primary key to define an indexed search.', 'SETUP', schemaName, tableName);
|
|
558
|
+
END IF;
|
|
559
|
+
|
|
560
|
+
PERFORM ax_define.drop_indexed_search(fieldName, tableName);
|
|
561
|
+
-- The function returns all matching keys. The calling query applies row level security to the final rows,
|
|
562
|
+
-- the permission and additional RLS checks here only prevent returning keys of rows the caller cannot see.
|
|
563
|
+
EXECUTE format($sql$
|
|
564
|
+
CREATE FUNCTION %1$s(pattern text) RETURNS TABLE (%2$s)
|
|
565
|
+
LANGUAGE plpgsql STABLE SECURITY DEFINER ROWS 100
|
|
566
|
+
SET search_path = pg_catalog, pg_temp
|
|
567
|
+
AS $fn$
|
|
568
|
+
BEGIN
|
|
569
|
+
%3$s
|
|
570
|
+
RETURN QUERY EXECUTE %4$L USING pattern;
|
|
571
|
+
END;
|
|
572
|
+
$fn$;
|
|
573
|
+
REVOKE ALL ON FUNCTION %1$s(text) FROM PUBLIC;
|
|
574
|
+
GRANT EXECUTE ON FUNCTION %1$s(text) TO %5$s;
|
|
575
|
+
$sql$,
|
|
576
|
+
functionName,
|
|
577
|
+
keyDefinitions,
|
|
578
|
+
CASE WHEN readPermissions <> '' THEN format('PERFORM ax_utils.user_has_permission(%L);', readPermissions) ELSE '' END,
|
|
579
|
+
format('SELECT %s FROM %s.%s WHERE %s ILIKE $1 AND (%s)', keyColumns, schemaName, tableName, fieldName, additionalRls),
|
|
580
|
+
gqlRole);
|
|
581
|
+
END;
|
|
582
|
+
$$;
|
|
583
|
+
|
|
506
584
|
/*-snippet
|
|
507
585
|
{
|
|
508
586
|
"body": [
|
|
@@ -247,6 +247,25 @@ BEGIN
|
|
|
247
247
|
END;
|
|
248
248
|
$$;
|
|
249
249
|
|
|
250
|
+
/*-snippet
|
|
251
|
+
{
|
|
252
|
+
"body": [
|
|
253
|
+
"SELECT ax_define.define_multitenant_indexed_search('${1:column_name}', '${2:table_name}', '${3:app_public}', '${4:READ_PERMISSIONS}', '${5::DATABASE_GQL_ROLE}');"
|
|
254
|
+
],
|
|
255
|
+
"description": [
|
|
256
|
+
"Same as 'define_indexed_search', for tables that use 'define_multitenant_authentication'.\n",
|
|
257
|
+
"The candidate function only returns rows of the current tenant and environment."
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
snippet-*/
|
|
261
|
+
CREATE OR REPLACE FUNCTION ax_define.define_multitenant_indexed_search(fieldName text, tableName text, schemaName text, readPermissions text, gqlRole text) RETURNS void
|
|
262
|
+
LANGUAGE plpgsql
|
|
263
|
+
AS $$
|
|
264
|
+
BEGIN
|
|
265
|
+
perform ax_define.define_indexed_search(fieldName, tableName, schemaName, readPermissions, gqlRole, '(tenant_id = (SELECT ax_utils.current_tenant_id()) AND environment_id = (SELECT ax_utils.current_environment_id()))');
|
|
266
|
+
END;
|
|
267
|
+
$$;
|
|
268
|
+
|
|
250
269
|
/*-snippet
|
|
251
270
|
{
|
|
252
271
|
"body": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axinom/mosaic-db-common",
|
|
3
|
-
"version": "0.64.0-rc.
|
|
3
|
+
"version": "0.64.0-rc.10",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=20.19.0"
|
|
6
6
|
},
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"zapatos": "3.6.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@axinom/mosaic-dev-be-common": "^0.26.0-rc.
|
|
74
|
+
"@axinom/mosaic-dev-be-common": "^0.26.0-rc.10",
|
|
75
75
|
"@types/express": "^4.17.17",
|
|
76
76
|
"@types/node": "^22.0.0",
|
|
77
77
|
"@types/pg": "^8.6.1",
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
"publishConfig": {
|
|
85
85
|
"access": "public"
|
|
86
86
|
},
|
|
87
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "af3c9f73c77084f371205c32a5689fc01dd5a876"
|
|
88
88
|
}
|