UNPKG

8.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var index_1 = require("../index");
5var ObjectUtils_1 = require("../util/ObjectUtils");
6/**
7 * Base abstract entity for all entities, used in ActiveRecord patterns.
8 */
9var BaseEntity = /** @class */ (function () {
10 function BaseEntity() {
11 }
12 // -------------------------------------------------------------------------
13 // Public Methods
14 // -------------------------------------------------------------------------
15 /**
16 * Checks if entity has an id.
17 * If entity composite compose ids, it will check them all.
18 */
19 BaseEntity.prototype.hasId = function () {
20 return this.constructor.getRepository().hasId(this);
21 };
22 /**
23 * Saves current entity in the database.
24 * If entity does not exist in the database then inserts, otherwise updates.
25 */
26 BaseEntity.prototype.save = function () {
27 return this.constructor.getRepository().save(this);
28 };
29 /**
30 * Removes current entity from the database.
31 */
32 BaseEntity.prototype.remove = function () {
33 return this.constructor.getRepository().remove(this);
34 };
35 /**
36 * Reloads entity data from the database.
37 */
38 BaseEntity.prototype.reload = function () {
39 return tslib_1.__awaiter(this, void 0, void 0, function () {
40 var base, newestEntity;
41 return tslib_1.__generator(this, function (_a) {
42 switch (_a.label) {
43 case 0:
44 base = this.constructor;
45 return [4 /*yield*/, base.getRepository().findOneOrFail(base.getId(this))];
46 case 1:
47 newestEntity = _a.sent();
48 ObjectUtils_1.ObjectUtils.assign(this, newestEntity);
49 return [2 /*return*/];
50 }
51 });
52 });
53 };
54 // -------------------------------------------------------------------------
55 // Public Static Methods
56 // -------------------------------------------------------------------------
57 /**
58 * Sets connection to be used by entity.
59 */
60 BaseEntity.useConnection = function (connection) {
61 this.usedConnection = connection;
62 };
63 /**
64 * Gets current entity's Repository.
65 */
66 BaseEntity.getRepository = function () {
67 var connection = this.usedConnection || index_1.getConnection();
68 return connection.getRepository(this);
69 };
70 Object.defineProperty(BaseEntity, "target", {
71 /**
72 * Returns object that is managed by this repository.
73 * If this repository manages entity from schema,
74 * then it returns a name of that schema instead.
75 */
76 get: function () {
77 return this.getRepository().target;
78 },
79 enumerable: true,
80 configurable: true
81 });
82 /**
83 * Checks entity has an id.
84 * If entity composite compose ids, it will check them all.
85 */
86 BaseEntity.hasId = function (entity) {
87 return this.getRepository().hasId(entity);
88 };
89 /**
90 * Gets entity mixed id.
91 */
92 BaseEntity.getId = function (entity) {
93 return this.getRepository().getId(entity);
94 };
95 /**
96 * Creates a new query builder that can be used to build a sql query.
97 */
98 BaseEntity.createQueryBuilder = function (alias) {
99 return this.getRepository().createQueryBuilder(alias);
100 };
101 /**
102 * Creates a new entity instance and copies all entity properties from this object into a new entity.
103 * Note that it copies only properties that present in entity schema.
104 */
105 BaseEntity.create = function (entityOrEntities) {
106 return this.getRepository().create(entityOrEntities);
107 };
108 /**
109 * Merges multiple entities (or entity-like objects) into a given entity.
110 */
111 BaseEntity.merge = function (mergeIntoEntity) {
112 var entityLikes = [];
113 for (var _i = 1; _i < arguments.length; _i++) {
114 entityLikes[_i - 1] = arguments[_i];
115 }
116 var _a;
117 return (_a = this.getRepository()).merge.apply(_a, tslib_1.__spread([mergeIntoEntity], entityLikes));
118 };
119 /**
120 * Creates a new entity from the given plan javascript object. If entity already exist in the database, then
121 * it loads it (and everything related to it), replaces all values with the new ones from the given object
122 * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
123 * replaced from the new object.
124 *
125 * Note that given entity-like object must have an entity id / primary key to find entity by.
126 * Returns undefined if entity with given id was not found.
127 */
128 BaseEntity.preload = function (entityLike) {
129 return this.getRepository().preload(entityLike);
130 };
131 /**
132 * Saves one or many given entities.
133 */
134 BaseEntity.save = function (entityOrEntities, options) {
135 return this.getRepository().save(entityOrEntities, options);
136 };
137 /**
138 * Removes one or many given entities.
139 */
140 BaseEntity.remove = function (entityOrEntities, options) {
141 return this.getRepository().remove(entityOrEntities, options);
142 };
143 /**
144 * Inserts a given entity into the database.
145 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
146 * Executes fast and efficient INSERT query.
147 * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
148 */
149 BaseEntity.insert = function (entity, options) {
150 return this.getRepository().insert(entity, options);
151 };
152 /**
153 * Updates entity partially. Entity can be found by a given conditions.
154 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
155 * Executes fast and efficient UPDATE query.
156 * Does not check if entity exist in the database.
157 */
158 BaseEntity.update = function (criteria, partialEntity, options) {
159 return this.getRepository().update(criteria, partialEntity, options);
160 };
161 /**
162 * Deletes entities by a given criteria.
163 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
164 * Executes fast and efficient DELETE query.
165 * Does not check if entity exist in the database.
166 */
167 BaseEntity.delete = function (criteria, options) {
168 return this.getRepository().delete(criteria, options);
169 };
170 /**
171 * Counts entities that match given find options or conditions.
172 */
173 BaseEntity.count = function (optionsOrConditions) {
174 return this.getRepository().count(optionsOrConditions);
175 };
176 /**
177 * Finds entities that match given find options or conditions.
178 */
179 BaseEntity.find = function (optionsOrConditions) {
180 return this.getRepository().find(optionsOrConditions);
181 };
182 /**
183 * Finds entities that match given find options or conditions.
184 * Also counts all entities that match given conditions,
185 * but ignores pagination settings (from and take options).
186 */
187 BaseEntity.findAndCount = function (optionsOrConditions) {
188 return this.getRepository().findAndCount(optionsOrConditions);
189 };
190 /**
191 * Finds entities by ids.
192 * Optionally find options can be applied.
193 */
194 BaseEntity.findByIds = function (ids, optionsOrConditions) {
195 return this.getRepository().findByIds(ids, optionsOrConditions);
196 };
197 /**
198 * Finds first entity that matches given conditions.
199 */
200 BaseEntity.findOne = function (optionsOrConditions, maybeOptions) {
201 return this.getRepository().findOne(optionsOrConditions, maybeOptions);
202 };
203 /**
204 * Finds first entity that matches given conditions.
205 */
206 BaseEntity.findOneOrFail = function (optionsOrConditions, maybeOptions) {
207 return this.getRepository().findOneOrFail(optionsOrConditions, maybeOptions);
208 };
209 /**
210 * Executes a raw SQL query and returns a raw database results.
211 * Raw query execution is supported only by relational databases (MongoDB is not supported).
212 */
213 BaseEntity.query = function (query, parameters) {
214 return this.getRepository().query(query, parameters);
215 };
216 /**
217 * Clears all the data from the given table/collection (truncates/drops it).
218 */
219 BaseEntity.clear = function () {
220 return this.getRepository().clear();
221 };
222 return BaseEntity;
223}());
224exports.BaseEntity = BaseEntity;
225
226//# sourceMappingURL=BaseEntity.js.map