Skip to content

Commit dcdc8de

Browse files
committed
feat: updated to latest repo and added scripts
1 parent a4e6296 commit dcdc8de

8 files changed

+78
-37
lines changed

.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export class AdvertRepository extends AbstractPolymorphicRepository<
2121
AdvertEntity
2222
> {}
2323
```
24+
25+
> The below decorators will only work when using the above extended PolymorphicRepository
26+
2427
### Setup the entities
2528

2629
#### Parents

package.json

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22
"name": "typeorm-polymorphic",
33
"version": "0.0.1",
44
"main": "index.js",
5-
"repository": "[email protected]:bashleigh/typeorm-polymorphic.git",
65
"author": "Aaryanna Simonelli <[email protected]>",
76
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/bashleigh/typeorm-polymorphic"
10+
},
11+
"description": "A simple pagination function to build a pagination object with types",
12+
"keywords": [
13+
"nestjs",
14+
"typeorm",
15+
"polymorphic",
16+
"relationships",
17+
"decorators",
18+
"mysql"
19+
],
820
"scripts": {
9-
"build": "tsc"
21+
"format": "prettier --write \"**/*.ts\"",
22+
"build": "rm -rf ./dist && tsc && npm run build:index",
23+
"build:index": "rm -rf ./index.js ./index.d.ts && tsc -d --skipLibCheck ./index.ts",
24+
"prepublish": "npm run format && npm run build"
1025
},
1126
"devDependencies": {
27+
"prettier": "^2.2.1",
28+
"reflect-metadata": "^0.1.13",
1229
"typeorm": "^0.2.29",
1330
"typescript": "^4.1.3"
1431
},

src/decorators.ts

-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,3 @@ export const PolymorphicParent = (
4646
deleteBeforeUpdate: false,
4747
...options,
4848
});
49-

src/polymorphic.interface.ts

-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,3 @@ export type PolymorphicParentType = {
5555
export type PolymorphicOptionsType =
5656
| PolymorphicChildType
5757
| PolymorphicParentType;
58-

src/polymorphic.repository.ts

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'reflect-metadata';
12
import {
23
Repository,
34
getMetadataArgsStorage,
@@ -8,7 +9,7 @@ import {
89
FindOneOptions,
910
ObjectID,
1011
} from 'typeorm';
11-
import { POLYMORPHIC_OPTIONS } from './contstants';
12+
import { POLYMORPHIC_OPTIONS } from './constants';
1213
import {
1314
PolymorphicChildType,
1415
PolymorphicParentType,
@@ -53,25 +54,23 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
5354
}
5455

5556
return keys
56-
.map(
57-
(key: string): PolymorphicMetadataInterface | undefined => {
58-
const data: PolymorphicMetadataOptionsInterface & {
59-
propertyKey: string;
60-
} = Reflect.getMetadata(
61-
key,
62-
(this.metadata.target as Function)['prototype'],
63-
);
64-
65-
if (typeof data === 'object') {
66-
const classType = data.classType();
67-
return {
68-
...data,
69-
classType,
70-
};
71-
}
72-
},
73-
)
74-
.filter(val => typeof val !== 'undefined');
57+
.map((key: string): PolymorphicMetadataInterface | undefined => {
58+
const data: PolymorphicMetadataOptionsInterface & {
59+
propertyKey: string;
60+
} = Reflect.getMetadata(
61+
key,
62+
(this.metadata.target as Function)['prototype'],
63+
);
64+
65+
if (typeof data === 'object') {
66+
const classType = data.classType();
67+
return {
68+
...data,
69+
classType,
70+
};
71+
}
72+
})
73+
.filter((val) => typeof val !== 'undefined');
7574
}
7675

7776
protected isPolymorph(): boolean {
@@ -94,7 +93,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
9493
}
9594

9695
public async hydrateMany(entities: E[]): Promise<E[]> {
97-
return Promise.all(entities.map(ent => this.hydrateOne(ent)));
96+
return Promise.all(entities.map((ent) => this.hydrateOne(ent)));
9897
}
9998

10099
public async hydrateOne(entity: E): Promise<E> {
@@ -116,7 +115,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
116115
return values.reduce<E>((e: E, vals: PolymorphicHydrationType) => {
117116
const values =
118117
vals.type === 'parent' && Array.isArray(vals.values)
119-
? vals.values.filter(v => typeof v !== 'undefined' && v !== null)
118+
? vals.values.filter((v) => typeof v !== 'undefined' && v !== null)
120119
: vals.values;
121120
e[vals.key] =
122121
vals.type === 'parent' && Array.isArray(values) ? values[0] : values; // TODO should be condition for !hasMany
@@ -198,7 +197,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
198197

199198
if (Array.isArray(entities)) {
200199
return Promise.all(
201-
entities.map(polymorph => {
200+
entities.map((polymorph) => {
202201
polymorph[entityIdColumn(options)] = entity[entityIdColumn(options)];
203202
polymorph[entityTypeColumn(options)] = this.metadata.targetName;
204203

@@ -219,7 +218,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
219218
const results = await Promise.all(
220219
options.map(
221220
(options: PolymorphicMetadataInterface) =>
222-
new Promise(async resolve =>
221+
new Promise(async (resolve) =>
223222
options.cascade
224223
? resolve({
225224
key: options.propertyKey,
@@ -253,7 +252,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
253252
await Promise.all(
254253
options.map(
255254
(option: PolymorphicMetadataInterface) =>
256-
new Promise(resolve => {
255+
new Promise((resolve) => {
257256
if (!option.deleteBeforeUpdate) {
258257
return Promise.resolve();
259258
}
@@ -325,7 +324,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
325324
public async save<T extends DeepPartial<E>>(
326325
entityOrEntities: T | Array<T>,
327326
options?: SaveOptions & { reload: false },
328-
): Promise<T & E | Array<T & E> | T | Array<T>> {
327+
): Promise<(T & E) | Array<T & E> | T | Array<T>> {
329328
if (!this.isPolymorph()) {
330329
return Array.isArray(entityOrEntities) && options
331330
? await super.save(entityOrEntities, options)
@@ -399,7 +398,7 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
399398
const metadata = this.getPolymorphicMetadata();
400399

401400
return Promise.all(
402-
results.map(entity => this.hydratePolymorphs(entity, metadata)),
401+
results.map((entity) => this.hydratePolymorphs(entity, metadata)),
403402
);
404403
}
405404

@@ -437,9 +436,9 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
437436
idOrOptionsOrConditions as number | string | ObjectID | Date,
438437
optionsOrConditions as FindConditions<E> | FindOneOptions<E>,
439438
)
440-
: super.findOne(idOrOptionsOrConditions as
441-
| FindConditions<E>
442-
| FindOneOptions<E>);
439+
: super.findOne(
440+
idOrOptionsOrConditions as FindConditions<E> | FindOneOptions<E>,
441+
);
443442
}
444443

445444
const entity =
@@ -452,9 +451,9 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
452451
idOrOptionsOrConditions as number | string | ObjectID | Date,
453452
optionsOrConditions as FindConditions<E> | FindOneOptions<E>,
454453
)
455-
: await super.findOne(idOrOptionsOrConditions as
456-
| FindConditions<E>
457-
| FindOneOptions<E>);
454+
: await super.findOne(
455+
idOrOptionsOrConditions as FindConditions<E> | FindOneOptions<E>,
456+
);
458457

459458
if (!entity) {
460459
return entity;

tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": true,
5+
"removeComments": true,
6+
"emitDecoratorMetadata": true,
7+
"experimentalDecorators": true,
8+
"target": "es2017",
9+
"sourceMap": true,
10+
"outDir": "./dist",
11+
"baseUrl": "./",
12+
"incremental": true
13+
},
14+
"exclude": ["node_modules", "dist"]
15+
}

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ path-is-absolute@^1.0.0:
359359
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
360360
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
361361

362+
prettier@^2.2.1:
363+
version "2.2.1"
364+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
365+
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
366+
362367
reflect-metadata@^0.1.13:
363368
version "0.1.13"
364369
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"

0 commit comments

Comments
 (0)