Skip to content

Commit

Permalink
fix(database): SQL create/createMany relation parsing, group unwrappi…
Browse files Browse the repository at this point in the history
…ng (#676)
  • Loading branch information
kon14 committed Sep 7, 2023
1 parent e90ce7b commit 0784716
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
17 changes: 15 additions & 2 deletions modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { extractRelations, getTransactionAndParsedQuery, sqlTypesProcess } from './utils';
import { SequelizeAdapter } from './index';
import ConduitGrpcSdk, { Indexable } from '@conduitplatform/grpc-sdk';
import { parseQuery } from './parser';
import { parseQuery, parseCreateRelations } from './parser';
import { isNil } from 'lodash';
import { processCreateQuery, unwrap } from './utils/pathUtils';
import {
Expand Down Expand Up @@ -256,13 +256,16 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
}
return doc;
})
.then(doc => (doc ? doc.toJSON() : doc))
.then(doc =>
doc ? parseCreateRelations(doc.toJSON(), this.extractedRelations) : doc,
)
.catch(err => {
if (!transactionProvided) {
t!.rollback();
}
throw err;
});
unwrap(obj, this.objectPaths);
await this.addPermissionToData(obj, options);
return obj;
}
Expand All @@ -279,13 +282,23 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
const t = await this.sequelize.transaction({ type: Transaction.TYPES.IMMEDIATE });
for (let i = 0; i < parsedQuery.length; i++) {
processCreateQuery(parsedQuery[i], this.objectPaths);
extractRelationsModification(this, parsedQuery[i]);
}
const docs = await this.model
.bulkCreate(parsedQuery, { transaction: t })
.then(docs => {
t.commit();
return docs;
})
.then(docs => {
const parsedDocs: Indexable[] = [];
for (const doc of docs) {
const document = parseCreateRelations(doc.toJSON(), this.extractedRelations);
unwrap(document, this.objectPaths);
parsedDocs.push(document);
}
return parsedDocs;
})
.catch(err => {
t.rollback();
throw err;
Expand Down
14 changes: 14 additions & 0 deletions modules/database/src/adapters/sequelize-adapter/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,17 @@ export function renameRelations(
exclude,
};
}

export function parseCreateRelations(
doc: Indexable,
relations: { [key: string]: SequelizeSchema | SequelizeSchema[] },
) {
for (const relation in relations) {
const dbName = `${relation}Id`;
if (doc.hasOwnProperty(dbName)) {
doc[relation] = doc[dbName];
delete doc[dbName];
}
}
return doc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export function unwrap(
keyMapping: {
[key: string]: { parentKey: string; childKey: string };
},
relations: {
relations?: {
[key: string]: SequelizeSchema | SequelizeSchema[];
},
) {
for (const key in object) {
if (potentialNesting(object[key])) {
if (relations.hasOwnProperty(key)) {
if (relations?.hasOwnProperty(key)) {
unwrap(
object[key],
(relations[key] as SequelizeSchema).objectPaths,
Expand All @@ -91,7 +91,7 @@ export function unwrap(
unwrapNestedKeys(object[key], keyMapping);
}
if (isArray(object[key])) {
if (relations.hasOwnProperty(key)) {
if (relations?.hasOwnProperty(key)) {
for (const element of object[key]) {
unwrap(
element,
Expand Down

0 comments on commit 0784716

Please sign in to comment.