Skip to content

Commit

Permalink
fixes typeorm#3460
Browse files Browse the repository at this point in the history
  • Loading branch information
pleerock committed Jan 20, 2019
1 parent fdb8c33 commit 9446499
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ however since API is already quite stable we don't expect too much breaking chan
If we missed a note on some change or you have a questions on migrating from old version,
feel free to ask us and community.

## 0.2.13

* fixed signatures of `update`/`insert` methods, some `find*` methods in repositories, entity managers, BaseEntity and QueryBuilders

## 0.2.12

* fixed mongodb entity listeners and subscribers (#1527)
Expand Down
5 changes: 4 additions & 1 deletion src/common/DeepPartial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
* Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties.
*/
export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
[P in keyof T]?:
T[P] extends Array<infer U> ? Array<DeepPartial<U>> :
T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> :
DeepPartial<T[P]>
};
12 changes: 6 additions & 6 deletions src/entity-manager/EntityManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Connection} from "../connection/Connection";
import {FindManyOptions} from "../find-options/FindManyOptions";
import {ObjectType} from "../common/ObjectType";
import { EntityNotFoundError } from "../error/EntityNotFoundError";
import {EntityNotFoundError} from "../error/EntityNotFoundError";
import {QueryRunnerProviderAlreadyReleasedError} from "../error/QueryRunnerProviderAlreadyReleasedError";
import {FindOneOptions} from "../find-options/FindOneOptions";
import {DeepPartial} from "../common/DeepPartial";
Expand All @@ -25,7 +25,7 @@ import {RepositoryNotFoundError} from "../error/RepositoryNotFoundError";
import {RepositoryNotTreeError} from "../error/RepositoryNotTreeError";
import {RepositoryFactory} from "../repository/RepositoryFactory";
import {TreeRepositoryNotSupportedError} from "../error/TreeRepositoryNotSupportedError";
import {QueryPartialEntity} from "../query-builder/QueryPartialEntity";
import {QueryDeepPartialEntity} from "../query-builder/QueryPartialEntity";
import {EntityPersistExecutor} from "../persistence/EntityPersistExecutor";
import {ObjectID} from "../driver/mongodb/typings";
import {InsertResult} from "../query-builder/result/InsertResult";
Expand Down Expand Up @@ -364,7 +364,7 @@ export class EntityManager {
* Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
* You can execute bulk inserts using this method.
*/
async insert<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, entity: QueryPartialEntity<Entity>|(QueryPartialEntity<Entity>[]), options?: SaveOptions): Promise<InsertResult> {
async insert<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, entity: QueryDeepPartialEntity<Entity>|(QueryDeepPartialEntity<Entity>[]), options?: SaveOptions): Promise<InsertResult> {

// TODO: Oracle does not support multiple values. Need to create another nice solution.
if (this.connection.driver instanceof OracleDriver && entity instanceof Array) {
Expand All @@ -385,7 +385,7 @@ export class EntityManager {
* Does not check if entity exist in the database.
* Condition(s) cannot be empty.
*/
update<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|FindConditions<Entity>, partialEntity: DeepPartial<Entity>, options?: SaveOptions): Promise<UpdateResult> {
update<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|FindConditions<Entity>, partialEntity: QueryDeepPartialEntity<Entity>, options?: SaveOptions): Promise<UpdateResult> {

// if user passed empty criteria or empty list of criterias, then throw an error
if (criteria === undefined ||
Expand Down Expand Up @@ -682,7 +682,7 @@ export class EntityManager {
throw new Error(`Value "${value}" is not a number.`);

return this
.createQueryBuilder(entityClass, "entity")
.createQueryBuilder<any>(entityClass, "entity")
.update(entityClass)
.set({
[propertyPath]: () => this.connection.driver.escape(column.databaseName) + " + " + value
Expand All @@ -708,7 +708,7 @@ export class EntityManager {
throw new Error(`Value "${value}" is not a number.`);

return this
.createQueryBuilder(entityClass, "entity")
.createQueryBuilder<any>(entityClass, "entity")
.update(entityClass)
.set({
[propertyPath]: () => this.connection.driver.escape(column.databaseName) + " - " + value
Expand Down
13 changes: 7 additions & 6 deletions src/entity-manager/MongoEntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
MongoCallback,
MongoCountPreferences,
MongodbIndexOptions,
MongoError, ObjectID,
MongoError,
ObjectID,
OrderedBulkOperation,
ParallelCollectionScanOptions,
ReadPreference,
Expand All @@ -43,14 +44,14 @@ import {FindOptionsUtils} from "../find-options/FindOptionsUtils";
import {FindOneOptions} from "../find-options/FindOneOptions";
import {PlatformTools} from "../platform/PlatformTools";
import {DeepPartial} from "../common/DeepPartial";
import {QueryPartialEntity} from "../query-builder/QueryPartialEntity";
import {QueryDeepPartialEntity} from "../query-builder/QueryPartialEntity";
import {SaveOptions} from "../repository/SaveOptions";
import {InsertResult} from "../query-builder/result/InsertResult";
import {UpdateResult} from "../query-builder/result/UpdateResult";
import {RemoveOptions} from "../repository/RemoveOptions";
import {DeleteResult} from "../query-builder/result/DeleteResult";
import {EntityMetadata} from "../metadata/EntityMetadata";
import {EntitySchema} from "../index";
import {EntitySchema, FindConditions} from "../index";
import {BroadcasterResult} from "../subscriber/BroadcasterResult";

/**
Expand Down Expand Up @@ -191,7 +192,7 @@ export class MongoEntityManager extends EntityManager {
* Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
* You can execute bulk inserts using this method.
*/
async insert<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, entity: QueryPartialEntity<Entity>|QueryPartialEntity<Entity>[], options?: SaveOptions): Promise<InsertResult> {
async insert<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, entity: QueryDeepPartialEntity<Entity>|QueryDeepPartialEntity<Entity>[], options?: SaveOptions): Promise<InsertResult> {
// todo: convert entity to its database name
const result = new InsertResult();
if (entity instanceof Array) {
Expand All @@ -217,7 +218,7 @@ export class MongoEntityManager extends EntityManager {
* Executes fast and efficient UPDATE query.
* Does not check if entity exist in the database.
*/
async update<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|DeepPartial<Entity>, partialEntity: DeepPartial<Entity>, options?: SaveOptions): Promise<UpdateResult> {
async update<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|FindConditions<Entity>, partialEntity: QueryDeepPartialEntity<Entity>, options?: SaveOptions): Promise<UpdateResult> {
if (criteria instanceof Array) {
await Promise.all((criteria as any[]).map(criteriaItem => {
return this.update(target, criteriaItem, partialEntity);
Expand All @@ -237,7 +238,7 @@ export class MongoEntityManager extends EntityManager {
* Executes fast and efficient DELETE query.
* Does not check if entity exist in the database.
*/
async delete<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|DeepPartial<Entity>, options?: RemoveOptions): Promise<DeleteResult> {
async delete<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|FindConditions<Entity>, options?: RemoveOptions): Promise<DeleteResult> {
if (criteria instanceof Array) {
await Promise.all((criteria as any[]).map(criteriaItem => {
return this.delete(target, criteriaItem);
Expand Down
4 changes: 2 additions & 2 deletions src/persistence/tree/MaterializedPathSubjectExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export class MaterializedPathSubjectExecutor {
.update(subject.metadata.target)
.set({
[subject.metadata.materializedPathColumn!.propertyPath]: parentPath + insertedEntityId + "."
})
} as any)
.where(subject.identifier!)
.execute();
}

}
}
4 changes: 2 additions & 2 deletions src/query-builder/InsertQueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {QueryBuilder} from "./QueryBuilder";
import {ObjectLiteral} from "../common/ObjectLiteral";
import {ObjectType} from "../common/ObjectType";
import {QueryPartialEntity} from "./QueryPartialEntity";
import {QueryDeepPartialEntity} from "./QueryPartialEntity";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {PostgresDriver} from "../driver/postgres/PostgresDriver";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
Expand Down Expand Up @@ -150,7 +150,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
/**
* Values needs to be inserted into table.
*/
values(values: QueryPartialEntity<Entity>|QueryPartialEntity<Entity>[]): this {
values(values: QueryDeepPartialEntity<Entity>|QueryDeepPartialEntity<Entity>[]): this {
this.expressionMap.valuesSet = values;
return this;
}
Expand Down
12 changes: 6 additions & 6 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {RelationQueryBuilder} from "./RelationQueryBuilder";
import {ObjectType} from "../common/ObjectType";
import {Alias} from "./Alias";
import {Brackets} from "./Brackets";
import {QueryPartialEntity} from "./QueryPartialEntity";
import {QueryDeepPartialEntity} from "./QueryPartialEntity";
import {EntityMetadata} from "../metadata/EntityMetadata";
import {ColumnMetadata} from "../metadata/ColumnMetadata";
import {SqljsDriver} from "../driver/sqljs/SqljsDriver";
Expand Down Expand Up @@ -180,27 +180,27 @@ export abstract class QueryBuilder<Entity> {
/**
* Creates UPDATE query and applies given update values.
*/
update(updateSet: QueryPartialEntity<Entity>): UpdateQueryBuilder<Entity>;
update(updateSet: QueryDeepPartialEntity<Entity>): UpdateQueryBuilder<Entity>;

/**
* Creates UPDATE query for the given entity and applies given update values.
*/
update<T>(entity: ObjectType<T>, updateSet?: QueryPartialEntity<T>): UpdateQueryBuilder<T>;
update<T>(entity: ObjectType<T>, updateSet?: QueryDeepPartialEntity<T>): UpdateQueryBuilder<T>;

/**
* Creates UPDATE query for the given entity and applies given update values.
*/
update<T>(entity: EntitySchema<T>, updateSet?: QueryPartialEntity<T>): UpdateQueryBuilder<T>;
update<T>(entity: EntitySchema<T>, updateSet?: QueryDeepPartialEntity<T>): UpdateQueryBuilder<T>;

/**
* Creates UPDATE query for the given entity and applies given update values.
*/
update(entity: Function|EntitySchema<Entity>|string, updateSet?: QueryPartialEntity<Entity>): UpdateQueryBuilder<Entity>;
update(entity: Function|EntitySchema<Entity>|string, updateSet?: QueryDeepPartialEntity<Entity>): UpdateQueryBuilder<Entity>;

/**
* Creates UPDATE query for the given table name and applies given update values.
*/
update(tableName: string, updateSet?: QueryPartialEntity<Entity>): UpdateQueryBuilder<Entity>;
update(tableName: string, updateSet?: QueryDeepPartialEntity<Entity>): UpdateQueryBuilder<Entity>;

/**
* Creates UPDATE query and applies given update values.
Expand Down
12 changes: 11 additions & 1 deletion src/query-builder/QueryPartialEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@
* Make all properties in T optional
*/
export type QueryPartialEntity<T> = {
[P in keyof T]?: T[P]|(() => string);
[P in keyof T]?: T[P] | (() => string);
};

/**
* Make all properties in T optional. Deep version.
*/
export type QueryDeepPartialEntity<T> = {
[P in keyof T]?:
T[P] extends Array<infer U> ? Array<QueryDeepPartialEntity<U>> :
T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<QueryDeepPartialEntity<U>> :
QueryDeepPartialEntity<T[P]> | (() => string);
};
3 changes: 2 additions & 1 deletion src/query-builder/UpdateQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {LimitOnUpdateNotSupportedError} from "../error/LimitOnUpdateNotSupported
import {OracleDriver} from "../driver/oracle/OracleDriver";
import {UpdateValuesMissingError} from "../error/UpdateValuesMissingError";
import {EntityColumnNotFound} from "../error/EntityColumnNotFound";
import {QueryDeepPartialEntity} from "./QueryPartialEntity";

/**
* Allows to build complex sql queries in a fashion way and execute those queries.
Expand Down Expand Up @@ -130,7 +131,7 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
/**
* Values needs to be updated.
*/
set(values: ObjectLiteral): this {
set(values: QueryDeepPartialEntity<Entity>): this {
this.expressionMap.valuesSet = values;
return this;
}
Expand Down
Loading

0 comments on commit 9446499

Please sign in to comment.