leoric 2.4.1 → 2.5.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,12 @@
1
+ 2.5.0 / 2022-05-13
2
+ ==================
3
+
4
+ ## What's Changed
5
+ * feat: support disconnect and fix timestamps init by @JimmyDaddy in https://github.com/cyjake/leoric/pull/313
6
+
7
+
8
+ **Full Changelog**: https://github.com/cyjake/leoric/compare/v2.4.1...v2.5.0
9
+
1
10
  2.4.1 / 2022-04-27
2
11
  ==================
3
12
 
package/index.js CHANGED
@@ -39,10 +39,17 @@ const connect = async function connect(opts) {
39
39
  return realm;
40
40
  };
41
41
 
42
+ const disconnect = async function disconnect(realm, ...args) {
43
+ if (realm instanceof Realm && realm.connected) {
44
+ return await realm.disconnect(...args);
45
+ }
46
+ };
47
+
42
48
  Object.assign(Realm.prototype, migrations, { DataTypes });
43
49
  Object.assign(Realm, {
44
50
  default: Realm,
45
51
  connect,
52
+ disconnect,
46
53
  Bone,
47
54
  Collection,
48
55
  DataTypes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "2.4.1",
3
+ "version": "2.5.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
@@ -20,6 +20,7 @@ const {
20
20
  TIMESTAMP_NAMES,
21
21
  LEGACY_TIMESTAMP_COLUMN_MAP,
22
22
  ASSOCIATE_METADATA_MAP,
23
+ TIMESTAMP_ATTRIBUTE_NAMES,
23
24
  } = require('./constants');
24
25
 
25
26
  const columnAttributesKey = Symbol('leoric#columns');
@@ -999,7 +1000,7 @@ class Bone {
999
1000
  const attribute = new Attribute(name, attributes[name], options.define);
1000
1001
  attributeMap[attribute.columnName] = attribute;
1001
1002
  attributes[name] = attribute;
1002
- if (TIMESTAMP_NAMES.includes(name)) {
1003
+ if (TIMESTAMP_ATTRIBUTE_NAMES.includes(name)) {
1003
1004
  const { columnName } = attribute;
1004
1005
  const legacyColumnName = LEGACY_TIMESTAMP_COLUMN_MAP[columnName];
1005
1006
  if (!columnMap[columnName] && legacyColumnName && columnMap[legacyColumnName]) {
package/src/constants.js CHANGED
@@ -20,6 +20,12 @@ const LEGACY_TIMESTAMP_COLUMN_MAP = {
20
20
  deleted_at: 'gmt_deleted',
21
21
  };
22
22
 
23
+ const TIMESTAMP_ATTRIBUTE_NAMES = [
24
+ 'createdAt', 'updatedAt', 'deletedAt',
25
+ 'gmtCreate', 'gmtModified', 'gmtDeleted',
26
+ 'created_at', 'updated_at', 'deleted_at',
27
+ 'gmt_create', 'gmt_modified', 'gmt_deleted',
28
+ ];
23
29
  const TIMESTAMP_NAMES = [ 'createdAt', 'updatedAt', 'deletedAt' ];
24
30
 
25
31
  const ASSOCIATE_METADATA_MAP = {
@@ -33,5 +39,6 @@ module.exports = {
33
39
  LEGACY_TIMESTAMP_MAP,
34
40
  TIMESTAMP_NAMES,
35
41
  LEGACY_TIMESTAMP_COLUMN_MAP,
36
- ASSOCIATE_METADATA_MAP
42
+ ASSOCIATE_METADATA_MAP,
43
+ TIMESTAMP_ATTRIBUTE_NAMES
37
44
  };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const SqlString = require('sqlstring');
4
+ const debug = require('debug')('leoric');
4
5
 
5
6
  const Logger = require('./logger');
6
7
  const Attribute = require('./attribute');
@@ -54,6 +55,14 @@ class AbstractDriver {
54
55
  throw new Error('unimplemented!');
55
56
  }
56
57
 
58
+ /**
59
+ * disconnect manually
60
+ * @param {Function} callback
61
+ */
62
+ async disconnect(callback) {
63
+ debug('[disconnect] called');
64
+ }
65
+
57
66
  get dialect() {
58
67
  return camelCase(this.constructor.name.replace('Driver', ''));
59
68
  }
package/src/realm.js CHANGED
@@ -160,6 +160,12 @@ class Realm {
160
160
  return this.Bone;
161
161
  }
162
162
 
163
+ async disconnect(callback) {
164
+ if (this.connected && this.driver) {
165
+ return await this.driver.disconnect(callback);
166
+ }
167
+ }
168
+
163
169
  async sync(options) {
164
170
  if (!this.connected) await this.connect();
165
171
  const { models } = this;
package/types/index.d.ts CHANGED
@@ -17,6 +17,8 @@ type DataTypes<T> = {
17
17
  [Property in keyof T as Exclude<Property, "toSqlString">]: T[Property]
18
18
  }
19
19
 
20
+ type RawQueryResult = typeof Bone | ResultSet | boolean | number;
21
+
20
22
  interface ExprIdentifier {
21
23
  type: 'id';
22
24
  value: string;
@@ -314,6 +316,12 @@ declare class AbstractDriver {
314
316
  * Grab a connection and query the database
315
317
  */
316
318
  query(sql: string | { sql: string, nestTables?: boolean}, values?: Array<Literal | Literal[]>, opts?: SpellMeta): Promise<QueryResult>;
319
+
320
+ /**
321
+ * disconnect manually
322
+ * @param callback
323
+ */
324
+ disconnect(callback?: Function): Promise<boolean | void>;
317
325
 
318
326
  /**
319
327
  * query with spell
@@ -686,8 +694,8 @@ export class Bone {
686
694
  * yield Muscle.create({ boneId: bone.id, bar: 1 })
687
695
  * });
688
696
  */
689
- static transaction(callback: GeneratorFunction): Promise<void>;
690
- static transaction(callback: (connection: Connection) => Promise<void>): Promise<void>;
697
+ static transaction(callback: GeneratorFunction): Promise<RawQueryResult>;
698
+ static transaction(callback: (connection: Connection) => Promise<RawQueryResult>): Promise<RawQueryResult>;
691
699
 
692
700
  /**
693
701
  * DROP the table
@@ -884,10 +892,10 @@ export default class Realm {
884
892
 
885
893
  escape(value: Literal): string;
886
894
 
887
- query(sql: string, values?: Array<Literal>, options?: RawQueryOptions): ResultSet;
895
+ query(sql: string, values?: Array<Literal>, options?: RawQueryOptions): RawQueryResult;
888
896
 
889
- transaction(callback: GeneratorFunction): Promise<void>;
890
- transaction(callback: (connection: Connection) => Promise<void>): Promise<void>;
897
+ transaction(callback: GeneratorFunction): Promise<RawQueryResult>;
898
+ transaction(callback: (connection: Connection) => Promise<RawQueryResult>): Promise<RawQueryResult>;
891
899
 
892
900
  sync(options?: SyncOptions): Promise<void>;
893
901
  }
@@ -903,7 +911,7 @@ export default class Realm {
903
911
  * })
904
912
  */
905
913
  export function connect(opts: ConnectOptions): Promise<Realm>;
906
-
914
+ export function disconnect(realm: Realm, callback?: Function): Promise<boolean | void>;
907
915
  export {
908
916
  Hint,
909
917
  IndexHint,