1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports["default"] = void 0;
|
8 | var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
9 | var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
10 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
11 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
12 | var _mapKeys = _interopRequireDefault(require("@lskjs/utils/mapKeys"));
|
13 | var _mapValuesKeys = _interopRequireDefault(require("@lskjs/utils/mapValuesKeys"));
|
14 | var _tryJSONparse = _interopRequireDefault(require("@lskjs/utils/tryJSONparse"));
|
15 | var _tryJSONstringify = _interopRequireDefault(require("@lskjs/utils/tryJSONstringify"));
|
16 | var _isFinite = _interopRequireDefault(require("lodash/isFinite"));
|
17 | var _map = _interopRequireDefault(require("lodash/map"));
|
18 | var _mapValues = _interopRequireDefault(require("lodash/mapValues"));
|
19 | var _snakeCase = _interopRequireDefault(require("lodash/snakeCase"));
|
20 | var notNan = function notNan(a) {
|
21 | return (0, _isFinite["default"])(a) ? a : null;
|
22 | };
|
23 | var BaseScyllaModel = function () {
|
24 | function BaseScyllaModel(_ref) {
|
25 | var app = _ref.app;
|
26 | (0, _classCallCheck2["default"])(this, BaseScyllaModel);
|
27 | this.app = app;
|
28 | this.log = app.log;
|
29 | if (!this.schema) this.schema = this.getSchema();
|
30 | }
|
31 | (0, _createClass2["default"])(BaseScyllaModel, [{
|
32 | key: "getSchema",
|
33 | value: function getSchema() {
|
34 | return {};
|
35 | }
|
36 | }, {
|
37 | key: "stringifyData",
|
38 | value: function stringifyData(data) {
|
39 | var _this = this;
|
40 | return (0, _mapValues["default"])(data, function (value, key) {
|
41 | var type = _this.schema[key];
|
42 | if (!type) {
|
43 | _this.log.warn('missing field in schema: ', key);
|
44 | return undefined;
|
45 | }
|
46 | if (['int', 'bigint', 'smallint'].includes(type)) return notNan(Number(value));
|
47 | if (type === 'text<json>') return (0, _tryJSONstringify["default"])(value);
|
48 | if (type === 'timestamp') return new Date(value).getTime();
|
49 | if (type.startsWith('list')) {
|
50 | var value2 = Array.isArray(value) ? value.filter(function (a) {
|
51 | return a !== null && a !== undefined;
|
52 | }) : null;
|
53 |
|
54 | return value2;
|
55 | }
|
56 | return value;
|
57 | });
|
58 | }
|
59 | }, {
|
60 | key: "parseData",
|
61 | value: function parseData(data) {
|
62 | var _this2 = this;
|
63 | return (0, _mapValuesKeys["default"])(data, function (value, key) {
|
64 | var type = _this2.schema[key];
|
65 | if (typeof value === 'undefined') return null;
|
66 | if (type === 'text<json>') return {
|
67 | key: key,
|
68 | value: (0, _tryJSONparse["default"])(value)
|
69 | };
|
70 | if (type === 'timestamp') return {
|
71 | key: key,
|
72 | value: new Date(value)
|
73 | };
|
74 | return {
|
75 | key: key,
|
76 | value: value
|
77 | };
|
78 | });
|
79 | }
|
80 | }, {
|
81 | key: "getSchemaString",
|
82 | value: function getSchemaString() {
|
83 | var schema = (0, _mapKeys["default"])(this.schema, function (value, key) {
|
84 | var newKey = key[0] === '_' ? "_".concat((0, _snakeCase["default"])(key)) : (0, _snakeCase["default"])(key);
|
85 | if (newKey[0] === '_') return "\"".concat(newKey, "\"");
|
86 | return newKey;
|
87 | });
|
88 | return (0, _map["default"])(schema, function (value, key) {
|
89 | return "".concat(key, " ").concat(value === 'text<json>' ? 'text' : value);
|
90 | }).join(',\n');
|
91 | }
|
92 | }, {
|
93 | key: "createInsertQuery",
|
94 | value: function createInsertQuery(table, data) {
|
95 | var keys = Object.keys(data);
|
96 | var defaultKeyspace = this.scylla.config.defaultKeyspace;
|
97 | return "INSERT INTO ".concat(defaultKeyspace, ".").concat(this.table, " (").concat(keys.map(function (key) {
|
98 | return "\"".concat(key, "\"");
|
99 | }).join(', '), ") VALUES (").concat(keys.map(function () {
|
100 | return '?';
|
101 | }).join(', '), ")");
|
102 | }
|
103 | }, {
|
104 | key: "execute",
|
105 | value: function () {
|
106 | var _execute = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee(query, values, options, json) {
|
107 | var res;
|
108 | return _regenerator["default"].wrap(function _callee$(_context) {
|
109 | while (1) switch (_context.prev = _context.next) {
|
110 | case 0:
|
111 | if (this.scylla.debug) this.scylla.log.trace("[scylla] table=".concat(this.table, "\n"), query, values);
|
112 | _context.prev = 1;
|
113 | _context.next = 4;
|
114 | return this.scylla.execute(query, values, options);
|
115 | case 4:
|
116 | res = _context.sent;
|
117 | return _context.abrupt("return", res);
|
118 | case 8:
|
119 | _context.prev = 8;
|
120 | _context.t0 = _context["catch"](1);
|
121 | this.scylla.log.warn("[scylla] table=".concat(this.table, "\n"), query);
|
122 | this.scylla.log.trace(json || values);
|
123 | this.scylla.log.error('[scylla] err ', _context.t0);
|
124 | throw _context.t0;
|
125 | case 14:
|
126 | case "end":
|
127 | return _context.stop();
|
128 | }
|
129 | }, _callee, this, [[1, 8]]);
|
130 | }));
|
131 | function execute(_x, _x2, _x3, _x4) {
|
132 | return _execute.apply(this, arguments);
|
133 | }
|
134 | return execute;
|
135 | }()
|
136 | }, {
|
137 | key: "run",
|
138 | value: function () {
|
139 | var _run = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee2() {
|
140 | return _regenerator["default"].wrap(function _callee2$(_context2) {
|
141 | while (1) switch (_context2.prev = _context2.next) {
|
142 | case 0:
|
143 | _context2.next = 2;
|
144 | return this.app.module('scylla');
|
145 | case 2:
|
146 | this.scylla = _context2.sent;
|
147 | case 3:
|
148 | case "end":
|
149 | return _context2.stop();
|
150 | }
|
151 | }, _callee2, this);
|
152 | }));
|
153 | function run() {
|
154 | return _run.apply(this, arguments);
|
155 | }
|
156 | return run;
|
157 | }()
|
158 | }], [{
|
159 | key: "run",
|
160 | value: function () {
|
161 | var _run2 = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee3(props) {
|
162 | var obj;
|
163 | return _regenerator["default"].wrap(function _callee3$(_context3) {
|
164 | while (1) switch (_context3.prev = _context3.next) {
|
165 | case 0:
|
166 | obj = new this(props);
|
167 | _context3.next = 3;
|
168 | return obj.run();
|
169 | case 3:
|
170 | return _context3.abrupt("return", obj);
|
171 | case 4:
|
172 | case "end":
|
173 | return _context3.stop();
|
174 | }
|
175 | }, _callee3, this);
|
176 | }));
|
177 | function run(_x5) {
|
178 | return _run2.apply(this, arguments);
|
179 | }
|
180 | return run;
|
181 | }()
|
182 | }]);
|
183 | return BaseScyllaModel;
|
184 | }();
|
185 | var _default = BaseScyllaModel;
|
186 | exports["default"] = _default;
|
187 |
|
\ | No newline at end of file |