Skip to content

Commit

Permalink
Option to activate optimistic lock per v1 model
Browse files Browse the repository at this point in the history
only entities model has this activated for now
  • Loading branch information
daneryl committed Nov 18, 2024
1 parent 2853db4 commit bb0fdc7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
4 changes: 3 additions & 1 deletion app/api/entities/entitiesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const mongoSchema = new mongoose.Schema(
mongoSchema.index({ title: 'text' }, { language_override: 'mongoLanguage' });
mongoSchema.index({ template: 1, language: 1, published: 1 });

const Model = instanceModelWithPermissions<EntitySchema>('entities', mongoSchema);
const Model = instanceModelWithPermissions<EntitySchema>('entities', mongoSchema, {
optimisticLock: true,
});

const supportedLanguages = [
'da',
Expand Down
5 changes: 3 additions & 2 deletions app/api/odm/ModelWithPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ export class ModelWithPermissions<T> extends OdmModel<WithPermissions<T>> {

export function instanceModelWithPermissions<T = any>(
collectionName: string,
schema: mongoose.Schema
schema: mongoose.Schema,
options: { optimisticLock: boolean } = { optimisticLock: false }
) {
const logHelper = createUpdateLogHelper<T>(collectionName);
const model = new ModelWithPermissions<T>(logHelper, collectionName, schema);
const model = new ModelWithPermissions<T>(logHelper, collectionName, schema, options);
models[collectionName] = () => model;
return model;
}
42 changes: 27 additions & 15 deletions app/api/odm/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SyncDBDataSource } from 'api/common.v2/database/SyncDBDataSource';
import { legacyLogger } from 'api/log';
import { ObjectId, UpdateOptions } from 'mongodb';
import mongoose, {
FilterQuery,
Expand All @@ -8,11 +9,10 @@ import mongoose, {
UpdateQuery,
} from 'mongoose';
import { ObjectIdSchema } from 'shared/types/commonTypes';
import { inspect } from 'util';
import { MultiTenantMongooseModel } from './MultiTenantMongooseModel';
import { UpdateLogger, createUpdateLogHelper } from './logHelper';
import { ModelBulkWriteStream } from './modelBulkWriteStream';
import { legacyLogger } from 'api/log';
import { inspect } from 'util';

/** Ideas!
* T is the actual model-specific document Schema!
Expand All @@ -39,6 +39,20 @@ export class OdmModel<T> implements SyncDBDataSource<T, T> {

logHelper: UpdateLogger<T>;

options: { optimisticLock: boolean };

constructor(
logHelper: UpdateLogger<T>,
collectionName: string,
schema: Schema,
options: { optimisticLock: boolean } = { optimisticLock: false }
) {
this.collectionName = collectionName;
this.db = new MultiTenantMongooseModel<T>(collectionName, schema);
this.logHelper = logHelper;
this.options = options;
}

private documentExists(data: Partial<DataType<T>>) {
return this.db.findById(data._id, '_id');
}
Expand All @@ -52,6 +66,9 @@ export class OdmModel<T> implements SyncDBDataSource<T, T> {
}

private async checkVersion(query: any, version: number, data: Partial<DataType<T>>) {
if (!this.options.optimisticLock) {
return;
}
if (version === undefined) {
legacyLogger.debug(
inspect(
Expand All @@ -74,22 +91,13 @@ export class OdmModel<T> implements SyncDBDataSource<T, T> {
}
}

constructor(logHelper: UpdateLogger<T>, collectionName: string, schema: Schema) {
this.collectionName = collectionName;
this.db = new MultiTenantMongooseModel<T>(collectionName, schema);
this.logHelper = logHelper;
}

async save(
data: Partial<DataType<T>>,
_query?: any
//options: { checkVersion: boolean } = { checkVersion: false }
) {
async save(data: Partial<DataType<T>>, _query?: any) {
if (await this.documentExists(data)) {
// @ts-ignore
const { __v: version, ...toSaveData } = data;
const query =
_query && (await this.documentExistsByQuery(_query)) ? _query : { _id: data._id };

await this.checkVersion(query, version, data);
const saved = await this.db.findOneAndUpdate(
query,
Expand Down Expand Up @@ -230,9 +238,13 @@ export class OdmModel<T> implements SyncDBDataSource<T, T> {
// export const models: { [index: string]: OdmModel<any> } = {};
export const models: { [index: string]: () => SyncDBDataSource<any, any> } = {};

export function instanceModel<T = any>(collectionName: string, schema: Schema) {
export function instanceModel<T = any>(
collectionName: string,
schema: Schema,
options: { optimisticLock: boolean } = { optimisticLock: false }
) {
const logHelper = createUpdateLogHelper<T>(collectionName);
const model = new OdmModel<T>(logHelper, collectionName, schema);
const model = new OdmModel<T>(logHelper, collectionName, schema, options);
models[collectionName] = () => model;
return model;
}
2 changes: 1 addition & 1 deletion app/api/odm/specs/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ODM Model', () => {
});

const instanceTestingModel = (collectionName: string, schema: Schema) => {
const model = instanceModel<TestDoc>(collectionName, schema);
const model = instanceModel<TestDoc>(collectionName, schema, { optimisticLock: true });
tenants.add(
testingTenants.createTenant({
name: testingDB.dbName,
Expand Down

0 comments on commit bb0fdc7

Please sign in to comment.