Skip to content

Commit b508afa

Browse files
HCK-9607: browser support (#43)
* chore: declared `lodash` as external resource * feat: allowed FE features in browser * chore: added `postinstall` hook
1 parent 2c3a9fc commit b508afa

19 files changed

+5025
-4838
lines changed

api/fe.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { generateScript } = require('../forward_engineering/generateScript');
2+
const { generateContainerScript } = require('../forward_engineering/generateContainerScript');
3+
4+
module.exports = {
5+
generateScript,
6+
generateContainerScript,
7+
};

esbuild.package.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const esbuild = require('esbuild');
44
const { clean } = require('esbuild-plugin-clean');
5+
const { copy } = require('esbuild-plugin-copy');
56
const { copyFolderFiles, addReleaseFlag } = require('@hackolade/hck-esbuild-plugins-pack');
67
const { EXCLUDED_EXTENSIONS, EXCLUDED_FILES, DEFAULT_RELEASE_FOLDER_PATH } = require('./buildConstants');
78

@@ -11,6 +12,7 @@ const RELEASE_FOLDER_PATH = path.join(DEFAULT_RELEASE_FOLDER_PATH, `${packageDat
1112
esbuild
1213
.build({
1314
entryPoints: [
15+
path.resolve(__dirname, 'api', 'fe.js'),
1416
path.resolve(__dirname, 'forward_engineering', 'api.js'),
1517
path.resolve(__dirname, 'reverse_engineering', 'api.js'),
1618
],
@@ -21,10 +23,17 @@ esbuild
2123
outdir: RELEASE_FOLDER_PATH,
2224
minify: true,
2325
logLevel: 'info',
26+
external: ['lodash'],
2427
plugins: [
2528
clean({
2629
patterns: [DEFAULT_RELEASE_FOLDER_PATH],
2730
}),
31+
copy({
32+
assets: {
33+
from: [path.join('node_modules', 'lodash', '**', '*')],
34+
to: [path.join('node_modules', 'lodash')],
35+
},
36+
}),
2837
copyFolderFiles({
2938
fromPath: __dirname,
3039
targetFolderPath: RELEASE_FOLDER_PATH,

forward_engineering/api.js

Lines changed: 7 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,10 @@
1-
const applyToInstanceHelper = require('./applyToInstance');
2-
const getIndexPolicyScript = require('./helpers/getIndexPolicyScript');
3-
const { getUniqueKeyPolicyScript } = require('./helpers/getUniqueKeyPolicyScript');
4-
const { buildAzureCLIScript } = require('./helpers/azureCLIScriptHelpers/buildAzureCLIScript');
5-
const getPartitionKey = require('./helpers/getPartitionKey');
1+
const { generateScript } = require('./generateScript');
2+
const { generateContainerScript } = require('./generateContainerScript');
3+
const { applyToInstance, testConnection } = require('./applyToInstance');
64

75
module.exports = {
8-
generateContainerScript(data, logger, callback, app) {
9-
try {
10-
const _ = app.require('lodash');
11-
const insertSamplesOption =
12-
_.get(data, 'options.additionalOptions', []).find(option => option.id === 'INCLUDE_SAMPLES') || {};
13-
const withSamples = data.options.origin !== 'ui';
14-
const samples = data.entities.map(entityId =>
15-
updateSample(
16-
JSON.parse(data.jsonData[entityId]),
17-
data.containerData[0],
18-
(data.entityData[entityId] || [])[0] || {},
19-
),
20-
);
21-
if (data.options?.targetScriptOptions?.keyword === 'containerSettingsJson') {
22-
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
23-
const scriptData = {
24-
partitionKey: getPartitionKey(_)(data.containerData),
25-
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
26-
indexingPolicy: getIndexPolicyScript(_)(data.containerData),
27-
...(withSamples && { sample: samples }),
28-
...addItems(_)(data.containerData),
29-
};
30-
const script = JSON.stringify(scriptData, null, 2);
31-
if (withSamples || !insertSamplesOption.value) {
32-
return callback(null, script);
33-
}
34-
35-
return callback(null, [
36-
{ title: 'CosmosDB script', script },
37-
{ title: 'Sample data', script: JSON.stringify(samples, null, 2) },
38-
]);
39-
}
40-
41-
const script = buildAzureCLIScript(_)({
42-
...data,
43-
});
44-
45-
if (withSamples || !insertSamplesOption.value) {
46-
return callback(null, script);
47-
}
48-
49-
return callback(null, [
50-
{ title: 'Azure CLI script', script },
51-
{ title: 'Sample data', script: JSON.stringify(samples, null, 2) },
52-
]);
53-
} catch (e) {
54-
const error = { message: e.message, stack: e.stack };
55-
logger.log('error', error, 'CosmosDB w\\ SQL API forward engineering error');
56-
callback(error);
57-
}
58-
},
59-
generateScript(data, logger, callback, app) {
60-
try {
61-
const _ = app.require('lodash');
62-
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
63-
64-
const script = {
65-
partitionKey: getPartitionKey(_)(data.containerData),
66-
indexingPolicy: getIndexPolicyScript(_)(data.containerData),
67-
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
68-
sample: updateSample(JSON.parse(data.jsonData), data.containerData[0], data.entityData[0]),
69-
...addItems(_)(data.containerData),
70-
};
71-
return callback(null, JSON.stringify(script, null, 2));
72-
} catch (e) {
73-
const error = { message: e.message, stack: e.stack };
74-
logger.log('error', error, 'CosmosDB w\\ SQL API forward engineering error');
75-
callback(error);
76-
}
77-
},
78-
applyToInstance: applyToInstanceHelper.applyToInstance,
79-
80-
testConnection: applyToInstanceHelper.testConnection,
81-
};
82-
83-
const updateSample = (sample, containerData, entityData) => {
84-
const docType = containerData?.docTypeName;
85-
86-
if (!docType) {
87-
return sample;
88-
}
89-
90-
return {
91-
...sample,
92-
[docType]: entityData.code || entityData.collectionName,
93-
};
94-
};
95-
96-
const add = (key, items, mapper) => script => {
97-
if (!items.length) {
98-
return script;
99-
}
100-
101-
return {
102-
...script,
103-
[key]: mapper(items),
104-
};
105-
};
106-
107-
const addItems = _ => containerData => {
108-
return _.flow(
109-
add('Stored Procedures', _.get(containerData, '[2].storedProcs', []), mapStoredProcs),
110-
add('User Defined Functions', _.get(containerData, '[4].udfs', []), mapUDFs),
111-
add('Triggers', _.get(containerData, '[3].triggers', []), mapTriggers),
112-
)();
113-
};
114-
115-
const mapUDFs = udfs => {
116-
return udfs.map(udf => {
117-
return {
118-
id: udf.udfID,
119-
body: udf.udfFunction,
120-
};
121-
});
122-
};
123-
124-
const mapTriggers = triggers => {
125-
return triggers.map(trigger => {
126-
return {
127-
id: trigger.triggerID,
128-
body: trigger.triggerFunction,
129-
triggerOperation: trigger.triggerOperation,
130-
triggerType: trigger.prePostTrigger === 'Pre-Trigger' ? 'Pre' : 'Post',
131-
};
132-
});
133-
};
134-
135-
const mapStoredProcs = storedProcs => {
136-
return storedProcs.map(proc => {
137-
return {
138-
id: proc.storedProcID,
139-
body: proc.storedProcFunction,
140-
};
141-
});
6+
generateScript,
7+
generateContainerScript,
8+
applyToInstance,
9+
testConnection,
14210
};

forward_engineering/applyToInstance/applyToInstanceHelper.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const { StoredProcedure, UserDefinedFunction, Trigger } = require('@azure/cosmos');
22
const setUpDocumentClient = require('../../reverse_engineering/helpers/setUpDocumentClient');
3-
const { TTL_ON_DEFAULT, TTL_ON, TTL_OFF } = require('../../shared/constants');
43

5-
const applyToInstanceHelper = _ => ({
4+
const applyToInstanceHelper = {
65
setUpDocumentClient(connectionInfo) {
76
return setUpDocumentClient(connectionInfo);
87
},
@@ -57,28 +56,6 @@ const applyToInstanceHelper = _ => ({
5756
}
5857
}, Promise.resolve());
5958
},
60-
61-
getTTL(containerData) {
62-
switch (containerData?.TTL) {
63-
case TTL_ON_DEFAULT:
64-
return -1;
65-
case TTL_ON:
66-
return _.parseInt(containerData?.TTLseconds) || -1;
67-
case TTL_OFF:
68-
default:
69-
return 0;
70-
}
71-
},
72-
73-
getContainerThroughputProps(containerData) {
74-
if (containerData?.capacityMode === 'Serverless') {
75-
return {};
76-
}
77-
if (containerData?.autopilot) {
78-
return { maxThroughput: containerData.throughput || 4000 };
79-
}
80-
return { throughput: containerData?.throughput || 400 };
81-
},
82-
});
59+
};
8360

8461
module.exports = applyToInstanceHelper;

forward_engineering/applyToInstance/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
const _ = require('lodash');
12
const reApi = require('../../reverse_engineering/api');
2-
const executeWithTimeout = require('../../reverse_engineering/helpers/executeWithTimeout');
3-
const { TTL_ON, TTL_ON_DEFAULT } = require('../../shared/constants');
43
const applyToInstanceHelper = require('./applyToInstanceHelper');
4+
const { executeWithTimeout } = require('../../shared/executeWithTimeout');
5+
const { TTL_ON, TTL_ON_DEFAULT } = require('../../shared/constants');
6+
const { getTTL } = require('../helpers/getTtl');
7+
const { getContainerThroughputProps } = require('../helpers/getContainerThroughputProps');
58

69
const createOrUpdate = async (sample, container) => {
710
try {
@@ -62,14 +65,13 @@ const updateIndexingPolicy = indexes => {
6265

6366
module.exports = {
6467
testConnection: reApi.testConnection,
68+
6569
async applyToInstance(connectionInfo, logger, callback, app) {
66-
const _ = app.require('lodash');
6770
try {
68-
const helper = applyToInstanceHelper(_);
6971
logger.progress = logger.progress || (() => {});
7072
logger.clear();
7173
logger.log('info', connectionInfo, 'Apply to instance connection settings', connectionInfo.hiddenKeys);
72-
const client = helper.setUpDocumentClient(connectionInfo);
74+
const client = applyToInstanceHelper.setUpDocumentClient(connectionInfo);
7375
const script = parseScript(connectionInfo.script);
7476
const containerData = _.get(connectionInfo, 'containerData[0]');
7577
const databaseId = _.get(containerData, 'dbId');
@@ -93,9 +95,9 @@ module.exports = {
9395
const { container, resource: containerDef } = await database.containers.createIfNotExists({
9496
id: containerId,
9597
partitionKey: script.partitionKey,
96-
...(shouldIncludeTtl && { defaultTtl: helper.getTTL(containerData) }),
98+
...(shouldIncludeTtl && { defaultTtl: getTTL(containerData) }),
9799
...(script.uniqueKeyPolicy && { uniqueKeyPolicy: script.uniqueKeyPolicy }),
98-
...helper.getContainerThroughputProps(containerData),
100+
...getContainerThroughputProps(containerData),
99101
});
100102

101103
progress('Add sample documents ...');
@@ -119,19 +121,19 @@ module.exports = {
119121
const storedProcs = _.get(script, 'Stored Procedures', []);
120122
if (storedProcs.length) {
121123
progress('Upload stored procs ...');
122-
await helper.createStoredProcs(storedProcs, container);
124+
await applyToInstanceHelper.createStoredProcs(storedProcs, container);
123125
}
124126

125127
const udfs = _.get(script, 'User Defined Functions', []);
126128
if (udfs.length) {
127129
progress('Upload user defined functions ...');
128-
await helper.createUDFs(udfs, container);
130+
await applyToInstanceHelper.createUDFs(udfs, container);
129131
}
130132

131133
const triggers = _.get(script, 'Triggers', []);
132134
if (triggers.length) {
133135
progress('Upload triggers ...');
134-
await helper.createTriggers(triggers, container);
136+
await applyToInstanceHelper.createTriggers(triggers, container);
135137
}
136138

137139
progress('Applying to instance finished');
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const _ = require('lodash');
2+
const { getIndexPolicyScript } = require('./helpers/getIndexPolicyScript');
3+
const { getPartitionKey } = require('./helpers/getPartitionKey');
4+
const { buildAzureCLIScript } = require('./helpers/azureCLIScriptHelpers/buildAzureCLIScript');
5+
const { getUniqueKeyPolicyScript } = require('./helpers/getUniqueKeyPolicyScript');
6+
const { updateSample } = require('./helpers/updateSample');
7+
const { addItems } = require('./helpers/addItems');
8+
9+
const generateContainerScript = (data, logger, callback, app) => {
10+
try {
11+
const insertSamplesOption =
12+
_.get(data, 'options.additionalOptions', []).find(option => option.id === 'INCLUDE_SAMPLES') || {};
13+
const withSamples = data.options.origin !== 'ui';
14+
const samples = data.entities.map(entityId =>
15+
updateSample(
16+
JSON.parse(data.jsonData[entityId]),
17+
data.containerData[0],
18+
(data.entityData[entityId] || [])[0] || {},
19+
),
20+
);
21+
if (data.options?.targetScriptOptions?.keyword === 'containerSettingsJson') {
22+
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
23+
const scriptData = {
24+
partitionKey: getPartitionKey(data.containerData),
25+
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
26+
indexingPolicy: getIndexPolicyScript(data.containerData),
27+
...(withSamples && { sample: samples }),
28+
...addItems(data.containerData),
29+
};
30+
const script = JSON.stringify(scriptData, null, 2);
31+
if (withSamples || !insertSamplesOption.value) {
32+
return callback(null, script);
33+
}
34+
35+
return callback(null, [
36+
{ title: 'CosmosDB script', script },
37+
{ title: 'Sample data', script: JSON.stringify(samples, null, 2) },
38+
]);
39+
}
40+
41+
const script = buildAzureCLIScript({
42+
...data,
43+
});
44+
45+
if (withSamples || !insertSamplesOption.value) {
46+
return callback(null, script);
47+
}
48+
49+
return callback(null, [
50+
{ title: 'Azure CLI script', script },
51+
{ title: 'Sample data', script: JSON.stringify(samples, null, 2) },
52+
]);
53+
} catch (e) {
54+
const error = { message: e.message, stack: e.stack };
55+
logger.log('error', error, 'CosmosDB w\\ SQL API forward engineering error');
56+
callback(error);
57+
}
58+
};
59+
60+
module.exports = {
61+
generateContainerScript,
62+
};

forward_engineering/generateScript.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const _ = require('lodash');
2+
const { getIndexPolicyScript } = require('./helpers/getIndexPolicyScript');
3+
const { getPartitionKey } = require('./helpers/getPartitionKey');
4+
const { addItems } = require('./helpers/addItems');
5+
const { getUniqueKeyPolicyScript } = require('./helpers/getUniqueKeyPolicyScript');
6+
const { updateSample } = require('./helpers/updateSample');
7+
8+
const generateScript = (data, logger, callback, app) => {
9+
try {
10+
const uniqueKeys = _.get(data.containerData, '[0].uniqueKey', []);
11+
12+
const script = {
13+
partitionKey: getPartitionKey(data.containerData),
14+
indexingPolicy: getIndexPolicyScript(data.containerData),
15+
...(uniqueKeys.length && getUniqueKeyPolicyScript(uniqueKeys)),
16+
sample: updateSample(JSON.parse(data.jsonData), data.containerData[0], data.entityData[0]),
17+
...addItems(data.containerData),
18+
};
19+
return callback(null, JSON.stringify(script, null, 2));
20+
} catch (e) {
21+
const error = { message: e.message, stack: e.stack };
22+
logger.log('error', error, 'CosmosDB w\\ SQL API forward engineering error');
23+
callback(error);
24+
}
25+
};
26+
27+
module.exports = {
28+
generateScript,
29+
};

0 commit comments

Comments
 (0)