Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Fix #50
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Jan 28, 2022
1 parent f09c17c commit 64dadd8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "safe-typeorm",
"version": "1.0.4",
"version": "1.0.5",
"description": "Safe Relationship Decorators for the TypeORM",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
19 changes: 13 additions & 6 deletions src/functional/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { v4 } from "uuid";
import { Creator } from "../typings";
import { ITableInfo } from "./internal/ITableInfo";
import { findRepository } from "./findRepository";
import { VariadicSingleton } from "tstl";

export function insert<T extends object>(records: T | T[], ignore?: boolean): Promise<void>;
export function insert<T extends object>(manager: orm.EntityManager, records: T | T[], ignore?: boolean): Promise<void>;
export function insert<T extends object>(records: T | T[], ignore?: string|boolean): Promise<void>;
export function insert<T extends object>(manager: orm.EntityManager, records: T | T[], ignore?: string|boolean): Promise<void>;

export async function insert<T extends object>(...args: any[]): Promise<void>
{
Expand All @@ -26,7 +27,7 @@ export async function insert<T extends object>(...args: any[]): Promise<void>
}

async function _Insert<T extends object>
(manager: orm.EntityManager, records: T | T[], ignore?: boolean): Promise<void>
(manager: orm.EntityManager, records: T | T[], ignore?: string|boolean): Promise<void>
{
if (ignore === undefined)
ignore = false;
Expand Down Expand Up @@ -57,8 +58,14 @@ async function _Insert<T extends object>
.insert()
.values(records)
.updateEntity(false);
if (ignore === true)
stmt.orIgnore();

const type = dbms.get(manager);
if (typeof ignore === "boolean" || (type !== "postgres" && type !== "aurora-data-api-pg"))
stmt.orIgnore(ignore);
else
stmt.onConflict(ignore);

await stmt.execute();
}
}

const dbms = new VariadicSingleton((manager: orm.EntityManager) => manager.connection.options.type);
18 changes: 9 additions & 9 deletions src/transactions/InsertCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { insert } from "../functional/insert";

export class InsertCollection
{
private dict_: Map<Creator<object>, Pair<object[], boolean>>;
private dict_: Map<Creator<object>, Pair<object[], string|boolean>>;

private readonly befores_: Vector<InsertCollection.Process>;
private readonly afters_: Vector<InsertCollection.Process>;
Expand All @@ -32,10 +32,10 @@ export class InsertCollection
/* -----------------------------------------------------------
ELEMENTS I/O
----------------------------------------------------------- */
public push<T extends object>(record: T, ignore?: boolean): T;
public push<T extends object>(records: T[], ignore?: boolean): T[];
public push<T extends object>(record: T, ignore?: string|boolean): T;
public push<T extends object>(records: T[], ignore?: string|boolean): T[];

public push<T extends object>(input: T | T[], ignore: boolean = false): T | T[]
public push<T extends object>(input: T | T[], ignore: string|boolean = false): T | T[]
{
if (input instanceof Array)
return this._Push(input, ignore);
Expand All @@ -53,13 +53,13 @@ export class InsertCollection
this.afters_.push_back(process);
}

private _Push<T extends object>(records: T[], ignore: boolean): T[]
private _Push<T extends object>(records: T[], ignore: string | boolean): T[]
{
if (records.length === 0)
return records;

const creator: Creator<T> = records[0].constructor as Creator<T>;
let tuple: Pair<object[], boolean> | undefined = this.dict_.get(creator);
let tuple: Pair<object[], string|boolean> | undefined = this.dict_.get(creator);

if (tuple === undefined)
{
Expand Down Expand Up @@ -112,15 +112,15 @@ export class InsertCollection
throw new DomainError("Error on InsertCollection.execute(): it's already on executing.");
}

private _Get_record_tuples(): Pair<object[], boolean>[]
private _Get_record_tuples(): Pair<object[], string|boolean>[]
{
function compare(x: Pair<object[], boolean>, y: Pair<object[], boolean>): boolean
function compare(x: Pair<object[], string|boolean>, y: Pair<object[], string|boolean>): boolean
{
const children: Set<Creator<object>> = getDependencies(y.first[0].constructor as Creator<object>);
return !children.has(x.first[0].constructor as Creator<object>);
}

const output: Vector<Pair<object[], boolean>> = new Vector();
const output: Vector<Pair<object[], string|boolean>> = new Vector();
for (const [_, tuple] of this.dict_)
output.push_back(tuple);

Expand Down

0 comments on commit 64dadd8

Please sign in to comment.