-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (182 loc) · 7.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair
/* eslint-disable sonarjs/no-dead-store */
import mssql from '@cityssm/mssql-multi-pool';
import { getTables, getViews } from '@cityssm/mssql-system-catalog';
import Debug from 'debug';
import { buildColumnLists } from './helpers.js';
const debug = Debug('mssql-query-replicate:index');
/**
* Replicates the results of a SQL query from a source database query
* to a destination database table.
* @param sourceConfiguration - Source database configuration.
* @param destinationConfiguration - Destination database configuration.
* @returns the status of the replication.
*/
export async function replicateQueryRecordset(sourceConfiguration, destinationConfiguration) {
let errorStep = 'source:connect';
let destinationRows = 0;
try {
/*
* Connect to source database
*/
debug('Connecting to source database...');
const sourcePool = await mssql.connect(sourceConfiguration.sourceDatabase);
debug('Connected successfully.');
/*
* Construct request
*/
errorStep = 'source:request';
let sourceRequest = sourcePool.request();
const sourceSql = sourceConfiguration.sourceType === 'sql'
? sourceConfiguration.sourceSql
: `select * from ${sourceConfiguration.sourceTableName}`;
if (sourceConfiguration.sourceType === 'sql' &&
sourceConfiguration.sourceParameters !== undefined) {
for (const [parameterName, parameterValue] of Object.entries(sourceConfiguration.sourceParameters)) {
sourceRequest = sourceRequest.input(parameterName, parameterValue);
}
}
/*
* Query data
*/
errorStep = 'source:query';
debug('Retrieving source data...');
const sourceResult = (await sourceRequest.query(sourceSql));
debug(`Source data retrieved, ${sourceResult.recordset.length} rows.`);
/*
* Build destination SQL statements
*/
errorStep = 'destination:buildSql';
debug('Building column lists...');
const columnLists = buildColumnLists(Object.values(sourceResult.recordset.columns));
debug('Building destination create statement...');
const destinationCreateSql = `create table ${destinationConfiguration.destinationTableName} (${columnLists.create})`;
debug(`Destination create statement:\n${destinationCreateSql}`);
debug('Building destination insert statement...');
const destinationInsertSql = `insert into ${destinationConfiguration.destinationTableName} (${columnLists.insert}) values (${columnLists.parameters})`;
debug(`Destination insert statement:\n${destinationInsertSql}`);
/*
* Connect to destination database
*/
errorStep = 'destination:connect';
debug('Connecting to destination database...');
const destinationPool = await mssql.connect(destinationConfiguration.destinationDatabase);
debug('Connected successfully.');
/*
* Create the table
*/
errorStep = 'destination:createTable';
debug('Creating the destination table...');
await destinationPool.request().query(destinationCreateSql);
debug('Destination table created successfully.');
/*
* Insert the data
*/
errorStep = 'destination:insert';
for (const row of sourceResult.recordset) {
let destinationRequest = destinationPool.request();
for (const [columnIndex, dataValue] of Object.values(row).entries()) {
destinationRequest = destinationRequest.input(columnIndex.toString(), dataValue);
}
await destinationRequest.query(destinationInsertSql);
destinationRows++;
}
}
catch (error) {
debug(error);
return {
success: false,
destinationRows,
errorStep,
errorMessage: error.toString()
};
}
return {
success: true,
destinationRows,
destinationTableName: destinationConfiguration.destinationTableName
};
}
/**
* Replicates the results of a SQL query from a source database query
* to a destination database table, updating a view that points to the destination table.
* Helpful for maintaining access to the replicated data during the replication process.
* @param sourceConfiguration - Source database configuration.
* @param destinationConfiguration - Destination database configuration.
* @returns the status of the replication.
*/
export async function replicateQueryRecordsetAsView(sourceConfiguration, destinationConfiguration) {
const destinationTablePrefix = `_${destinationConfiguration.destinationViewName}_`;
const destinationTableName = `${destinationTablePrefix}${Date.now()}`;
const result = await replicateQueryRecordset(sourceConfiguration, {
destinationTableName,
destinationDatabase: destinationConfiguration.destinationDatabase
});
if (!result.success) {
return result;
}
let errorStep = 'destination:connect';
try {
/*
* Connect to destination database
*/
const destinationPool = await mssql.connect(destinationConfiguration.destinationDatabase);
/*
* Get destination views
*/
const destinationViews = await getViews(destinationPool);
const destinationHasView = destinationViews.some((possibleViewRecord) => {
return (possibleViewRecord.name === destinationConfiguration.destinationViewName);
});
/*
* Alter or create view
*/
if (destinationHasView) {
errorStep = 'destination:alterView';
await destinationPool.request()
.query(`alter view ${destinationConfiguration.destinationViewName} as
select * from ${destinationTableName}`);
}
else {
errorStep = 'destination:createView';
await destinationPool.request()
.query(`create view ${destinationConfiguration.destinationViewName} as
select * from ${destinationTableName}`);
}
/*
* Drop old tables
*/
if (destinationConfiguration.dropOldTables ?? false) {
const destinationTables = await getTables(destinationPool);
const destinationTablesToDrop = destinationTables.filter((possibleTableRecord) => {
return (possibleTableRecord.name.startsWith(destinationTablePrefix) &&
possibleTableRecord.name !== destinationTableName);
});
errorStep = 'destination:dropTable';
for (const destinationTableToDrop of destinationTablesToDrop) {
debug(`Dropping table: ${destinationTableToDrop.name}`);
await destinationPool
.request()
.query(`drop table ${destinationTableToDrop.name}`);
}
}
}
catch (error) {
debug(error);
return {
success: false,
destinationRows: result.destinationRows,
errorStep,
errorMessage: error.toString()
};
}
return {
success: true,
destinationRows: result.destinationRows,
destinationTableName
};
}
export default {
replicateQueryRecordset,
replicateQueryRecordsetAsView
};