@alanszp/typeorm 20.3.0 → 20.4.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/dist/repository/AbstractBaseRepository.d.ts +30 -0
- package/dist/repository/AbstractBaseRepository.js +110 -0
- package/dist/repository/AbstractBaseRepository.js.map +1 -0
- package/dist/repository/BaseRepository.d.ts +4 -25
- package/dist/repository/BaseRepository.js +3 -101
- package/dist/repository/BaseRepository.js.map +1 -1
- package/dist/repository/GlobalBaseRepository.d.ts +10 -0
- package/dist/repository/GlobalBaseRepository.js +33 -0
- package/dist/repository/GlobalBaseRepository.js.map +1 -0
- package/dist/repository/index.d.ts +3 -0
- package/dist/repository/index.js +3 -0
- package/dist/repository/index.js.map +1 -1
- package/dist/repository/types.d.ts +4 -0
- package/dist/repository/types.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DeepPartial, EntityManager, EntityTarget, FindManyOptions, FindOneOptions, FindOptionsWhere, Repository } from "typeorm";
|
|
2
|
+
import { DbConnection } from "../DbConnection";
|
|
3
|
+
import { IsolationLevel } from "./types";
|
|
4
|
+
import { BaseEntityWithoutId } from "../BaseEntityWithoutId";
|
|
5
|
+
export declare abstract class AbstractBaseRepository<T extends BaseEntityWithoutId> {
|
|
6
|
+
protected Entity: EntityTarget<T>;
|
|
7
|
+
protected repo: Repository<T>;
|
|
8
|
+
protected entityName: string;
|
|
9
|
+
protected dbConnection: DbConnection;
|
|
10
|
+
protected transactionalEntityManager?: EntityManager;
|
|
11
|
+
constructor(Entity: EntityTarget<T>, dbConnection: DbConnection, transactionalEntityManager?: EntityManager);
|
|
12
|
+
protected abstract getWhere(where?: FindOptionsWhere<T> | FindOptionsWhere<T>[]): FindOptionsWhere<T> | FindOptionsWhere<T>[];
|
|
13
|
+
protected setTransaction(transaction: EntityManager): void;
|
|
14
|
+
protected createTransactionalRepository(transaction: EntityManager): this;
|
|
15
|
+
getTransaction(): EntityManager;
|
|
16
|
+
protected getRepo(): Repository<T>;
|
|
17
|
+
protected executeQuery<ReturnType>(query: string, params?: unknown[]): Promise<ReturnType>;
|
|
18
|
+
protected doSave<Entity extends DeepPartial<T>>(entity: Entity[]): Promise<Entity[]>;
|
|
19
|
+
protected doSave<Entity extends DeepPartial<T>>(entity: Entity): Promise<Entity>;
|
|
20
|
+
save(entity: T): Promise<T>;
|
|
21
|
+
saveMany(entity: T[]): Promise<T[]>;
|
|
22
|
+
softRemove(entity: T): Promise<T>;
|
|
23
|
+
softRemoveMany(entity: T[]): Promise<T[]>;
|
|
24
|
+
findOne(opts: FindOneOptions<T>): Promise<T | null>;
|
|
25
|
+
findOneOrFail(opts: FindOneOptions<T>): Promise<T>;
|
|
26
|
+
findMany(opts?: FindManyOptions<T>): Promise<T[]>;
|
|
27
|
+
findManyAndCount(opts?: FindManyOptions<T>): Promise<[T[], number]>;
|
|
28
|
+
private execTransaction;
|
|
29
|
+
doWithTransaction<ReturnType>(fn: (txRepo: this) => Promise<ReturnType>, isolationLevel?: IsolationLevel): Promise<ReturnType>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AbstractBaseRepository = void 0;
|
|
13
|
+
const typeorm_1 = require("typeorm");
|
|
14
|
+
const lodash_1 = require("lodash");
|
|
15
|
+
const EntityAlreadyExists_1 = require("../errors/EntityAlreadyExists");
|
|
16
|
+
const EntityNotFound_1 = require("../errors/EntityNotFound");
|
|
17
|
+
const core_1 = require("@alanszp/core");
|
|
18
|
+
class AbstractBaseRepository {
|
|
19
|
+
constructor(Entity, dbConnection, transactionalEntityManager) {
|
|
20
|
+
this.dbConnection = dbConnection;
|
|
21
|
+
const ds = dbConnection.getDataSource();
|
|
22
|
+
this.Entity = Entity;
|
|
23
|
+
this.repo = ds.getRepository(Entity);
|
|
24
|
+
this.entityName = ds.getMetadata(Entity).name;
|
|
25
|
+
this.transactionalEntityManager = transactionalEntityManager;
|
|
26
|
+
}
|
|
27
|
+
setTransaction(transaction) {
|
|
28
|
+
this.transactionalEntityManager = transaction;
|
|
29
|
+
}
|
|
30
|
+
createTransactionalRepository(transaction) {
|
|
31
|
+
const cloned = (0, lodash_1.clone)(this);
|
|
32
|
+
cloned.setTransaction(transaction);
|
|
33
|
+
return cloned;
|
|
34
|
+
}
|
|
35
|
+
getTransaction() {
|
|
36
|
+
var _a;
|
|
37
|
+
return ((_a = this.transactionalEntityManager) !== null && _a !== void 0 ? _a : this.dbConnection.getDataSource().manager);
|
|
38
|
+
}
|
|
39
|
+
getRepo() {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
return (_b = (_a = this.getTransaction()) === null || _a === void 0 ? void 0 : _a.getRepository(this.Entity)) !== null && _b !== void 0 ? _b : this.repo;
|
|
42
|
+
}
|
|
43
|
+
executeQuery(query, params) {
|
|
44
|
+
return this.getRepo().query(query, params);
|
|
45
|
+
}
|
|
46
|
+
doSave(entity) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
try {
|
|
50
|
+
return yield this.getRepo().save(entity, { chunk: 500 });
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error instanceof typeorm_1.QueryFailedError &&
|
|
54
|
+
(0, core_1.hasOwnProperty)(error, "code") &&
|
|
55
|
+
error.code === "23505") {
|
|
56
|
+
const duplicateError = error;
|
|
57
|
+
throw new EntityAlreadyExists_1.EntityAlreadyExistsError(this.entityName, (_a = duplicateError.constraint) !== null && _a !== void 0 ? _a : null, (_b = duplicateError.detail) !== null && _b !== void 0 ? _b : null);
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
save(entity) {
|
|
64
|
+
return this.doSave(entity);
|
|
65
|
+
}
|
|
66
|
+
saveMany(entity) {
|
|
67
|
+
return this.doSave(entity);
|
|
68
|
+
}
|
|
69
|
+
softRemove(entity) {
|
|
70
|
+
return this.getRepo().softRemove(entity);
|
|
71
|
+
}
|
|
72
|
+
softRemoveMany(entity) {
|
|
73
|
+
return this.getRepo().softRemove(entity);
|
|
74
|
+
}
|
|
75
|
+
findOne(opts) {
|
|
76
|
+
return this.getRepo().findOne(Object.assign(Object.assign({}, opts), { where: this.getWhere(opts.where) }));
|
|
77
|
+
}
|
|
78
|
+
findOneOrFail(opts) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const entity = yield this.findOne(opts);
|
|
81
|
+
if (entity === null) {
|
|
82
|
+
throw new EntityNotFound_1.EntityNotFoundError(this.entityName);
|
|
83
|
+
}
|
|
84
|
+
return entity;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
findMany() {
|
|
88
|
+
return __awaiter(this, arguments, void 0, function* (opts = {}) {
|
|
89
|
+
return this.getRepo().find(Object.assign(Object.assign({}, opts), { where: this.getWhere(opts.where) }));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
findManyAndCount() {
|
|
93
|
+
return __awaiter(this, arguments, void 0, function* (opts = {}) {
|
|
94
|
+
return this.getRepo().findAndCount(Object.assign(Object.assign({}, opts), { where: this.getWhere(opts.where) }));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
execTransaction(fn, isolationLevel) {
|
|
98
|
+
return isolationLevel === undefined
|
|
99
|
+
? this.dbConnection.getDataSource().transaction(fn)
|
|
100
|
+
: this.dbConnection.getDataSource().transaction(isolationLevel, fn);
|
|
101
|
+
}
|
|
102
|
+
doWithTransaction(fn, isolationLevel) {
|
|
103
|
+
if (!(0, lodash_1.isNil)(this.transactionalEntityManager)) {
|
|
104
|
+
return fn(this);
|
|
105
|
+
}
|
|
106
|
+
return this.execTransaction((transaction) => fn(this.createTransactionalRepository(transaction)), isolationLevel);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.AbstractBaseRepository = AbstractBaseRepository;
|
|
110
|
+
//# sourceMappingURL=AbstractBaseRepository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractBaseRepository.js","sourceRoot":"","sources":["../../src/repository/AbstractBaseRepository.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCASiB;AACjB,mCAAsC;AAEtC,uEAAyE;AACzE,6DAA+D;AAC/D,wCAA+C;AAI/C,MAAsB,sBAAsB;IAW1C,YACE,MAAuB,EACvB,YAA0B,EAC1B,0BAA0C;QAE1C,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,MAAM,EAAE,GAAG,YAAY,CAAC,aAAa,EAAE,CAAC;QACxC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;QAC9C,IAAI,CAAC,0BAA0B,GAAG,0BAA0B,CAAC;IAC/D,CAAC;IAMS,cAAc,CAAC,WAA0B;QACjD,IAAI,CAAC,0BAA0B,GAAG,WAAW,CAAC;IAChD,CAAC;IAES,6BAA6B,CAAC,WAA0B;QAChE,MAAM,M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Aa,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC;YACnD,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,aAAa,EAAE,CAAC,WAAW,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;IACxE,CAAC;IAED,iBAAiB,CACf,EAAyC,EACzC,cAA+B;QAE/B,IAAI,CAAC,IAAA,cAAK,EAAC,IAAI,CAAC,0BAA0B,CAAC,EAAE,CAAC;YAC5C,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;QAED,OAAO,IAAI,CAAC,eAAe,CACzB,CAAC,WAAW,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,6BAA6B,CAAC,WAAW,CAAC,CAAC,EACpE,cAAc,CACf,CAAC;IACJ,CAAC;CACF;AA9JD,wDA8JC"}
|
|
@@ -1,30 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntityManager, EntityTarget, FindOptionsWhere } from "typeorm";
|
|
2
2
|
import { DbConnection } from "../DbConnection";
|
|
3
|
-
import { RepoBaseEntity
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
protected repo: Repository<T>;
|
|
7
|
-
protected entityName: string;
|
|
3
|
+
import { RepoBaseEntity } from "./types";
|
|
4
|
+
import { AbstractBaseRepository } from "./AbstractBaseRepository";
|
|
5
|
+
export declare abstract class BaseRepository<T extends RepoBaseEntity> extends AbstractBaseRepository<T> {
|
|
8
6
|
protected organizationReference: string;
|
|
9
|
-
protected dbConnection: DbConnection;
|
|
10
|
-
protected transactionalEntityManager?: EntityManager;
|
|
11
7
|
constructor(Entity: EntityTarget<T>, organizationReference: string, dbConnection: DbConnection, transactionalEntityManager?: EntityManager);
|
|
12
|
-
protected setTransaction(transaction: EntityManager): void;
|
|
13
|
-
protected createTransactionalRepository(transaction: EntityManager): this;
|
|
14
|
-
getTransaction(): EntityManager;
|
|
15
8
|
protected getWhere(where?: FindOptionsWhere<T> | FindOptionsWhere<T>[]): FindOptionsWhere<T> | FindOptionsWhere<T>[];
|
|
16
|
-
protected getRepo(): Repository<T>;
|
|
17
|
-
protected executeQuery<ReturnType>(query: string, params?: unknown[]): Promise<ReturnType>;
|
|
18
|
-
protected doSave<Entity extends DeepPartial<T>>(entity: Entity[]): Promise<Entity[]>;
|
|
19
|
-
protected doSave<Entity extends DeepPartial<T>>(entity: Entity): Promise<Entity>;
|
|
20
|
-
save(entity: T): Promise<T>;
|
|
21
|
-
saveMany(entity: T[]): Promise<T[]>;
|
|
22
|
-
softRemove(entity: T): Promise<T>;
|
|
23
|
-
softRemoveMany(entity: T[]): Promise<T[]>;
|
|
24
|
-
findOne(opts: FindOneOptions<T>): Promise<T | null>;
|
|
25
|
-
findOneOrFail(opts: FindOneOptions<T>): Promise<T>;
|
|
26
|
-
findMany(opts?: FindManyOptions<T>): Promise<T[]>;
|
|
27
|
-
findManyAndCount(opts?: FindManyOptions<T>): Promise<[T[], number]>;
|
|
28
|
-
private execTransaction;
|
|
29
|
-
doWithTransaction<ReturnType>(fn: (txRepo: this) => Promise<ReturnType>, isolationLevel?: IsolationLevel): Promise<ReturnType>;
|
|
30
9
|
}
|
|
@@ -1,41 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.BaseRepository = void 0;
|
|
13
|
-
const typeorm_1 = require("typeorm");
|
|
14
4
|
const lodash_1 = require("lodash");
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
const core_1 = require("@alanszp/core");
|
|
18
|
-
class BaseRepository {
|
|
5
|
+
const AbstractBaseRepository_1 = require("./AbstractBaseRepository");
|
|
6
|
+
class BaseRepository extends AbstractBaseRepository_1.AbstractBaseRepository {
|
|
19
7
|
constructor(Entity, organizationReference, dbConnection, transactionalEntityManager) {
|
|
20
|
-
|
|
21
|
-
const ds = dbConnection.getDataSource();
|
|
22
|
-
this.Entity = Entity;
|
|
23
|
-
this.repo = ds.getRepository(Entity);
|
|
24
|
-
this.entityName = ds.getMetadata(Entity).name;
|
|
8
|
+
super(Entity, dbConnection, transactionalEntityManager);
|
|
25
9
|
this.organizationReference = organizationReference;
|
|
26
|
-
this.transactionalEntityManager = transactionalEntityManager;
|
|
27
|
-
}
|
|
28
|
-
setTransaction(transaction) {
|
|
29
|
-
this.transactionalEntityManager = transaction;
|
|
30
|
-
}
|
|
31
|
-
createTransactionalRepository(transaction) {
|
|
32
|
-
const cloned = (0, lodash_1.clone)(this);
|
|
33
|
-
cloned.setTransaction(transaction);
|
|
34
|
-
return cloned;
|
|
35
|
-
}
|
|
36
|
-
getTransaction() {
|
|
37
|
-
var _a;
|
|
38
|
-
return ((_a = this.transactionalEntityManager) !== null && _a !== void 0 ? _a : this.dbConnection.getDataSource().manager);
|
|
39
10
|
}
|
|
40
11
|
getWhere(where) {
|
|
41
12
|
if ((0, lodash_1.isArray)(where)) {
|
|
@@ -43,75 +14,6 @@ class BaseRepository {
|
|
|
43
14
|
}
|
|
44
15
|
return Object.assign(Object.assign({}, where), { organizationReference: this.organizationReference });
|
|
45
16
|
}
|
|
46
|
-
getRepo() {
|
|
47
|
-
var _a, _b;
|
|
48
|
-
return (_b = (_a = this.getTransaction()) === null || _a === void 0 ? void 0 : _a.getRepository(this.Entity)) !== null && _b !== void 0 ? _b : this.repo;
|
|
49
|
-
}
|
|
50
|
-
executeQuery(query, params) {
|
|
51
|
-
return this.getRepo().query(query, params);
|
|
52
|
-
}
|
|
53
|
-
doSave(entity) {
|
|
54
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
var _a, _b;
|
|
56
|
-
try {
|
|
57
|
-
return yield this.getRepo().save(entity, { chunk: 500 });
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
if (error instanceof typeorm_1.QueryFailedError &&
|
|
61
|
-
(0, core_1.hasOwnProperty)(error, "code") &&
|
|
62
|
-
error.code === "23505") {
|
|
63
|
-
const duplicateError = error;
|
|
64
|
-
throw new EntityAlreadyExists_1.EntityAlreadyExistsError(this.entityName, (_a = duplicateError.constraint) !== null && _a !== void 0 ? _a : null, (_b = duplicateError.detail) !== null && _b !== void 0 ? _b : null);
|
|
65
|
-
}
|
|
66
|
-
throw error;
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
save(entity) {
|
|
71
|
-
return this.doSave(entity);
|
|
72
|
-
}
|
|
73
|
-
saveMany(entity) {
|
|
74
|
-
return this.doSave(entity);
|
|
75
|
-
}
|
|
76
|
-
softRemove(entity) {
|
|
77
|
-
return this.getRepo().softRemove(entity);
|
|
78
|
-
}
|
|
79
|
-
softRemoveMany(entity) {
|
|
80
|
-
return this.getRepo().softRemove(entity);
|
|
81
|
-
}
|
|
82
|
-
findOne(opts) {
|
|
83
|
-
return this.getRepo().findOne(Object.assign(Object.assign({}, opts), { where: this.getWhere(opts.where) }));
|
|
84
|
-
}
|
|
85
|
-
findOneOrFail(opts) {
|
|
86
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
const entity = yield this.findOne(opts);
|
|
88
|
-
if (entity === null) {
|
|
89
|
-
throw new EntityNotFound_1.EntityNotFoundError(this.entityName);
|
|
90
|
-
}
|
|
91
|
-
return entity;
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
findMany() {
|
|
95
|
-
return __awaiter(this, arguments, void 0, function* (opts = {}) {
|
|
96
|
-
return this.getRepo().find(Object.assign(Object.assign({}, opts), { where: this.getWhere(opts.where) }));
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
findManyAndCount() {
|
|
100
|
-
return __awaiter(this, arguments, void 0, function* (opts = {}) {
|
|
101
|
-
return this.getRepo().findAndCount(Object.assign(Object.assign({}, opts), { where: this.getWhere(opts.where) }));
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
execTransaction(fn, isolationLevel) {
|
|
105
|
-
return isolationLevel === undefined
|
|
106
|
-
? this.dbConnection.getDataSource().transaction(fn)
|
|
107
|
-
: this.dbConnection.getDataSource().transaction(isolationLevel, fn);
|
|
108
|
-
}
|
|
109
|
-
doWithTransaction(fn, isolationLevel) {
|
|
110
|
-
if (!(0, lodash_1.isNil)(this.transactionalEntityManager)) {
|
|
111
|
-
return fn(this);
|
|
112
|
-
}
|
|
113
|
-
return this.execTransaction((transaction) => fn(this.createTransactionalRepository(transaction)), isolationLevel);
|
|
114
|
-
}
|
|
115
17
|
}
|
|
116
18
|
exports.BaseRepository = BaseRepository;
|
|
117
19
|
//# sourceMappingURL=BaseRepository.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseRepository.js","sourceRoot":"","sources":["../../src/repository/BaseRepository.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BaseRepository.js","sourceRoot":"","sources":["../../src/repository/BaseRepository.ts"],"names":[],"mappings":";;;AACA,mCAAiC;AAGjC,qEAAkE;AAElE,MAAsB,cAEpB,SAAQ,+CAAyB;IAGjC,YACE,MAAuB,EACvB,qBAA6B,EAC7B,YAA0B,EAC1B,0BAA0C;QAE1C,KAAK,CAAC,MAAM,EAAE,YAAY,EAAE,0BAA0B,CAAC,CAAC;QACxD,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;IACrD,CAAC;IAES,QAAQ,CAChB,KAAmD;QAEnD,IAAI,IAAA,gBAAO,EAAC,KAAK,CAAC,EAAE,CAAC;YACnB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,iCACnB,CAAC,KACJ,qBAAqB,EAAE,IAAI,CAAC,qBAAqB,IACjD,CAAC,CAAC;QACN,CAAC;QACD,OAAO,gCACF,KAAK,KACR,qBAAqB,EAAE,IAAI,CAAC,qBAAqB,GAC3B,CAAC;IAC3B,CAAC;CACF;AA7BD,wCA6BC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EntityManager, EntityTarget, FindOptionsWhere } from "typeorm";
|
|
2
|
+
import { DbConnection } from "../DbConnection";
|
|
3
|
+
import { GlobalRepoBaseEntity } from "./types";
|
|
4
|
+
import { AbstractBaseRepository } from "./AbstractBaseRepository";
|
|
5
|
+
export declare abstract class GlobalBaseRepository<T extends GlobalRepoBaseEntity> extends AbstractBaseRepository<T> {
|
|
6
|
+
protected organizationReference: string | null;
|
|
7
|
+
constructor(Entity: EntityTarget<T>, organizationReference: string | null, dbConnection: DbConnection, transactionalEntityManager?: EntityManager);
|
|
8
|
+
protected getWhere(where?: FindOptionsWhere<T> | FindOptionsWhere<T>[]): FindOptionsWhere<T> | FindOptionsWhere<T>[];
|
|
9
|
+
protected getWhereExact(where?: FindOptionsWhere<T> | FindOptionsWhere<T>[]): FindOptionsWhere<T> | FindOptionsWhere<T>[];
|
|
10
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GlobalBaseRepository = void 0;
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const AbstractBaseRepository_1 = require("./AbstractBaseRepository");
|
|
7
|
+
class GlobalBaseRepository extends AbstractBaseRepository_1.AbstractBaseRepository {
|
|
8
|
+
constructor(Entity, organizationReference, dbConnection, transactionalEntityManager) {
|
|
9
|
+
super(Entity, dbConnection, transactionalEntityManager);
|
|
10
|
+
this.organizationReference = organizationReference;
|
|
11
|
+
}
|
|
12
|
+
getWhere(where) {
|
|
13
|
+
if (this.organizationReference === null) {
|
|
14
|
+
return where !== null && where !== void 0 ? where : {};
|
|
15
|
+
}
|
|
16
|
+
const orgCondition = (0, typeorm_1.Or)((0, typeorm_1.Equal)(this.organizationReference), (0, typeorm_1.IsNull)());
|
|
17
|
+
if ((0, lodash_1.isArray)(where)) {
|
|
18
|
+
return where.map((w) => (Object.assign(Object.assign({}, w), { organizationReference: orgCondition })));
|
|
19
|
+
}
|
|
20
|
+
return Object.assign(Object.assign({}, where), { organizationReference: orgCondition });
|
|
21
|
+
}
|
|
22
|
+
getWhereExact(where) {
|
|
23
|
+
if (this.organizationReference === null) {
|
|
24
|
+
return where !== null && where !== void 0 ? where : {};
|
|
25
|
+
}
|
|
26
|
+
if ((0, lodash_1.isArray)(where)) {
|
|
27
|
+
return where.map((w) => (Object.assign(Object.assign({}, w), { organizationReference: this.organizationReference })));
|
|
28
|
+
}
|
|
29
|
+
return Object.assign(Object.assign({}, where), { organizationReference: this.organizationReference });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.GlobalBaseRepository = GlobalBaseRepository;
|
|
33
|
+
//# sourceMappingURL=GlobalBaseRepository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GlobalBaseRepository.js","sourceRoot":"","sources":["../../src/repository/GlobalBaseRepository.ts"],"names":[],"mappings":";;;AAAA,qCAOiB;AACjB,mCAAiC;AAGjC,qEAAkE;AAElE,MAAsB,oBAEpB,SAAQ,+CAAyB;IAGjC,YACE,MAAuB,EACvB,qBAAoC,EACpC,YAA0B,EAC1B,0BAA0C;QAE1C,KAAK,CAAC,MAAM,EAAE,YAAY,EAAE,0BAA0B,CAAC,CAAC;QACxD,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;IACrD,CAAC;IAES,QAAQ,CAChB,KAAmD;QAEnD,IAAI,IAAI,CAAC,qBAAqB,KAAK,IAAI,EAAE,CAAC;YACxC,OAAO,KAAK,aAAL,KAAK,cAAL,KAAK,GAAK,EAA0B,CAAC;QAC9C,CAAC;QAED,MAAM,YAAY,GAAG,IAAA,YAAE,EACrB,IAAA,eAAK,EAAC,IAAI,CAAC,qBAAqB,CAAC,EACjC,IAAA,gBAAM,GAAE,CACT,CAAC;QAEF,IAAI,IAAA,gBAAO,EAAC,KAAK,CAAC,EAAE,CAAC;YACnB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,iCACnB,CAAC,KACJ,qBAAqB,EAAE,YAAY,IACnC,CAAC,CAAC;QACN,CAAC;QACD,OAAO,gCACF,KAAK,KACR,qBAAqB,EAAE,YAAY,GACb,CAAC;IAC3B,CAAC;IAES,aAAa,CACrB,KAAmD;QAEnD,IAAI,IAAI,CAAC,qBAAqB,KAAK,IAAI,EAAE,CAAC;YACxC,OAAO,KAAK,aAAL,KAAK,cAAL,KAAK,GAAK,EAA0B,CAAC;QAC9C,CAAC;QACD,IAAI,IAAA,gBAAO,EAAC,KAAK,CAAC,EAAE,CAAC;YACnB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,iCACnB,CAAC,KACJ,qBAAqB,EAAE,IAAI,CAAC,qBAAqB,IACjD,CAAC,CAAC;QACN,CAAC;QACD,OAAO,gCACF,KAAK,KACR,qBAAqB,EAAE,IAAI,CAAC,qBAAqB,GAC3B,CAAC;IAC3B,CAAC;CACF;AAxDD,oDAwDC"}
|
package/dist/repository/index.js
CHANGED
|
@@ -14,5 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./AbstractBaseRepository"), exports);
|
|
17
18
|
__exportStar(require("./BaseRepository"), exports);
|
|
19
|
+
__exportStar(require("./GlobalBaseRepository"), exports);
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
18
21
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/repository/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/repository/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC;AACzC,mDAAiC;AACjC,yDAAuC;AACvC,0CAAwB"}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { EntityManager } from "typeorm/entity-manager/EntityManager";
|
|
2
2
|
import { BaseEntity } from "../BaseEntity";
|
|
3
|
+
import { BaseEntityWithoutId } from "../BaseEntityWithoutId";
|
|
3
4
|
export type RepoBaseEntity = BaseEntity & {
|
|
4
5
|
organizationReference: string;
|
|
5
6
|
};
|
|
7
|
+
export type GlobalRepoBaseEntity = BaseEntityWithoutId & {
|
|
8
|
+
organizationReference: string | null;
|
|
9
|
+
};
|
|
6
10
|
export declare enum IsolationLevel {
|
|
7
11
|
READ_UNCOMMITTED = "READ UNCOMMITTED",
|
|
8
12
|
READ_COMMITTED = "READ COMMITTED",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/repository/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/repository/types.ts"],"names":[],"mappings":";;;AAQA,IAAY,cAKX;AALD,WAAY,cAAc;IACxB,uDAAqC,CAAA;IACrC,mDAAiC,CAAA;IACjC,qDAAmC,CAAA;IACnC,+CAA6B,CAAA;AAC/B,CAAC,EALW,cAAc,8BAAd,cAAc,QAKzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/typeorm",
|
|
3
|
-
"version": "20.
|
|
3
|
+
"version": "20.4.0",
|
|
4
4
|
"description": "Alan's typeorm utils.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"yalc-publish": "yarn run yalc publish"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@alanszp/logger": "^20.
|
|
19
|
+
"@alanszp/logger": "^20.4.0",
|
|
20
20
|
"@types/node": "^24.0.0",
|
|
21
21
|
"class-validator": "^0.14.1",
|
|
22
22
|
"ts-node": "^10.0.0",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@alanszp/validations": "^20.3.0"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "9e9913800c00b1398ca4ead841b942d0580c40e4"
|
|
38
38
|
}
|