diff --git a/src/statements/Insert.ts b/src/statements/Insert.ts index 95c3625..5768092 100644 --- a/src/statements/Insert.ts +++ b/src/statements/Insert.ts @@ -1,11 +1,12 @@ import { QueryResult } from "pg"; -import { Initialiser, InputTypeFromString, ValidType, Value } from "../IStrongPG"; +import { Initialiser, InputTypeFromString, OutputTypeFromString, ValidType, Value } from "../IStrongPG"; import Schema, { TableSchema } from "../Schema"; import Expression from "../expressions/Expression"; import Statement from "./Statement"; import UpdateTable from "./Update"; export interface InsertIntoTableFactory[] = Schema.Column[]> { + prepare (): InsertIntoTable; values (...values: { [I in keyof COLUMNS]: InputTypeFromString }): InsertIntoTable; } @@ -20,8 +21,9 @@ export default class InsertIntoTable new InsertIntoTable(tableName, schema, columns, []), values: (...values: any[]) => { - const query = new InsertIntoTable(tableName, schema, columns, [values] as never); + const query = new InsertIntoTable(tableName, schema, columns, columns.length && !values.length ? [] : [values] as never); if (isUpsert) { query.onConflict(primaryKey!).doUpdate(update => { for (let i = 0; i < columns.length; i++) { @@ -63,6 +65,15 @@ export default class InsertIntoTable | "*")[]; + public returning[]> (...columns: RETURNING_COLUMNS): InsertIntoTable }[]>; + public returning | "*"> (columns: RETURNING_COLUMN): InsertIntoTable : RETURNING_COLUMN]: OutputTypeFromString }[]>; + public returning (...columns: (Schema.Column | "*")[]) { + this.returningColumns = columns; + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return this as InsertIntoTable; + } + public compile () { const rows = this.rows .map(row => row @@ -86,7 +97,10 @@ export default class InsertIntoTable) {