Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Mongoose 8 #154

Merged
merged 7 commits into from
Jan 4, 2024
Merged
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"eslint": "8.47.0",
"jsdoc-babel": "^0.5.0",
"jsdoc-to-markdown": "^7.1.1",
"mongoose": "^7.5.0",
"mongoose": "8.x",
"nyc": "^15.1.0",
"sinon": "15.2.0",
"ts-mocha": "^10.0.0",
Expand All @@ -94,7 +94,7 @@
"winston": "^3.7.2"
},
"peerDependencies": {
"mongoose": "^7.5.0"
"mongoose": "^7.5.0 || ^8.0.0"
},
"engines": {
"node": ">=14.0.0"
Expand Down
3 changes: 3 additions & 0 deletions src/collections/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export const findOneInternalOptionsKeys: Set<string> = new Set(

export interface FindOneAndDeleteOptions {
sort?: SortOption;
includeResultMetadata?: boolean;
}


class _FindOneAndReplaceOptions {
upsert?: boolean = undefined;
returnDocument?: 'before' | 'after' = undefined;
sort?: SortOption;
includeResultMetadata?: boolean;
}

export interface FindOneAndReplaceOptions extends _FindOneAndReplaceOptions {}
Expand All @@ -76,6 +78,7 @@ class _FindOneAndUpdateOptions {
upsert?: boolean = undefined;
returnDocument?: 'before' | 'after' = undefined;
sort?: SortOption;
includeResultMetadata?: boolean;
}

export interface FindOneAndUpdateOptions extends _FindOneAndUpdateOptions {}
Expand Down
34 changes: 28 additions & 6 deletions src/driver/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {
} from '@/src/collections/options';
import { JSONAPIDeleteResult } from '../collections/collection';

import { version } from 'mongoose';

const IS_MONGOOSE_7 = version.startsWith('7.');

type NodeCallback<ResultType = any> = (err: Error | null, res: ResultType | null) => unknown;

/**
Expand Down Expand Up @@ -120,23 +124,35 @@ export class Collection extends MongooseCollection {
* @param update
* @param options
*/
findOneAndUpdate(filter: Record<string, any>, update: Record<string, any>, options?: FindOneAndUpdateOptions) {
async findOneAndUpdate(filter: Record<string, any>, update: Record<string, any>, options?: FindOneAndUpdateOptions) {
if (options != null) {
processSortOption(options);
}
return this.collection.findOneAndUpdate(filter, update, options);
const res = await this.collection.findOneAndUpdate(filter, update, options);
if (IS_MONGOOSE_7) {
return options?.includeResultMetadata === false ? res.value : res;
} else if (options?.includeResultMetadata !== false) {
return res.value;
}
return res;
}

/**
* Find a single document in the collection and delete it.
* @param filter
* @param options
*/
findOneAndDelete(filter: Record<string, any>, options?: FindOneAndDeleteOptions) {
async findOneAndDelete(filter: Record<string, any>, options?: FindOneAndDeleteOptions) {
if (options != null) {
processSortOption(options);
}
return this.collection.findOneAndDelete(filter, options);
const res = await this.collection.findOneAndDelete(filter, options);
if (IS_MONGOOSE_7) {
return options?.includeResultMetadata === false ? res.value : res;
} else if (options?.includeResultMetadata !== false) {
return res.value;
}
return res;
}

/**
Expand All @@ -145,11 +161,17 @@ export class Collection extends MongooseCollection {
* @param newDoc
* @param options
*/
findOneAndReplace(filter: Record<string, any>, newDoc: Record<string, any>, options?: FindOneAndReplaceOptions) {
async findOneAndReplace(filter: Record<string, any>, newDoc: Record<string, any>, options?: FindOneAndReplaceOptions) {
if (options != null) {
processSortOption(options);
}
return this.collection.findOneAndReplace(filter, newDoc, options);
const res = await this.collection.findOneAndReplace(filter, newDoc, options);
if (IS_MONGOOSE_7) {
return options?.includeResultMetadata === false ? res.value : res;
} else if (options?.includeResultMetadata !== false) {
return res.value;
}
return res;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/driver/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { Client } from '@/src/collections/client';
import { Collection } from './collection';
import { default as MongooseConnection } from 'mongoose/lib/connection';
import STATES from 'mongoose/lib/connectionstate';
import { STATES } from 'mongoose';
import { executeOperation } from '../collections/utils';

export class Connection extends MongooseConnection {
Expand Down
1 change: 0 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
declare module '@astrajs/client';
declare module 'mongoose/lib/collection';
declare module 'mongoose/lib/connection';
declare module 'mongoose/lib/connectionstate';
4 changes: 2 additions & 2 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const LIB_NAME = 'stargate-mongoose';
export const LIB_VERSION = '0.3.0';
export const LIB_NAME = "stargate-mongoose";

Check warning on line 1 in src/version.ts

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
export const LIB_VERSION = "0.3.0";

Check warning on line 2 in src/version.ts

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
3 changes: 2 additions & 1 deletion tests/collections/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ describe('Options tests', async () => {
//findOneAndReplace with rawResult option
const findOneAndReplaceResp = await Product.findOneAndReplace({ name: 'Product 25' },
{ price: 20, isCertified: false, name: 'Product 25'},
{ rawResult: false, upsert: true, returnDocument: 'after' });
{ rawResult: false, upsert: true, returnDocument: 'after' }
);
assert.strictEqual(findOneAndReplaceResp.isCertified,false);
assert.strictEqual(findOneAndReplaceResp.price,20);
assert.ok(findOneAndReplaceResp._id);
Expand Down
26 changes: 3 additions & 23 deletions tests/driver/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ describe('Mongoose Model API level tests', async () => {
//cleanIndexes invokes listIndexes() which is not supported
assert.strictEqual(error!.message, 'listIndexes() Not Implemented');
});
it('API ops tests Model.count()', async () => {
it('API ops tests Model.countDocuments()', async () => {
const product1 = new Product({name: 'Product 1', price: 10, isCertified: true, category: 'cat 1'});
await product1.save();
const countResp = await Product.count({name: 'Product 1'});
const countResp = await Product.countDocuments({name: 'Product 1'});
assert.strictEqual(countResp, 1);
});
it('API ops tests Model.create()', async () => {
Expand Down Expand Up @@ -512,14 +512,6 @@ describe('Mongoose Model API level tests', async () => {
const findDeletedDoc = await Product.findById(product1._id);
assert.strictEqual(findDeletedDoc, null);
});
it('API ops tests Model.findByIdAndRemove()', async () => {
const product1 = new Product({name: 'Product 1', price: 10, isCertified: true, category: 'cat 1'});
await product1.save();
const deleteResp = await Product.findByIdAndRemove(product1._id);
assert.strictEqual(deleteResp?.name, 'Product 1');
const findDeletedDoc = await Product.findById(product1._id);
assert.strictEqual(findDeletedDoc, null);
});
it('API ops tests Model.findByIdAndUpdate()', async () => {
const product1 = new Product({name: 'Product 1', price: 10, isCertified: true, category: 'cat 1', url: 'http://product1.com'});
await product1.save();
Expand Down Expand Up @@ -547,17 +539,6 @@ describe('Mongoose Model API level tests', async () => {
const findDeletedDoc = await Product.findOne({category: 'cat 1'});
assert.strictEqual(findDeletedDoc, null);
});
it('API ops tests Model.findOneAndRemove()', async () => {
const product1 = new Product({name: 'Product 1', price: 10, isCertified: true, category: 'cat 2'});
const product2 = new Product({name: 'Product 2', price: 10, isCertified: true, category: 'cat 2'});
const product3 = new Product({name: 'Product 3', price: 10, isCertified: true, category: 'cat 1'});
await Product.insertMany([product1, product2, product3]);
const deleteResp = await Product.findOneAndRemove({category: 'cat 1'});
assert.strictEqual(deleteResp?.category, 'cat 1');
//check if it exists again
const findDeletedDoc = await Product.findOne({category: 'cat 1'});
assert.strictEqual(findDeletedDoc, null);
});
it('API ops tests Model.findOneAndUpdate()', async () => {
const product1 = new Product({name: 'Product 1', price: 10, isCertified: true, category: 'cat 2'});
const product2 = new Product({name: 'Product 2', price: 10, isCertified: true, category: 'cat 2'});
Expand Down Expand Up @@ -633,8 +614,7 @@ describe('Mongoose Model API level tests', async () => {
await product1.save();
const docSaved = await Product.findOne({name: 'Product 1'});
assert.strictEqual(docSaved.name, 'Product 1');
const deleteOneResp = await product1.deleteOne();
assert.strictEqual(deleteOneResp.name, 'Product 1');
await product1.deleteOne();
const findDeletedDoc = await Product.findOne({name: 'Product 1'});
assert.strictEqual(findDeletedDoc, null);
});
Expand Down