backend-plus 2.5.1-betha.0 → 2.5.2-betha.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.
@@ -1,6 +1,6 @@
1
- create or replace function get_app_user() returns text
1
+ create or replace function get_app_user(p_var text default 'user') returns text
2
2
  stable language sql
3
3
  as
4
4
  $sql$
5
- select split_part(current_setting('application_name'),' ',1);
5
+ select current_setting('backend_plus._' || p_var);
6
6
  $sql$;
@@ -172,6 +172,10 @@ export type SequenceDefinition = {
172
172
  name:string
173
173
  firstValue:number
174
174
  prefix?:string /* Prefix for the generated value */
175
+ }
176
+ export type SequenceMadMaxDefinition = {
177
+ madMax: string[] // grouping of mad max sequences
178
+ firstValue?: number
175
179
  }
176
180
  export type ExportMetadataDefinition={ /* TODO: define */ }
177
181
  export type PostInputOptions='upperSpanish' | 'upperWithoutDiacritics' | 'parseDecimal'
@@ -218,11 +222,21 @@ export type FieldDefinition = EditableDbDefinition & {
218
222
  alwaysShow?:boolean /* show when appears in fixed fields */
219
223
  suggestingKeys?:string[]
220
224
  postInput?:PostInputOptions
221
- } & ({} | {
222
- sequence:SequenceDefinition
223
- nullable:true
224
- editable:false
225
- })
225
+ } & (
226
+ {
227
+ sequence?: undefined
228
+ nullable?: boolean
229
+ editable?: boolean
230
+ } | {
231
+ sequence: SequenceDefinition
232
+ nullable: true
233
+ editable: false
234
+ } | {
235
+ sequence: SequenceMadMaxDefinition
236
+ nullable: true
237
+ editable: boolean
238
+ }
239
+ );
226
240
  export type EditableDbDefinition = {
227
241
  editable?:boolean
228
242
  allow?:{
@@ -20,6 +20,7 @@ MiniTools.globalOpts.serveErr.propertiesWhiteList=['message','detail','code','ta
20
20
  var crypto = require('crypto');
21
21
  var serveContent = require('serve-content');
22
22
  var pg = require('pg-promise-strict');
23
+ var pgTriggers = require('pg-triggers');
23
24
  var SessionFileStore = require('session-file-store');
24
25
  var memorystore = require('memorystore');
25
26
  var jsToHtml=require('js-to-html');
@@ -780,16 +781,17 @@ AppBackend.prototype.start = function start(opts){
780
781
  }
781
782
  be.config.db.search_path = be.config.db.search_path ?? [be.config.db.schema, 'public'];
782
783
  be.getDbClient = function getDbClient(req){
783
- var paramsDb = be.DoubleDragon?.dbParams?.[req?.user?.[be.config.login.userFieldName]] ?? be.config.db;
784
+ var username = req?.user?.[be.config.login.userFieldName];
785
+ var paramsDb = be.DoubleDragon?.dbParams?.[username] ?? be.config.db;
784
786
  return pg.connect(paramsDb).then(function(client){
785
787
  var dbAppName=req?(
786
788
  ((req.user||{})[be.config.login.userFieldName]||'!logged')+
787
789
  ' '+(req.machineId||'?')+
788
790
  ' '+(((req.useragent)||{}).shortDescription||'?')
789
791
  ):'!app local internal';
790
- return client.query(
791
- "SET application_name = "+be.db.quoteLiteral(dbAppName)
792
- ).execute().then(function(){
792
+ return client.executeSentences([
793
+ "SET application_name = "+be.db.quoteLiteral(dbAppName),
794
+ ]).then(function(){
793
795
  var search_path = be.config.db.search_path;
794
796
  if(search_path.length>0){
795
797
  return client.query("set SEARCH_PATH TO "+be.db.quoteIdentList(search_path)).execute().then(function(){
@@ -798,6 +800,14 @@ AppBackend.prototype.start = function start(opts){
798
800
  }else{
799
801
  return client;
800
802
  }
803
+ }).then(function(client){
804
+ if(username){
805
+ return client.query(`SELECT set_app_user(${be.db.quoteLiteral(username)})`).execute().then(function(){
806
+ return client;
807
+ });
808
+ }else{
809
+ return client;
810
+ }
801
811
  }).then(function(client){
802
812
  if(be.config["client-setup"].lang=='es'){
803
813
  return client.query("set datestyle TO iso,dmy").execute().then(function(){
@@ -1012,13 +1022,17 @@ AppBackend.prototype.start = function start(opts){
1012
1022
  }
1013
1023
  be.getDbClient(req).then(function(cli){
1014
1024
  client = cli;
1025
+ return client.query("select set_app_user('!login')").execute();
1026
+ }).then(function(){
1015
1027
  var infoFieldList=be.config.login.infoFieldList||(be.config.login.rolFieldName?[be.config.login.userFieldName,be.config.login.rolFieldName]:[be.config.login.userFieldName]);
1016
1028
  return client.query(
1017
1029
  "SELECT "+infoFieldList.map(function(fieldOrPair){ return fieldOrPair.split(' as ').map(function(ident){ return be.db.quoteIdent(ident)}).join(' as '); })+
1018
1030
  ", "+be.config.login.activeClausule+" as active "+
1019
1031
  ", "+be.config.login.lockedClausule+" as locked "+
1020
- " FROM "+(be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')
1021
- +be.db.quoteIdent(be.config.login.table)+
1032
+ " FROM "+(be.config.login.from ?? (
1033
+ (be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')+
1034
+ be.db.quoteIdent(be.config.login.table)
1035
+ ))+
1022
1036
  " WHERE "+be.db.quoteIdent(be.config.login.userFieldName)+" = $1 "+
1023
1037
  " AND "+be.db.quoteIdent(be.config.login.passFieldName)+" = $2 ",
1024
1038
  [username, md5(password+username)]
@@ -2640,7 +2654,16 @@ AppBackend.prototype.dumpFkConstraint = function dumpFkConstraint(fk, tableDef,
2640
2654
  return {consName, clause, sourceFieldList};
2641
2655
  }
2642
2656
 
2657
+ AppBackend.prototype.isGeneratedSequence = function isGeneratedSequence(sequence, not){
2658
+ return sequence && ((!sequence.name && !sequence.madMax) == !not)
2659
+ }
2660
+
2661
+ AppBackend.prototype.isSequenceNonGenerated = function isSequenceNonGenerated(sequence){
2662
+ return this.isGeneratedSequence(sequence, true);
2663
+ }
2664
+
2643
2665
  AppBackend.prototype.dumpDbTableFields = function dumpDbTableFields(tableDef, opts = {}, complements = null){
2666
+ var be = this;
2644
2667
  var db = this.db;
2645
2668
  var fields=[];
2646
2669
  tableDef.fields.forEach(function(fieldDef){
@@ -2654,7 +2677,7 @@ AppBackend.prototype.dumpDbTableFields = function dumpDbTableFields(tableDef, op
2654
2677
  ' '+(fieldDef.dataLength?(fieldType=='text'?'varchar':fieldType)+'('+fieldDef.dataLength+')':fieldType)+
2655
2678
  (fieldDef.defaultValue!=null?' default '+db.quoteLiteral(fieldDef.defaultValue):'')+
2656
2679
  (fieldDef.defaultDbValue!=null?' default '+fieldDef.defaultDbValue:'')+
2657
- (fieldDef.sequence && !fieldDef.sequence.name?' generated always as identity':'')+
2680
+ (be.isGeneratedSequence(fieldDef.sequence)?' generated always as identity':'')+
2658
2681
  (fieldDef.generatedAs!=null?` generated always as (${fieldDef.generatedAs}) stored`:'')
2659
2682
  );
2660
2683
  if(complements){
@@ -2761,7 +2784,7 @@ AppBackend.prototype.dumpDbSchemaPartial = async function dumpDbSchemaPartial(pa
2761
2784
  lines.push('create table '+cualQuoteTableName+' (');
2762
2785
  var fields = be.dumpDbTableFields(tableDef, opts,
2763
2786
  function complements(fieldDef){
2764
- if(fieldDef.sequence && !fieldDef.sequence.name){
2787
+ if(be.isGeneratedSequence(fieldDef.sequence)){
2765
2788
  tablesWithStrictSequence[tableName]={}
2766
2789
  }
2767
2790
  if(fieldDef.typeName==='text' && !fieldDef.allowEmptyText){
@@ -2785,7 +2808,7 @@ AppBackend.prototype.dumpDbSchemaPartial = async function dumpDbSchemaPartial(pa
2785
2808
  ' alter column '+db.quoteIdent(fieldDef.name)+' set not null;'
2786
2809
  );
2787
2810
  }
2788
- if(fieldDef.sequence && fieldDef.sequence.name){
2811
+ if(be.isSequenceNonGenerated(fieldDef.sequence)){
2789
2812
  fieldsForSequences.push(fieldDef);
2790
2813
  }
2791
2814
  }
@@ -2888,19 +2911,26 @@ AppBackend.prototype.dumpDbSchemaPartial = async function dumpDbSchemaPartial(pa
2888
2911
  });
2889
2912
  lines.push(tableDef.sql.postCreateSqls);
2890
2913
  lines.push('');
2891
- fieldsForSequences.forEach(function(fieldDef) {
2914
+ await Promise.all(fieldsForSequences.map(async function(fieldDef) {
2892
2915
  var sequence = fieldDef.sequence;
2893
- lines.push("CREATE SEQUENCE "+db.quoteIdent(sequence.name)+" START "+db.quoteInteger(sequence.firstValue||1)+";");
2894
- lines.push(
2895
- "ALTER TABLE "+cualQuoteTableName+
2896
- " ALTER COLUMN "+db.quoteIdent(fieldDef.name)+
2897
- (sequence.prefix==null
2898
- ?" SET DEFAULT nextval("+db.quoteLiteral(sequence.name)+"::regclass);"
2899
- :" SET DEFAULT ("+db.quoteLiteral(sequence.prefix)+" || nextval("+db.quoteLiteral(sequence.name)+"::regclass)::text);"
2900
- )
2901
- );
2902
- lines.push('GRANT USAGE, SELECT ON SEQUENCE '+db.quoteIdent(sequence.name)+' TO '+user+';');
2903
- });
2916
+ if (sequence.name) {
2917
+ if (sequence.madMax) throw new Error('a sequence with madMax cannot have name');
2918
+ lines.push("CREATE SEQUENCE "+db.quoteIdent(sequence.name)+" START "+db.quoteInteger(sequence.firstValue||1)+";");
2919
+ lines.push(
2920
+ "ALTER TABLE "+cualQuoteTableName+
2921
+ " ALTER COLUMN "+db.quoteIdent(fieldDef.name)+
2922
+ (sequence.prefix==null
2923
+ ?" SET DEFAULT nextval("+db.quoteLiteral(sequence.name)+"::regclass);"
2924
+ :" SET DEFAULT ("+db.quoteLiteral(sequence.prefix)+" || nextval("+db.quoteLiteral(sequence.name)+"::regclass)::text);"
2925
+ )
2926
+ );
2927
+ lines.push('GRANT USAGE, SELECT ON SEQUENCE '+db.quoteIdent(sequence.name)+' TO '+user+';');
2928
+ } else if (sequence.madMax) {
2929
+ lines.push(await pgTriggers.dumpMaxIdTrigger(tableDef.sql.tableName, fieldDef.name, {grouping: sequence.madMax.grouping, firstValue: sequence.firstValue}));
2930
+ } else {
2931
+ throw new Error('a sequence without madMax nor name');
2932
+ }
2933
+ }));
2904
2934
  lines.push('');
2905
2935
  if(tableDef.sql.policies.enabled){
2906
2936
  policyLines.push(`ALTER TABLE ${cualQuoteTableName} ENABLE ROW LEVEL SECURITY;`);
@@ -2935,6 +2965,40 @@ AppBackend.prototype.dumpDbSchemaPartial = async function dumpDbSchemaPartial(pa
2935
2965
  throw err;
2936
2966
  }
2937
2967
  }
2968
+ lines.push(`
2969
+ create or replace function set_app_user(p_user text) returns text
2970
+ security definer volatile language plpgsql
2971
+ as
2972
+ $body$
2973
+ declare
2974
+ ${be.config.login.infoFieldList.map(fieldName => `
2975
+ ${db.quoteIdent('v_'+fieldName)} text;`
2976
+ ).join('')}
2977
+ begin
2978
+ if p_user = '!login' then
2979
+ ${be.config.login.infoFieldList.map(fieldName => `
2980
+ set backend_plus._${fieldName} = '!';`).join('')}
2981
+
2982
+ set backend_plus._mode = login;
2983
+ else
2984
+ select ${be.config.login.infoFieldList.map(fieldName => db.quoteIdent(fieldName)).join(', ')}
2985
+ into ${be.config.login.infoFieldList.map(fieldName => db.quoteIdent('v_'+fieldName)).join(', ')}
2986
+
2987
+ from ${(be.config.login.from ?? (
2988
+ (be.config.login.schema?be.db.quoteIdent(be.config.login.schema)+'.':'')+
2989
+ be.db.quoteIdent(be.config.login.table)))}
2990
+ where ${db.quoteIdent(be.config.login.userFieldName)} = p_user;
2991
+ ${be.config.login.infoFieldList.map(fieldName => `
2992
+ perform set_config('backend_plus._${fieldName}', v_${fieldName}, false);`).join('')}
2993
+
2994
+ set backend_plus._mode = normal;
2995
+ end if;
2996
+ perform set_config('backend_plus._user', p_user, false);
2997
+ return p_user;
2998
+ end;
2999
+ $body$;
3000
+
3001
+ `)
2938
3002
  var enancePart= 'do $SQL_ENANCE$\n begin\n' + enanceLines.join('\n')+'\n' + 'end\n$SQL_ENANCE$;';
2939
3003
  var someNotFound=false;
2940
3004
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "backend-plus",
3
3
  "description": "Backend for the anti Pareto rule",
4
- "version": "2.5.1-betha.0",
4
+ "version": "2.5.2-betha.10",
5
5
  "author": "Codenautas <codenautas@googlegroups.com>",
6
6
  "license": "MIT",
7
7
  "repository": "codenautas/backend-plus",
@@ -35,13 +35,13 @@
35
35
  "best-globals": "^2.0.1",
36
36
  "big.js": "^7.0.1",
37
37
  "body-parser": "^2.2.0",
38
- "cast-error": "^0.1.1",
38
+ "cast-error": "^0.1.2",
39
39
  "castellano": "^0.1.4",
40
40
  "connect-pg-simple": "^10.0.0",
41
41
  "cookie-parser": "^1.4.7",
42
42
  "cors": "^2.8.5",
43
- "dialog-promise": "^0.10.1",
44
- "discrepances": "^0.2.8",
43
+ "dialog-promise": "^0.10.2",
44
+ "discrepances": "^0.2.9",
45
45
  "express": "^5.1.0",
46
46
  "express-session": "^1.18.1",
47
47
  "express-useragent": "^1.0.15",
@@ -51,14 +51,15 @@
51
51
  "json4all": "^1.4.0",
52
52
  "lazy-some": "^0.1.0",
53
53
  "like-ar": "^0.5.1",
54
- "login-plus": "^1.7.2",
54
+ "login-plus": "^1.7.3",
55
55
  "memorystore": "^1.6.7",
56
- "mini-tools": "^1.13.2",
56
+ "mini-tools": "^1.13.3",
57
57
  "moment": "^2.30.1",
58
58
  "multiparty": "^4.2.3",
59
59
  "nodemailer": "^7.0.3",
60
60
  "numeral": "^2.0.6",
61
- "pg-promise-strict": "^1.4.2",
61
+ "pg-promise-strict": "^1.4.3",
62
+ "pg-triggers": "0.4.3",
62
63
  "pikaday": "^1.8.2",
63
64
  "pug": "^3.0.3",
64
65
  "read-yaml-promise": "^1.0.2",
@@ -77,13 +78,13 @@
77
78
  "devDependencies": {
78
79
  "@types/big.js": "^6.2.2",
79
80
  "@types/expect.js": "~0.3.32",
80
- "@types/express": "^5.0.2",
81
+ "@types/express": "^5.0.3",
81
82
  "@types/express-useragent": "^1.0.5",
82
83
  "@types/fs-extra": "^11.0.4",
83
84
  "@types/js-yaml": "^4.0.9",
84
85
  "@types/mocha": "^10.0.10",
85
86
  "@types/multiparty": "~4.2.1",
86
- "@types/node": "^22.15.21",
87
+ "@types/node": "^24.0.3",
87
88
  "@types/nodemailer": "^6.4.17",
88
89
  "@types/numeral": "~2.0.5",
89
90
  "@types/session-file-store": "^1.2.5",
@@ -98,10 +99,10 @@
98
99
  "karma-ie-launcher": "^1.0.0",
99
100
  "karma-mocha": "^2.0.1",
100
101
  "kill-9": "~0.4.3",
101
- "mocha": "^11.5.0",
102
+ "mocha": "^11.7.0",
102
103
  "nyc": "^17.1.0",
103
- "puppeteer": "^24.9.0",
104
- "sinon": "^20.0.0",
104
+ "puppeteer": "^24.10.2",
105
+ "sinon": "^21.0.0",
105
106
  "supertest": "^7.1.1",
106
107
  "types.d.ts": "~0.6.22",
107
108
  "typescript": "^5.8.3",