Skip to content

Commit

Permalink
add uniqueColumnGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 1, 2024
1 parent c11489d commit 8100d24
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/DboBuilder/DboBuilderTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type TableSchemaColumn = ColumnInfo & {
privileges: Partial<Record<"INSERT" | "REFERENCES" | "SELECT" | "UPDATE", true>>;
}

export type TableSchema = {
export type TableSchema = Pick<TableInfo, "uniqueColumnGroups"> & {
schema: string;
name: string;
escaped_identifier: string;
Expand Down
7 changes: 6 additions & 1 deletion lib/DboBuilder/ViewHandler/getInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function getInfo(this: ViewHandler, lang?: string, param2?: any, pa
const fileTableName = this.dboBuilder.prostgles?.opts?.fileTable?.tableName;

await this._log({ command: "getInfo", localParams, data: { lang }, duration: 0 });
const allowedFieldsToSelect = this.parseFieldFilter(tableRules?.select?.fields);
return {
oid: this.tableOrViewInfo.oid,
comment: this.tableOrViewInfo.comment,
Expand All @@ -27,6 +28,10 @@ export async function getInfo(this: ViewHandler, lang?: string, param2?: any, pa
fileTableName,
dynamicRules: {
update: Boolean(tableRules?.update?.dynamicFields?.length)
}
},
/**
* Only show column groups that are fully allowed to be selected by the user
*/
uniqueColumnGroups: this.tableOrViewInfo.uniqueColumnGroups?.filter(g => !localParams || g.every(c => allowedFieldsToSelect.includes(c))),
}
}
41 changes: 40 additions & 1 deletion lib/DboBuilder/getTablesForSchemaPostgresSQL.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SQLResult, asName } from "prostgles-types";
import { omitKeys, tryCatch } from "prostgles-types/dist/util";
import { omitKeys, tryCatch, tryCatchV2 } from "prostgles-types/dist/util";
import { DboBuilder } from "../DboBuilder/DboBuilder";
import { DBorTx } from "../Prostgles";
import { clone } from "../utils";
Expand Down Expand Up @@ -167,6 +167,43 @@ export async function getTablesForSchemaPostgresSQL(
if(getFkeys.error !== undefined){
throw getFkeys.error;
}

const uniqueColsReq = await tryCatchV2(async () => {
const res = await t.any(`
select
t.relname as table_name,
(SELECT pnm.nspname FROM pg_catalog.pg_namespace pnm WHERE pnm.oid = i.relnamespace) as table_schema,
i.relname as index_name,
array_agg(a.attname)::_TEXT as column_names
from
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a
where
t.oid = ix.indrelid
and i.oid = ix.indexrelid
and a.attrelid = t.oid
and a.attnum = ANY(ix.indkey)
and t.relkind = 'r'
and ix.indisunique
group by 1,2,3
order by
t.relname,
i.relname;
`) as {
table_name: string;
table_schema: string;
index_name: string;
column_names: string[];
}[];

return res;
});

if(uniqueColsReq.error){
throw uniqueColsReq.error;
}

const badFkey = getFkeys.fkeys!.find(r => r.fcols.includes(null as any));
if(badFkey){
Expand Down Expand Up @@ -386,6 +423,8 @@ export async function getTablesForSchemaPostgresSQL(

});
table.isHyperTable = getHyperTablesReq.hyperTables?.includes(table.name);

table.uniqueColumnGroups = uniqueColsReq.data?.filter(r => r.table_name === table.name && r.table_schema === table.schema).map(r => r.column_names);

return table;
}));
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.170",
"version": "4.2.171",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -54,7 +54,7 @@
"pg": "^8.11.5",
"pg-cursor": "^2.11.0",
"pg-promise": "^11.9.1",
"prostgles-types": "^4.0.109"
"prostgles-types": "^4.0.112"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
16 changes: 8 additions & 8 deletions tests/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "ISC",
"dependencies": {
"@types/node": "^20.9.2",
"prostgles-client": "^4.0.158",
"prostgles-client": "^4.0.162",
"prostgles-types": "^4.0.51",
"socket.io-client": "^4.7.5"
},
Expand Down
25 changes: 25 additions & 0 deletions tests/isomorphicQueries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log
}
});

await test("Table info", async () => {
const {
/** Excluded because their value may vary */
oid, isFileTable,
...tableInfo
} = (await db.items.getInfo?.()) ?? {};
assert.deepStrictEqual(tableInfo, {
"comment": null,
"info": {
"label": "items"
},
"isView": false,
"hasFiles": false,
"fileTableName": "files",
"dynamicRules": {
"update": false
},
"uniqueColumnGroups": [
[
"id"
]
]
});
});

await test("Prepare data", async () => {
if(!db.sql) throw "db.sql missing";
const res = await db.items.insert!([{ name: "a" }, { name: "a" }, { name: "b" }], { returning: "*" });
Expand Down
6 changes: 3 additions & 3 deletions tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8100d24

Please sign in to comment.