Skip to content

Commit

Permalink
Merge branch 'beta' into improve-types-performance
Browse files Browse the repository at this point in the history
  • Loading branch information
dankochetov committed Aug 6, 2023
2 parents 1cecc76 + 34a0dee commit 62e8537
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,11 @@ export class PgDialect {
}

buildInsertQuery({ table, values, onConflict, returning }: PgInsertConfig): SQL {
const isSingleValue = values.length === 1;
const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = [];
const columns: Record<string, PgColumn> = table[Table.Symbol.Columns];
const colEntries: [string, PgColumn][] = isSingleValue
? Object.keys(values[0]!).map((fieldName) => [fieldName, columns[fieldName]!])
: Object.entries(columns);

const colEntries: [string, PgColumn][] = Object.entries(columns);

const insertOrder = colEntries.map(([, column]) => sql.identifier(column.name));

for (const [valueIndex, value] of values.entries()) {
Expand All @@ -364,6 +363,7 @@ export class PgDialect {
valueList.push(colValue);
}
}

valuesSqlList.push(valueList);
if (valueIndex < values.length - 1) {
valuesSqlList.push(sql`, `);
Expand Down
11 changes: 7 additions & 4 deletions src/sqlite-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,11 @@ export abstract class SQLiteDialect {
}

buildInsertQuery({ table, values, onConflict, returning }: SQLiteInsertConfig): SQL {
const isSingleValue = values.length === 1;
// const isSingleValue = values.length === 1;
const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = [];
const columns: Record<string, SQLiteColumn> = table[Table.Symbol.Columns];
const colEntries: [string, SQLiteColumn][] = isSingleValue
? Object.keys(values[0]!).map((fieldName) => [fieldName, columns[fieldName]!])
: Object.entries(columns);

const colEntries: [string, SQLiteColumn][] = Object.entries(columns);
const insertOrder = colEntries.map(([, column]) => sql.identifier(column.name));

for (const [valueIndex, value] of values.entries()) {
Expand Down Expand Up @@ -312,6 +311,10 @@ export abstract class SQLiteDialect {

const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : undefined;

// if (isSingleValue && valuesSqlList.length === 0){
// return sql`insert into ${table} default values ${onConflictSql}${returningSql}`;
// }

return sql`insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
}

Expand Down
6 changes: 6 additions & 0 deletions src/sqlite-core/query-builders/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export class SQLiteInsertBuilder<
return result;
});

// if (mappedValues.length > 1 && mappedValues.some((t) => Object.keys(t).length === 0)) {
// throw new Error(
// `One of the values you want to insert is empty. In SQLite you can insert only one empty object per statement. For this case Drizzle with use "INSERT INTO ... DEFAULT VALUES" syntax`,
// );
// }

return new SQLiteInsert(this.table, mappedValues, this.session, this.dialect);
}
}
Expand Down

0 comments on commit 62e8537

Please sign in to comment.