@vida-global/core 2.0.9 → 2.0.10
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.
|
@@ -18,6 +18,11 @@ class BaseRecord extends Model {
|
|
|
18
18
|
static get col() { return this.connection._sequelize.col }
|
|
19
19
|
|
|
20
20
|
|
|
21
|
+
static sqlLiteral(sql) {
|
|
22
|
+
return this.sequelize.literal(sql);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
21
26
|
constructor() {
|
|
22
27
|
if (new.target == BaseRecord) throw new Error("Cannot create an instance of BaseRecord");
|
|
23
28
|
super(...arguments);
|
|
@@ -142,6 +147,23 @@ class BaseRecord extends Model {
|
|
|
142
147
|
/***********************************************************************************************
|
|
143
148
|
* DB CONNECTION
|
|
144
149
|
***********************************************************************************************/
|
|
150
|
+
static async withStatementTimeout(milliseconds, callback) {
|
|
151
|
+
return await this.sequelize.transaction(
|
|
152
|
+
{readOnly: true},
|
|
153
|
+
async(transaction) => {
|
|
154
|
+
await this.sequelize.query(
|
|
155
|
+
'SELECT set_config(\'statement_timeout\', :timeout, true)',
|
|
156
|
+
{
|
|
157
|
+
replacements: {timeout: String(milliseconds)},
|
|
158
|
+
transaction,
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
return await callback(transaction);
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
145
167
|
static get connectionClass() {
|
|
146
168
|
return Connection;
|
|
147
169
|
}
|
package/package.json
CHANGED
|
@@ -80,6 +80,58 @@ describe('BaseRecord', () => {
|
|
|
80
80
|
});
|
|
81
81
|
|
|
82
82
|
|
|
83
|
+
describe('BaseRecord.withStatementTimeout', () => {
|
|
84
|
+
it ('runs the callback in a read-only transaction with a local statement timeout', async () => {
|
|
85
|
+
const callback = jest.fn().mockResolvedValue('result');
|
|
86
|
+
const transaction = {};
|
|
87
|
+
const query = jest.fn().mockResolvedValue(undefined);
|
|
88
|
+
const transact = jest.fn(
|
|
89
|
+
async (_options, transactionCallback) => {
|
|
90
|
+
return await transactionCallback(transaction);
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
class User extends BaseRecord {}
|
|
94
|
+
User.sequelize = {query, transaction: transact};
|
|
95
|
+
|
|
96
|
+
await expect(
|
|
97
|
+
User.withStatementTimeout(15000, callback)
|
|
98
|
+
).resolves.toEqual('result');
|
|
99
|
+
|
|
100
|
+
expect(transact).toHaveBeenCalledTimes(1);
|
|
101
|
+
expect(transact).toHaveBeenCalledWith(
|
|
102
|
+
{readOnly: true},
|
|
103
|
+
expect.any(Function)
|
|
104
|
+
);
|
|
105
|
+
expect(query).toHaveBeenCalledTimes(1);
|
|
106
|
+
expect(query).toHaveBeenCalledWith(
|
|
107
|
+
'SELECT set_config(\'statement_timeout\', :timeout, true)',
|
|
108
|
+
{
|
|
109
|
+
replacements: {timeout: '15000'},
|
|
110
|
+
transaction,
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
114
|
+
expect(callback).toHaveBeenCalledWith(transaction);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
describe('BaseRecord.sqlLiteral', () => {
|
|
120
|
+
it ('returns the SQL literal built by the model sequelize connection', () => {
|
|
121
|
+
const sql = TestHelpers.Faker.Text.randomString();
|
|
122
|
+
const literal = {};
|
|
123
|
+
const literalBuilder = jest.fn().mockReturnValue(literal);
|
|
124
|
+
class User extends BaseRecord {}
|
|
125
|
+
User.sequelize = {literal: literalBuilder};
|
|
126
|
+
|
|
127
|
+
expect(User.sqlLiteral(sql)).toBe(literal);
|
|
128
|
+
|
|
129
|
+
expect(literalBuilder).toHaveBeenCalledTimes(1);
|
|
130
|
+
expect(literalBuilder).toHaveBeenCalledWith(sql);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
|
|
83
135
|
describe('BaseRecord.initialize', () => {
|
|
84
136
|
it ('calls `getBaseRecordSchema` and passes the response to init', () => {
|
|
85
137
|
class User extends BaseRecord {}
|