@miso.ai/server-sdk 0.6.6-beta.2 → 0.6.6-beta.4
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.
- package/cli/index.js +2 -0
- package/cli/merge-local.js +129 -0
- package/package.json +2 -2
- package/src/version.js +1 -1
package/cli/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import upload from './upload.js';
|
|
|
7
7
|
import del from './delete.js';
|
|
8
8
|
import ids from './ids.js';
|
|
9
9
|
import transform from './transform.js';
|
|
10
|
+
import mergeLocal from './merge-local.js';
|
|
10
11
|
import status from './status.js';
|
|
11
12
|
import get from './get.js';
|
|
12
13
|
import search from './search.js';
|
|
@@ -70,6 +71,7 @@ yargs.build(yargs => {
|
|
|
70
71
|
.command(users)
|
|
71
72
|
.command(experiments)
|
|
72
73
|
.command(transform)
|
|
74
|
+
.command(mergeLocal)
|
|
73
75
|
.command(search)
|
|
74
76
|
.command(hybridSearch)
|
|
75
77
|
.version(MisoClient.version);
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import 'dotenv/config';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { createReadStream } from 'fs';
|
|
5
|
+
import { createGunzip } from 'zlib';
|
|
6
|
+
import { pipeline } from 'stream/promises';
|
|
7
|
+
import { chain } from 'stream-chain';
|
|
8
|
+
import split2 from 'split2';
|
|
9
|
+
import { stream, trimObj } from '@miso.ai/server-commons';
|
|
10
|
+
|
|
11
|
+
const PWD = process.env.PWD;
|
|
12
|
+
|
|
13
|
+
process.stdout.on('error', err => err.code == 'EPIPE' && process.exit(0));
|
|
14
|
+
|
|
15
|
+
function build(yargs) {
|
|
16
|
+
return yargs
|
|
17
|
+
.positional('file1', {
|
|
18
|
+
describe: 'First record file',
|
|
19
|
+
type: 'string',
|
|
20
|
+
demandOption: true,
|
|
21
|
+
})
|
|
22
|
+
.positional('file2', {
|
|
23
|
+
describe: 'Second record file',
|
|
24
|
+
type: 'string',
|
|
25
|
+
})
|
|
26
|
+
.option('join-mode', {
|
|
27
|
+
alias: 'm',
|
|
28
|
+
describe: 'Join mode',
|
|
29
|
+
type: 'string',
|
|
30
|
+
default: 'merge',
|
|
31
|
+
choices: ['merge', 'rewrite-meta', 'remeta'],
|
|
32
|
+
})
|
|
33
|
+
.option('join-function', {
|
|
34
|
+
alias: 'f',
|
|
35
|
+
describe: 'Join function',
|
|
36
|
+
type: 'string',
|
|
37
|
+
})
|
|
38
|
+
.option('stdin', {
|
|
39
|
+
alias: 'i',
|
|
40
|
+
describe: 'Read the first stream from stdin',
|
|
41
|
+
type: 'boolean',
|
|
42
|
+
default: false,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function run({ file1, file2, stdin, joinMode, joinFunction }) {
|
|
47
|
+
if (stdin) {
|
|
48
|
+
file2 = file1;
|
|
49
|
+
file1 = undefined;
|
|
50
|
+
} else if (!file2) {
|
|
51
|
+
throw new Error('Second file is required');
|
|
52
|
+
}
|
|
53
|
+
const stream1 = chain([
|
|
54
|
+
stdin ? process.stdin : createReadStream(file1),
|
|
55
|
+
...(file1 && file1.endsWith('.gz')) ? [createGunzip()] : [],
|
|
56
|
+
split2(JSON.parse),
|
|
57
|
+
]);
|
|
58
|
+
const stream2 = chain([
|
|
59
|
+
createReadStream(file2),
|
|
60
|
+
...(file2 && file2.endsWith('.gz')) ? [createGunzip()] : [],
|
|
61
|
+
split2(JSON.parse),
|
|
62
|
+
]);
|
|
63
|
+
await pipeline(
|
|
64
|
+
stream.joinStreams([stream1, stream2], await getJoinFunction(joinMode, joinFunction)),
|
|
65
|
+
new stream.OutputStream(),
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function getJoinFunction(joinMode, joinFunction) {
|
|
70
|
+
if (joinFunction) {
|
|
71
|
+
try {
|
|
72
|
+
const fn = await import(join(PWD, joinFunction));
|
|
73
|
+
if (!fn.default) {
|
|
74
|
+
throw new Error('Join function must have a default export');
|
|
75
|
+
}
|
|
76
|
+
return fn.default;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
console.error(`Failed to load join function ${joinFunction}:`, e);
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
switch (joinMode) {
|
|
83
|
+
case 'merge':
|
|
84
|
+
return merge;
|
|
85
|
+
case 'rewrite-meta':
|
|
86
|
+
case 'remeta':
|
|
87
|
+
return rewriteMeta;
|
|
88
|
+
default:
|
|
89
|
+
throw new Error(`Unsupported join mode: ${joinMode}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function merge(record1, record2) {
|
|
94
|
+
assertSameProductId(record1, record2);
|
|
95
|
+
return trimObj({
|
|
96
|
+
...record1,
|
|
97
|
+
...record2,
|
|
98
|
+
custom_attributes: trimObj({
|
|
99
|
+
...(record1 && record1.custom_attributes),
|
|
100
|
+
...(record2 && record2.custom_attributes),
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function rewriteMeta(record1, record2) {
|
|
106
|
+
assertSameProductId(record1, record2);
|
|
107
|
+
const { description, html, children } = record1;
|
|
108
|
+
const { description: _0, html: _1, children: _2, ...rest } = record2;
|
|
109
|
+
return trimObj({
|
|
110
|
+
...rest,
|
|
111
|
+
description,
|
|
112
|
+
html,
|
|
113
|
+
children,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function assertSameProductId(record1, record2) {
|
|
118
|
+
if (record1.product_id !== record2.product_id) {
|
|
119
|
+
throw new Error(`Product IDs do not match: ${record1.product_id} and ${record2.product_id}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default {
|
|
124
|
+
command: 'merge-local <file1> [file2]',
|
|
125
|
+
alias: ['patch-local'],
|
|
126
|
+
description: 'Apply patch records from file onto input record stream',
|
|
127
|
+
builder: build,
|
|
128
|
+
handler: run,
|
|
129
|
+
};
|
package/package.json
CHANGED
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"simonpai <simon.pai@askmiso.com>"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@miso.ai/server-commons": "0.6.6-beta.
|
|
19
|
+
"@miso.ai/server-commons": "0.6.6-beta.4",
|
|
20
20
|
"axios": "^1.6.2",
|
|
21
21
|
"axios-retry": "^4.5.0",
|
|
22
22
|
"dotenv": "^16.0.1",
|
|
23
23
|
"split2": "^4.1.0",
|
|
24
24
|
"yargs": "^17.5.1"
|
|
25
25
|
},
|
|
26
|
-
"version": "0.6.6-beta.
|
|
26
|
+
"version": "0.6.6-beta.4"
|
|
27
27
|
}
|
package/src/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '0.6.6-beta.
|
|
1
|
+
export default '0.6.6-beta.4';
|