leoric 1.14.4 → 1.15.0

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/History.md CHANGED
@@ -1,3 +1,13 @@
1
+ 1.15.0 / 2021-11-22
2
+ ===================
3
+
4
+ ## What's Changed
5
+ * feat: make duration in precise milliseconds by @fengmk2 in https://github.com/cyjake/leoric/pull/236
6
+ * fix: spell.increment() & spell.decrement() @cyjake https://github.com/cyjake/leoric/pull/234
7
+ * fix: bulkCreate should adapte empty data @JimmyDaddy https://github.com/cyjake/leoric/pull/232
8
+
9
+ **Full Changelog**: https://github.com/cyjake/leoric/compare/v1.14.4...v1.14.5
10
+
1
11
  1.14.4 / 2021-11-15
2
12
  ===================
3
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "1.14.4",
3
+ "version": "1.15.0",
4
4
  "description": "JavaScript Object-relational mapping alchemy",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
package/src/bone.js CHANGED
@@ -1322,6 +1322,7 @@ class Bone {
1322
1322
  }
1323
1323
 
1324
1324
  static async bulkCreate(records, options = {}) {
1325
+ if (!records || !records.length) return records;
1325
1326
  const { driver, attributes, primaryKey, primaryColumn } = this;
1326
1327
 
1327
1328
  const { createdAt, updatedAt } = this.timestamps;
package/src/collection.js CHANGED
@@ -11,7 +11,8 @@ class Collection extends Array {
11
11
  * @param {Array} fields
12
12
  * @returns {Collection|Array}
13
13
  */
14
- static init({ spell, rows, fields }) {
14
+ static init({ spell, rows, fields, insertId, affectedRows}) {
15
+ if (spell.command !== 'select') return { insertId, affectedRows };
15
16
  return dispatch(spell, rows, fields);
16
17
  }
17
18
 
@@ -1,10 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ const { performance } = require('perf_hooks');
4
+
3
5
  const AbstractDriver = require('../abstract');
4
6
  const Attribute = require('./attribute');
5
7
  const DataTypes = require('./data_types');
6
8
  const spellbook = require('./spellbook');
7
9
  const schema = require('./schema');
10
+ const { calculateDuration } = require('../../utils');
8
11
 
9
12
  class MysqlDriver extends AbstractDriver {
10
13
  /**
@@ -84,19 +87,19 @@ class MysqlDriver extends AbstractDriver {
84
87
  });
85
88
  });
86
89
  const sql = logger.format(query, values, opts);
87
- const start = Date.now();
90
+ const start = performance.now();
88
91
  let result;
89
92
 
90
93
  try {
91
94
  result = await promise;
92
95
  } catch (err) {
93
- logger.logQueryError(sql, err, Date.now() - start, opts);
96
+ logger.logQueryError(sql, err, calculateDuration(start), opts);
94
97
  throw err;
95
98
  } finally {
96
99
  if (!opts.connection) connection.release();
97
100
  }
98
101
 
99
- logger.tryLogQuery(sql, Date.now() - start, opts);
102
+ logger.tryLogQuery(sql, calculateDuration(start), opts);
100
103
  const [ results, fields ] = result;
101
104
  if (fields) return { rows: results, fields };
102
105
  return results;
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const { Pool } = require('pg');
4
+ const { performance } = require('perf_hooks');
4
5
 
5
6
  const AbstractDriver = require('../abstract');
6
7
  const Attribute = require('./attribute');
@@ -8,6 +9,7 @@ const DataTypes = require('./data_types');
8
9
  const { escape, escapeId } = require('./sqlstring');
9
10
  const spellbook = require('./spellbook');
10
11
  const schema = require('./schema');
12
+ const { calculateDuration } = require('../../utils');
11
13
 
12
14
  /**
13
15
  * The actual column type can be found by mapping the `oid` (which is called `dataTypeID`) in the `RowDescription`.
@@ -129,19 +131,19 @@ class PostgresDriver extends AbstractDriver {
129
131
 
130
132
  async function tryQuery(...args) {
131
133
  const formatted = logger.format(sql, values, spell);
132
- const start = new Date();
134
+ const start = performance.now();
133
135
  let result;
134
136
 
135
137
  try {
136
138
  result = await connection.query(...args);
137
139
  } catch (err) {
138
- logger.logQueryError(formatted, err, Date.now() - start, spell);
140
+ logger.logQueryError(formatted, err, calculateDuration(start), spell);
139
141
  throw err;
140
142
  } finally {
141
143
  if (!spell.connection) connection.release();
142
144
  }
143
145
 
144
- logger.tryLogQuery(formatted, Date.now() - start, spell);
146
+ logger.tryLogQuery(formatted, calculateDuration(start), spell);
145
147
  return result;
146
148
  }
147
149
 
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const strftime = require('strftime');
4
+ const { performance } = require('perf_hooks');
4
5
 
5
6
  const AbstractDriver = require('../abstract');
6
7
  const Attribute = require('./attribute');
@@ -9,6 +10,7 @@ const { escapeId, escape } = require('./sqlstring');
9
10
  const schema = require('./schema');
10
11
  const spellbook = require('./spellbook');
11
12
  const Pool = require('./pool');
13
+ const { calculateDuration } = require('../../utils');
12
14
 
13
15
  class SqliteDriver extends AbstractDriver {
14
16
  constructor(opts = {}) {
@@ -40,19 +42,19 @@ class SqliteDriver extends AbstractDriver {
40
42
 
41
43
  const { logger } = this;
42
44
  const sql = logger.format(query, values, opts);
43
- const start = Date.now();
45
+ const start = performance.now();
44
46
  let result;
45
47
 
46
48
  try {
47
49
  result = await connection.query(query, values, opts);
48
50
  } catch (err) {
49
- logger.logQueryError(sql, err, Date.now() - start, opts);
51
+ logger.logQueryError(sql, err, calculateDuration(start), opts);
50
52
  throw err;
51
53
  } finally {
52
54
  if (!opts.connection) connection.release();
53
55
  }
54
56
 
55
- logger.tryLogQuery(sql, Date.now() - start, opts);
57
+ logger.tryLogQuery(sql, calculateDuration(start), opts);
56
58
  return result;
57
59
  }
58
60
 
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { performance } = require('perf_hooks');
4
+
3
5
  function isPlainObject(value) {
4
6
  return Object.prototype.toString.call(value) === '[object Object]';
5
7
  }
@@ -28,6 +30,11 @@ function getPropertyNames(obj) {
28
30
  return Array.from(propertyNamesSet);
29
31
  }
30
32
 
33
+ // microseconds to millisecond, 10.456
34
+ function calculateDuration(starttime) {
35
+ return Math.floor((performance.now() - starttime) * 1000) / 1000;
36
+ }
37
+
31
38
  const logger = {};
32
39
 
33
40
  [ 'log', 'warn', 'debug', 'info', 'error' ].forEach(key => {
@@ -40,5 +47,6 @@ module.exports = {
40
47
  isPlainObject,
41
48
  compose,
42
49
  getPropertyNames,
50
+ calculateDuration,
43
51
  logger,
44
52
  };
package/types/index.d.ts CHANGED
@@ -107,6 +107,9 @@ export class Spell<T extends typeof Bone, U = InstanceType<T> | Collection<Insta
107
107
 
108
108
  batch(size?: number): AsyncIterable<T>;
109
109
 
110
+ increment(name: string, by?: number, options?: QueryOptions): Spell<T, number>;
111
+ decrement(name: string, by?: number, options?: QueryOptions): Spell<T, number>;
112
+
110
113
  toSqlString(): string;
111
114
  toString(): string;
112
115
  }
@@ -158,6 +161,7 @@ interface QueryOptions {
158
161
  individualHooks?: boolean;
159
162
  hooks?: boolean;
160
163
  paranoid?: boolean;
164
+ silent?: boolean;
161
165
  }
162
166
 
163
167
  interface QueryResult {