Skip to content

Commit

Permalink
fix(database): schema/extension creation timestamp field values misma…
Browse files Browse the repository at this point in the history
…tch (#682)
  • Loading branch information
kon14 committed Sep 8, 2023
1 parent 0784716 commit 1543e07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
5 changes: 3 additions & 2 deletions modules/database/src/adapters/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,13 @@ export abstract class DatabaseAdapter<T extends Schema> {
if (extIndex === -1 && extFieldsCount === 0) {
return Promise.resolve(schema as unknown as Schema); // @dirty-type-cast
} else if (extIndex === -1) {
const date = new Date(); // TODO FORMAT
// Create Extension
schema.extensions.push({
fields: extFields,
ownerModule: extOwner,
createdAt: new Date(), // TODO FORMAT
updatedAt: new Date(), // TODO FORMAT
createdAt: date,
updatedAt: date,
});
} else {
if (extFieldsCount === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ export class MongooseSchema extends SchemaAdapter<Model<any>> {
},
) {
await this.createPermissionCheck(options?.userId, options?.scope);
const parsedQuery = {
...this.parseStringToQuery(query),
createdAt: new Date(),
updatedAt: new Date(),
};
const parsedQuery = this.parseStringToQuery(query);

const obj = await this.model.create(parsedQuery).then(r => r.toObject());
await this.addPermissionToData(obj, options);
Expand Down Expand Up @@ -140,7 +136,6 @@ export class MongooseSchema extends SchemaAdapter<Model<any>> {
if (parsedQuery.hasOwnProperty('$set')) {
parsedQuery = parsedQuery['$set'];
}
parsedQuery['updatedAt'] = new Date();
let finalQuery = this.model.findOneAndReplace(parsedFilter!, parsedQuery, {
new: true,
});
Expand Down Expand Up @@ -171,7 +166,6 @@ export class MongooseSchema extends SchemaAdapter<Model<any>> {
if (parsedQuery.hasOwnProperty('$set')) {
parsedQuery = parsedQuery['$set'];
}
parsedQuery['updatedAt'] = new Date();
let finalQuery = this.model.findOneAndUpdate(parsedFilter!, parsedQuery, {
new: true,
});
Expand Down Expand Up @@ -205,7 +199,6 @@ export class MongooseSchema extends SchemaAdapter<Model<any>> {
if (parsedQuery.hasOwnProperty('$set')) {
parsedQuery = parsedQuery['$set'];
}
parsedQuery['updatedAt'] = new Date();
return this.model.updateMany(parsedFilter, parsedQuery).exec();
}

Expand Down
23 changes: 17 additions & 6 deletions modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
this.model = sequelize.define(schema.collectionName, schema.fields, {
...schema.modelOptions,
freezeTableName: true,
hooks: this.schema.modelOptions
? {
beforeCreate: doc => {
const date = new Date();
doc.createdAt = date;
doc.updatedAt = date;
},
beforeBulkCreate: docs => {
const date = new Date();
docs.forEach(doc => {
doc.createdAt = date;
doc.updatedAt = date;
});
},
}
: undefined,
});
// if a relation is to self, then it will be undefined inside the extractedRelations
// so we set it manually to self
Expand Down Expand Up @@ -186,7 +202,6 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
}
// process the update query after special conditions have been handled.
processCreateQuery(parsedQuery, this.objectPaths);
parsedQuery.updatedAt = new Date();
incrementDbQueries();
const relationObjects = extractRelationsModification(this, parsedQuery);
await this.model.update(
Expand Down Expand Up @@ -233,8 +248,6 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
) {
await this.createPermissionCheck(options?.userId, options?.scope);
const parsedQuery: ParsedQuery = this.parseStringToQuery(query);
parsedQuery.createdAt = new Date();
parsedQuery.updatedAt = new Date();
incrementDbQueries();
processCreateQuery(parsedQuery, this.objectPaths);
const relationObjects = extractRelationsModification(this, parsedQuery);
Expand All @@ -247,9 +260,7 @@ export class SequelizeSchema extends SchemaAdapter<ModelStatic<any>> {
.create(parsedQuery, {
transaction: t,
})
.then(doc => {
return createWithPopulation(this, doc, relationObjects, t);
})
.then(doc => createWithPopulation(this, doc, relationObjects, t))
.then(doc => {
if (!transactionProvided) {
t!.commit();
Expand Down

0 comments on commit 1543e07

Please sign in to comment.