forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatches.js
More file actions
147 lines (128 loc) · 3.87 KB
/
batches.js
File metadata and controls
147 lines (128 loc) · 3.87 KB
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
const sqlLimiter = require('sql-limiter');
const _ = require('lodash');
const ensureJson = require('./ensure-json');
class Batches {
/**
* @param {import('../sequelize-db')} sequelizeDb
* @param {import('../lib/config')} config
*/
constructor(sequelizeDb, config) {
this.sequelizeDb = sequelizeDb;
this.config = config;
}
async findOneById(id) {
let batch = await this.sequelizeDb.Batches.findOne({ where: { id } });
if (!batch) {
return;
}
batch = batch.toJSON();
batch.chart = ensureJson(batch.chart);
const statements = await this.sequelizeDb.Statements.findAll({
where: { batchId: id },
order: [['sequence', 'ASC']],
});
batch.statements = statements.map((s) => {
s = s.toJSON();
s.columns = ensureJson(s.columns);
s.error = ensureJson(s.error);
return s;
});
return batch;
}
async findAllForUser(user) {
let items = await this.sequelizeDb.Batches.findAll({
where: { userId: user.id },
});
items = items.map((item) => item.toJSON());
return items;
}
/**
* Get all batches and more for user and query id
* @param {object} user
* @param {string} [queryId]
* @param {boolean} [includeStatements]
* @param {number} [limit]
*/
async findAllForUserQuery(
user,
queryId = null,
includeStatements = false,
limit = 40
) {
let batches = await this.sequelizeDb.Batches.findAll({
where: { userId: user.id, queryId },
limit,
order: [['createdAt', 'DESC']],
});
batches = batches.map((item) => item.toJSON());
if (includeStatements) {
const batchIds = batches.map((batch) => batch.id);
let statements = await this.sequelizeDb.Statements.findAll({
where: { batchId: batchIds },
});
statements = statements.map((statement) => statement.toJSON());
const statementsByBatchId = _.groupBy(statements, 'batchId');
batches.forEach((batch) => {
batch.statements = statementsByBatchId[batch.id];
if (batch.statements) {
_.sortBy(batch.statements, ['sequence']);
}
});
}
// Results are in desc order, but we'll return them in ascending
batches = _.sortBy(batches, ['createdAt']);
return batches;
}
/**
* Create a new batch (and statements)
* selectedText is parsed out into statements
* @param {object} batch
*/
async create(batch) {
let createdBatch;
const queryText = batch.selectedText || batch.batchText;
// sqlLimiter could fail at parsing the SQL text
// If this happens the error is captured and reported as if it were a query error
let error;
let statementTexts = [queryText];
try {
statementTexts = sqlLimiter
.getStatements(queryText)
.map((s) => sqlLimiter.removeTerminator(s))
.filter((s) => s && s.trim() !== '');
} catch (e) {
error = e;
}
await this.sequelizeDb.sequelize.transaction(async (transaction) => {
const createData = { ...batch };
if (error) {
createData.status = 'error';
}
createdBatch = await this.sequelizeDb.Batches.create(createData, {
transaction,
});
const statements = statementTexts.map((statementText, i) => {
return {
batchId: createdBatch.id,
sequence: i + 1,
statementText,
status: error ? 'error' : 'queued',
error: error && { title: error.message },
};
});
await this.sequelizeDb.Statements.bulkCreate(statements, { transaction });
});
return this.findOneById(createdBatch.id);
}
/**
* Update batch object
* Statements are not updated through this method
* @param {string} id
* @param {data} data
*/
async update(id, data) {
await this.sequelizeDb.Batches.update(data, { where: { id } });
return this.findOneById(id);
}
}
module.exports = Batches;