@sqb/connect 4.12.0 → 4.13.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.
Files changed (65) hide show
  1. package/cjs/client/cursor.js +1 -1
  2. package/cjs/client/extensions.js +38 -11
  3. package/cjs/client/helpers.js +7 -6
  4. package/cjs/client/sqb-client.js +2 -2
  5. package/cjs/client/sqb-connection.js +7 -4
  6. package/cjs/index.js +19 -20
  7. package/cjs/orm/backward.js +5 -5
  8. package/cjs/orm/commands/command.helper.js +39 -11
  9. package/cjs/orm/commands/create.command.js +2 -1
  10. package/cjs/orm/commands/find.command.js +21 -35
  11. package/cjs/orm/commands/row-converter.js +5 -10
  12. package/cjs/orm/decorators/column.decorator.js +6 -3
  13. package/cjs/orm/decorators/embedded.decorator.js +1 -2
  14. package/cjs/orm/decorators/entity.decorator.js +1 -2
  15. package/cjs/orm/decorators/events.decorator.js +6 -7
  16. package/cjs/orm/decorators/foreignkey.decorator.js +1 -2
  17. package/cjs/orm/decorators/index.decorator.js +1 -2
  18. package/cjs/orm/decorators/link.decorator.js +7 -5
  19. package/cjs/orm/decorators/primarykey.decorator.js +3 -3
  20. package/cjs/orm/decorators/transform.decorator.js +2 -3
  21. package/cjs/orm/model/association.js +9 -11
  22. package/cjs/orm/model/entity-metadata.js +4 -2
  23. package/cjs/orm/orm.const.js +2 -1
  24. package/cjs/orm/repository.class.js +31 -36
  25. package/cjs/orm/util/apply-mixins.js +3 -3
  26. package/cjs/orm/util/extract-keyvalues.js +7 -5
  27. package/cjs/orm/util/orm.helper.js +8 -12
  28. package/cjs/orm/util/parse-fields-projection.js +64 -0
  29. package/cjs/orm/util/serialize-field.js +1 -2
  30. package/esm/client/cursor.js +1 -1
  31. package/esm/client/extensions.js +36 -8
  32. package/esm/client/helpers.js +2 -0
  33. package/esm/client/sqb-client.js +3 -3
  34. package/esm/client/sqb-connection.js +7 -4
  35. package/esm/index.js +18 -17
  36. package/esm/orm/commands/command.helper.js +36 -7
  37. package/esm/orm/commands/create.command.js +2 -1
  38. package/esm/orm/commands/find.command.js +21 -35
  39. package/esm/orm/commands/row-converter.js +5 -10
  40. package/esm/orm/decorators/column.decorator.js +5 -1
  41. package/esm/orm/decorators/link.decorator.js +6 -3
  42. package/esm/orm/decorators/primarykey.decorator.js +2 -1
  43. package/esm/orm/model/association.js +9 -11
  44. package/esm/orm/model/entity-metadata.js +4 -2
  45. package/esm/orm/orm.const.js +1 -0
  46. package/esm/orm/repository.class.js +31 -36
  47. package/esm/orm/util/apply-mixins.js +2 -1
  48. package/esm/orm/util/extract-keyvalues.js +6 -3
  49. package/esm/orm/util/orm.helper.js +2 -6
  50. package/esm/orm/util/parse-fields-projection.js +59 -0
  51. package/package.json +10 -6
  52. package/types/client/adapter.d.ts +1 -1
  53. package/types/client/cursor-stream.d.ts +0 -1
  54. package/types/client/extensions.d.ts +14 -3
  55. package/types/client/sqb-client.d.ts +1 -1
  56. package/types/client/sqb-connection.d.ts +1 -1
  57. package/types/client/types.d.ts +1 -1
  58. package/types/index.d.ts +18 -17
  59. package/types/orm/commands/find.command.d.ts +3 -4
  60. package/types/orm/commands/row-converter.d.ts +1 -1
  61. package/types/orm/decorators/column.decorator.d.ts +1 -0
  62. package/types/orm/model/field-metadata.d.ts +1 -1
  63. package/types/orm/orm.const.d.ts +1 -0
  64. package/types/orm/repository.class.d.ts +11 -8
  65. package/types/orm/util/parse-fields-projection.d.ts +10 -0
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AfterDestroy = exports.AfterUpdate = exports.AfterInsert = exports.BeforeDestroy = exports.BeforeUpdate = exports.BeforeInsert = void 0;
3
+ exports.BeforeInsert = BeforeInsert;
4
+ exports.BeforeUpdate = BeforeUpdate;
5
+ exports.BeforeDestroy = BeforeDestroy;
6
+ exports.AfterInsert = AfterInsert;
7
+ exports.AfterUpdate = AfterUpdate;
8
+ exports.AfterDestroy = AfterDestroy;
4
9
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
10
  function BeforeInsert() {
6
11
  return (target, propertyKey) => {
@@ -11,7 +16,6 @@ function BeforeInsert() {
11
16
  entity_metadata_js_1.EntityMetadata.addEventListener(model, 'before-insert', fn);
12
17
  };
13
18
  }
14
- exports.BeforeInsert = BeforeInsert;
15
19
  function BeforeUpdate() {
16
20
  return (target, propertyKey) => {
17
21
  if (typeof propertyKey !== 'string')
@@ -21,7 +25,6 @@ function BeforeUpdate() {
21
25
  entity_metadata_js_1.EntityMetadata.addEventListener(model, 'before-update', fn);
22
26
  };
23
27
  }
24
- exports.BeforeUpdate = BeforeUpdate;
25
28
  function BeforeDestroy() {
26
29
  return (target, propertyKey) => {
27
30
  if (typeof propertyKey !== 'string')
@@ -31,7 +34,6 @@ function BeforeDestroy() {
31
34
  entity_metadata_js_1.EntityMetadata.addEventListener(model, 'before-destroy', fn);
32
35
  };
33
36
  }
34
- exports.BeforeDestroy = BeforeDestroy;
35
37
  function AfterInsert() {
36
38
  return (target, propertyKey) => {
37
39
  if (typeof propertyKey !== 'string')
@@ -41,7 +43,6 @@ function AfterInsert() {
41
43
  entity_metadata_js_1.EntityMetadata.addEventListener(model, 'after-insert', fn);
42
44
  };
43
45
  }
44
- exports.AfterInsert = AfterInsert;
45
46
  function AfterUpdate() {
46
47
  return (target, propertyKey) => {
47
48
  if (typeof propertyKey !== 'string')
@@ -51,7 +52,6 @@ function AfterUpdate() {
51
52
  entity_metadata_js_1.EntityMetadata.addEventListener(model, 'after-update', fn);
52
53
  };
53
54
  }
54
- exports.AfterUpdate = AfterUpdate;
55
55
  function AfterDestroy() {
56
56
  return (target, propertyKey) => {
57
57
  if (typeof propertyKey !== 'string')
@@ -61,4 +61,3 @@ function AfterDestroy() {
61
61
  entity_metadata_js_1.EntityMetadata.addEventListener(model, 'after-destroy', fn);
62
62
  };
63
63
  }
64
- exports.AfterDestroy = AfterDestroy;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ForeignKey = void 0;
3
+ exports.ForeignKey = ForeignKey;
4
4
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
5
  function ForeignKey(type, targetKey) {
6
6
  return function (target, propertyKey) {
@@ -10,4 +10,3 @@ function ForeignKey(type, targetKey) {
10
10
  entity_metadata_js_1.EntityMetadata.addForeignKey(entity, propertyKey, type, targetKey);
11
11
  };
12
12
  }
13
- exports.ForeignKey = ForeignKey;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Index = void 0;
3
+ exports.Index = Index;
4
4
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
5
  function Index(arg0, arg1) {
6
6
  return function (target, propertyKey) {
@@ -18,4 +18,3 @@ function Index(arg0, arg1) {
18
18
  entity_metadata_js_1.EntityMetadata.addIndex(model, { ...arg0, columns: [propertyKey] });
19
19
  };
20
20
  }
21
- exports.Index = Index;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Link = void 0;
3
+ exports.Link = Link;
4
4
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
5
  const link_chain_js_1 = require("../model/link-chain.js");
6
6
  function Link(options) {
@@ -11,16 +11,19 @@ function Link(options) {
11
11
  throw new TypeError('Symbol properties are not allowed');
12
12
  const reflectType = Reflect.getMetadata('design:type', target, propertyKey);
13
13
  if (!root) {
14
- if (reflectType === Array)
14
+ if (reflectType === Array) {
15
15
  throw new TypeError(`Can't get type information while it is an array. Please define entity type`);
16
+ }
16
17
  if (!entity_metadata_js_1.EntityMetadata.get(reflectType))
17
18
  throw new TypeError(`No entity metadata found for type "${reflectType}"`);
18
19
  fn.toOne(reflectType);
19
20
  }
20
- if (reflectType !== Array && root.first.returnsMany())
21
+ if (reflectType !== Array && root.first.returnsMany()) {
21
22
  throw new TypeError(`Link returns single instance however property type is an array`);
22
- if (reflectType === Array && !root.first.returnsMany())
23
+ }
24
+ if (reflectType === Array && !root.first.returnsMany()) {
23
25
  throw new TypeError(`Link returns array of instances however property type is not an array`);
26
+ }
24
27
  const entity = entity_metadata_js_1.EntityMetadata.define(target.constructor);
25
28
  // @ts-ignore
26
29
  // noinspection JSConstantReassignment
@@ -55,7 +58,6 @@ function Link(options) {
55
58
  };
56
59
  return fn;
57
60
  }
58
- exports.Link = Link;
59
61
  /**
60
62
  * Crates an Link Chain object
61
63
  */
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PrimaryKey = void 0;
3
+ exports.PrimaryKey = PrimaryKey;
4
4
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
5
  const column_decorator_js_1 = require("./column.decorator.js");
6
6
  function PrimaryKey(arg0, arg1) {
7
7
  return function (target, propertyKey) {
8
8
  if (arguments.length === 1) {
9
- if (!(typeof arg0 === 'string' || Array.isArray(arg0)))
9
+ if (!(typeof arg0 === 'string' || Array.isArray(arg0))) {
10
10
  throw new Error(`You must specify primary index column(s)`);
11
+ }
11
12
  const meta = entity_metadata_js_1.EntityMetadata.define(target);
12
13
  entity_metadata_js_1.EntityMetadata.setPrimaryKeys(meta, arg0, arg1);
13
14
  return;
@@ -21,4 +22,3 @@ function PrimaryKey(arg0, arg1) {
21
22
  entity_metadata_js_1.EntityMetadata.setPrimaryKeys(meta, propertyKey, arg0);
22
23
  };
23
24
  }
24
- exports.PrimaryKey = PrimaryKey;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Serialize = exports.Parse = void 0;
3
+ exports.Parse = Parse;
4
+ exports.Serialize = Serialize;
4
5
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
6
  function Parse(fn) {
6
7
  return (target, propertyKey) => {
@@ -10,7 +11,6 @@ function Parse(fn) {
10
11
  entity_metadata_js_1.EntityMetadata.defineColumnField(entity, propertyKey).parse = fn;
11
12
  };
12
13
  }
13
- exports.Parse = Parse;
14
14
  function Serialize(fn) {
15
15
  return (target, propertyKey) => {
16
16
  if (typeof propertyKey !== 'string')
@@ -19,4 +19,3 @@ function Serialize(fn) {
19
19
  entity_metadata_js_1.EntityMetadata.defineColumnField(entity, propertyKey).serialize = fn;
20
20
  };
21
21
  }
22
- exports.Serialize = Serialize;
@@ -66,17 +66,15 @@ class Association {
66
66
  this._targetProperty = foreign._targetProperty;
67
67
  return;
68
68
  }
69
- else {
70
- // Try to determine key fields from foreign key from target to source
71
- foreign = await entity_metadata_js_1.EntityMetadata.getForeignKeyFor(target, source);
72
- if (foreign && foreign !== this) {
73
- await foreign._resolveKeys();
74
- this._sourceKey = foreign._targetKey;
75
- this._sourceProperty = foreign._targetProperty;
76
- this._targetKey = foreign._sourceKey;
77
- this._targetProperty = foreign._sourceProperty;
78
- return;
79
- }
69
+ // Try to determine key fields from foreign key from target to source
70
+ foreign = await entity_metadata_js_1.EntityMetadata.getForeignKeyFor(target, source);
71
+ if (foreign && foreign !== this) {
72
+ await foreign._resolveKeys();
73
+ this._sourceKey = foreign._targetKey;
74
+ this._sourceProperty = foreign._targetProperty;
75
+ this._targetKey = foreign._sourceKey;
76
+ this._targetProperty = foreign._sourceProperty;
77
+ return;
80
78
  }
81
79
  if (this.many) {
82
80
  if (!sourceKey) {
@@ -59,8 +59,9 @@ var EntityMetadata;
59
59
  EntityMetadata.getEmbeddedField = getEmbeddedField;
60
60
  function getAssociationField(entity, fieldName) {
61
61
  const el = getField(entity, fieldName);
62
- if (el && !(0, orm_helper_js_1.isAssociationField)(el))
62
+ if (el && !(0, orm_helper_js_1.isAssociationField)(el)) {
63
63
  throw new Error(`"${el.name}" requested as "association" but it is "${el.kind}"`);
64
+ }
64
65
  return el;
65
66
  }
66
67
  EntityMetadata.getAssociationField = getAssociationField;
@@ -88,13 +89,14 @@ var EntityMetadata;
88
89
  return out;
89
90
  }
90
91
  // Create a cached name array
91
- if (!Object.prototype.hasOwnProperty.call(entity, '_fieldNames'))
92
+ if (!Object.prototype.hasOwnProperty.call(entity, '_fieldNames')) {
92
93
  Object.defineProperty(entity, '_fieldNames', {
93
94
  enumerable: false,
94
95
  configurable: true,
95
96
  writable: true,
96
97
  value: Object.values(entity.fields).map(m => m.name),
97
98
  });
99
+ }
98
100
  return entity._fieldNames;
99
101
  }
100
102
  EntityMetadata.getFieldNames = getFieldNames;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.REPOSITORY_KEY = exports.ENTITY_METADATA_KEY = void 0;
3
+ exports.DECORATOR_FACTORY = exports.REPOSITORY_KEY = exports.ENTITY_METADATA_KEY = void 0;
4
4
  exports.ENTITY_METADATA_KEY = Symbol.for('SQB_ENTITY_METADATA');
5
5
  exports.REPOSITORY_KEY = Symbol.for('SQB_REPOSITORY');
6
+ exports.DECORATOR_FACTORY = Symbol.for('decorator.factory');
@@ -37,46 +37,31 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
37
37
  }, options);
38
38
  }
39
39
  exists(keyValue, options) {
40
- return this._execute(async (connection) => {
41
- return this._exists(keyValue, { ...options, connection });
42
- }, options);
40
+ return this._execute(async (connection) => this._exists(keyValue, { ...options, connection }), options);
43
41
  }
44
- count(options) {
45
- return this._execute(async (connection) => {
46
- return this._count({ ...options, connection });
47
- }, options);
42
+ existsOne(options) {
43
+ return this._execute(async (connection) => this._existsOne({ ...options, connection }), options);
48
44
  }
49
- /** @deprecated - Use findById instead */
50
- find(keyValue, options) {
51
- return this.findById(keyValue, options);
45
+ count(options) {
46
+ return this._execute(async (connection) => this._count({ ...options, connection }), options);
52
47
  }
53
48
  findById(keyValue, options) {
54
- return this._execute(async (connection) => {
55
- return this._find(keyValue, { ...options, connection });
56
- }, options);
49
+ return this._execute(async (connection) => this._find(keyValue, { ...options, connection }), options);
57
50
  }
58
51
  findOne(options) {
59
- return this._execute(async (connection) => {
60
- return await this._findOne({
61
- ...options,
62
- connection,
63
- });
64
- }, options);
52
+ return this._execute(async (connection) => await this._findOne({
53
+ ...options,
54
+ connection,
55
+ }), options);
65
56
  }
66
57
  findMany(options) {
67
- return this._execute(async (connection) => {
68
- return this._findMany({ ...options, connection });
69
- }, options);
58
+ return this._execute(async (connection) => this._findMany({ ...options, connection }), options);
70
59
  }
71
60
  delete(keyValue, options) {
72
- return this._execute(async (connection) => {
73
- return this._delete(keyValue, { ...options, connection });
74
- }, options);
61
+ return this._execute(async (connection) => this._delete(keyValue, { ...options, connection }), options);
75
62
  }
76
63
  deleteMany(options) {
77
- return this._execute(async (connection) => {
78
- return this._deleteMany({ ...options, connection });
79
- }, options);
64
+ return this._execute(async (connection) => this._deleteMany({ ...options, connection }), options);
80
65
  }
81
66
  update(keyValue, values, options) {
82
67
  return this._execute(async (connection) => {
@@ -87,14 +72,10 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
87
72
  }, options);
88
73
  }
89
74
  updateOnly(keyValue, values, options) {
90
- return this._execute(async (connection) => {
91
- return !!(await this._update(keyValue, values, { ...options, connection }));
92
- }, options);
75
+ return this._execute(async (connection) => !!(await this._update(keyValue, values, { ...options, connection })), options);
93
76
  }
94
77
  updateMany(values, options) {
95
- return this._execute(async (connection) => {
96
- return this._updateMany(values, { ...options, connection });
97
- });
78
+ return this._execute(async (connection) => this._updateMany(values, { ...options, connection }));
98
79
  }
99
80
  async _execute(fn, opts) {
100
81
  let connection = opts?.connection;
@@ -132,11 +113,25 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
132
113
  else
133
114
  filter.push(options.filter);
134
115
  }
135
- return !!(await count_command_js_1.CountCommand.execute({
116
+ return this._existsOne({
117
+ ...options,
118
+ filter,
119
+ });
120
+ }
121
+ async _existsOne(options) {
122
+ const filter = [];
123
+ if (options && options.filter) {
124
+ if (Array.isArray(options.filter))
125
+ filter.push(...options.filter);
126
+ else
127
+ filter.push(options.filter);
128
+ }
129
+ const resp = await find_command_js_1.FindCommand.execute({
136
130
  ...options,
137
131
  filter,
138
132
  entity: this._entity,
139
- }));
133
+ });
134
+ return resp.length > 0;
140
135
  }
141
136
  async _count(options) {
142
137
  return count_command_js_1.CountCommand.execute({
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyMixins = void 0;
3
+ exports.applyMixins = applyMixins;
4
4
  function applyMixins(derivedCtor, baseCtor, filter) {
5
5
  for (const name of Object.getOwnPropertyNames(baseCtor.prototype)) {
6
6
  if (name === 'constructor' ||
7
7
  name === '__proto__' ||
8
8
  name === 'toJSON' ||
9
9
  name === 'toString' ||
10
- (filter && !filter(name)))
10
+ (filter && !filter(name))) {
11
11
  continue;
12
+ }
12
13
  Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null));
13
14
  }
14
15
  }
15
- exports.applyMixins = applyMixins;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extractKeyValues = void 0;
3
+ exports.extractKeyValues = extractKeyValues;
4
4
  const entity_metadata_js_1 = require("../model/entity-metadata.js");
5
5
  const orm_helper_js_1 = require("./orm.helper.js");
6
6
  function extractKeyValues(entityDef, valueOrInstance, keepOther) {
@@ -11,14 +11,16 @@ function extractKeyValues(entityDef, valueOrInstance, keepOther) {
11
11
  const col = entity_metadata_js_1.EntityMetadata.getField(entityDef, k);
12
12
  if (!col)
13
13
  throw new Error(`Unknown column (${k}) defined as primary key in entity "${entityDef.name}"`);
14
- if (!(0, orm_helper_js_1.isColumnField)(col))
14
+ if (!(0, orm_helper_js_1.isColumnField)(col)) {
15
15
  throw new Error(`Column (${k}) defined as primary key in entity "${entityDef.name}" is not a data column`);
16
+ }
16
17
  };
17
18
  // if entity's primary key has more than one key field
18
19
  if (primaryIndex.columns.length > 1) {
19
- if (typeof valueOrInstance !== 'object')
20
+ if (typeof valueOrInstance !== 'object') {
20
21
  throw new Error(`"${entityDef.name}" entity` +
21
22
  ` has more than one primary key field and you must provide all values with an key/value pair`);
23
+ }
22
24
  const valueKeys = Object.keys(valueOrInstance);
23
25
  const valueKeysUpper = valueKeys.map(x => x.toUpperCase());
24
26
  const out = {};
@@ -44,8 +46,9 @@ function extractKeyValues(entityDef, valueOrInstance, keepOther) {
44
46
  const valueKeys = Object.keys(valueOrInstance);
45
47
  const valueKeysUpper = valueKeys.map(x => x.toUpperCase());
46
48
  const k = valueKeysUpper.indexOf(primaryColumnName.toUpperCase());
47
- if (k < 0)
49
+ if (k < 0) {
48
50
  throw new Error(`Value of key field "${entityDef.name}.${primaryColumnName}" required to perform this operation`);
51
+ }
49
52
  const out = { [primaryColumnName]: valueOrInstance[valueKeys[k]] };
50
53
  if (keepOther) {
51
54
  for (let i = 0; i < valueKeys.length; i++) {
@@ -58,4 +61,3 @@ function extractKeyValues(entityDef, valueOrInstance, keepOther) {
58
61
  }
59
62
  return { [primaryColumnName]: valueOrInstance };
60
63
  }
61
- exports.extractKeyValues = extractKeyValues;
@@ -1,26 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveEntityMeta = exports.resolveEntity = exports.isAssociationField = exports.isEmbeddedField = exports.isColumnField = exports.isEntityClass = exports.isClass = void 0;
3
+ exports.isAssociationField = exports.isEmbeddedField = void 0;
4
+ exports.isClass = isClass;
5
+ exports.isEntityClass = isEntityClass;
6
+ exports.isColumnField = isColumnField;
7
+ exports.resolveEntity = resolveEntity;
8
+ exports.resolveEntityMeta = resolveEntityMeta;
4
9
  const orm_const_js_1 = require("../orm.const.js");
5
10
  function isClass(fn) {
6
11
  return typeof fn === 'function' && /^\s*class/.test(fn.toString());
7
12
  }
8
- exports.isClass = isClass;
9
13
  function isEntityClass(fn) {
10
14
  return isClass(fn) && Reflect.hasMetadata(orm_const_js_1.ENTITY_METADATA_KEY, fn);
11
15
  }
12
- exports.isEntityClass = isEntityClass;
13
16
  function isColumnField(f) {
14
17
  return !!(f && f.name && f.kind === 'column');
15
18
  }
16
- exports.isColumnField = isColumnField;
17
- const isEmbeddedField = (f) => {
18
- return !!(f && f.name && f.kind === 'object');
19
- };
19
+ const isEmbeddedField = (f) => !!(f && f.name && f.kind === 'object');
20
20
  exports.isEmbeddedField = isEmbeddedField;
21
- const isAssociationField = (f) => {
22
- return !!(f && f.name && f.kind === 'association');
23
- };
21
+ const isAssociationField = (f) => !!(f && f.name && f.kind === 'association');
24
22
  exports.isAssociationField = isAssociationField;
25
23
  async function resolveEntity(ctorThunk) {
26
24
  if (typeof ctorThunk !== 'function')
@@ -30,9 +28,7 @@ async function resolveEntity(ctorThunk) {
30
28
  if (isEntityClass(ctorThunk))
31
29
  return ctorThunk;
32
30
  }
33
- exports.resolveEntity = resolveEntity;
34
31
  async function resolveEntityMeta(ctorThunk) {
35
32
  const ctor = await resolveEntity(ctorThunk);
36
33
  return ctor && Reflect.getMetadata(orm_const_js_1.ENTITY_METADATA_KEY, ctor);
37
34
  }
38
- exports.resolveEntityMeta = resolveEntityMeta;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FieldsProjection = void 0;
4
+ exports.parseFieldsProjection = parseFieldsProjection;
5
+ const fast_tokenizer_1 = require("fast-tokenizer");
6
+ const FIELD_PATTERN = /^([+-])?([a-z_$]\w*)$/i;
7
+ const NO_DOT_BRACKET_PATTERN = /[^.]\(/g;
8
+ class FieldsProjection {
9
+ }
10
+ exports.FieldsProjection = FieldsProjection;
11
+ (function (FieldsProjection) {
12
+ class Item {
13
+ }
14
+ FieldsProjection.Item = Item;
15
+ })(FieldsProjection || (exports.FieldsProjection = FieldsProjection = {}));
16
+ function parseFieldsProjection(projection, keepCase) {
17
+ const arr = Array.isArray(projection) ? projection : [projection];
18
+ if (!(arr && arr.length))
19
+ return;
20
+ const out = new FieldsProjection();
21
+ for (let s of arr) {
22
+ if (!keepCase)
23
+ s = s.toLowerCase();
24
+ parse(s, out);
25
+ }
26
+ return out;
27
+ }
28
+ function parse(input, target) {
29
+ /** Add dot before brackets which is required to split fields */
30
+ input = input.replace(NO_DOT_BRACKET_PATTERN, s => s.charAt(0) + '.' + s.substring(1));
31
+ const fields = (0, fast_tokenizer_1.splitString)(input, {
32
+ delimiters: '.',
33
+ brackets: true,
34
+ keepBrackets: false,
35
+ });
36
+ for (let i = 0; i < fields.length; i++) {
37
+ const f = fields[i];
38
+ if (f.includes(',')) {
39
+ const subFields = (0, fast_tokenizer_1.splitString)(f, {
40
+ delimiters: ',',
41
+ brackets: true,
42
+ keepBrackets: true,
43
+ });
44
+ for (const n of subFields) {
45
+ parse(n, target);
46
+ }
47
+ continue;
48
+ }
49
+ const m = FIELD_PATTERN.exec(f);
50
+ /* istanbul ignore next */
51
+ if (!m)
52
+ throw new TypeError(`Invalid field path (${input})`);
53
+ const fieldName = m[2];
54
+ const treeItem = (target[fieldName] = target[fieldName] || new FieldsProjection.Item());
55
+ if (m[1])
56
+ treeItem.sign = m[1];
57
+ if (i === fields.length - 1) {
58
+ delete treeItem.projection;
59
+ }
60
+ else {
61
+ target = treeItem.projection = treeItem.projection || new FieldsProjection();
62
+ }
63
+ }
64
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.serializeColumn = void 0;
3
+ exports.serializeColumn = serializeColumn;
4
4
  const builder_1 = require("@sqb/builder");
5
5
  const orm_helper_js_1 = require("./orm.helper.js");
6
6
  function serializeColumn(col, v) {
@@ -16,7 +16,6 @@ function serializeColumn(col, v) {
16
16
  return v.toJSON();
17
17
  return v;
18
18
  }
19
- exports.serializeColumn = serializeColumn;
20
19
  const padZero = (n) => (n < 9 ? '0' : '') + n;
21
20
  function serializeDataValue(dataType, v) {
22
21
  if (v == null)
@@ -176,7 +176,7 @@ export class Cursor extends TypedEventEmitterClass(AsyncEventEmitter) {
176
176
  throw new Error('To move cursor back, it needs cache to be enabled');
177
177
  const _this = this;
178
178
  await this._taskQueue
179
- .enqueue(async function () {
179
+ .enqueue(async () => {
180
180
  /* If moving backward */
181
181
  if (step < 0) {
182
182
  /* Seek cache */
@@ -1,9 +1,37 @@
1
- export let adapters = [];
2
- export function registerAdapter(adapter) {
3
- if (!adapter.driver)
4
- throw new TypeError('A DatabaseAdapter must contain "driver" property');
5
- adapters.push(adapter);
6
- }
7
- export function unRegisterAdapter(...adapter) {
8
- adapters = adapters.filter(x => !adapter.includes(x));
1
+ export class AdapterRegistry {
2
+ static get size() {
3
+ return this.adapters.length;
4
+ }
5
+ static register(...adapters) {
6
+ for (const adapter of adapters) {
7
+ if (!adapter.driver)
8
+ throw new TypeError('A DatabaseAdapter must contain "driver" property');
9
+ this.adapters.push(adapter);
10
+ }
11
+ }
12
+ static forEach(callback, thisArg) {
13
+ return this.adapters.forEach(callback, thisArg);
14
+ }
15
+ static items() {
16
+ return this.adapters.values();
17
+ }
18
+ static unRegister(...extensions) {
19
+ this.adapters = extensions.filter(x => !extensions.includes(x));
20
+ }
21
+ static getAll(dialect) {
22
+ return this.adapters.filter(x => x.dialect === dialect);
23
+ }
24
+ static get(index) {
25
+ return this.adapters[index];
26
+ }
27
+ static findDialect(dialect) {
28
+ return this.adapters.find(x => x.dialect === dialect);
29
+ }
30
+ static findDriver(dialect) {
31
+ return this.adapters.find(x => x.driver === dialect);
32
+ }
33
+ static has(extension) {
34
+ return !!this.adapters.find(x => x === extension);
35
+ }
9
36
  }
37
+ AdapterRegistry.adapters = [];
@@ -16,6 +16,8 @@ export function applyNamingStrategy(value, namingStrategy) {
16
16
  if (!value.match(/[a-z]/))
17
17
  return pascalCase(value.toLowerCase());
18
18
  return pascalCase(value);
19
+ default:
20
+ break;
19
21
  }
20
22
  }
21
23
  else if (typeof namingStrategy === 'function')
@@ -4,7 +4,7 @@ import { coerceToBoolean, coerceToInt } from 'putil-varhelpers';
4
4
  import { AsyncEventEmitter, TypedEventEmitterClass } from 'strict-typed-events';
5
5
  import { EntityMetadata } from '../orm/model/entity-metadata.js';
6
6
  import { Repository } from '../orm/repository.class.js';
7
- import { adapters } from './extensions.js';
7
+ import { AdapterRegistry } from './extensions.js';
8
8
  import { SqbConnection } from './sqb-connection.js';
9
9
  const debug = _debug('sqb:client');
10
10
  const inspect = Symbol.for('nodejs.util.inspect.custom');
@@ -16,12 +16,12 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
16
16
  throw new TypeError('Configuration object required');
17
17
  let adapter;
18
18
  if (config.driver) {
19
- adapter = adapters.find(x => x.driver === config.driver);
19
+ adapter = AdapterRegistry.findDriver(config.driver);
20
20
  if (!adapter)
21
21
  throw new Error(`No database adapter registered for "${config.driver}" driver`);
22
22
  }
23
23
  else if (config.dialect) {
24
- adapter = adapters.find(x => x.dialect === config.dialect);
24
+ adapter = AdapterRegistry.findDialect(config.dialect);
25
25
  if (!adapter)
26
26
  throw new Error(`No database adapter registered for "${config.dialect}" dialect`);
27
27
  }
@@ -1,9 +1,9 @@
1
+ import { classes } from '@sqb/builder';
1
2
  import assert from 'assert';
2
3
  import _debug from 'debug';
3
4
  import { TaskQueue } from 'power-tasks';
4
5
  import { coalesce, coerceToBoolean, coerceToInt, coerceToString } from 'putil-varhelpers';
5
6
  import { AsyncEventEmitter, TypedEventEmitterClass } from 'strict-typed-events';
6
- import { classes } from '@sqb/builder';
7
7
  import { EntityMetadata } from '../orm/model/entity-metadata.js';
8
8
  import { Repository } from '../orm/repository.class.js';
9
9
  import { Cursor } from './cursor.js';
@@ -187,8 +187,9 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
187
187
  async setSavepoint(savepoint) {
188
188
  if (!this._intlcon)
189
189
  throw new Error('Can not call setSavepoint() on a released connection');
190
- if (typeof this._intlcon.setSavepoint !== 'function')
190
+ if (typeof this._intlcon.setSavepoint !== 'function') {
191
191
  throw new Error(this.client.driver + ' does not support setSavepoint method');
192
+ }
192
193
  if (!this.inTransaction)
193
194
  await this.startTransaction();
194
195
  await this._intlcon.setSavepoint(savepoint);
@@ -197,16 +198,18 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
197
198
  async releaseSavepoint(savepoint) {
198
199
  if (!this._intlcon)
199
200
  throw new Error('Can not call releaseSavepoint() on a released connection');
200
- if (typeof this._intlcon.releaseSavepoint !== 'function')
201
+ if (typeof this._intlcon.releaseSavepoint !== 'function') {
201
202
  throw new Error(this.client.driver + ' does not support releaseSavepoint method');
203
+ }
202
204
  await this._intlcon.releaseSavepoint(savepoint);
203
205
  this.emit('release-savepoint');
204
206
  }
205
207
  async rollbackSavepoint(savepoint) {
206
208
  if (!this._intlcon)
207
209
  throw new Error('Can not call rollbackSavepoint() on a released connection');
208
- if (typeof this._intlcon.rollbackSavepoint !== 'function')
210
+ if (typeof this._intlcon.rollbackSavepoint !== 'function') {
209
211
  throw new Error(this.client.driver + ' does not support rollbackSavepoint method');
212
+ }
210
213
  await this._intlcon.rollbackSavepoint(savepoint);
211
214
  this.emit('rollback-savepoint');
212
215
  }