@balena/pinejs 14.60.0-build-translated-models-85baf731130de328e2067a26c40b37309cdb1ac5-1 → 15.0.0-build-15-x-4681209f5dd8d896491fb5ed64aea47df511c14d-1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. package/.pinejs-cache.json +1 -1
  2. package/.versionbot/CHANGELOG.yml +106 -6
  3. package/CHANGELOG.md +16 -4
  4. package/VERSION +1 -1
  5. package/build/browser.ts +0 -1
  6. package/out/bin/abstract-sql-compiler.js +0 -0
  7. package/out/bin/abstract-sql-compiler.js.map +1 -1
  8. package/out/bin/odata-compiler.js +1 -4
  9. package/out/bin/odata-compiler.js.map +1 -1
  10. package/out/bin/sbvr-compiler.js +0 -0
  11. package/out/bin/sbvr-compiler.js.map +1 -1
  12. package/out/config-loader/config-loader.d.ts +2 -5
  13. package/out/config-loader/config-loader.js +19 -38
  14. package/out/config-loader/config-loader.js.map +1 -1
  15. package/out/config-loader/env.js +4 -4
  16. package/out/config-loader/env.js.map +1 -1
  17. package/out/data-server/sbvr-server.d.ts +1 -13
  18. package/out/data-server/sbvr-server.js +17 -1
  19. package/out/data-server/sbvr-server.js.map +1 -1
  20. package/out/database-layer/db.js +12 -15
  21. package/out/database-layer/db.js.map +1 -1
  22. package/out/express-emulator/express.js +4 -4
  23. package/out/express-emulator/express.js.map +1 -1
  24. package/out/http-transactions/transactions.d.ts +1 -12
  25. package/out/http-transactions/transactions.js +18 -0
  26. package/out/http-transactions/transactions.js.map +1 -1
  27. package/out/migrator/async.js +12 -16
  28. package/out/migrator/async.js.map +1 -1
  29. package/out/migrator/sync.js +6 -12
  30. package/out/migrator/sync.js.map +1 -1
  31. package/out/migrator/utils.d.ts +5 -4
  32. package/out/migrator/utils.js +38 -20
  33. package/out/migrator/utils.js.map +1 -1
  34. package/out/pinejs-session-store/pinejs-session-store.js +18 -3
  35. package/out/pinejs-session-store/pinejs-session-store.js.map +1 -1
  36. package/out/sbvr-api/abstract-sql.js +2 -4
  37. package/out/sbvr-api/abstract-sql.js.map +1 -1
  38. package/out/sbvr-api/cached-compile.js +1 -1
  39. package/out/sbvr-api/cached-compile.js.map +1 -1
  40. package/out/sbvr-api/hooks.d.ts +3 -3
  41. package/out/sbvr-api/hooks.js +32 -48
  42. package/out/sbvr-api/hooks.js.map +1 -1
  43. package/out/sbvr-api/odata-response.js +4 -5
  44. package/out/sbvr-api/odata-response.js.map +1 -1
  45. package/out/sbvr-api/permissions.js +25 -65
  46. package/out/sbvr-api/permissions.js.map +1 -1
  47. package/out/sbvr-api/sbvr-utils.d.ts +5 -11
  48. package/out/sbvr-api/sbvr-utils.js +94 -145
  49. package/out/sbvr-api/sbvr-utils.js.map +1 -1
  50. package/out/sbvr-api/uri-parser.d.ts +1 -4
  51. package/out/sbvr-api/uri-parser.js +4 -11
  52. package/out/sbvr-api/uri-parser.js.map +1 -1
  53. package/package.json +8 -8
  54. package/src/bin/abstract-sql-compiler.ts +2 -1
  55. package/src/bin/odata-compiler.ts +2 -4
  56. package/src/bin/sbvr-compiler.ts +2 -1
  57. package/src/config-loader/config-loader.ts +24 -62
  58. package/src/config-loader/env.ts +3 -7
  59. package/src/data-server/sbvr-server.js +17 -1
  60. package/src/database-layer/db.ts +1 -1
  61. package/src/express-emulator/express.js +4 -4
  62. package/src/http-transactions/transactions.js +18 -0
  63. package/src/migrator/utils.ts +40 -20
  64. package/src/pinejs-session-store/pinejs-session-store.ts +15 -0
  65. package/src/sbvr-api/abstract-sql.ts +1 -2
  66. package/src/sbvr-api/hooks.ts +33 -80
  67. package/src/sbvr-api/odata-response.ts +1 -2
  68. package/src/sbvr-api/permissions.ts +20 -68
  69. package/src/sbvr-api/sbvr-utils.ts +107 -195
  70. package/src/sbvr-api/uri-parser.ts +5 -8
  71. package/tsconfig.json +1 -1
  72. package/out/sbvr-api/translations.d.ts +0 -6
  73. package/out/sbvr-api/translations.js +0 -136
  74. package/out/sbvr-api/translations.js.map +0 -1
  75. package/src/sbvr-api/translations.ts +0 -219
@@ -1,136 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translateAbstractSqlModel = void 0;
4
- const _ = require("lodash");
5
- const aliasFields = (abstractSqlModel, resourceName, aliases) => {
6
- const fieldNames = abstractSqlModel.tables[resourceName].fields.map(({ fieldName }) => fieldName);
7
- const nonexistentFields = _.difference(Object.keys(aliases), fieldNames);
8
- if (nonexistentFields.length > 0) {
9
- throw new Error(`Tried to alias non-existent fields: '${nonexistentFields.join(', ')}'`);
10
- }
11
- return fieldNames.map((fieldName) => {
12
- const alias = aliases[fieldName];
13
- if (alias) {
14
- if (typeof alias === 'string') {
15
- return ['Alias', ['ReferencedField', resourceName, alias], fieldName];
16
- }
17
- return ['Alias', alias, fieldName];
18
- }
19
- return ['ReferencedField', resourceName, fieldName];
20
- });
21
- };
22
- const aliasResource = (abstractSqlModel, resourceName, toResource, aliases) => {
23
- if (!abstractSqlModel.tables[toResource]) {
24
- throw new Error(`Tried to alias to a non-existent resource: ${toResource}`);
25
- }
26
- return {
27
- abstractSql: [
28
- 'SelectQuery',
29
- ['Select', aliasFields(abstractSqlModel, resourceName, aliases)],
30
- ['From', ['Alias', ['Resource', toResource], resourceName]],
31
- ],
32
- };
33
- };
34
- const namespaceRelationships = (relationships, alias) => {
35
- for (const [key, relationship] of Object.entries(relationships)) {
36
- if (key === '$') {
37
- return;
38
- }
39
- let mapping = relationship.$;
40
- if (mapping != null && mapping.length === 2) {
41
- if (!key.includes('$')) {
42
- mapping = _.cloneDeep(mapping);
43
- mapping[1][0] = `${mapping[1][0]}$${alias}`;
44
- relationships[`${key}$${alias}`] = {
45
- $: mapping,
46
- };
47
- delete relationships[key];
48
- }
49
- }
50
- namespaceRelationships(relationship, alias);
51
- }
52
- };
53
- const translateAbstractSqlModel = (fromAbstractSqlModel, toAbstractSqlModel, fromVersion, toVersion, translationDefinitions = {}) => {
54
- var _a, _b, _c;
55
- const isDefinition = (d) => 'abstractSql' in d;
56
- const resourceRenames = {};
57
- fromAbstractSqlModel.rules = toAbstractSqlModel.rules;
58
- const fromResourceKeys = Object.keys(fromAbstractSqlModel.tables);
59
- const nonexistentTables = _.difference(Object.keys(translationDefinitions), fromResourceKeys);
60
- if (nonexistentTables.length > 0) {
61
- throw new Error(`Tried to define non-existent resources: '${nonexistentTables.join(', ')}'`);
62
- }
63
- for (const [synonym, canonicalForm] of Object.entries(toAbstractSqlModel.synonyms)) {
64
- if (synonym.includes('$')) {
65
- fromAbstractSqlModel.synonyms[synonym] = canonicalForm;
66
- }
67
- else {
68
- fromAbstractSqlModel.synonyms[`${synonym}$${toVersion}`] = `${canonicalForm}$${toVersion}`;
69
- }
70
- }
71
- const relationships = _.cloneDeep(toAbstractSqlModel.relationships);
72
- namespaceRelationships(relationships, toVersion);
73
- for (let [key, relationship] of Object.entries(relationships)) {
74
- if (!key.includes('$')) {
75
- key = `${key}$${toVersion}`;
76
- }
77
- fromAbstractSqlModel.relationships[key] = relationship;
78
- }
79
- const aliasedFromRelationships = _.cloneDeep(fromAbstractSqlModel.relationships);
80
- namespaceRelationships(aliasedFromRelationships, fromVersion);
81
- for (let [key, relationship] of Object.entries(aliasedFromRelationships)) {
82
- if (!key.includes('$')) {
83
- key = `${key}$${fromVersion}`;
84
- fromAbstractSqlModel.relationships[key] = relationship;
85
- }
86
- }
87
- for (let [key, table] of Object.entries(toAbstractSqlModel.tables)) {
88
- if (!key.includes('$')) {
89
- key = `${key}$${toVersion}`;
90
- }
91
- fromAbstractSqlModel.tables[key] = _.cloneDeep(table);
92
- }
93
- for (const key of fromResourceKeys) {
94
- const translationDefinition = translationDefinitions[key];
95
- const table = fromAbstractSqlModel.tables[key];
96
- if (translationDefinition) {
97
- const { $toResource, ...definition } = translationDefinition;
98
- const hasToResource = typeof $toResource === 'string';
99
- if (hasToResource) {
100
- resourceRenames[key] = `${$toResource}`;
101
- }
102
- const toResource = hasToResource ? $toResource : `${key}$${toVersion}`;
103
- const toTable = fromAbstractSqlModel.tables[toResource];
104
- if (!toTable) {
105
- if (hasToResource) {
106
- throw new Error(`Unknown $toResource: '${toResource}'`);
107
- }
108
- else {
109
- throw new Error(`Missing $toResource: '${toResource}'`);
110
- }
111
- }
112
- table.modifyFields = _.cloneDeep((_a = toTable.modifyFields) !== null && _a !== void 0 ? _a : toTable.fields);
113
- table.modifyName = (_b = toTable.modifyName) !== null && _b !== void 0 ? _b : toTable.name;
114
- if (isDefinition(definition)) {
115
- table.definition = definition;
116
- }
117
- else {
118
- table.definition = aliasResource(fromAbstractSqlModel, key, toResource, definition);
119
- }
120
- }
121
- else {
122
- const toTable = fromAbstractSqlModel.tables[`${key}$${toVersion}`];
123
- if (!toTable) {
124
- throw new Error(`Missing translation for: '${key}'`);
125
- }
126
- table.modifyFields = _.cloneDeep((_c = toTable.modifyFields) !== null && _c !== void 0 ? _c : toTable.fields);
127
- table.definition = {
128
- abstractSql: ['Resource', `${key}$${toVersion}`],
129
- };
130
- }
131
- fromAbstractSqlModel.tables[`${key}$${fromVersion}`] = table;
132
- }
133
- return resourceRenames;
134
- };
135
- exports.translateAbstractSqlModel = translateAbstractSqlModel;
136
- //# sourceMappingURL=translations.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"translations.js","sourceRoot":"","sources":["../../src/sbvr-api/translations.ts"],"names":[],"mappings":";;;AAAA,4BAA4B;AAyB5B,MAAM,WAAW,GAAG,CACnB,gBAAkC,EAClC,YAAoB,EACpB,OAAgD,EAChC,EAAE;IAClB,MAAM,UAAU,GAAG,gBAAgB,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,GAAG,CAClE,CAAC,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,SAAS,CAC5B,CAAC;IACF,MAAM,iBAAiB,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC;IACzE,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;QACjC,MAAM,IAAI,KAAK,CACd,wCAAwC,iB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,MAAA,OAAO,CAAC,UAAU,mCAAI,OAAO,CAAC,IAAI,CAAC;YACtD,IAAI,YAAY,CAAC,UAAU,CAAC,EAAE;gBAC7B,KAAK,CAAC,UAAU,GAAG,UAAU,CAAC;aAC9B;iBAAM;gBACN,KAAK,CAAC,UAAU,GAAG,aAAa,CAC/B,oBAAoB,EACpB,GAAG,EACH,UAAU,EACV,UAAU,CACV,CAAC;aACF;SACD;aAAM;YACN,MAAM,OAAO,GAAG,oBAAoB,CAAC,MAAM,CAAC,GAAG,GAAG,IAAI,SAAS,EAAE,CAAC,CAAC;YACnE,IAAI,CAAC,OAAO,EAAE;gBACb,MAAM,IAAI,KAAK,CAAC,6BAA6B,GAAG,GAAG,CAAC,CAAC;aACrD;YACD,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,SAAS,CAAC,MAAA,OAAO,CAAC,YAAY,mCAAI,OAAO,CAAC,MAAM,CAAC,CAAC;YACzE,KAAK,CAAC,UAAU,GAAG;gBAClB,WAAW,EAAE,CAAC,UAAU,EAAE,GAAG,GAAG,IAAI,SAAS,EAAE,CAAC;aAChD,CAAC;SACF;QAED,oBAAoB,CAAC,MAAM,CAAC,GAAG,GAAG,IAAI,WAAW,EAAE,CAAC,GAAG,KAAK,CAAC;KAC7D;IAED,OAAO,eAAe,CAAC;AACxB,CAAC,CAAC;AAzHW,QAAA,yBAAyB,6BAyHpC"}
@@ -1,219 +0,0 @@
1
- import * as _ from 'lodash';
2
- import {
3
- AbstractSqlModel,
4
- Relationship,
5
- ReferencedFieldNode,
6
- SelectNode,
7
- AliasNode,
8
- Definition,
9
- RelationshipInternalNode,
10
- RelationshipLeafNode,
11
- SelectQueryNode,
12
- NumberTypeNodes,
13
- BooleanTypeNodes,
14
- UnknownTypeNodes,
15
- NullNode,
16
- } from '@balena/abstract-sql-compiler';
17
- import { Dictionary } from './common-types';
18
-
19
- export type AliasValidNodeType =
20
- | ReferencedFieldNode
21
- | SelectQueryNode
22
- | NumberTypeNodes
23
- | BooleanTypeNodes
24
- | UnknownTypeNodes
25
- | NullNode;
26
- const aliasFields = (
27
- abstractSqlModel: AbstractSqlModel,
28
- resourceName: string,
29
- aliases: Dictionary<string | AliasValidNodeType>,
30
- ): SelectNode[1] => {
31
- const fieldNames = abstractSqlModel.tables[resourceName].fields.map(
32
- ({ fieldName }) => fieldName,
33
- );
34
- const nonexistentFields = _.difference(Object.keys(aliases), fieldNames);
35
- if (nonexistentFields.length > 0) {
36
- throw new Error(
37
- `Tried to alias non-existent fields: '${nonexistentFields.join(', ')}'`,
38
- );
39
- }
40
- return fieldNames.map(
41
- (fieldName): AliasNode<AliasValidNodeType> | ReferencedFieldNode => {
42
- const alias = aliases[fieldName];
43
- if (alias) {
44
- if (typeof alias === 'string') {
45
- return ['Alias', ['ReferencedField', resourceName, alias], fieldName];
46
- }
47
- return ['Alias', alias, fieldName];
48
- }
49
- return ['ReferencedField', resourceName, fieldName];
50
- },
51
- );
52
- };
53
-
54
- const aliasResource = (
55
- abstractSqlModel: AbstractSqlModel,
56
- resourceName: string,
57
- toResource: string,
58
- aliases: Dictionary<string | AliasValidNodeType>,
59
- ): Definition => {
60
- if (!abstractSqlModel.tables[toResource]) {
61
- throw new Error(`Tried to alias to a non-existent resource: ${toResource}`);
62
- }
63
- return {
64
- abstractSql: [
65
- 'SelectQuery',
66
- ['Select', aliasFields(abstractSqlModel, resourceName, aliases)],
67
- ['From', ['Alias', ['Resource', toResource], resourceName]],
68
- ],
69
- };
70
- };
71
-
72
- const namespaceRelationships = (
73
- relationships: Relationship,
74
- alias: string,
75
- ): void => {
76
- for (const [key, relationship] of Object.entries(
77
- relationships as RelationshipInternalNode,
78
- )) {
79
- if (key === '$') {
80
- return;
81
- }
82
-
83
- let mapping = (relationship as RelationshipLeafNode).$;
84
- if (mapping != null && mapping.length === 2) {
85
- if (!key.includes('$')) {
86
- mapping = _.cloneDeep(mapping);
87
- mapping[1]![0] = `${mapping[1]![0]}$${alias}`;
88
- (relationships as RelationshipInternalNode)[`${key}$${alias}`] = {
89
- $: mapping,
90
- };
91
- delete (relationships as RelationshipInternalNode)[key];
92
- }
93
- }
94
- namespaceRelationships(relationship, alias);
95
- }
96
- };
97
-
98
- export const translateAbstractSqlModel = (
99
- fromAbstractSqlModel: AbstractSqlModel,
100
- toAbstractSqlModel: AbstractSqlModel,
101
- fromVersion: string,
102
- toVersion: string,
103
- translationDefinitions: Dictionary<
104
- | (Definition & { $toResource?: string })
105
- | Dictionary<string | AliasValidNodeType>
106
- > = {},
107
- ): Dictionary<string> => {
108
- const isDefinition = (
109
- d: (typeof translationDefinitions)[string],
110
- ): d is Definition => 'abstractSql' in d;
111
-
112
- const resourceRenames: Dictionary<string> = {};
113
-
114
- fromAbstractSqlModel.rules = toAbstractSqlModel.rules;
115
-
116
- const fromResourceKeys = Object.keys(fromAbstractSqlModel.tables);
117
- const nonexistentTables = _.difference(
118
- Object.keys(translationDefinitions),
119
- fromResourceKeys,
120
- );
121
- if (nonexistentTables.length > 0) {
122
- throw new Error(
123
- `Tried to define non-existent resources: '${nonexistentTables.join(
124
- ', ',
125
- )}'`,
126
- );
127
- }
128
- for (const [synonym, canonicalForm] of Object.entries(
129
- toAbstractSqlModel.synonyms,
130
- )) {
131
- // Don't double alias
132
- if (synonym.includes('$')) {
133
- fromAbstractSqlModel.synonyms[synonym] = canonicalForm;
134
- } else {
135
- fromAbstractSqlModel.synonyms[
136
- `${synonym}$${toVersion}`
137
- ] = `${canonicalForm}$${toVersion}`;
138
- }
139
- }
140
- const relationships = _.cloneDeep(toAbstractSqlModel.relationships);
141
- namespaceRelationships(relationships, toVersion);
142
- for (let [key, relationship] of Object.entries(relationships)) {
143
- // Don't double alias
144
- if (!key.includes('$')) {
145
- key = `${key}$${toVersion}`;
146
- }
147
- fromAbstractSqlModel.relationships[key] = relationship;
148
- }
149
-
150
- // TODO: We also need to keep the original relationship refs to non $version resources
151
-
152
- // Also alias for ourselves to allow explicit referencing
153
- const aliasedFromRelationships = _.cloneDeep(
154
- fromAbstractSqlModel.relationships,
155
- );
156
- namespaceRelationships(aliasedFromRelationships, fromVersion);
157
- for (let [key, relationship] of Object.entries(aliasedFromRelationships)) {
158
- // Don't double alias
159
- if (!key.includes('$')) {
160
- key = `${key}$${fromVersion}`;
161
- fromAbstractSqlModel.relationships[key] = relationship;
162
- }
163
- }
164
-
165
- for (let [key, table] of Object.entries(toAbstractSqlModel.tables)) {
166
- // Don't double alias
167
- if (!key.includes('$')) {
168
- key = `${key}$${toVersion}`;
169
- }
170
- fromAbstractSqlModel.tables[key] = _.cloneDeep(table);
171
- }
172
-
173
- for (const key of fromResourceKeys) {
174
- const translationDefinition = translationDefinitions[key];
175
- const table = fromAbstractSqlModel.tables[key];
176
- if (translationDefinition) {
177
- const { $toResource, ...definition } = translationDefinition;
178
- const hasToResource = typeof $toResource === 'string';
179
- if (hasToResource) {
180
- resourceRenames[key] = `${$toResource}`;
181
- }
182
- const toResource = hasToResource ? $toResource : `${key}$${toVersion}`;
183
- // TODO: Should this use the toAbstractSqlModel?
184
- const toTable = fromAbstractSqlModel.tables[toResource];
185
- if (!toTable) {
186
- if (hasToResource) {
187
- throw new Error(`Unknown $toResource: '${toResource}'`);
188
- } else {
189
- throw new Error(`Missing $toResource: '${toResource}'`);
190
- }
191
- }
192
- table.modifyFields = _.cloneDeep(toTable.modifyFields ?? toTable.fields);
193
- table.modifyName = toTable.modifyName ?? toTable.name;
194
- if (isDefinition(definition)) {
195
- table.definition = definition;
196
- } else {
197
- table.definition = aliasResource(
198
- fromAbstractSqlModel,
199
- key,
200
- toResource,
201
- definition,
202
- );
203
- }
204
- } else {
205
- const toTable = fromAbstractSqlModel.tables[`${key}$${toVersion}`];
206
- if (!toTable) {
207
- throw new Error(`Missing translation for: '${key}'`);
208
- }
209
- table.modifyFields = _.cloneDeep(toTable.modifyFields ?? toTable.fields);
210
- table.definition = {
211
- abstractSql: ['Resource', `${key}$${toVersion}`],
212
- };
213
- }
214
- // Also alias the current version so it can be explicitly referenced
215
- fromAbstractSqlModel.tables[`${key}$${fromVersion}`] = table;
216
- }
217
-
218
- return resourceRenames;
219
- };