@sonisoft/now-sdk-ext-core 2.2.1 → 2.5.1
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/dist/comm/http/RequestHandler.js +7 -11
- package/dist/comm/http/RequestHandler.js.map +1 -1
- package/dist/comm/http/TableAPIRequest.js +3 -10
- package/dist/comm/http/TableAPIRequest.js.map +1 -1
- package/dist/index.d.ts +28 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -1
- package/dist/sn/BackgroundScriptExecutor.d.ts +34 -0
- package/dist/sn/BackgroundScriptExecutor.js +96 -0
- package/dist/sn/BackgroundScriptExecutor.js.map +1 -1
- package/dist/sn/aggregate/AggregateModels.d.ts +89 -0
- package/dist/sn/aggregate/AggregateModels.js +5 -0
- package/dist/sn/aggregate/AggregateModels.js.map +1 -0
- package/dist/sn/aggregate/AggregateQuery.d.ts +50 -0
- package/dist/sn/aggregate/AggregateQuery.js +144 -0
- package/dist/sn/aggregate/AggregateQuery.js.map +1 -0
- package/dist/sn/application/ApplicationManager.d.ts +58 -1
- package/dist/sn/application/ApplicationManager.js +202 -11
- package/dist/sn/application/ApplicationManager.js.map +1 -1
- package/dist/sn/application/StoreApplicationModels.d.ts +99 -0
- package/dist/sn/application/StoreApplicationModels.js +2 -0
- package/dist/sn/application/StoreApplicationModels.js.map +1 -0
- package/dist/sn/attachment/AttachmentManager.d.ts +32 -0
- package/dist/sn/attachment/AttachmentManager.js +87 -0
- package/dist/sn/attachment/AttachmentManager.js.map +1 -0
- package/dist/sn/attachment/AttachmentModels.d.ts +28 -0
- package/dist/sn/attachment/AttachmentModels.js +2 -0
- package/dist/sn/attachment/AttachmentModels.js.map +1 -0
- package/dist/sn/batch/BatchModels.d.ts +90 -0
- package/dist/sn/batch/BatchModels.js +5 -0
- package/dist/sn/batch/BatchModels.js.map +1 -0
- package/dist/sn/batch/BatchOperations.d.ts +40 -0
- package/dist/sn/batch/BatchOperations.js +169 -0
- package/dist/sn/batch/BatchOperations.js.map +1 -0
- package/dist/sn/batch/QueryBatchModels.d.ts +93 -0
- package/dist/sn/batch/QueryBatchModels.js +5 -0
- package/dist/sn/batch/QueryBatchModels.js.map +1 -0
- package/dist/sn/batch/QueryBatchOperations.d.ts +41 -0
- package/dist/sn/batch/QueryBatchOperations.js +195 -0
- package/dist/sn/batch/QueryBatchOperations.js.map +1 -0
- package/dist/sn/cmdb/CMDBModels.d.ts +141 -0
- package/dist/sn/cmdb/CMDBModels.js +5 -0
- package/dist/sn/cmdb/CMDBModels.js.map +1 -0
- package/dist/sn/cmdb/CMDBRelationships.d.ts +60 -0
- package/dist/sn/cmdb/CMDBRelationships.js +286 -0
- package/dist/sn/cmdb/CMDBRelationships.js.map +1 -0
- package/dist/sn/discovery/DiscoveryModels.d.ts +160 -0
- package/dist/sn/discovery/DiscoveryModels.js +5 -0
- package/dist/sn/discovery/DiscoveryModels.js.map +1 -0
- package/dist/sn/discovery/InstanceDiscovery.d.ts +45 -0
- package/dist/sn/discovery/InstanceDiscovery.js +163 -0
- package/dist/sn/discovery/InstanceDiscovery.js.map +1 -0
- package/dist/sn/health/HealthModels.d.ts +124 -0
- package/dist/sn/health/HealthModels.js +5 -0
- package/dist/sn/health/HealthModels.js.map +1 -0
- package/dist/sn/health/InstanceHealth.d.ts +58 -0
- package/dist/sn/health/InstanceHealth.js +221 -0
- package/dist/sn/health/InstanceHealth.js.map +1 -0
- package/dist/sn/schema/SchemaDiscovery.d.ts +68 -0
- package/dist/sn/schema/SchemaDiscovery.js +346 -0
- package/dist/sn/schema/SchemaDiscovery.js.map +1 -0
- package/dist/sn/schema/SchemaModels.d.ts +277 -0
- package/dist/sn/schema/SchemaModels.js +5 -0
- package/dist/sn/schema/SchemaModels.js.map +1 -0
- package/dist/sn/scope/ScopeManager.d.ts +51 -0
- package/dist/sn/scope/ScopeManager.js +182 -0
- package/dist/sn/scope/ScopeManager.js.map +1 -0
- package/dist/sn/scope/ScopeModels.d.ts +72 -0
- package/dist/sn/scope/ScopeModels.js +5 -0
- package/dist/sn/scope/ScopeModels.js.map +1 -0
- package/dist/sn/scriptsync/ScriptSync.d.ts +38 -0
- package/dist/sn/scriptsync/ScriptSync.js +266 -0
- package/dist/sn/scriptsync/ScriptSync.js.map +1 -0
- package/dist/sn/scriptsync/ScriptSyncModels.d.ts +55 -0
- package/dist/sn/scriptsync/ScriptSyncModels.js +8 -0
- package/dist/sn/scriptsync/ScriptSyncModels.js.map +1 -0
- package/dist/sn/scriptsync/ScriptWatcher.d.ts +30 -0
- package/dist/sn/scriptsync/ScriptWatcher.js +74 -0
- package/dist/sn/scriptsync/ScriptWatcher.js.map +1 -0
- package/dist/sn/task/TaskModels.d.ts +85 -0
- package/dist/sn/task/TaskModels.js +5 -0
- package/dist/sn/task/TaskModels.js.map +1 -0
- package/dist/sn/task/TaskOperations.d.ts +63 -0
- package/dist/sn/task/TaskOperations.js +195 -0
- package/dist/sn/task/TaskOperations.js.map +1 -0
- package/dist/sn/updateset/UpdateSetManager.d.ts +78 -0
- package/dist/sn/updateset/UpdateSetManager.js +396 -0
- package/dist/sn/updateset/UpdateSetManager.js.map +1 -0
- package/dist/sn/updateset/UpdateSetModels.d.ts +188 -0
- package/dist/sn/updateset/UpdateSetModels.js +5 -0
- package/dist/sn/updateset/UpdateSetModels.js.map +1 -0
- package/dist/sn/workflow/WorkflowManager.d.ts +81 -0
- package/dist/sn/workflow/WorkflowManager.js +388 -0
- package/dist/sn/workflow/WorkflowManager.js.map +1 -0
- package/dist/sn/workflow/WorkflowModels.d.ts +242 -0
- package/dist/sn/workflow/WorkflowModels.js +5 -0
- package/dist/sn/workflow/WorkflowModels.js.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ServiceNowInstance } from "../ServiceNowInstance.js";
|
|
2
|
+
import { AttachmentRecord, UploadAttachmentOptions, ListAttachmentsOptions } from './AttachmentModels.js';
|
|
3
|
+
/**
|
|
4
|
+
* AttachmentManager provides methods for uploading, listing, and retrieving
|
|
5
|
+
* attachments on ServiceNow records via the Attachment API.
|
|
6
|
+
*/
|
|
7
|
+
export declare class AttachmentManager {
|
|
8
|
+
static readonly ATTACHMENT_API_PATH = "/api/now/attachment";
|
|
9
|
+
static readonly ATTACHMENT_FILE_API_PATH = "/api/now/attachment/file";
|
|
10
|
+
static readonly ATTACHMENT_TABLE = "sys_attachment";
|
|
11
|
+
private _logger;
|
|
12
|
+
private _req;
|
|
13
|
+
private _tableAPI;
|
|
14
|
+
private _instance;
|
|
15
|
+
constructor(instance: ServiceNowInstance);
|
|
16
|
+
/**
|
|
17
|
+
* Upload an attachment to a ServiceNow record.
|
|
18
|
+
* POST to /api/now/attachment/file with multipart/form-data headers
|
|
19
|
+
* and query params for table_name, table_sys_id, and file_name.
|
|
20
|
+
*/
|
|
21
|
+
uploadAttachment(options: UploadAttachmentOptions): Promise<AttachmentRecord>;
|
|
22
|
+
/**
|
|
23
|
+
* List attachments for a specific record.
|
|
24
|
+
* Uses TableAPIRequest.get on sys_attachment with encoded query.
|
|
25
|
+
*/
|
|
26
|
+
listAttachments(options: ListAttachmentsOptions): Promise<AttachmentRecord[]>;
|
|
27
|
+
/**
|
|
28
|
+
* Get a single attachment record by sys_id.
|
|
29
|
+
* Uses TableAPIRequest.get on sys_attachment with sys_id filter.
|
|
30
|
+
*/
|
|
31
|
+
getAttachment(sysId: string): Promise<AttachmentRecord>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Logger } from "../../util/Logger.js";
|
|
2
|
+
import { ServiceNowRequest } from "../../comm/http/ServiceNowRequest.js";
|
|
3
|
+
import { TableAPIRequest } from "../../comm/http/TableAPIRequest.js";
|
|
4
|
+
/**
|
|
5
|
+
* AttachmentManager provides methods for uploading, listing, and retrieving
|
|
6
|
+
* attachments on ServiceNow records via the Attachment API.
|
|
7
|
+
*/
|
|
8
|
+
export class AttachmentManager {
|
|
9
|
+
static ATTACHMENT_API_PATH = '/api/now/attachment';
|
|
10
|
+
static ATTACHMENT_FILE_API_PATH = '/api/now/attachment/file';
|
|
11
|
+
static ATTACHMENT_TABLE = 'sys_attachment';
|
|
12
|
+
_logger = new Logger("AttachmentManager");
|
|
13
|
+
_req;
|
|
14
|
+
_tableAPI;
|
|
15
|
+
_instance;
|
|
16
|
+
constructor(instance) {
|
|
17
|
+
this._instance = instance;
|
|
18
|
+
this._req = new ServiceNowRequest(instance);
|
|
19
|
+
this._tableAPI = new TableAPIRequest(instance);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Upload an attachment to a ServiceNow record.
|
|
23
|
+
* POST to /api/now/attachment/file with multipart/form-data headers
|
|
24
|
+
* and query params for table_name, table_sys_id, and file_name.
|
|
25
|
+
*/
|
|
26
|
+
async uploadAttachment(options) {
|
|
27
|
+
const { tableName, recordSysId, fileName, contentType, data } = options;
|
|
28
|
+
this._logger.info(`Uploading attachment '${fileName}' to ${tableName}/${recordSysId}`);
|
|
29
|
+
const query = {
|
|
30
|
+
table_name: tableName,
|
|
31
|
+
table_sys_id: recordSysId,
|
|
32
|
+
file_name: fileName
|
|
33
|
+
};
|
|
34
|
+
const request = {
|
|
35
|
+
path: AttachmentManager.ATTACHMENT_FILE_API_PATH,
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: {
|
|
38
|
+
'Content-Type': contentType,
|
|
39
|
+
'Accept': 'application/json'
|
|
40
|
+
},
|
|
41
|
+
query: query,
|
|
42
|
+
body: data
|
|
43
|
+
};
|
|
44
|
+
const response = await this._req.post(request);
|
|
45
|
+
if (response && (response.status === 200 || response.status === 201) && response.bodyObject?.result) {
|
|
46
|
+
this._logger.info(`Successfully uploaded attachment '${fileName}' with sys_id: ${response.bodyObject.result.sys_id}`);
|
|
47
|
+
return response.bodyObject.result;
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`Failed to upload attachment '${fileName}' to ${tableName}/${recordSysId}. Status: ${response?.status ?? 'unknown'}`);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* List attachments for a specific record.
|
|
53
|
+
* Uses TableAPIRequest.get on sys_attachment with encoded query.
|
|
54
|
+
*/
|
|
55
|
+
async listAttachments(options) {
|
|
56
|
+
const { tableName, recordSysId, limit = 100 } = options;
|
|
57
|
+
this._logger.info(`Listing attachments for ${tableName}/${recordSysId}`);
|
|
58
|
+
const query = {
|
|
59
|
+
sysparm_query: `table_name=${tableName}^table_sys_id=${recordSysId}`,
|
|
60
|
+
sysparm_limit: limit
|
|
61
|
+
};
|
|
62
|
+
const response = await this._tableAPI.get(AttachmentManager.ATTACHMENT_TABLE, query);
|
|
63
|
+
if (response && response.status === 200 && response.bodyObject?.result) {
|
|
64
|
+
this._logger.info(`Found ${response.bodyObject.result.length} attachments`);
|
|
65
|
+
return response.bodyObject.result;
|
|
66
|
+
}
|
|
67
|
+
throw new Error(`Failed to list attachments for ${tableName}/${recordSysId}. Status: ${response?.status ?? 'unknown'}`);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Get a single attachment record by sys_id.
|
|
71
|
+
* Uses TableAPIRequest.get on sys_attachment with sys_id filter.
|
|
72
|
+
*/
|
|
73
|
+
async getAttachment(sysId) {
|
|
74
|
+
this._logger.info(`Getting attachment with sys_id: ${sysId}`);
|
|
75
|
+
const query = {
|
|
76
|
+
sysparm_query: `sys_id=${sysId}`,
|
|
77
|
+
sysparm_limit: 1
|
|
78
|
+
};
|
|
79
|
+
const response = await this._tableAPI.get(AttachmentManager.ATTACHMENT_TABLE, query);
|
|
80
|
+
if (response && response.status === 200 && response.bodyObject?.result && response.bodyObject.result.length > 0) {
|
|
81
|
+
this._logger.info(`Found attachment: ${response.bodyObject.result[0].file_name}`);
|
|
82
|
+
return response.bodyObject.result[0];
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`Attachment with sys_id '${sysId}' not found. Status: ${response?.status ?? 'unknown'}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=AttachmentMan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sys_id: string;
|
|
3
|
+
file_name: string;
|
|
4
|
+
table_name: string;
|
|
5
|
+
table_sys_id: string;
|
|
6
|
+
content_type: string;
|
|
7
|
+
size_bytes?: string;
|
|
8
|
+
sys_created_on?: string;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
export interface UploadAttachmentOptions {
|
|
12
|
+
tableName: string;
|
|
13
|
+
recordSysId: string;
|
|
14
|
+
fileName: string;
|
|
15
|
+
contentType: string;
|
|
16
|
+
data: Buffer | string;
|
|
17
|
+
}
|
|
18
|
+
export interface ListAttachmentsOptions {
|
|
19
|
+
tableName: string;
|
|
20
|
+
recordSysId: string;
|
|
21
|
+
limit?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface AttachmentResponse {
|
|
24
|
+
result: AttachmentRecord[];
|
|
25
|
+
}
|
|
26
|
+
export interface AttachmentSingleResponse {
|
|
27
|
+
result: AttachmentRecord;
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AttachmentModels.js","sourceRoot":"","sources":["../../../src/sn/attachment/AttachmentModels.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Models for batch operations against ServiceNow tables.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* A single create operation within a batch.
|
|
6
|
+
*/
|
|
7
|
+
export interface BatchCreateOperation {
|
|
8
|
+
/** The target table name (e.g., "incident", "sys_user") */
|
|
9
|
+
table: string;
|
|
10
|
+
/** The field data to create the record with */
|
|
11
|
+
data: Record<string, unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Optional key to save the resulting sys_id under.
|
|
14
|
+
* Other operations can reference this via ${saveAs} in their data values.
|
|
15
|
+
*/
|
|
16
|
+
saveAs?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Options for a batch create operation.
|
|
20
|
+
*/
|
|
21
|
+
export interface BatchCreateOptions {
|
|
22
|
+
/** The ordered list of create operations to execute */
|
|
23
|
+
operations: BatchCreateOperation[];
|
|
24
|
+
/**
|
|
25
|
+
* When true (default), stops on first error (transactional behavior).
|
|
26
|
+
* When false, continues past errors.
|
|
27
|
+
*/
|
|
28
|
+
transaction?: boolean;
|
|
29
|
+
/** Optional progress callback invoked after each operation */
|
|
30
|
+
onProgress?: (message: string) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Result of a batch create operation.
|
|
34
|
+
*/
|
|
35
|
+
export interface BatchCreateResult {
|
|
36
|
+
/** True if all operations completed without error */
|
|
37
|
+
success: boolean;
|
|
38
|
+
/** Number of records successfully created */
|
|
39
|
+
createdCount: number;
|
|
40
|
+
/** Map of saveAs keys to their created sys_ids */
|
|
41
|
+
sysIds: Record<string, string>;
|
|
42
|
+
/** Details of any errors encountered */
|
|
43
|
+
errors: Array<{
|
|
44
|
+
operationIndex: number;
|
|
45
|
+
table: string;
|
|
46
|
+
error: string;
|
|
47
|
+
}>;
|
|
48
|
+
/** Total execution time in milliseconds */
|
|
49
|
+
executionTimeMs: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A single update operation within a batch.
|
|
53
|
+
*/
|
|
54
|
+
export interface BatchUpdateOperation {
|
|
55
|
+
/** The target table name */
|
|
56
|
+
table: string;
|
|
57
|
+
/** The sys_id of the record to update */
|
|
58
|
+
sysId: string;
|
|
59
|
+
/** The field data to update */
|
|
60
|
+
data: Record<string, unknown>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Options for a batch update operation.
|
|
64
|
+
*/
|
|
65
|
+
export interface BatchUpdateOptions {
|
|
66
|
+
/** The ordered list of update operations to execute */
|
|
67
|
+
updates: BatchUpdateOperation[];
|
|
68
|
+
/** When true, stops on first error. Defaults to false. */
|
|
69
|
+
stopOnError?: boolean;
|
|
70
|
+
/** Optional progress callback invoked after each operation */
|
|
71
|
+
onProgress?: (message: string) => void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Result of a batch update operation.
|
|
75
|
+
*/
|
|
76
|
+
export interface BatchUpdateResult {
|
|
77
|
+
/** True if all updates completed without error */
|
|
78
|
+
success: boolean;
|
|
79
|
+
/** Number of records successfully updated */
|
|
80
|
+
updatedCount: number;
|
|
81
|
+
/** Details of any errors encountered */
|
|
82
|
+
errors: Array<{
|
|
83
|
+
updateIndex: number;
|
|
84
|
+
table: string;
|
|
85
|
+
sysId: string;
|
|
86
|
+
error: string;
|
|
87
|
+
}>;
|
|
88
|
+
/** Total execution time in milliseconds */
|
|
89
|
+
executionTimeMs: number;
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BatchModels.js","sourceRoot":"","sources":["../../../src/sn/batch/BatchModels.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ServiceNowInstance } from "../ServiceNowInstance.js";
|
|
2
|
+
import { BatchCreateOptions, BatchCreateResult, BatchUpdateOptions, BatchUpdateResult } from './BatchModels.js';
|
|
3
|
+
/**
|
|
4
|
+
* Provides batch create and update operations against ServiceNow tables.
|
|
5
|
+
* Operations are executed sequentially, with optional transactional (stop-on-error) behavior
|
|
6
|
+
* and variable reference resolution between operations.
|
|
7
|
+
*/
|
|
8
|
+
export declare class BatchOperations {
|
|
9
|
+
private _logger;
|
|
10
|
+
private _req;
|
|
11
|
+
private _tableAPI;
|
|
12
|
+
private _instance;
|
|
13
|
+
constructor(instance: ServiceNowInstance);
|
|
14
|
+
/**
|
|
15
|
+
* Execute a batch of create operations sequentially.
|
|
16
|
+
* Supports variable references: if an operation has saveAs="myKey", later operations
|
|
17
|
+
* can reference ${myKey} in their data values to substitute the created sys_id.
|
|
18
|
+
*
|
|
19
|
+
* @param options Batch create options including operations list and transaction flag
|
|
20
|
+
* @returns BatchCreateResult with counts, sys_ids, errors, and timing
|
|
21
|
+
*/
|
|
22
|
+
batchCreate(options: BatchCreateOptions): Promise<BatchCreateResult>;
|
|
23
|
+
/**
|
|
24
|
+
* Execute a batch of update operations sequentially.
|
|
25
|
+
*
|
|
26
|
+
* @param options Batch update options including updates list and stopOnError flag
|
|
27
|
+
* @returns BatchUpdateResult with counts, errors, and timing
|
|
28
|
+
*/
|
|
29
|
+
batchUpdate(options: BatchUpdateOptions): Promise<BatchUpdateResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Resolve ${variable} references in data values using the sysIds map.
|
|
32
|
+
* Serializes the data to JSON, replaces all ${key} patterns with the
|
|
33
|
+
* corresponding sys_id value, then deserializes back.
|
|
34
|
+
*
|
|
35
|
+
* @param data The record data that may contain variable references
|
|
36
|
+
* @param sysIds Map of variable names to sys_id values
|
|
37
|
+
* @returns A new data object with all references resolved
|
|
38
|
+
*/
|
|
39
|
+
private resolveVariableReferences;
|
|
40
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Logger } from "../../util/Logger.js";
|
|
2
|
+
import { ServiceNowRequest } from "../../comm/http/ServiceNowRequest.js";
|
|
3
|
+
import { TableAPIRequest } from "../../comm/http/TableAPIRequest.js";
|
|
4
|
+
/**
|
|
5
|
+
* Provides batch create and update operations against ServiceNow tables.
|
|
6
|
+
* Operations are executed sequentially, with optional transactional (stop-on-error) behavior
|
|
7
|
+
* and variable reference resolution between operations.
|
|
8
|
+
*/
|
|
9
|
+
export class BatchOperations {
|
|
10
|
+
_logger = new Logger("BatchOperations");
|
|
11
|
+
_req;
|
|
12
|
+
_tableAPI;
|
|
13
|
+
_instance;
|
|
14
|
+
constructor(instance) {
|
|
15
|
+
this._instance = instance;
|
|
16
|
+
this._req = new ServiceNowRequest(instance);
|
|
17
|
+
this._tableAPI = new TableAPIRequest(instance);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Execute a batch of create operations sequentially.
|
|
21
|
+
* Supports variable references: if an operation has saveAs="myKey", later operations
|
|
22
|
+
* can reference ${myKey} in their data values to substitute the created sys_id.
|
|
23
|
+
*
|
|
24
|
+
* @param options Batch create options including operations list and transaction flag
|
|
25
|
+
* @returns BatchCreateResult with counts, sys_ids, errors, and timing
|
|
26
|
+
*/
|
|
27
|
+
async batchCreate(options) {
|
|
28
|
+
const startTime = Date.now();
|
|
29
|
+
const transaction = options.transaction !== undefined ? options.transaction : true;
|
|
30
|
+
const sysIds = {};
|
|
31
|
+
const errors = [];
|
|
32
|
+
let createdCount = 0;
|
|
33
|
+
this._logger.info(`Starting batch create with ${options.operations.length} operations (transaction=${transaction})`);
|
|
34
|
+
for (let i = 0; i < options.operations.length; i++) {
|
|
35
|
+
const op = options.operations[i];
|
|
36
|
+
try {
|
|
37
|
+
// Resolve variable references in the data using previously saved sys_ids
|
|
38
|
+
const resolvedData = this.resolveVariableReferences(op.data, sysIds);
|
|
39
|
+
if (options.onProgress) {
|
|
40
|
+
options.onProgress(`Creating record ${i + 1}/${options.operations.length} in table '${op.table}'`);
|
|
41
|
+
}
|
|
42
|
+
this._logger.info(`Batch create [${i + 1}/${options.operations.length}]: POST to '${op.table}'`);
|
|
43
|
+
const response = await this._tableAPI.post(op.table, {}, resolvedData);
|
|
44
|
+
if (response && (response.status === 200 || response.status === 201) && response.bodyObject?.result?.sys_id) {
|
|
45
|
+
const createdSysId = response.bodyObject.result.sys_id;
|
|
46
|
+
createdCount++;
|
|
47
|
+
if (op.saveAs) {
|
|
48
|
+
sysIds[op.saveAs] = createdSysId;
|
|
49
|
+
}
|
|
50
|
+
this._logger.info(`Batch create [${i + 1}]: Created sys_id=${createdSysId} in '${op.table}'`);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const errorMsg = `Failed to create record in '${op.table}'. Status: ${response?.status ?? 'unknown'}`;
|
|
54
|
+
this._logger.error(errorMsg);
|
|
55
|
+
errors.push({ operationIndex: i, table: op.table, error: errorMsg });
|
|
56
|
+
if (transaction) {
|
|
57
|
+
if (options.onProgress) {
|
|
58
|
+
options.onProgress(`Stopping batch: error at operation ${i + 1}`);
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
66
|
+
this._logger.error(`Batch create [${i + 1}] exception: ${errorMsg}`);
|
|
67
|
+
errors.push({ operationIndex: i, table: op.table, error: errorMsg });
|
|
68
|
+
if (transaction) {
|
|
69
|
+
if (options.onProgress) {
|
|
70
|
+
options.onProgress(`Stopping batch: error at operation ${i + 1}`);
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const executionTimeMs = Date.now() - startTime;
|
|
77
|
+
if (options.onProgress) {
|
|
78
|
+
options.onProgress(`Batch create complete: ${createdCount} created, ${errors.length} errors`);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
success: errors.length === 0,
|
|
82
|
+
createdCount,
|
|
83
|
+
sysIds,
|
|
84
|
+
errors,
|
|
85
|
+
executionTimeMs
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Execute a batch of update operations sequentially.
|
|
90
|
+
*
|
|
91
|
+
* @param options Batch update options including updates list and stopOnError flag
|
|
92
|
+
* @returns BatchUpdateResult with counts, errors, and timing
|
|
93
|
+
*/
|
|
94
|
+
async batchUpdate(options) {
|
|
95
|
+
const startTime = Date.now();
|
|
96
|
+
const stopOnError = options.stopOnError ?? false;
|
|
97
|
+
const errors = [];
|
|
98
|
+
let updatedCount = 0;
|
|
99
|
+
this._logger.info(`Starting batch update with ${options.updates.length} updates (stopOnError=${stopOnError})`);
|
|
100
|
+
for (let i = 0; i < options.updates.length; i++) {
|
|
101
|
+
const update = options.updates[i];
|
|
102
|
+
try {
|
|
103
|
+
if (options.onProgress) {
|
|
104
|
+
options.onProgress(`Updating record ${i + 1}/${options.updates.length} in table '${update.table}'`);
|
|
105
|
+
}
|
|
106
|
+
this._logger.info(`Batch update [${i + 1}/${options.updates.length}]: PUT to '${update.table}/${update.sysId}'`);
|
|
107
|
+
const response = await this._tableAPI.put(update.table, update.sysId, update.data);
|
|
108
|
+
if (response && response.status === 200 && response.bodyObject?.result) {
|
|
109
|
+
updatedCount++;
|
|
110
|
+
this._logger.info(`Batch update [${i + 1}]: Updated '${update.table}/${update.sysId}'`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const errorMsg = `Failed to update record '${update.sysId}' in '${update.table}'. Status: ${response?.status ?? 'unknown'}`;
|
|
114
|
+
this._logger.error(errorMsg);
|
|
115
|
+
errors.push({ updateIndex: i, table: update.table, sysId: update.sysId, error: errorMsg });
|
|
116
|
+
if (stopOnError) {
|
|
117
|
+
if (options.onProgress) {
|
|
118
|
+
options.onProgress(`Stopping batch: error at update ${i + 1}`);
|
|
119
|
+
}
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
126
|
+
this._logger.error(`Batch update [${i + 1}] exception: ${errorMsg}`);
|
|
127
|
+
errors.push({ updateIndex: i, table: update.table, sysId: update.sysId, error: errorMsg });
|
|
128
|
+
if (stopOnError) {
|
|
129
|
+
if (options.onProgress) {
|
|
130
|
+
options.onProgress(`Stopping batch: error at update ${i + 1}`);
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const executionTimeMs = Date.now() - startTime;
|
|
137
|
+
if (options.onProgress) {
|
|
138
|
+
options.onProgress(`Batch update complete: ${updatedCount} updated, ${errors.length} errors`);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
success: errors.length === 0,
|
|
142
|
+
updatedCount,
|
|
143
|
+
errors,
|
|
144
|
+
executionTimeMs
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Resolve ${variable} references in data values using the sysIds map.
|
|
149
|
+
* Serializes the data to JSON, replaces all ${key} patterns with the
|
|
150
|
+
* corresponding sys_id value, then deserializes back.
|
|
151
|
+
*
|
|
152
|
+
* @param data The record data that may contain variable references
|
|
153
|
+
* @param sysIds Map of variable names to sys_id values
|
|
154
|
+
* @returns A new data object with all references resolved
|
|
155
|
+
*/
|
|
156
|
+
resolveVariableReferences(data, sysIds) {
|
|
157
|
+
if (Object.keys(sysIds).length === 0) {
|
|
158
|
+
return data;
|
|
159
|
+
}
|
|
160
|
+
let json = JSON.stringify(data);
|
|
161
|
+
for (const [key, value] of Object.entries(sysIds)) {
|
|
162
|
+
// Use a global regex to replace all occurrences of ${key}
|
|
163
|
+
const pattern = new RegExp('\\$\\{' + key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\}', 'g');
|
|
164
|
+
json = json.replace(pattern, value);
|
|
165
|
+
}
|
|
166
|
+
return JSON.parse(json);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=BatchOperations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BatchOperations.js","sourceRoot":"","sources":["../../../src/sn/batch/BatchOperations.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AASlE;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAChB,OAAO,GAAW,IAAI,MAAM,CAAC,iBAAiB,CAAC,CAAC;IAChD,IAAI,CAAoB;IACxB,SAAS,CAAkB;IAC3B,SAAS,CAAqB;IAEtC,YAAmB,QAA4B;QAC3C,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAC5C,I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eAAe;SAClB,CAAC;IACN,CAAC;IAED;;;;;;;;OAQG;IACK,yBAAyB,CAC7B,IAA6B,EAC7B,MAA8B;QAE9B,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnC,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,IAAI,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAEhC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAChD,0DAA0D;YAC1D,MAAM,OAAO,GAAG,IAAI,MAAM,CAAC,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC;YAC/F,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACxC,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAA4B,CAAC;IACvD,CAAC;CACJ"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for a query-based batch update operation.
|
|
3
|
+
*/
|
|
4
|
+
export interface QueryUpdateOptions {
|
|
5
|
+
/** The table to update records in (required) */
|
|
6
|
+
table: string;
|
|
7
|
+
/** Encoded query to find matching records (required) */
|
|
8
|
+
query: string;
|
|
9
|
+
/** The field data to apply to all matching records (required) */
|
|
10
|
+
data: Record<string, unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* When true, actually executes the update.
|
|
13
|
+
* When false (default), performs a dry run and returns match count only.
|
|
14
|
+
*/
|
|
15
|
+
confirm?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Maximum number of records to update.
|
|
18
|
+
* Defaults to 200. Maximum allowed: 10000.
|
|
19
|
+
*/
|
|
20
|
+
limit?: number;
|
|
21
|
+
/** Optional progress callback invoked periodically */
|
|
22
|
+
onProgress?: (message: string) => void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for a query-based batch delete operation.
|
|
26
|
+
*/
|
|
27
|
+
export interface QueryDeleteOptions {
|
|
28
|
+
/** The table to delete records from (required) */
|
|
29
|
+
table: string;
|
|
30
|
+
/** Encoded query to find matching records (required) */
|
|
31
|
+
query: string;
|
|
32
|
+
/**
|
|
33
|
+
* When true, actually executes the delete.
|
|
34
|
+
* When false (default), performs a dry run and returns match count only.
|
|
35
|
+
*/
|
|
36
|
+
confirm?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Maximum number of records to delete.
|
|
39
|
+
* Defaults to 200. Maximum allowed: 10000.
|
|
40
|
+
*/
|
|
41
|
+
limit?: number;
|
|
42
|
+
/** Optional progress callback invoked periodically */
|
|
43
|
+
onProgress?: (message: string) => void;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Result of a query-based batch update operation.
|
|
47
|
+
*/
|
|
48
|
+
export interface QueryUpdateResult {
|
|
49
|
+
/** Whether this was a dry run or live execution */
|
|
50
|
+
dryRun: boolean;
|
|
51
|
+
/** Number of records that matched the query */
|
|
52
|
+
matchCount: number;
|
|
53
|
+
/** Number of records successfully updated (0 if dry run) */
|
|
54
|
+
updatedCount: number;
|
|
55
|
+
/** True if all matched records were updated without error (or if dry run) */
|
|
56
|
+
success: boolean;
|
|
57
|
+
/** Details of any errors encountered during execution */
|
|
58
|
+
errors: Array<{
|
|
59
|
+
sysId: string;
|
|
60
|
+
error: string;
|
|
61
|
+
}>;
|
|
62
|
+
/** Total execution time in milliseconds */
|
|
63
|
+
executionTimeMs: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Result of a query-based batch delete operation.
|
|
67
|
+
*/
|
|
68
|
+
export interface QueryDeleteResult {
|
|
69
|
+
/** Whether this was a dry run or live execution */
|
|
70
|
+
dryRun: boolean;
|
|
71
|
+
/** Number of records that matched the query */
|
|
72
|
+
matchCount: number;
|
|
73
|
+
/** Number of records successfully deleted (0 if dry run) */
|
|
74
|
+
deletedCount: number;
|
|
75
|
+
/** True if all matched records were deleted without error (or if dry run) */
|
|
76
|
+
success: boolean;
|
|
77
|
+
/** Details of any errors encountered during execution */
|
|
78
|
+
errors: Array<{
|
|
79
|
+
sysId: string;
|
|
80
|
+
error: string;
|
|
81
|
+
}>;
|
|
82
|
+
/** Total execution time in milliseconds */
|
|
83
|
+
executionTimeMs: number;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Response from table query used to find matching records.
|
|
87
|
+
*/
|
|
88
|
+
export interface QueryMatchResponse {
|
|
89
|
+
result: Array<{
|
|
90
|
+
sys_id: string;
|
|
91
|
+
[key: string]: unknown;
|
|
92
|
+
}>;
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryBatchModels.js","sourceRoot":"","sources":["../../../src/sn/batch/QueryBatchModels.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,gBAAgB;AAChB,+DAA+D"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ServiceNowInstance } from "../ServiceNowInstance.js";
|
|
2
|
+
import { QueryUpdateOptions, QueryDeleteOptions, QueryUpdateResult, QueryDeleteResult } from './QueryBatchModels.js';
|
|
3
|
+
/**
|
|
4
|
+
* QueryBatchOperations provides query-based bulk update and delete operations.
|
|
5
|
+
* Records are found by encoded query, then updated or deleted in bulk.
|
|
6
|
+
* Includes a dry-run mode (default) for safe previewing before execution.
|
|
7
|
+
*/
|
|
8
|
+
export declare class QueryBatchOperations {
|
|
9
|
+
private static readonly DEFAULT_LIMIT;
|
|
10
|
+
private static readonly MAX_LIMIT;
|
|
11
|
+
private _logger;
|
|
12
|
+
private _req;
|
|
13
|
+
private _tableAPI;
|
|
14
|
+
private _instance;
|
|
15
|
+
private _headers;
|
|
16
|
+
constructor(instance: ServiceNowInstance);
|
|
17
|
+
/**
|
|
18
|
+
* Find records matching a query and update them with the provided data.
|
|
19
|
+
* Defaults to dry-run mode (confirm=false) which returns match count without executing.
|
|
20
|
+
*
|
|
21
|
+
* @param options Query update options
|
|
22
|
+
* @returns QueryUpdateResult with match count, update count, and any errors
|
|
23
|
+
* @throws Error if table, query, or data is empty
|
|
24
|
+
*/
|
|
25
|
+
queryUpdate(options: QueryUpdateOptions): Promise<QueryUpdateResult>;
|
|
26
|
+
/**
|
|
27
|
+
* Find records matching a query and delete them.
|
|
28
|
+
* Defaults to dry-run mode (confirm=false) which returns match count without executing.
|
|
29
|
+
*
|
|
30
|
+
* @param options Query delete options
|
|
31
|
+
* @returns QueryDeleteResult with match count, delete count, and any errors
|
|
32
|
+
* @throws Error if table or query is empty
|
|
33
|
+
*/
|
|
34
|
+
queryDelete(options: QueryDeleteOptions): Promise<QueryDeleteResult>;
|
|
35
|
+
/**
|
|
36
|
+
* Find records matching a query and return their sys_ids.
|
|
37
|
+
* Only fetches sys_id field for efficiency.
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
private _findMatchingRecords;
|
|
41
|
+
}
|