prostgles-server 3.0.129 → 3.0.131

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.
Files changed (27) hide show
  1. package/dist/TableConfig/TableConfig.d.ts +2 -2
  2. package/dist/TableConfig/TableConfig.d.ts.map +1 -1
  3. package/dist/TableConfig/TableConfig.js +65 -34
  4. package/dist/TableConfig/TableConfig.js.map +1 -1
  5. package/dist/TableConfig/getColumnDefinitionQuery.d.ts +22 -2
  6. package/dist/TableConfig/getColumnDefinitionQuery.d.ts.map +1 -1
  7. package/dist/TableConfig/getColumnDefinitionQuery.js +29 -13
  8. package/dist/TableConfig/getColumnDefinitionQuery.js.map +1 -1
  9. package/dist/TableConfig/getConstraintDefinitionQueries.d.ts +8 -0
  10. package/dist/TableConfig/getConstraintDefinitionQueries.d.ts.map +1 -0
  11. package/dist/TableConfig/getConstraintDefinitionQueries.js +32 -0
  12. package/dist/TableConfig/getConstraintDefinitionQueries.js.map +1 -0
  13. package/lib/TableConfig/TableConfig.d.ts +2 -2
  14. package/lib/TableConfig/TableConfig.d.ts.map +1 -1
  15. package/lib/TableConfig/TableConfig.js +65 -34
  16. package/lib/TableConfig/TableConfig.ts +97 -63
  17. package/lib/TableConfig/getColumnDefinitionQuery.d.ts +22 -2
  18. package/lib/TableConfig/getColumnDefinitionQuery.d.ts.map +1 -1
  19. package/lib/TableConfig/getColumnDefinitionQuery.js +29 -13
  20. package/lib/TableConfig/getColumnDefinitionQuery.ts +43 -14
  21. package/lib/TableConfig/getConstraintDefinitionQueries.d.ts +8 -0
  22. package/lib/TableConfig/getConstraintDefinitionQueries.d.ts.map +1 -0
  23. package/lib/TableConfig/getConstraintDefinitionQueries.js +31 -0
  24. package/lib/TableConfig/getConstraintDefinitionQueries.ts +37 -0
  25. package/package.json +1 -1
  26. package/tests/client/PID.txt +1 -1
  27. package/tests/server/package-lock.json +1 -1
@@ -1,8 +1,9 @@
1
- import { asName, isObject, pickKeys } from "prostgles-types";
1
+ import { asName, pickKeys } from "prostgles-types";
2
2
  import { DB } from "../Prostgles";
3
3
  import { asValue } from "../PubSubManager/PubSubManager";
4
4
  import { VALIDATE_SCHEMA_FUNCNAME } from "../JSONBValidation/validate_jsonb_schema_sql";
5
5
  import { BaseColumnTypes, ColumnConfig } from "./TableConfig";
6
+ import pgPromise from "pg-promise";
6
7
 
7
8
  type Args = {
8
9
  column: string;
@@ -14,7 +15,8 @@ type Args = {
14
15
  /**
15
16
  * Column create statement for a given config
16
17
  */
17
- export const getColumnDefinitionQuery = async ({ colConf, column, db, table }: Args): Promise<string> => {
18
+ export const getColumnDefinitionQuery = async ({ colConf: colConfRaw, column, db, table }: Args): Promise<string | undefined> => {
19
+ const colConf = typeof colConfRaw === "string"? { sqlDefinition: colConfRaw } : colConfRaw;
18
20
  const colNameEsc = asName(column);
19
21
  const getColTypeDef = (colConf: BaseColumnTypes, pgType: "TEXT" | "JSONB") => {
20
22
  const { nullable, defaultValue } = colConf;
@@ -22,23 +24,21 @@ export const getColumnDefinitionQuery = async ({ colConf, column, db, table }: A
22
24
  }
23
25
 
24
26
  const jsonbSchema =
25
- isObject(colConf) ? (
26
27
  ("jsonbSchema" in colConf && colConf.jsonbSchema) ? { jsonbSchema: colConf.jsonbSchema, jsonbSchemaType: undefined } :
27
28
  ("jsonbSchemaType" in colConf && colConf.jsonbSchemaType) ? { jsonbSchema: undefined, jsonbSchemaType: colConf.jsonbSchemaType } :
28
- undefined
29
- ) :
30
- undefined;
29
+ undefined;
30
+
31
31
 
32
- if (isObject(colConf) && "references" in colConf && colConf.references) {
32
+ if ("references" in colConf && colConf.references) {
33
33
 
34
34
  const { tableName: lookupTable, columnName: lookupCol = "id" } = colConf.references;
35
35
  return ` ${colNameEsc} ${getColTypeDef(colConf.references, "TEXT")} REFERENCES ${lookupTable} (${lookupCol}) `;
36
36
 
37
- } else if (typeof colConf === "string" || "sqlDefinition" in colConf && colConf.sqlDefinition) {
37
+ } else if ("sqlDefinition" in colConf && colConf.sqlDefinition) {
38
38
 
39
- return ` ${colNameEsc} ${typeof colConf === "string" ? colConf : colConf.sqlDefinition} `;
39
+ return ` ${colNameEsc} ${colConf.sqlDefinition} `;
40
40
 
41
- } else if (isObject(colConf) && "isText" in colConf && colConf.isText) {
41
+ } else if ("isText" in colConf && colConf.isText) {
42
42
  let checks = "";
43
43
  const colChecks: string[] = [];
44
44
  if (colConf.lowerCased) {
@@ -78,7 +78,7 @@ export const getColumnDefinitionQuery = async ({ colConf, column, db, table }: A
78
78
  const namePreffix = 'prostgles_jsonb_' as const;
79
79
  const { val: nameEnding } = await db.one("SELECT MD5( ${table} || ${column} || ${schema}) as val", { table: table, column, schema: jsonbSchemaStr });
80
80
  const constraintName = namePreffix + nameEnding;
81
- const colConstraints = await getColConstraints(db, table, column);
81
+ const colConstraints = await getColConstraints({ db, table, column });
82
82
  const existingNonMatchingConstraints = colConstraints.filter(c => c.name.startsWith(namePreffix) && c.name !== constraintName);
83
83
  for await (const oldCons of existingNonMatchingConstraints) {
84
84
  await db.any(`ALTER TABLE ${asName(table)} DROP CONSTRAINT ${asName(oldCons.name)};`);
@@ -93,7 +93,8 @@ export const getColumnDefinitionQuery = async ({ colConf, column, db, table }: A
93
93
  return ` ${colNameEsc} ${type} ${colConf.nullable ? "" : "NOT NULL"} ${"defaultValue" in colConf ? ` DEFAULT ${asValue(colConf.defaultValue)}` : ""} CHECK(${checks})`;
94
94
 
95
95
  } else {
96
- throw "Unknown column config: " + JSON.stringify(colConf);
96
+ return undefined;
97
+ // throw "Unknown column config: " + JSON.stringify(colConf);
97
98
  }
98
99
  }
99
100
 
@@ -105,7 +106,13 @@ export type ColConstraint = {
105
106
  definition: string;
106
107
  schema: string;
107
108
  }
108
- export const getColConstraints = (db: DB, table?: string, column?: string, types?: ColConstraint["type"][]): Promise<ColConstraint[]> => {
109
+ type ColConstraintsArgs = {
110
+ db: DB | pgPromise.ITask<{}>;
111
+ table?: string;
112
+ column?: string;
113
+ types?: ColConstraint["type"][];
114
+ }
115
+ export const getColConstraintsQuery = ({ column, table, types }: Omit<ColConstraintsArgs, "db">) => {
109
116
  let query = `
110
117
  SELECT *
111
118
  FROM (
@@ -129,5 +136,27 @@ export const getColConstraints = (db: DB, table?: string, column?: string, types
129
136
  if (table) query += `\nAND "table" = ${asValue(table)}`;
130
137
  if (column) query += `\nAND cols @> ARRAY[${asValue(column)}]`;
131
138
  if (types?.length) query += `\nAND type IN (${types.map(v => asValue(v)).join(", ")})`;
132
- return db.manyOrNone(query);
139
+ return query;
140
+ }
141
+ export const getColConstraints = ({ db, column, table, types }: ColConstraintsArgs ): Promise<ColConstraint[]> => {
142
+
143
+ return db.manyOrNone(getColConstraintsQuery({ column, table, types }));
144
+ }
145
+ export type ColumnMinimalInfo = {
146
+ table_name: string;
147
+ table_schema: string;
148
+ column_name: string;
149
+ column_default: string | null;
150
+ udt_name: string;
151
+ nullable: boolean;
152
+ };
153
+ export const getTableColumns = ({ db, tableName }: { db: DB; tableName: string;}): Promise<ColumnMinimalInfo[]> => {
154
+ return db.manyOrNone(`
155
+ SELECT table_name,
156
+ table_schema, column_name,
157
+ column_default, udt_name,
158
+ is_nullable = 'YES' as nullable
159
+ FROM information_schema.columns
160
+ WHERE table_name = $1
161
+ `, [tableName]);
133
162
  }
@@ -0,0 +1,8 @@
1
+ import { TableConfig } from "./TableConfig";
2
+ type Args = {
3
+ tableName: string;
4
+ tableConf: TableConfig[string];
5
+ };
6
+ export declare const getConstraintDefinitionQueries: ({ tableConf, tableName }: Args) => string[] | undefined;
7
+ export {};
8
+ //# sourceMappingURL=getConstraintDefinitionQueries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getConstraintDefinitionQueries.d.ts","sourceRoot":"","sources":["getConstraintDefinitionQueries.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,KAAK,IAAI,GAAG;IACV,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAE/B,CAAC;AAEF,eAAO,MAAM,8BAA8B,6BAA8B,IAAI,KAAG,MAAM,EAAE,GAAG,SA2B1F,CAAA"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConstraintDefinitionQueries = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const getConstraintDefinitionQueries = ({ tableConf, tableName }) => {
6
+ if ("constraints" in tableConf && tableConf.constraints) {
7
+ const { constraints } = tableConf;
8
+ if (!constraints) {
9
+ return undefined;
10
+ }
11
+ const queries = [];
12
+ if (Array.isArray(constraints)) {
13
+ return constraints.map(c => `ALTER TABLE ${(0, prostgles_types_1.asName)(tableName)} ADD ${c}`);
14
+ }
15
+ else {
16
+ const constraintNames = (0, prostgles_types_1.getKeys)(tableConf.constraints);
17
+ constraintNames.map(constraintName => {
18
+ const _cnstr = constraints[constraintName];
19
+ const constraintDef = typeof _cnstr === "string" ? _cnstr : `${_cnstr.type} (${_cnstr.content})`;
20
+ /** Drop constraints with the same name */
21
+ // const existingConstraint = constraints.some(c => c.conname === constraintName);
22
+ // if(existingConstraint){
23
+ // if(canDrop) queries.push(`ALTER TABLE ${asName(tableName)} DROP CONSTRAINT ${asName(constraintName)};`);
24
+ // }
25
+ queries.push(`ALTER TABLE ${(0, prostgles_types_1.asName)(tableName)} ADD CONSTRAINT ${(0, prostgles_types_1.asName)(constraintName)} ${constraintDef} ;`);
26
+ });
27
+ return queries;
28
+ }
29
+ }
30
+ };
31
+ exports.getConstraintDefinitionQueries = getConstraintDefinitionQueries;
@@ -0,0 +1,37 @@
1
+ import { asName, getKeys } from "prostgles-types";
2
+ import { TableConfig } from "./TableConfig";
3
+
4
+ type Args = {
5
+ tableName: string;
6
+ tableConf: TableConfig[string]
7
+ // tableConf: BaseTableDefinition<LANG_IDS> & (TableDefinition<LANG_IDS> | LookupTableDefinition<LANG_IDS>)
8
+ };
9
+
10
+ export const getConstraintDefinitionQueries = ({ tableConf, tableName }: Args): string[] | undefined => {
11
+
12
+ if ("constraints" in tableConf && tableConf.constraints) {
13
+ const { constraints } = tableConf;
14
+ if(!constraints){
15
+ return undefined;
16
+ }
17
+ const queries: string[] = [];
18
+ if(Array.isArray(constraints)){
19
+ return constraints.map(c => `ALTER TABLE ${asName(tableName)} ADD ${c}`);
20
+ } else {
21
+ const constraintNames = getKeys(tableConf.constraints);
22
+ constraintNames.map(constraintName => {
23
+ const _cnstr = constraints[constraintName];
24
+ const constraintDef = typeof _cnstr === "string"? _cnstr : `${_cnstr.type} (${_cnstr.content})`;
25
+
26
+ /** Drop constraints with the same name */
27
+ // const existingConstraint = constraints.some(c => c.conname === constraintName);
28
+ // if(existingConstraint){
29
+ // if(canDrop) queries.push(`ALTER TABLE ${asName(tableName)} DROP CONSTRAINT ${asName(constraintName)};`);
30
+ // }
31
+
32
+ queries.push(`ALTER TABLE ${asName(tableName)} ADD CONSTRAINT ${asName(constraintName)} ${constraintDef} ;`);
33
+ });
34
+ return queries;
35
+ }
36
+ }
37
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "3.0.129",
3
+ "version": "3.0.131",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +1 @@
1
- 85239
1
+ 34106
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "3.0.128",
24
+ "version": "3.0.130",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.272.0",