Skip to content

Commit

Permalink
v1.162.0
Browse files Browse the repository at this point in the history
  • Loading branch information
varovaro committed Apr 16, 2024
2 parents dffb90e + 5c91e70 commit 9d49c49
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 121 deletions.
8 changes: 1 addition & 7 deletions app/api/common.v2/database/CollectionWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
ChangeStream,
ChangeStreamDocument,
ChangeStreamOptions,
CollStats,
CollStatsOptions,
CommandOperationOptions,
CountOptions,
CreateIndexesOptions,
Expand Down Expand Up @@ -114,7 +112,7 @@ export abstract class CollectionWrapper<TSchema extends Document = Document> {
throw new Error('Method not implemented.');
}

async dropIndexes(_options?: CommandOperationOptions | undefined): Promise<Document> {
async dropIndexes(_options?: CommandOperationOptions | undefined): Promise<boolean> {
throw new Error('Method not implemented.');
}

Expand Down Expand Up @@ -143,10 +141,6 @@ export abstract class CollectionWrapper<TSchema extends Document = Document> {
throw new Error('Method not implemented.');
}

async stats(_options?: CollStatsOptions | undefined): Promise<CollStats> {
throw new Error('Method not implemented.');
}

watch<TLocal extends Document = TSchema, TChange extends Document = ChangeStreamDocument<TLocal>>(
_pipeline?: Document[] | undefined,
_options?: ChangeStreamOptions | undefined
Expand Down
18 changes: 9 additions & 9 deletions app/api/common.v2/database/SessionScopedCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ export class SessionScopedCollection<TSchema extends Document = Document>
findOneAndDelete(
filter: Filter<TSchema>,
options: FindOneAndDeleteOptions
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

findOneAndDelete(filter: Filter<TSchema>): Promise<ModifyResult<TSchema>>;
findOneAndDelete(filter: Filter<TSchema>): Promise<WithId<TSchema> | null>;

async findOneAndDelete(
filter: Filter<TSchema>,
options?: FindOneAndDeleteOptions & { includeResultMetadata?: boolean }
options?: FindOneAndDeleteOptions
): Promise<ModifyResult<TSchema> | WithId<TSchema> | null> {
return this.collection.findOneAndDelete(filter, this.appendSession(options));
}
Expand All @@ -185,17 +185,17 @@ export class SessionScopedCollection<TSchema extends Document = Document>
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options: FindOneAndReplaceOptions
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options?: FindOneAndDeleteOptions & { includeResultMetadata?: boolean }
options?: FindOneAndDeleteOptions
): Promise<ModifyResult<TSchema> | WithId<TSchema> | null> {
return this.collection.findOneAndReplace(filter, replacement, this.appendSession(options));
}
Expand All @@ -216,17 +216,17 @@ export class SessionScopedCollection<TSchema extends Document = Document>
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: FindOneAndUpdateOptions
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options?: FindOneAndDeleteOptions & { includeResultMetadata?: boolean }
options?: FindOneAndDeleteOptions
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
return this.collection.findOneAndUpdate(filter, update, this.appendSession(options));
}
Expand Down
18 changes: 9 additions & 9 deletions app/api/common.v2/database/SyncedCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ export class SyncedCollection<TSchema extends Document = Document>
findOneAndDelete(
filter: Filter<TSchema>,
options: FindOneAndDeleteOptions
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

findOneAndDelete(filter: Filter<TSchema>): Promise<ModifyResult<TSchema>>;
findOneAndDelete(filter: Filter<TSchema>): Promise<WithId<TSchema> | null>;

async findOneAndDelete(
filter: Filter<TSchema>,
options: FindOneAndDeleteOptions & { includeResultMetadata?: boolean } = {}
options: FindOneAndDeleteOptions = {}
): Promise<ModifyResult<TSchema> | WithId<TSchema> | null> {
await this.upsertSyncLogs([filter], true);
const result = await this.collection.findOneAndDelete(filter, options);
Expand All @@ -260,17 +260,17 @@ export class SyncedCollection<TSchema extends Document = Document>
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options: FindOneAndReplaceOptions
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

async findOneAndReplace(
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options: FindOneAndDeleteOptions & { includeResultMetadata?: boolean } = {}
options: FindOneAndDeleteOptions = {}
): Promise<ModifyResult<TSchema> | WithId<TSchema> | null> {
const result = await this.collection.findOneAndReplace(filter, replacement, options);
await this.upsertSyncLogs([filter]);
Expand All @@ -293,17 +293,17 @@ export class SyncedCollection<TSchema extends Document = Document>
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: FindOneAndUpdateOptions
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>
): Promise<ModifyResult<TSchema>>;
): Promise<WithId<TSchema> | null>;

async findOneAndUpdate(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: FindOneAndDeleteOptions & { includeResultMetadata?: boolean } = {}
options: FindOneAndDeleteOptions = {}
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
const result = await this.collection.findOneAndUpdate(filter, update, options);
await this.upsertSyncLogs([filter]);
Expand Down
36 changes: 28 additions & 8 deletions app/api/common.v2/database/specs/MongoDataSourceSync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ describe('collection with automatic log to updatelogs', () => {
jest.spyOn(Date, 'now').mockReturnValue(updatedTimestamp);
return ds
.collection()
.findOneAndUpdate({ _id: id('collection id 1') }, { $set: { data: 'updated data' } });
.findOneAndUpdate(
{ _id: id('collection id 1') },
{ $set: { data: 'updated data' } },
{ includeResultMetadata: true }
);
},
expectedDBState: [
{ ...updateLogsBlankState[0], timestamp: updatedTimestamp },
Expand All @@ -228,7 +232,7 @@ describe('collection with automatic log to updatelogs', () => {
.findOneAndUpdate(
{ _id: id('non existent id') },
{ $set: { data: 'upserted data' } },
{ upsert: true }
{ upsert: true, includeResultMetadata: true }
);
},
expectedDBState: [
Expand All @@ -247,7 +251,11 @@ describe('collection with automatic log to updatelogs', () => {
await getConnection().collection('updatelogs').deleteMany();
return ds
.collection()
.findOneAndUpdate({ _id: id('collection id 1') }, { $set: { data: 'updated data' } });
.findOneAndUpdate(
{ _id: id('collection id 1') },
{ $set: { data: 'updated data' } },
{ includeResultMetadata: true }
);
},
expectedDBStateOnTransactionError: [],
expectedDBState: [
Expand All @@ -261,7 +269,11 @@ describe('collection with automatic log to updatelogs', () => {
jest.spyOn(Date, 'now').mockReturnValue(updatedTimestamp);
return ds
.collection()
.findOneAndReplace({ _id: id('collection id 1') }, { data: 'updated data' });
.findOneAndReplace(
{ _id: id('collection id 1') },
{ data: 'updated data' },
{ includeResultMetadata: true }
);
},
expectedDBState: [
{ ...updateLogsBlankState[0], timestamp: updatedTimestamp },
Expand All @@ -278,7 +290,7 @@ describe('collection with automatic log to updatelogs', () => {
.findOneAndReplace(
{ _id: id('non existent id') },
{ data: 'upserted data' },
{ upsert: true }
{ upsert: true, includeResultMetadata: true }
);
},
expectedDBState: [
Expand All @@ -297,7 +309,11 @@ describe('collection with automatic log to updatelogs', () => {
await getConnection().collection('updatelogs').deleteMany();
return ds
.collection()
.findOneAndReplace({ _id: id('collection id 1') }, { data: 'updated data' });
.findOneAndReplace(
{ _id: id('collection id 1') },
{ data: 'updated data' },
{ includeResultMetadata: true }
);
},
expectedDBStateOnTransactionError: [],
expectedDBState: [
Expand Down Expand Up @@ -436,7 +452,9 @@ describe('collection with automatic log to updatelogs', () => {
description: 'findOneAndDelete',
callback: async (ds: DataSource) => {
jest.spyOn(Date, 'now').mockReturnValue(updatedTimestamp);
return ds.collection().findOneAndDelete({ _id: id('collection id 1') });
return ds
.collection()
.findOneAndDelete({ _id: id('collection id 1') }, { includeResultMetadata: true });
},
expectedDBState: [
{ ...updateLogsBlankState[0], deleted: true, timestamp: updatedTimestamp },
Expand All @@ -452,7 +470,9 @@ describe('collection with automatic log to updatelogs', () => {
callback: async (ds: DataSource) => {
await getConnection().collection('updatelogs').deleteMany();
jest.spyOn(Date, 'now').mockReturnValue(updatedTimestamp);
return ds.collection().findOneAndDelete({ _id: id('collection id 1') });
return ds
.collection()
.findOneAndDelete({ _id: id('collection id 1') }, { includeResultMetadata: true });
},
expectedDBStateOnTransactionError: [],
expectedDBState: [
Expand Down
3 changes: 2 additions & 1 deletion app/api/odm/MultiTenantMongooseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
UwaziUpdateQuery,
UwaziQueryOptions,
EnforcedWithId,
UwaziUpdateOptions,
} from './model';
import { tenants } from '../tenants/tenantContext';
import { DB } from './DB';
Expand Down Expand Up @@ -58,7 +59,7 @@ class MultiTenantMongooseModel<T> {
async _updateMany(
conditions: UwaziFilterQuery<DataType<T>>,
doc: UwaziUpdateQuery<DataType<T>>,
options?: UwaziQueryOptions
options?: UwaziUpdateOptions<DataType<T>>
) {
return this.dbForCurrentTenant().updateMany(conditions, doc, options);
}
Expand Down
13 changes: 10 additions & 3 deletions app/api/odm/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { SyncDBDataSource } from 'api/common.v2/database/SyncDBDataSource';
import { ObjectId } from 'mongodb';
import mongoose, { FilterQuery, QueryOptions, Schema, UpdateQuery } from 'mongoose';
import { ObjectId, UpdateOptions } from 'mongodb';
import mongoose, {
FilterQuery,
MongooseQueryOptions,
QueryOptions,
Schema,
UpdateQuery,
} from 'mongoose';
import { ObjectIdSchema } from 'shared/types/commonTypes';
import { MultiTenantMongooseModel } from './MultiTenantMongooseModel';
import { UpdateLogger, createUpdateLogHelper } from './logHelper';
Expand All @@ -22,6 +28,7 @@ export type EnforcedWithId<T> = T & { _id: ObjectId };
export type UwaziFilterQuery<T> = FilterQuery<T>;
export type UwaziUpdateQuery<T> = UpdateQuery<DataType<T>>;
export type UwaziQueryOptions = QueryOptions;
export type UwaziUpdateOptions<T> = (UpdateOptions & Omit<MongooseQueryOptions<T>, 'lean'>) | null;

export class OdmModel<T> implements SyncDBDataSource<T, T> {
db: MultiTenantMongooseModel<T>;
Expand Down Expand Up @@ -127,7 +134,7 @@ export class OdmModel<T> implements SyncDBDataSource<T, T> {
async updateMany(
conditions: UwaziFilterQuery<DataType<T>>,
doc: UwaziUpdateQuery<T>,
options?: UwaziQueryOptions
options?: UwaziUpdateOptions<DataType<T>>
) {
await this.logHelper.upsertLogMany(conditions);
return this.db._updateMany(conditions, doc, options);
Expand Down
13 changes: 5 additions & 8 deletions app/api/odm/specs/ModelWithPermissions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PermissionSchema } from 'shared/types/permissionType';
import { AccessLevels, permissionSchema, PermissionType } from 'shared/types/permissionSchema';
import { AccessLevels, PermissionType } from 'shared/types/permissionSchema';
import { instanceModelWithPermissions, ModelWithPermissions } from 'api/odm/ModelWithPermissions';
import { permissionsContext } from 'api/permissions/permissionsContext';
import testingDB from 'api/utils/testing_db';
Expand All @@ -15,11 +15,7 @@ describe('ModelWithPermissions', () => {
let connection;
const testSchema = new mongoose.Schema({
name: String,
permissions: {
type: 'array',
items: permissionSchema,
select: false,
},
permissions: { type: mongoose.Schema.Types.Mixed, select: false },
fixed: Boolean,
});
const readDocId = testingDB.id();
Expand Down Expand Up @@ -508,13 +504,14 @@ describe('ModelWithPermissions', () => {
expect(saved).toMatchObject([
{
name: 'newDoc',
permissions: [],
},
{
name: 'newDoc2',
permissions: [],
},
]);
saved.forEach(item => {
expect(item.permissions).toBe(undefined);
});
});
});

Expand Down
6 changes: 3 additions & 3 deletions app/api/queue.v2/infrastructure/MongoQueueAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export class MongoQueueAdapter extends MongoDataSource<JobDBO> implements QueueA
{ sort: { createdAt: 1 }, returnDocument: 'after' }
);

if (result.value) {
const { _id, ...withoutId } = result.value;
if (result) {
const { _id, ...withoutId } = result;
return {
id: result.value._id.toHexString(),
id: result._id.toHexString(),
...withoutId,
};
}
Expand Down
Loading

0 comments on commit 9d49c49

Please sign in to comment.