@sqb/connect 4.2.1 → 4.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.
Files changed (154) hide show
  1. package/README.md +67 -0
  2. package/{dist/client/Adapter.js → cjs/client/adapter.js} +0 -0
  3. package/{dist/client/CursorStream.js → cjs/client/cursor-stream.js} +5 -2
  4. package/{dist/client/Cursor.js → cjs/client/cursor.js} +18 -11
  5. package/{dist → cjs}/client/extensions.js +0 -0
  6. package/{dist/client/FieldInfoMap.js → cjs/client/field-info-map.js} +2 -0
  7. package/{dist → cjs}/client/helpers.js +2 -2
  8. package/{dist/client/SqbClient.js → cjs/client/sqb-client.js} +16 -10
  9. package/{dist/client/SqbConnection.js → cjs/client/sqb-connection.js} +17 -14
  10. package/{dist → cjs}/client/types.js +0 -0
  11. package/cjs/index.js +42 -0
  12. package/{dist → cjs}/orm/backward.js +8 -8
  13. package/{dist → cjs}/orm/base-entity.js +8 -7
  14. package/{dist → cjs}/orm/commands/command.helper.js +9 -9
  15. package/{dist → cjs}/orm/commands/count.command.js +2 -2
  16. package/{dist → cjs}/orm/commands/create.command.js +12 -12
  17. package/{dist → cjs}/orm/commands/destroy.command.js +2 -2
  18. package/{dist → cjs}/orm/commands/find.command.js +100 -88
  19. package/{dist → cjs}/orm/commands/row-converter.js +10 -7
  20. package/{dist → cjs}/orm/commands/update.command.js +10 -10
  21. package/{dist → cjs}/orm/decorators/column.decorator.js +3 -3
  22. package/{dist → cjs}/orm/decorators/embedded.decorator.js +3 -3
  23. package/cjs/orm/decorators/entity.decorator.js +179 -0
  24. package/{dist → cjs}/orm/decorators/events.decorator.js +13 -13
  25. package/{dist → cjs}/orm/decorators/foreignkey.decorator.js +3 -3
  26. package/{dist → cjs}/orm/decorators/index.decorator.js +5 -5
  27. package/cjs/orm/decorators/link.decorator.js +70 -0
  28. package/{dist → cjs}/orm/decorators/primarykey.decorator.js +7 -7
  29. package/{dist → cjs}/orm/decorators/transform.decorator.js +5 -5
  30. package/cjs/orm/model/association-field-metadata.js +24 -0
  31. package/{dist → cjs}/orm/model/association-node.js +5 -2
  32. package/{dist → cjs}/orm/model/association.js +31 -39
  33. package/{dist/orm/model/column-element-metadata.js → cjs/orm/model/column-field-metadata.js} +8 -8
  34. package/{dist/orm/model/embedded-element-metadata.js → cjs/orm/model/embedded-field-metadata.js} +8 -8
  35. package/{dist → cjs}/orm/model/entity-metadata.js +90 -91
  36. package/{dist/orm/model/element-metadata.js → cjs/orm/model/field-metadata.js} +0 -0
  37. package/{dist → cjs}/orm/model/index-metadata.js +0 -0
  38. package/{dist → cjs}/orm/model/link-chain.js +12 -15
  39. package/{dist → cjs}/orm/orm.const.js +0 -0
  40. package/{dist → cjs}/orm/orm.type.js +0 -0
  41. package/{dist → cjs}/orm/repository.class.js +25 -22
  42. package/{dist → cjs}/orm/util/apply-mixins.js +0 -0
  43. package/{dist → cjs}/orm/util/extract-keyvalues.js +5 -5
  44. package/{dist → cjs}/orm/util/orm.helper.js +10 -10
  45. package/{dist/orm/util/serialize-element.js → cjs/orm/util/serialize-field.js} +2 -2
  46. package/cjs/package.json +3 -0
  47. package/{dist → cjs}/types.js +0 -0
  48. package/{dist/client/Adapter.d.ts → esm/client/adapter.d.ts} +1 -1
  49. package/esm/client/adapter.js +1 -0
  50. package/{dist/client/CursorStream.d.ts → esm/client/cursor-stream.d.ts} +1 -1
  51. package/esm/client/cursor-stream.js +96 -0
  52. package/{dist/client/Cursor.d.ts → esm/client/cursor.d.ts} +5 -5
  53. package/esm/client/cursor.js +252 -0
  54. package/{dist → esm}/client/extensions.d.ts +1 -1
  55. package/esm/client/extensions.js +9 -0
  56. package/{dist/client/FieldInfoMap.d.ts → esm/client/field-info-map.d.ts} +1 -1
  57. package/esm/client/field-info-map.js +50 -0
  58. package/{dist → esm}/client/helpers.d.ts +3 -3
  59. package/esm/client/helpers.js +125 -0
  60. package/{dist/client/SqbClient.d.ts → esm/client/sqb-client.d.ts} +4 -10
  61. package/esm/client/sqb-client.js +166 -0
  62. package/{dist/client/SqbConnection.d.ts → esm/client/sqb-connection.d.ts} +4 -4
  63. package/esm/client/sqb-connection.js +270 -0
  64. package/{dist → esm}/client/types.d.ts +5 -13
  65. package/esm/client/types.js +1 -0
  66. package/esm/index.d.ts +33 -0
  67. package/esm/index.js +32 -0
  68. package/{dist → esm}/orm/backward.d.ts +1 -1
  69. package/esm/orm/backward.js +17 -0
  70. package/{dist → esm}/orm/base-entity.d.ts +1 -1
  71. package/esm/orm/base-entity.js +25 -0
  72. package/{dist → esm}/orm/commands/command.helper.d.ts +2 -2
  73. package/esm/orm/commands/command.helper.js +135 -0
  74. package/{dist → esm}/orm/commands/count.command.d.ts +3 -3
  75. package/esm/orm/commands/count.command.js +26 -0
  76. package/{dist → esm}/orm/commands/create.command.d.ts +2 -2
  77. package/esm/orm/commands/create.command.js +85 -0
  78. package/{dist → esm}/orm/commands/destroy.command.d.ts +3 -3
  79. package/esm/orm/commands/destroy.command.js +26 -0
  80. package/{dist → esm}/orm/commands/find.command.d.ts +9 -9
  81. package/esm/orm/commands/find.command.js +318 -0
  82. package/{dist → esm}/orm/commands/row-converter.d.ts +5 -5
  83. package/esm/orm/commands/row-converter.js +186 -0
  84. package/{dist → esm}/orm/commands/update.command.d.ts +3 -3
  85. package/esm/orm/commands/update.command.js +80 -0
  86. package/esm/orm/decorators/column.decorator.d.ts +4 -0
  87. package/esm/orm/decorators/column.decorator.js +19 -0
  88. package/esm/orm/decorators/embedded.decorator.d.ts +3 -0
  89. package/esm/orm/decorators/embedded.decorator.js +12 -0
  90. package/{dist → esm}/orm/decorators/entity.decorator.d.ts +17 -17
  91. package/esm/orm/decorators/entity.decorator.js +175 -0
  92. package/{dist → esm}/orm/decorators/events.decorator.d.ts +0 -0
  93. package/esm/orm/decorators/events.decorator.js +55 -0
  94. package/{dist → esm}/orm/decorators/foreignkey.decorator.d.ts +1 -1
  95. package/esm/orm/decorators/foreignkey.decorator.js +9 -0
  96. package/{dist → esm}/orm/decorators/index.decorator.d.ts +1 -1
  97. package/esm/orm/decorators/index.decorator.js +17 -0
  98. package/esm/orm/decorators/link.decorator.d.ts +13 -0
  99. package/esm/orm/decorators/link.decorator.js +66 -0
  100. package/{dist → esm}/orm/decorators/primarykey.decorator.d.ts +1 -1
  101. package/esm/orm/decorators/primarykey.decorator.js +20 -0
  102. package/{dist → esm}/orm/decorators/transform.decorator.d.ts +1 -1
  103. package/esm/orm/decorators/transform.decorator.js +19 -0
  104. package/esm/orm/model/association-field-metadata.d.ts +12 -0
  105. package/esm/orm/model/association-field-metadata.js +20 -0
  106. package/{dist → esm}/orm/model/association-node.d.ts +1 -1
  107. package/esm/orm/model/association-node.js +24 -0
  108. package/{dist → esm}/orm/model/association.d.ts +6 -7
  109. package/esm/orm/model/association.js +118 -0
  110. package/{dist/orm/model/column-element-metadata.d.ts → esm/orm/model/column-field-metadata.d.ts} +10 -14
  111. package/esm/orm/model/column-field-metadata.js +28 -0
  112. package/esm/orm/model/embedded-field-metadata.d.ts +14 -0
  113. package/esm/orm/model/embedded-field-metadata.js +25 -0
  114. package/esm/orm/model/entity-metadata.d.ts +50 -0
  115. package/esm/orm/model/entity-metadata.js +337 -0
  116. package/esm/orm/model/field-metadata.d.ts +16 -0
  117. package/esm/orm/model/field-metadata.js +1 -0
  118. package/{dist → esm}/orm/model/index-metadata.d.ts +0 -0
  119. package/esm/orm/model/index-metadata.js +1 -0
  120. package/esm/orm/model/link-chain.d.ts +13 -0
  121. package/esm/orm/model/link-chain.js +43 -0
  122. package/{dist → esm}/orm/orm.const.d.ts +0 -0
  123. package/esm/orm/orm.const.js +2 -0
  124. package/{dist → esm}/orm/orm.type.d.ts +3 -4
  125. package/esm/orm/orm.type.js +1 -0
  126. package/{dist → esm}/orm/repository.class.d.ts +23 -23
  127. package/esm/orm/repository.class.js +238 -0
  128. package/{dist → esm}/orm/util/apply-mixins.d.ts +0 -0
  129. package/esm/orm/util/apply-mixins.js +9 -0
  130. package/{dist → esm}/orm/util/extract-keyvalues.d.ts +1 -1
  131. package/esm/orm/util/extract-keyvalues.js +57 -0
  132. package/esm/orm/util/orm.helper.d.ts +13 -0
  133. package/esm/orm/util/orm.helper.js +28 -0
  134. package/esm/orm/util/serialize-field.d.ts +2 -0
  135. package/esm/orm/util/serialize-field.js +39 -0
  136. package/esm/types.d.ts +7 -0
  137. package/esm/types.js +1 -0
  138. package/package.json +54 -41
  139. package/dist/index.d.ts +0 -33
  140. package/dist/index.js +0 -42
  141. package/dist/orm/decorators/column.decorator.d.ts +0 -4
  142. package/dist/orm/decorators/embedded.decorator.d.ts +0 -3
  143. package/dist/orm/decorators/entity.decorator.js +0 -179
  144. package/dist/orm/decorators/link.decorator.d.ts +0 -23
  145. package/dist/orm/decorators/link.decorator.js +0 -88
  146. package/dist/orm/model/association-element-metadata.d.ts +0 -10
  147. package/dist/orm/model/association-element-metadata.js +0 -15
  148. package/dist/orm/model/element-metadata.d.ts +0 -7
  149. package/dist/orm/model/embedded-element-metadata.d.ts +0 -14
  150. package/dist/orm/model/entity-metadata.d.ts +0 -50
  151. package/dist/orm/model/link-chain.d.ts +0 -15
  152. package/dist/orm/util/orm.helper.d.ts +0 -13
  153. package/dist/orm/util/serialize-element.d.ts +0 -2
  154. package/dist/types.d.ts +0 -2
@@ -0,0 +1,238 @@
1
+ import { AsyncEventEmitter, TypedEventEmitterClass } from 'strict-typed-events';
2
+ import { SqbConnection } from '../client/sqb-connection.js';
3
+ import { CountCommand } from './commands/count.command.js';
4
+ import { CreateCommand } from './commands/create.command.js';
5
+ import { DestroyCommand } from './commands/destroy.command.js';
6
+ import { FindCommand } from './commands/find.command.js';
7
+ import { UpdateCommand } from './commands/update.command.js';
8
+ import { extractKeyValues } from './util/extract-keyvalues.js';
9
+ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
10
+ _executor;
11
+ _entity;
12
+ _schema;
13
+ constructor(entityDef, executor, schema) {
14
+ super();
15
+ this._executor = executor;
16
+ this._entity = entityDef;
17
+ this._schema = schema;
18
+ }
19
+ get entity() {
20
+ return this._entity;
21
+ }
22
+ get type() {
23
+ return this._entity.ctor;
24
+ }
25
+ create(values, options) {
26
+ return this._execute(async (connection) => {
27
+ return this._create(values, { ...options, connection });
28
+ }, options);
29
+ }
30
+ createOnly(values, options) {
31
+ return this._execute(async (connection) => {
32
+ return this._createOnly(values, { ...options, connection });
33
+ }, options);
34
+ }
35
+ exists(keyValue, options) {
36
+ return this._execute(async (connection) => {
37
+ return this._exists(keyValue, { ...options, connection });
38
+ }, options);
39
+ }
40
+ count(options) {
41
+ return this._execute(async (connection) => {
42
+ return this._count({ ...options, connection });
43
+ }, options);
44
+ }
45
+ findAll(options) {
46
+ return this._execute(async (connection) => {
47
+ return this._findAll({ ...options, connection });
48
+ }, options);
49
+ }
50
+ findOne(options) {
51
+ return this._execute(async (connection) => {
52
+ return this._findOne({ ...options, connection });
53
+ }, options);
54
+ }
55
+ findByPk(keyValue, options) {
56
+ return this._execute(async (connection) => {
57
+ return this._findByPk(keyValue, { ...options, connection });
58
+ }, options);
59
+ }
60
+ destroy(keyValue, options) {
61
+ return this._execute(async (connection) => {
62
+ return this._destroy(keyValue, { ...options, connection });
63
+ }, options);
64
+ }
65
+ destroyAll(options) {
66
+ return this._execute(async (connection) => {
67
+ return this._destroyAll({ ...options, connection });
68
+ }, options);
69
+ }
70
+ update(keyValue, values, options) {
71
+ return this._execute(async (connection) => {
72
+ const opts = { ...options, connection };
73
+ const keyValues = await this._update(keyValue, values, opts);
74
+ if (keyValues)
75
+ return this._findByPk(keyValues, opts);
76
+ }, options);
77
+ }
78
+ updateOnly(keyValue, values, options) {
79
+ return this._execute(async (connection) => {
80
+ return !!(await this._update(keyValue, values, { ...options, connection }));
81
+ }, options);
82
+ }
83
+ updateAll(values, options) {
84
+ return this._execute(async (connection) => {
85
+ return this._updateAll(values, { ...options, connection });
86
+ });
87
+ }
88
+ async _execute(fn, opts) {
89
+ let connection = opts?.connection;
90
+ if (!connection && this._executor instanceof SqbConnection)
91
+ connection = this._executor;
92
+ if (connection) {
93
+ if (this._schema)
94
+ await connection.setSchema(this._schema);
95
+ return fn(connection);
96
+ }
97
+ return this._executor.acquire(async (conn) => {
98
+ if (this._schema)
99
+ await conn.setSchema(this._schema);
100
+ await this.emitAsyncSerial('acquire', conn);
101
+ return fn(conn);
102
+ });
103
+ }
104
+ async _create(values, options) {
105
+ await this._emit('before-create', values, options);
106
+ const keyValues = await CreateCommand.execute({
107
+ ...options,
108
+ entity: this._entity,
109
+ values,
110
+ returning: true
111
+ });
112
+ const result = keyValues && (await this.findByPk(keyValues, options));
113
+ if (!result)
114
+ throw new Error('Unable to insert new row');
115
+ await this._emit('after-create', result, options);
116
+ return result;
117
+ }
118
+ async _createOnly(values, options) {
119
+ await this._emit('before-create', values, options);
120
+ await CreateCommand.execute({
121
+ ...options,
122
+ entity: this._entity,
123
+ values,
124
+ returning: false
125
+ });
126
+ }
127
+ async _exists(keyValue, options) {
128
+ const filter = [extractKeyValues(this._entity, keyValue, true)];
129
+ if (options && options.filter) {
130
+ if (Array.isArray(options.filter))
131
+ filter.push(...options.filter);
132
+ else
133
+ filter.push(options.filter);
134
+ }
135
+ return !!(await CountCommand.execute({
136
+ ...options,
137
+ filter,
138
+ entity: this._entity
139
+ }));
140
+ }
141
+ async _count(options) {
142
+ return CountCommand.execute({
143
+ ...options,
144
+ entity: this._entity
145
+ });
146
+ }
147
+ async _findAll(options) {
148
+ return await FindCommand.execute({
149
+ ...options,
150
+ entity: this._entity,
151
+ });
152
+ }
153
+ async _findOne(options) {
154
+ const rows = await FindCommand.execute({
155
+ ...options,
156
+ entity: this._entity,
157
+ limit: 1
158
+ });
159
+ return rows && rows[0];
160
+ }
161
+ async _findByPk(keyValue, options) {
162
+ const filter = [extractKeyValues(this._entity, keyValue, true)];
163
+ if (options && options.filter) {
164
+ if (Array.isArray(options.filter))
165
+ filter.push(...options.filter);
166
+ else
167
+ filter.push(options.filter);
168
+ }
169
+ return await this._findOne({ ...options, filter, offset: 0 });
170
+ }
171
+ async _destroy(keyValue, options) {
172
+ await this._emit('before-destroy', keyValue, options);
173
+ const filter = [extractKeyValues(this._entity, keyValue, true)];
174
+ if (options && options.filter) {
175
+ if (Array.isArray(options.filter))
176
+ filter.push(...options.filter);
177
+ else
178
+ filter.push(options.filter);
179
+ }
180
+ const result = !!(await DestroyCommand.execute({
181
+ ...options,
182
+ filter,
183
+ entity: this._entity,
184
+ }));
185
+ await this._emit('after-destroy', result, keyValue, options);
186
+ return result;
187
+ }
188
+ async _destroyAll(options) {
189
+ await this._emit('before-destroy-all', options);
190
+ const rowsAffected = DestroyCommand.execute({
191
+ ...options,
192
+ entity: this._entity,
193
+ filter: options?.filter,
194
+ params: options?.params
195
+ });
196
+ await this._emit('after-destroy-all', rowsAffected, options);
197
+ return rowsAffected;
198
+ }
199
+ async _update(keyValue, values, options) {
200
+ await this._emit('before-update', keyValue, values, options);
201
+ const keyValues = extractKeyValues(this._entity, keyValue, true);
202
+ const filter = [keyValues];
203
+ if (options.filter) {
204
+ if (Array.isArray(options.filter))
205
+ filter.push(...options.filter);
206
+ else
207
+ filter.push(options.filter);
208
+ }
209
+ const updateValues = { ...values };
210
+ Object.keys(keyValues).forEach(k => delete updateValues[k]);
211
+ const rowsAffected = await UpdateCommand.execute({
212
+ ...options,
213
+ entity: this._entity,
214
+ values: updateValues,
215
+ filter
216
+ });
217
+ await this._emit('after-update', rowsAffected, keyValues, options);
218
+ return rowsAffected ? keyValues : undefined;
219
+ }
220
+ async _updateAll(values, options) {
221
+ await this._emit('before-update-all', values, options);
222
+ const rowsAffected = await UpdateCommand.execute({
223
+ ...options,
224
+ entity: this._entity,
225
+ values
226
+ });
227
+ await this._emit('after-update-all', rowsAffected, values, options);
228
+ return rowsAffected;
229
+ }
230
+ async _emit(event, ...args) {
231
+ const events = this._entity.eventListeners && this._entity.eventListeners[event];
232
+ if (events) {
233
+ for (const fn of events) {
234
+ await fn(this, ...args);
235
+ }
236
+ }
237
+ }
238
+ }
File without changes
@@ -0,0 +1,9 @@
1
+ export function applyMixins(derivedCtor, baseCtor, filter) {
2
+ for (const name of Object.getOwnPropertyNames(baseCtor.prototype)) {
3
+ if ((name === 'constructor' || name === '__proto__' || name === 'toJSON' || name === 'toString') ||
4
+ filter && !filter(name))
5
+ continue;
6
+ Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
7
+ Object.create(null));
8
+ }
9
+ }
@@ -1,2 +1,2 @@
1
- import { EntityMetadata } from '../model/entity-metadata';
1
+ import { EntityMetadata } from '../model/entity-metadata.js';
2
2
  export declare function extractKeyValues<T>(entityDef: EntityMetadata, valueOrInstance: any | Record<string, any> | T, keepOther?: boolean): Record<string, any>;
@@ -0,0 +1,57 @@
1
+ import { EntityMetadata } from '../model/entity-metadata.js';
2
+ import { isColumnField } from './orm.helper.js';
3
+ export function extractKeyValues(entityDef, valueOrInstance, keepOther) {
4
+ const primaryIndex = EntityMetadata.getPrimaryIndex(entityDef);
5
+ if (!primaryIndex)
6
+ throw new Error(`No primary fields defined for "${entityDef.name}" entity`);
7
+ const validateCol = (k) => {
8
+ const col = EntityMetadata.getField(entityDef, k);
9
+ if (!col)
10
+ throw new Error(`Unknown column (${k}) defined as primary key in entity "${entityDef.name}"`);
11
+ if (!isColumnField(col))
12
+ throw new Error(`Column (${k}) defined as primary key in entity "${entityDef.name}" is not a data column`);
13
+ };
14
+ // if entity's primary key has more than one key field
15
+ if (primaryIndex.columns.length > 1) {
16
+ if (typeof valueOrInstance !== 'object')
17
+ throw new Error(`"${entityDef.name}" entity` +
18
+ ` has more than one primary key field and you must provide all values with an key/value pair`);
19
+ const valueKeys = Object.keys(valueOrInstance);
20
+ const valueKeysUpper = valueKeys.map(x => x.toUpperCase());
21
+ const out = {};
22
+ for (const k of primaryIndex.columns) {
23
+ const i = valueKeysUpper.indexOf(k.toUpperCase());
24
+ if (i < 0)
25
+ throw new Error(`Value of key field "${entityDef.name}.${k}" required to perform this operation`);
26
+ validateCol(k);
27
+ out[k] = valueOrInstance[valueKeys[i]];
28
+ }
29
+ if (keepOther) {
30
+ for (let i = 0; i < valueKeys.length; i++) {
31
+ if (primaryIndex.columns.find(x => x.toUpperCase() === valueKeysUpper[i]))
32
+ continue;
33
+ out[valueKeys[i]] = valueOrInstance[valueKeys[i]];
34
+ }
35
+ }
36
+ return out;
37
+ }
38
+ const primaryColumnName = primaryIndex.columns[0];
39
+ validateCol(primaryColumnName);
40
+ if (typeof valueOrInstance === 'object') {
41
+ const valueKeys = Object.keys(valueOrInstance);
42
+ const valueKeysUpper = valueKeys.map(x => x.toUpperCase());
43
+ const k = valueKeysUpper.indexOf(primaryColumnName.toUpperCase());
44
+ if (k < 0)
45
+ throw new Error(`Value of key field "${entityDef.name}.${primaryColumnName}" required to perform this operation`);
46
+ const out = { [primaryColumnName]: valueOrInstance[valueKeys[k]] };
47
+ if (keepOther) {
48
+ for (let i = 0; i < valueKeys.length; i++) {
49
+ if (primaryIndex.columns.find(x => x.toUpperCase() === valueKeysUpper[i]))
50
+ continue;
51
+ out[valueKeys[i]] = valueOrInstance[valueKeys[i]];
52
+ }
53
+ }
54
+ return out;
55
+ }
56
+ return { [primaryColumnName]: valueOrInstance };
57
+ }
@@ -0,0 +1,13 @@
1
+ import { Type } from 'ts-gems';
2
+ import { AssociationFieldMetadata } from '../model/association-field-metadata.js';
3
+ import type { ColumnFieldMetadata } from '../model/column-field-metadata.js';
4
+ import { EmbeddedFieldMetadata } from '../model/embedded-field-metadata.js';
5
+ import type { EntityMetadata } from '../model/entity-metadata.js';
6
+ import type { TypeThunk } from '../orm.type.js';
7
+ export declare function isClass(fn: any): fn is Type;
8
+ export declare function isEntityClass(fn: any): fn is Type;
9
+ export declare function isColumnField(f: any): f is ColumnFieldMetadata;
10
+ export declare const isEmbeddedField: (f: any) => f is EmbeddedFieldMetadata;
11
+ export declare const isAssociationField: (f: any) => f is AssociationFieldMetadata;
12
+ export declare function resolveEntity(ctorThunk: TypeThunk): Promise<Type | undefined>;
13
+ export declare function resolveEntityMeta(ctorThunk: TypeThunk): Promise<EntityMetadata | undefined>;
@@ -0,0 +1,28 @@
1
+ import { ENTITY_METADATA_KEY } from '../orm.const.js';
2
+ export function isClass(fn) {
3
+ return typeof fn === 'function' && /^\s*class/.test(fn.toString());
4
+ }
5
+ export function isEntityClass(fn) {
6
+ return isClass(fn) && Reflect.hasMetadata(ENTITY_METADATA_KEY, fn);
7
+ }
8
+ export function isColumnField(f) {
9
+ return !!(f && f.name && f.kind === 'column');
10
+ }
11
+ export const isEmbeddedField = (f) => {
12
+ return !!(f && f.name && f.kind === 'object');
13
+ };
14
+ export const isAssociationField = (f) => {
15
+ return !!(f && f.name && f.kind === 'association');
16
+ };
17
+ export async function resolveEntity(ctorThunk) {
18
+ if (typeof ctorThunk !== 'function')
19
+ return;
20
+ if (!isClass(ctorThunk))
21
+ ctorThunk = await ctorThunk();
22
+ if (isEntityClass(ctorThunk))
23
+ return ctorThunk;
24
+ }
25
+ export async function resolveEntityMeta(ctorThunk) {
26
+ const ctor = await resolveEntity(ctorThunk);
27
+ return ctor && Reflect.getMetadata(ENTITY_METADATA_KEY, ctor);
28
+ }
@@ -0,0 +1,2 @@
1
+ import { FieldMetadata } from '../model/field-metadata.js';
2
+ export declare function serializeColumn(col: FieldMetadata, v: any): any;
@@ -0,0 +1,39 @@
1
+ import { DataType } from '@sqb/builder';
2
+ import { isColumnField } from './orm.helper.js';
3
+ export function serializeColumn(col, v) {
4
+ if (isColumnField(col)) {
5
+ if (col.isArray) {
6
+ if (Array.isArray(v))
7
+ return v.map(x => serializeDataValue(col.dataType || DataType.VARCHAR, x));
8
+ return [serializeDataValue(col.dataType || DataType.VARCHAR, v)];
9
+ }
10
+ return serializeDataValue(col.dataType || DataType.VARCHAR, v);
11
+ }
12
+ if (typeof v === 'object' && typeof v.toJSON === 'function')
13
+ return v.toJSON();
14
+ return v;
15
+ }
16
+ const padZero = (n) => (n < 9 ? '0' : '') + n;
17
+ function serializeDataValue(dataType, v) {
18
+ if (v == null)
19
+ return;
20
+ if (v instanceof Date &&
21
+ (dataType === DataType.DATE || dataType === DataType.TIMESTAMP)) {
22
+ return v.getFullYear() + '-' +
23
+ padZero(v.getMonth() + 1) + '-' +
24
+ padZero(v.getDate()) + (dataType === DataType.TIMESTAMP ?
25
+ 'T' + padZero(v.getHours()) + ':' +
26
+ padZero(v.getMinutes()) + ':' +
27
+ padZero(v.getSeconds()) : '');
28
+ }
29
+ if (v instanceof Buffer)
30
+ return v.toString('base64');
31
+ if (typeof v === 'number' && (dataType === DataType.SMALLINT ||
32
+ dataType === DataType.INTEGER))
33
+ return Math.trunc(v);
34
+ if (typeof v === 'bigint')
35
+ return v.toString();
36
+ if (v && typeof v === 'object' && typeof v.toJSON === 'function')
37
+ return v.toJSON();
38
+ return v;
39
+ }
package/esm/types.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Builtin, DeepPickWritable } from 'ts-gems';
2
+ export declare type EntityData<T> = DeepNullableIfPartial<DeepPickWritable<T>>;
3
+ export declare type DeepNullableIfPartial<T> = _DeepNullableIfPartial<T>;
4
+ declare type _DeepNullableIfPartial<T> = T extends Builtin ? T : T extends Promise<infer U> ? Promise<DeepNullableIfPartial<U>> : T extends (infer U)[] ? DeepNullableIfPartial<U>[] : {
5
+ [P in keyof T]?: DeepNullableIfPartial<Exclude<T[P], undefined> | null>;
6
+ };
7
+ export {};
package/esm/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@sqb/connect",
3
3
  "description": "Multi-dialect database connection framework written with TypeScript",
4
- "version": "4.2.1",
4
+ "version": "4.5.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
- "Eray Hanoglu <e.hanoglu@panates.com>"
7
+ "Eray Hanoglu <e.hanoglu@panates.com>",
8
+ "Ilker Gurelli <i.gurelli@panates.com>"
8
9
  ],
9
10
  "license": "Apache-2.0",
10
11
  "repository": {
@@ -12,23 +13,21 @@
12
13
  "url": "https://github.com/sqbjs/sqb.git",
13
14
  "directory": "packages/connect"
14
15
  },
15
- "keywords": [
16
- "javascript",
17
- "typescript",
18
- "sqb",
19
- "rdbms",
20
- "sql",
21
- "client",
22
- "dialect",
23
- "multi-dialect",
24
- "database",
25
- "oracle",
26
- "postgres",
27
- "pg",
28
- "mssql",
29
- "sqlite",
30
- "mysql"
31
- ],
16
+ "scripts": {
17
+ "compile": "tsc",
18
+ "prebuild": "npm run lint && npm run clean",
19
+ "build": "npm run build:cjs && npm run build:esm",
20
+ "build:cjs": "tsc -b tsconfig-build-cjs.json",
21
+ "build:esm": "tsc -b tsconfig-build-esm.json",
22
+ "postbuild": "cp README.md package.json ../../LICENSE ../../build/connect && cp ../../package.cjs.json ../../build/connect/cjs/package.json",
23
+ "lint": "eslint .",
24
+ "test": "jest",
25
+ "cover": "jest --collect-coverage",
26
+ "clean": "npm run clean:src | npm run clean:dist | npm run clean:cover",
27
+ "clean:src": "ts-cleanup -s src --all",
28
+ "clean:dist": "rimraf ../../build/connectt",
29
+ "clean:cover": "rimraf ../../coverage/connect"
30
+ },
32
31
  "dependencies": {
33
32
  "debug": "^4.3.4",
34
33
  "lightning-pool": "^3.1.4",
@@ -44,33 +43,47 @@
44
43
  },
45
44
  "devDependencies": {
46
45
  "@types/debug": "^4.1.7",
47
- "@types/lodash": "^4.14.182"
46
+ "@types/lodash": "^4.14.184"
48
47
  },
49
48
  "peerDependencies": {
50
- "@sqb/builder": "^4.2.0"
49
+ "@sqb/builder": "^4.5.0"
51
50
  },
52
- "main": "dist/index.js",
53
- "types": "dist/index.d.ts",
54
- "engines": {
55
- "node": ">= 16.0"
51
+ "type": "module",
52
+ "main": "cjs/index.js",
53
+ "module": "esm/index.js",
54
+ "types": "esm/index.d.ts",
55
+ "exports": {
56
+ ".": {
57
+ "require": "./cjs/index.js",
58
+ "default": "./esm/index.js"
59
+ },
60
+ "./cjs": "./cjs/index.js",
61
+ "./esm": "./esm/index.js"
56
62
  },
57
- "directories": {
58
- "lib": "dist",
59
- "test": "test"
63
+ "engines": {
64
+ "node": ">=16.0",
65
+ "npm": ">=7.0.0"
60
66
  },
61
67
  "files": [
68
+ "bin/",
69
+ "cjs/",
70
+ "esm/",
62
71
  "LICENSE",
63
- "README.md",
64
- "dist/"
72
+ "README.md"
65
73
  ],
66
- "scripts": {
67
- "build": "tsc -b tsconfig-build.json",
68
- "prebuild": "npm run lint && npm run clean:dist",
69
- "postbuild": "copyfiles package.json README.md LICENSE ../../build/connect",
70
- "clean": "ts-cleanup -s src --all & ts-cleanup -s test",
71
- "clean:dist": "rimraf ../../build/connect",
72
- "compile": "tsc",
73
- "lint": "eslint src/** --no-error-on-unmatched-pattern",
74
- "test": "TS_NODE_PROJECT='./test/tsconfig.json' mocha -r ts-node/register -r tsconfig-paths/register --reporter spec test/**/*.spec.ts"
75
- }
76
- }
74
+ "keywords": [
75
+ "sqb",
76
+ "rdbms",
77
+ "sql",
78
+ "client",
79
+ "dialect",
80
+ "multi-dialect",
81
+ "database",
82
+ "oracle",
83
+ "postgres",
84
+ "pg",
85
+ "mssql",
86
+ "sqlite",
87
+ "mysql"
88
+ ]
89
+ }
package/dist/index.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import 'reflect-metadata';
2
- export { Type, Maybe, DeepPartial, DeepPickWritable, DeepBuildable } from 'ts-gems';
3
- export * from './types';
4
- export * from './client/types';
5
- export * from './client/Adapter';
6
- export * from './client/SqbClient';
7
- export * from './client/SqbConnection';
8
- export * from './client/Cursor';
9
- export { registerAdapter, unRegisterAdapter } from './client/extensions';
10
- export * from './orm/orm.type';
11
- export * from './orm/orm.const';
12
- export * from './orm/base-entity';
13
- export * from './orm/repository.class';
14
- export * from './orm/model/entity-metadata';
15
- export * from './orm/model/element-metadata';
16
- export * from './orm/model/column-element-metadata';
17
- export * from './orm/model/embedded-element-metadata';
18
- export * from './orm/model/association-element-metadata';
19
- export * from './orm/model/association';
20
- export * from './orm/model/association-node';
21
- export * from './orm/model/index-metadata';
22
- export * from './orm/model/link-chain';
23
- export * from './orm/decorators/column.decorator';
24
- export * from './orm/decorators/embedded.decorator';
25
- export * from './orm/decorators/entity.decorator';
26
- export * from './orm/decorators/events.decorator';
27
- export * from './orm/decorators/foreignkey.decorator';
28
- export * from './orm/decorators/index.decorator';
29
- export * from './orm/decorators/link.decorator';
30
- export * from './orm/decorators/primarykey.decorator';
31
- export * from './orm/decorators/transform.decorator';
32
- export { isColumnElement, isEmbeddedElement, isAssociationElement, isEntityClass } from './orm/util/orm.helper';
33
- export * from './orm/backward';
package/dist/index.js DELETED
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isEntityClass = exports.isAssociationElement = exports.isEmbeddedElement = exports.isColumnElement = exports.unRegisterAdapter = exports.registerAdapter = void 0;
4
- const tslib_1 = require("tslib");
5
- require("reflect-metadata");
6
- tslib_1.__exportStar(require("./types"), exports);
7
- tslib_1.__exportStar(require("./client/types"), exports);
8
- tslib_1.__exportStar(require("./client/Adapter"), exports);
9
- tslib_1.__exportStar(require("./client/SqbClient"), exports);
10
- tslib_1.__exportStar(require("./client/SqbConnection"), exports);
11
- tslib_1.__exportStar(require("./client/Cursor"), exports);
12
- var extensions_1 = require("./client/extensions");
13
- Object.defineProperty(exports, "registerAdapter", { enumerable: true, get: function () { return extensions_1.registerAdapter; } });
14
- Object.defineProperty(exports, "unRegisterAdapter", { enumerable: true, get: function () { return extensions_1.unRegisterAdapter; } });
15
- tslib_1.__exportStar(require("./orm/orm.type"), exports);
16
- tslib_1.__exportStar(require("./orm/orm.const"), exports);
17
- tslib_1.__exportStar(require("./orm/base-entity"), exports);
18
- tslib_1.__exportStar(require("./orm/repository.class"), exports);
19
- tslib_1.__exportStar(require("./orm/model/entity-metadata"), exports);
20
- tslib_1.__exportStar(require("./orm/model/element-metadata"), exports);
21
- tslib_1.__exportStar(require("./orm/model/column-element-metadata"), exports);
22
- tslib_1.__exportStar(require("./orm/model/embedded-element-metadata"), exports);
23
- tslib_1.__exportStar(require("./orm/model/association-element-metadata"), exports);
24
- tslib_1.__exportStar(require("./orm/model/association"), exports);
25
- tslib_1.__exportStar(require("./orm/model/association-node"), exports);
26
- tslib_1.__exportStar(require("./orm/model/index-metadata"), exports);
27
- tslib_1.__exportStar(require("./orm/model/link-chain"), exports);
28
- tslib_1.__exportStar(require("./orm/decorators/column.decorator"), exports);
29
- tslib_1.__exportStar(require("./orm/decorators/embedded.decorator"), exports);
30
- tslib_1.__exportStar(require("./orm/decorators/entity.decorator"), exports);
31
- tslib_1.__exportStar(require("./orm/decorators/events.decorator"), exports);
32
- tslib_1.__exportStar(require("./orm/decorators/foreignkey.decorator"), exports);
33
- tslib_1.__exportStar(require("./orm/decorators/index.decorator"), exports);
34
- tslib_1.__exportStar(require("./orm/decorators/link.decorator"), exports);
35
- tslib_1.__exportStar(require("./orm/decorators/primarykey.decorator"), exports);
36
- tslib_1.__exportStar(require("./orm/decorators/transform.decorator"), exports);
37
- var orm_helper_1 = require("./orm/util/orm.helper");
38
- Object.defineProperty(exports, "isColumnElement", { enumerable: true, get: function () { return orm_helper_1.isColumnElement; } });
39
- Object.defineProperty(exports, "isEmbeddedElement", { enumerable: true, get: function () { return orm_helper_1.isEmbeddedElement; } });
40
- Object.defineProperty(exports, "isAssociationElement", { enumerable: true, get: function () { return orm_helper_1.isAssociationElement; } });
41
- Object.defineProperty(exports, "isEntityClass", { enumerable: true, get: function () { return orm_helper_1.isEntityClass; } });
42
- tslib_1.__exportStar(require("./orm/backward"), exports);
@@ -1,4 +0,0 @@
1
- import { DataType } from '@sqb/builder';
2
- import { ColumnElementOptions } from '../model/column-element-metadata';
3
- export declare function Column(type?: DataType): PropertyDecorator;
4
- export declare function Column(options?: ColumnElementOptions): PropertyDecorator;
@@ -1,3 +0,0 @@
1
- import { EmbeddedElementOptions } from '../model/embedded-element-metadata';
2
- import { TypeThunk } from '../orm.type';
3
- export declare function Embedded(type?: TypeThunk, options?: EmbeddedElementOptions): PropertyDecorator;