Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,56 @@ export class StubPostgresDatabaseAdapter<
}
}

protected async batchInsertInternalAsync(
_queryInterface: any,
tableName: string,
objects: readonly object[],
): Promise<object[]> {
const objectCollection = this.getObjectCollectionForTable(tableName);

const idField = getDatabaseFieldForEntityField(
this.entityConfiguration2,
this.entityConfiguration2.idField,
);
const insertedObjects: object[] = [];
for (const object of objects) {
const objectToInsert = {
[idField]: this.generateRandomID(),
...object,
};
objectCollection.push(objectToInsert);
insertedObjects.push(objectToInsert);
}
return insertedObjects;
}

protected async batchUpdateInternalAsync(
_queryInterface: any,
tableName: string,
tableIdField: string,
ids: readonly any[],
object: object,
): Promise<object[]> {
if (Object.keys(object).length === 0) {
throw new Error(`Empty batch update (${tableIdField} IN (${ids.join(', ')}))`);
}

const objectCollection = this.getObjectCollectionForTable(tableName);
const updatedObjects: object[] = [];

for (const id of ids) {
const objectIndex = objectCollection.findIndex((obj) => obj[tableIdField] === id);
if (objectIndex >= 0) {
objectCollection[objectIndex] = {
...objectCollection[objectIndex],
...object,
};
updatedObjects.push(objectCollection[objectIndex]);
}
}
return updatedObjects;
}

protected async insertInternalAsync(
_queryInterface: any,
tableName: string,
Expand Down Expand Up @@ -277,6 +327,25 @@ export class StubPostgresDatabaseAdapter<
return [objectCollection[objectIndex]];
}

protected async batchDeleteInternalAsync(
_queryInterface: any,
tableName: string,
tableIdField: string,
ids: readonly any[],
): Promise<number> {
const objectCollection = this.getObjectCollectionForTable(tableName);
let count = 0;

for (const id of ids) {
const objectIndex = objectCollection.findIndex((obj) => obj[tableIdField] === id);
if (objectIndex >= 0) {
objectCollection.splice(objectIndex, 1);
count++;
}
}
return count;
}

protected async deleteInternalAsync(
_queryInterface: any,
tableName: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,35 @@ export class PostgresEntityDatabaseAdapter<
return await wrapNativePostgresCallAsync(() => query);
}

protected async batchInsertInternalAsync(
queryInterface: Knex,
tableName: string,
objects: readonly object[],
): Promise<object[]> {
return await wrapNativePostgresCallAsync(() =>
queryInterface
.insert([...objects])
.into(tableName)
.returning('*'),
);
}

protected async batchUpdateInternalAsync(
queryInterface: Knex,
tableName: string,
tableIdField: string,
ids: readonly any[],
object: object,
): Promise<object[]> {
return await wrapNativePostgresCallAsync(() =>
queryInterface
.update(object)
.into(tableName)
.whereIn(tableIdField, [...ids])
.returning('*'),
);
}

protected async insertInternalAsync(
queryInterface: Knex,
tableName: string,
Expand All @@ -272,6 +301,20 @@ export class PostgresEntityDatabaseAdapter<
);
}

protected async batchDeleteInternalAsync(
queryInterface: Knex,
tableName: string,
tableIdField: string,
ids: readonly any[],
): Promise<number> {
return await wrapNativePostgresCallAsync(() =>
queryInterface
.into(tableName)
.whereIn(tableIdField, [...ids])
.del(),
);
}

protected async deleteInternalAsync(
queryInterface: Knex,
tableName: string,
Expand Down
Loading