@cuecue/store-mysql 0.0.43 → 0.0.44
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/index.js +16 -64
- package/package.json +10 -9
package/dist/index.js
CHANGED
|
@@ -9,16 +9,6 @@ var core = require('@cuecue/core');
|
|
|
9
9
|
var isString = require('lodash/isString');
|
|
10
10
|
var isArray = require('lodash/isArray');
|
|
11
11
|
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var mysql__default = /*#__PURE__*/_interopDefaultLegacy(mysql);
|
|
15
|
-
var StateMachine__default = /*#__PURE__*/_interopDefaultLegacy(StateMachine);
|
|
16
|
-
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
17
|
-
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
|
|
18
|
-
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
|
|
19
|
-
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
|
|
20
|
-
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
|
|
21
|
-
|
|
22
12
|
const now = () => new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
23
13
|
const getIdFromParams = params => Object.keys(params).reduce((acc, key) => {
|
|
24
14
|
if (key === 'internalId') {
|
|
@@ -28,7 +18,6 @@ const getIdFromParams = params => Object.keys(params).reduce((acc, key) => {
|
|
|
28
18
|
} else {
|
|
29
19
|
acc[key] = params[key];
|
|
30
20
|
}
|
|
31
|
-
|
|
32
21
|
return acc;
|
|
33
22
|
}, {});
|
|
34
23
|
const getWhereFromParams = (params = {}, operator = 'AND') => Object.keys(params).reduce((items, key) => [...items, `${key} = ?`], []).join(` ${operator} `);
|
|
@@ -42,45 +31,41 @@ const getParams = params => {
|
|
|
42
31
|
values
|
|
43
32
|
};
|
|
44
33
|
};
|
|
45
|
-
const setUpdateData = data => Object.keys(data).map(key => (
|
|
34
|
+
const setUpdateData = data => Object.keys(data).map(key => (isObject(data[key]) || isArray(data[key])) && key === 'data' ? JSON.stringify(data[key]) : data[key]);
|
|
46
35
|
const getUpdateFields = data => ({
|
|
47
36
|
fields: Object.keys(data).reduce((items, key) => [...items, `${key} = ?`], []).join(', '),
|
|
48
37
|
values: setUpdateData(data)
|
|
49
38
|
});
|
|
50
|
-
const setInsertData = data =>
|
|
51
|
-
if (key === 'data' && (
|
|
39
|
+
const setInsertData = data => isObject(data) ? Object.keys(data).reduce((acc, key) => {
|
|
40
|
+
if (key === 'data' && (isObject(data[key]) || isArray(data[key]))) {
|
|
52
41
|
acc[key] = JSON.stringify(data[key]);
|
|
53
42
|
} else {
|
|
54
43
|
acc[key] = data[key];
|
|
55
44
|
}
|
|
56
|
-
|
|
57
45
|
return acc;
|
|
58
46
|
}, {}) : null;
|
|
59
47
|
const getInsertFields = data => {
|
|
60
|
-
if (!
|
|
48
|
+
if (!isObject(data)) {
|
|
61
49
|
return null;
|
|
62
50
|
}
|
|
63
|
-
|
|
64
51
|
return {
|
|
65
52
|
columns: Object.keys(data).map(col => `\`${col}\``).join(', '),
|
|
66
53
|
fields: Object.values(data).map(() => '?').join(', '),
|
|
67
54
|
values: Object.values(setInsertData(data))
|
|
68
55
|
};
|
|
69
56
|
};
|
|
70
|
-
const getResult = result =>
|
|
71
|
-
if (key === 'data' &&
|
|
57
|
+
const getResult = result => isObject(result) ? Object.keys(result).reduce((acc, key) => {
|
|
58
|
+
if (key === 'data' && isString(result[key])) {
|
|
72
59
|
acc[key] = JSON.parse(result[key]);
|
|
73
60
|
} else {
|
|
74
61
|
acc[key] = result[key];
|
|
75
62
|
}
|
|
76
|
-
|
|
77
63
|
return acc;
|
|
78
64
|
}, {}) : null;
|
|
79
65
|
const getResults = results => results.map(res => getResult(res));
|
|
80
66
|
|
|
81
|
-
const debug =
|
|
82
|
-
|
|
83
|
-
class MysqlStore extends EventEmitter__default["default"] {
|
|
67
|
+
const debug = createDebug('cuecue:store:mysql');
|
|
68
|
+
class MysqlStore extends EventEmitter {
|
|
84
69
|
constructor(opts = {}) {
|
|
85
70
|
super();
|
|
86
71
|
this.options = {
|
|
@@ -103,7 +88,7 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
103
88
|
this.onDisconnected = this.onDisconnected.bind(this);
|
|
104
89
|
this.onInvalidTransition = this.onInvalidTransition.bind(this);
|
|
105
90
|
this.client = null;
|
|
106
|
-
this.state = new
|
|
91
|
+
this.state = new StateMachine({
|
|
107
92
|
transitions: [{
|
|
108
93
|
name: 'init',
|
|
109
94
|
from: 'none',
|
|
@@ -134,38 +119,31 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
134
119
|
}
|
|
135
120
|
});
|
|
136
121
|
}
|
|
137
|
-
|
|
138
122
|
init() {
|
|
139
123
|
return this.state.init();
|
|
140
124
|
}
|
|
141
|
-
|
|
142
125
|
destroy() {
|
|
143
126
|
return this.state.destroy();
|
|
144
127
|
}
|
|
145
|
-
|
|
146
128
|
connect() {
|
|
147
129
|
return this.state.connect();
|
|
148
130
|
}
|
|
149
|
-
|
|
150
131
|
disconnect() {
|
|
151
132
|
return this.state.disconnect();
|
|
152
133
|
}
|
|
153
|
-
|
|
154
134
|
initialized() {
|
|
155
135
|
return this.state.is('initialized');
|
|
156
136
|
}
|
|
157
|
-
|
|
158
137
|
connected() {
|
|
159
138
|
return this.state.is('connected');
|
|
160
139
|
}
|
|
161
|
-
|
|
162
140
|
disconnected() {
|
|
163
141
|
return this.state.is('disconnected');
|
|
164
142
|
}
|
|
165
|
-
|
|
166
143
|
findItem(type, externalId = null, internalId = null, withDeleted = false) {
|
|
167
144
|
return new Promise((resolve, reject) => {
|
|
168
|
-
const idParams =
|
|
145
|
+
const idParams = isObject(externalId) ? externalId : {
|
|
146
|
+
...(externalId !== null ? {
|
|
169
147
|
externalId
|
|
170
148
|
} : null),
|
|
171
149
|
...(internalId !== null ? {
|
|
@@ -181,16 +159,15 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
181
159
|
reject(error);
|
|
182
160
|
return;
|
|
183
161
|
}
|
|
184
|
-
|
|
185
162
|
const result = results !== null && results.length === 1 ? getResult(results[0]) : null;
|
|
186
163
|
resolve(core.parseStoreOutputId(result));
|
|
187
164
|
});
|
|
188
165
|
});
|
|
189
166
|
}
|
|
190
|
-
|
|
191
167
|
addItem(type, data) {
|
|
192
168
|
const parsedData = core.parseStoreInputId(data);
|
|
193
|
-
const withCreated = parsedData !== null && !parsedData.created_at ? {
|
|
169
|
+
const withCreated = parsedData !== null && !parsedData.created_at ? {
|
|
170
|
+
...parsedData,
|
|
194
171
|
created_at: now()
|
|
195
172
|
} : parsedData;
|
|
196
173
|
const {
|
|
@@ -204,21 +181,19 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
204
181
|
reject(error);
|
|
205
182
|
return;
|
|
206
183
|
}
|
|
207
|
-
|
|
208
184
|
resolve(results);
|
|
209
185
|
});
|
|
210
186
|
}).then(({
|
|
211
187
|
insertId
|
|
212
188
|
}) => this.findItem(type, null, insertId));
|
|
213
189
|
}
|
|
214
|
-
|
|
215
190
|
addItems(type, items) {
|
|
216
191
|
return Promise.all(items.map(it => this.addItem(type, it)));
|
|
217
192
|
}
|
|
218
|
-
|
|
219
193
|
updateItem(type, externalId, data) {
|
|
220
194
|
const parsedData = core.parseStoreInputId(data);
|
|
221
|
-
const withUpdated = {
|
|
195
|
+
const withUpdated = {
|
|
196
|
+
...parsedData,
|
|
222
197
|
updated_at: now()
|
|
223
198
|
};
|
|
224
199
|
return new Promise((resolve, reject) => {
|
|
@@ -231,18 +206,15 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
231
206
|
reject(error);
|
|
232
207
|
return;
|
|
233
208
|
}
|
|
234
|
-
|
|
235
209
|
resolve(results);
|
|
236
210
|
});
|
|
237
211
|
}).then(() => this.findItem(type, externalId));
|
|
238
212
|
}
|
|
239
|
-
|
|
240
213
|
deleteItem(type, externalId) {
|
|
241
214
|
return this.deleteItems(type, {
|
|
242
215
|
externalId
|
|
243
216
|
});
|
|
244
217
|
}
|
|
245
|
-
|
|
246
218
|
deleteItems(type, params = {}) {
|
|
247
219
|
return new Promise((resolve, reject) => {
|
|
248
220
|
const {
|
|
@@ -254,12 +226,10 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
254
226
|
reject(error);
|
|
255
227
|
return;
|
|
256
228
|
}
|
|
257
|
-
|
|
258
229
|
resolve(results);
|
|
259
230
|
});
|
|
260
231
|
});
|
|
261
232
|
}
|
|
262
|
-
|
|
263
233
|
getItems(type, params = {}, withDeleted = false) {
|
|
264
234
|
return new Promise((resolve, reject) => {
|
|
265
235
|
const {
|
|
@@ -273,13 +243,11 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
273
243
|
reject(error);
|
|
274
244
|
return;
|
|
275
245
|
}
|
|
276
|
-
|
|
277
246
|
const allResults = getResults(results);
|
|
278
247
|
resolve(core.parseStoreOutputId(allResults));
|
|
279
248
|
});
|
|
280
249
|
});
|
|
281
250
|
}
|
|
282
|
-
|
|
283
251
|
async onInit() {
|
|
284
252
|
const {
|
|
285
253
|
host,
|
|
@@ -289,32 +257,27 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
289
257
|
port,
|
|
290
258
|
autoConnect
|
|
291
259
|
} = this.options;
|
|
292
|
-
this.client =
|
|
260
|
+
this.client = mysql.createConnection({
|
|
293
261
|
host,
|
|
294
262
|
user,
|
|
295
263
|
password,
|
|
296
264
|
database,
|
|
297
265
|
port
|
|
298
266
|
});
|
|
299
|
-
|
|
300
267
|
if (autoConnect) {
|
|
301
268
|
this.connect();
|
|
302
269
|
}
|
|
303
|
-
|
|
304
270
|
this.emit('init');
|
|
305
271
|
return Promise.resolve();
|
|
306
272
|
}
|
|
307
|
-
|
|
308
273
|
onInitialized() {
|
|
309
274
|
debug('initialized');
|
|
310
275
|
this.emit('initialized');
|
|
311
276
|
}
|
|
312
|
-
|
|
313
277
|
async onDestroy() {
|
|
314
278
|
debug('destroying...');
|
|
315
279
|
this.emit('destroy');
|
|
316
280
|
await this.disconnect();
|
|
317
|
-
|
|
318
281
|
if (this.client !== null) {
|
|
319
282
|
return new Promise((resolve, reject) => {
|
|
320
283
|
this.client.destroy(err => {
|
|
@@ -322,21 +285,17 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
322
285
|
reject(err);
|
|
323
286
|
return;
|
|
324
287
|
}
|
|
325
|
-
|
|
326
288
|
this.client = null;
|
|
327
289
|
resolve();
|
|
328
290
|
});
|
|
329
291
|
});
|
|
330
292
|
}
|
|
331
|
-
|
|
332
293
|
return Promise.resolve();
|
|
333
294
|
}
|
|
334
|
-
|
|
335
295
|
onDestroyed() {
|
|
336
296
|
debug('destroyed');
|
|
337
297
|
return process.nextTick(() => this.emit('destroyed'));
|
|
338
298
|
}
|
|
339
|
-
|
|
340
299
|
onConnect() {
|
|
341
300
|
debug('connecting...');
|
|
342
301
|
this.emit('connect');
|
|
@@ -346,17 +305,14 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
346
305
|
reject(err);
|
|
347
306
|
return;
|
|
348
307
|
}
|
|
349
|
-
|
|
350
308
|
resolve();
|
|
351
309
|
});
|
|
352
310
|
});
|
|
353
311
|
}
|
|
354
|
-
|
|
355
312
|
onConnected() {
|
|
356
313
|
debug('connected');
|
|
357
314
|
return process.nextTick(() => this.emit('connected'));
|
|
358
315
|
}
|
|
359
|
-
|
|
360
316
|
onDisconnect() {
|
|
361
317
|
this.emit('disconnect');
|
|
362
318
|
return new Promise((resolve, reject) => {
|
|
@@ -365,22 +321,18 @@ class MysqlStore extends EventEmitter__default["default"] {
|
|
|
365
321
|
reject(err);
|
|
366
322
|
return;
|
|
367
323
|
}
|
|
368
|
-
|
|
369
324
|
resolve();
|
|
370
325
|
});
|
|
371
326
|
});
|
|
372
327
|
}
|
|
373
|
-
|
|
374
328
|
onDisconnected() {
|
|
375
329
|
debug('disconnected');
|
|
376
330
|
return process.nextTick(() => this.emit('disconnected'));
|
|
377
331
|
}
|
|
378
|
-
|
|
379
332
|
onInvalidTransition(transition, from, to) {
|
|
380
333
|
this.debug('ERROR: Current state', this.state.state);
|
|
381
334
|
this.debug('ERROR: Invalid base transition: %s from: %s to: %s', transition, from, to);
|
|
382
335
|
}
|
|
383
|
-
|
|
384
336
|
}
|
|
385
337
|
|
|
386
338
|
module.exports = MysqlStore;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cuecue/store-mysql",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.44",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript"
|
|
@@ -29,6 +29,9 @@
|
|
|
29
29
|
}
|
|
30
30
|
],
|
|
31
31
|
"license": "ISC",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22"
|
|
34
|
+
},
|
|
32
35
|
"main": "dist/index.js",
|
|
33
36
|
"files": [
|
|
34
37
|
"dist"
|
|
@@ -38,14 +41,13 @@
|
|
|
38
41
|
"migrate:down": "db-migrate down --migrations-dir=./migrations --config=./migrations/config.json -e prod --dotenvCustomPath '../../.env'",
|
|
39
42
|
"migrate:reset": "db-migrate reset --migrations-dir=./migrations --config=./migrations/config.json -e prod --dotenvCustomPath '../../.env'",
|
|
40
43
|
"clean": "rm -rf dist",
|
|
41
|
-
"build": "rollup --config ../../rollup.config.
|
|
42
|
-
"
|
|
44
|
+
"build": "rollup --config ../../rollup.config.mjs",
|
|
45
|
+
"prepack": "npm run clean && npm run build"
|
|
43
46
|
},
|
|
44
47
|
"dependencies": {
|
|
45
|
-
"@
|
|
46
|
-
"
|
|
47
|
-
"db-migrate": "^
|
|
48
|
-
"db-migrate-mysql": "^2.1.2",
|
|
48
|
+
"@cuecue/core": "^0.0.44",
|
|
49
|
+
"db-migrate": "^1.9.0",
|
|
50
|
+
"db-migrate-mysql": "^3.2.1",
|
|
49
51
|
"debug": "^4.3.1",
|
|
50
52
|
"dotenv": "^16.0.1",
|
|
51
53
|
"javascript-state-machine": "^3.1.0",
|
|
@@ -55,6 +57,5 @@
|
|
|
55
57
|
},
|
|
56
58
|
"publishConfig": {
|
|
57
59
|
"access": "public"
|
|
58
|
-
}
|
|
59
|
-
"gitHead": "59235dd281e2779bf0f964cb069f539ba85a8066"
|
|
60
|
+
}
|
|
60
61
|
}
|