@iamkirbki/database-handler-core 4.4.3 → 4.4.4

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.
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import Table from "../base/Table.js";
11
2
  class Repository {
12
3
  constructor(tableName, ModelClass, customDatabaseAdapter, tableFactory = (name, adapter) => new Table(name, adapter)) {
@@ -40,38 +31,30 @@ class Repository {
40
31
  [relation.pivotForeignKey]: isLocal ? modelOfOrigin.values[relation.foreignKey] : foreignKey
41
32
  };
42
33
  }
43
- insertRecordIntoPivotTable(foreignKey, modelOfOrigin, relation) {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- const table = this.tableFactory(relation.pivotTable, this.customDatabaseAdapter);
46
- yield table.Insert(this.generatePivotTableKeys(foreignKey, modelOfOrigin, relation));
47
- });
34
+ async insertRecordIntoPivotTable(foreignKey, modelOfOrigin, relation) {
35
+ const table = this.tableFactory(relation.pivotTable, this.customDatabaseAdapter);
36
+ await table.Insert(this.generatePivotTableKeys(foreignKey, modelOfOrigin, relation));
48
37
  }
49
- deleteRecordFromPivotTable(foreignKey, modelOfOrigin, relation) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- const table = this.tableFactory(relation.pivotTable, this.customDatabaseAdapter);
52
- const record = yield table.Record({ base: { where: this.generatePivotTableKeys(foreignKey, modelOfOrigin, relation) } });
53
- yield (record === null || record === void 0 ? void 0 : record.Delete());
54
- });
38
+ async deleteRecordFromPivotTable(foreignKey, modelOfOrigin, relation) {
39
+ const table = this.tableFactory(relation.pivotTable, this.customDatabaseAdapter);
40
+ const record = await table.Record({ base: { where: this.generatePivotTableKeys(foreignKey, modelOfOrigin, relation) } });
41
+ await (record === null || record === void 0 ? void 0 : record.Delete());
55
42
  }
56
- getManyToManyRelation(relation) {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- if (relation.pivotTable && this.manyToManyRelations.has(relation.pivotTable)) {
59
- return this.manyToManyRelations.get(relation.pivotTable);
60
- }
61
- if (yield this.doesTableExist(relation.pivotTable)) {
62
- this.manyToManyRelations.set(relation.pivotTable, relation);
63
- return relation;
64
- }
65
- else {
66
- throw new Error(`Pivot table ${relation.pivotTable} does not exist. Create it in alphabetical order before using many-to-many relationships.`);
67
- }
68
- });
43
+ async getManyToManyRelation(relation) {
44
+ if (relation.pivotTable && this.manyToManyRelations.has(relation.pivotTable)) {
45
+ return this.manyToManyRelations.get(relation.pivotTable);
46
+ }
47
+ if (await this.doesTableExist(relation.pivotTable)) {
48
+ this.manyToManyRelations.set(relation.pivotTable, relation);
49
+ return relation;
50
+ }
51
+ else {
52
+ throw new Error(`Pivot table ${relation.pivotTable} does not exist. Create it in alphabetical order before using many-to-many relationships.`);
53
+ }
69
54
  }
70
- doesTableExist(name) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- const table = this.tableFactory(name, this.customDatabaseAdapter);
73
- return yield table.exists();
74
- });
55
+ async doesTableExist(name) {
56
+ const table = this.tableFactory(name, this.customDatabaseAdapter);
57
+ return await table.exists();
75
58
  }
76
59
  syncModel(model) {
77
60
  var _a;
@@ -81,70 +64,63 @@ class Repository {
81
64
  getModel(name) {
82
65
  return this.models.get(name);
83
66
  }
84
- save(attributes) {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- yield this.Table.Insert(attributes);
87
- });
88
- }
89
- first(queryLayers, Model) {
90
- return __awaiter(this, void 0, void 0, function* () {
91
- let record;
92
- if (Model.JoinedEntities.length > 0) {
93
- const result = (yield this.join(Model, Object.assign(Object.assign({}, queryLayers), { final: Object.assign(Object.assign({}, queryLayers.final), { limit: 1 }) })))[0];
94
- record = result ? { values: result } : undefined;
95
- }
96
- else {
97
- record = yield this.Table.Record(queryLayers);
98
- }
99
- return record === null || record === void 0 ? void 0 : record.values;
100
- });
67
+ async save(attributes) {
68
+ await this.Table.Insert(attributes);
101
69
  }
102
- get(QueryLayers, Model) {
103
- return __awaiter(this, void 0, void 0, function* () {
104
- if (Model.JoinedEntities.length > 0) {
105
- return yield this.join(Model, QueryLayers);
106
- }
107
- else {
108
- const records = yield this.Table.Records(QueryLayers);
109
- return records.map(record => record.values);
110
- }
111
- });
70
+ async first(queryLayers, Model) {
71
+ let record;
72
+ if (Model.JoinedEntities.length > 0) {
73
+ const result = (await this.join(Model, { ...queryLayers, final: { ...queryLayers.final, limit: 1 } }))[0];
74
+ record = result ? { values: result } : undefined;
75
+ }
76
+ else {
77
+ record = await this.Table.Record(queryLayers);
78
+ }
79
+ return record === null || record === void 0 ? void 0 : record.values;
112
80
  }
113
- all(Model, QueryLayers) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- return this.get(QueryLayers, Model);
116
- });
81
+ async get(QueryLayers, Model) {
82
+ if (Model.JoinedEntities.length > 0) {
83
+ return await this.join(Model, QueryLayers);
84
+ }
85
+ else {
86
+ const records = await this.Table.Records(QueryLayers);
87
+ return records.map(record => record.values);
88
+ }
117
89
  }
118
- update(primaryKey, newAttributes) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- const record = yield this.Table.Record({ base: { where: primaryKey } });
121
- if (record) {
122
- return yield record.Update(newAttributes, primaryKey);
123
- }
124
- });
90
+ async all(Model, QueryLayers) {
91
+ return this.get(QueryLayers, Model);
125
92
  }
126
- join(Model, queryLayers) {
127
- return __awaiter(this, void 0, void 0, function* () {
128
- const { joins, queryLayers: nextLayers } = this.buildJoinObject(Model, queryLayers);
129
- nextLayers.base.joins = joins;
130
- const records = yield this.Table.Join(nextLayers);
131
- return records.map(record => record.values);
132
- });
93
+ async update(primaryKey, newAttributes, table) {
94
+ const record = await this.Table.Record({ base: { from: table, where: primaryKey } });
95
+ if (record) {
96
+ return await record.Update(newAttributes, primaryKey);
97
+ }
133
98
  }
134
- toSql(queryLayers, Model) {
135
- return __awaiter(this, void 0, void 0, function* () {
136
- let nextLayers = queryLayers;
137
- if (Model.JoinedEntities.length > 0) {
138
- const result = this.buildJoinObject(Model, queryLayers);
139
- nextLayers = result.queryLayers;
140
- nextLayers.base.joins = result.joins;
141
- }
142
- return this.Table.toSql(nextLayers);
143
- });
99
+ async join(Model, queryLayers) {
100
+ const { joins, queryLayers: nextLayers } = this.buildJoinObject(Model, queryLayers);
101
+ nextLayers.base.joins = joins;
102
+ const records = await this.Table.Join(nextLayers);
103
+ return records.map(record => record.values);
104
+ }
105
+ async toSql(queryLayers, Model) {
106
+ let nextLayers = queryLayers;
107
+ if (Model.JoinedEntities.length > 0) {
108
+ const result = this.buildJoinObject(Model, queryLayers);
109
+ nextLayers = result.queryLayers;
110
+ nextLayers.base.joins = result.joins;
111
+ }
112
+ return this.Table.toSql(nextLayers);
144
113
  }
145
114
  buildJoinObject(Model, inputLayers) {
146
- const queryLayers = Object.assign(Object.assign({}, inputLayers), { base: Object.assign({}, inputLayers.base), final: inputLayers.final
147
- ? Object.assign({}, inputLayers.final) : undefined });
115
+ const queryLayers = {
116
+ ...inputLayers,
117
+ base: {
118
+ ...inputLayers.base,
119
+ },
120
+ final: inputLayers.final
121
+ ? { ...inputLayers.final }
122
+ : undefined
123
+ };
148
124
  const joins = Model.JoinedEntities.flatMap(join => {
149
125
  var _a, _b;
150
126
  var _c;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iamkirbki/database-handler-core",
3
- "version": "4.4.3",
3
+ "version": "4.4.4",
4
4
  "author": "iamkirbki",
5
5
  "description": "Core database abstractions and interfaces",
6
6
  "license": "ISC",