Skip to content

Commit

Permalink
feat: upgrade mongoose 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamensuli committed Nov 3, 2023
1 parent 6e25d2b commit 931dd18
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 283 deletions.
35 changes: 17 additions & 18 deletions app/config/nodefony/mongoose-bundle.js
Expand Up @@ -23,24 +23,23 @@ const vault = async () => {
};

switch (kernel.appEnvironment.environment) {
case "production":
case "development":
default:
connectors.nodefony = {
host: "localhost",
port: 27017,
dbname: "nodefony",
// credentials: vault,
settings: {
user: "nodefony",
pass: "nodefony",
maxPoolSize: 50,
useNewUrlParser: true,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 5000,
connectTimeoutMS: 5000
}
};
case "production":
case "development":
default:
connectors.nodefony = {
host: "localhost",
port: 27017,
dbname: "nodefony",
// credentials: vault,
settings: {
user: "nodefony",
pass: "nodefony",
maxPoolSize: 50,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 5000,
connectTimeoutMS: 5000
}
};
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"graphql": "16.8.1",
"mongoose": "7.6.4",
"mongoose": "8.0.0",
"pm2": "5.3.0",
"pm2-logrotate": "2.7.0",
"sequelize": "6.33.0",
Expand Down
Expand Up @@ -36,8 +36,8 @@ const schema = {
scheme: {
type: String
},
username: {
type: String,
user: {
type: Schema.Types.ObjectId,
ref: "user"
},
data: {
Expand All @@ -46,12 +46,6 @@ const schema = {
};
module.exports = class requests extends nodefony.Entity {
constructor (bundle) {
/*
* @param bundle instance
* @param Entity name
* @param orm name
* @param connection name
*/
super(bundle, "requests", "mongoose", "nodefony");
}

Expand Down
Expand Up @@ -11,8 +11,8 @@ const schema = {
type: String,
default: "default"
},
username: {
type: String,
user: {
type: Schema.Types.ObjectId,
ref: "user"
},
Attributes: {
Expand All @@ -31,12 +31,6 @@ const schema = {

module.exports = class session extends nodefony.Entity {
constructor (bundle) {
/*
* @param bundle instance
* @param Entity name
* @param orm name
* @param connection name
*/
super(bundle, "session", "mongoose", "nodefony");
}

Expand All @@ -48,7 +42,7 @@ module.exports = class session extends nodefony.Entity {
updatedAt: "updatedAt"
}
});
mySchema.statics.fetchAll = function (callback) {
mySchema.statics.fetchAll = function fetchAll (callback) {
return this.findAll().then((result) => callback(null, result))
.catch((error) => {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/nodefony/bundles/mongoose-bundle/package.json
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"mongodb": "6.2.0",
"mongoose": "7.6.4"
"mongoose": "8.0.0"
},
"devDependencies": {},
"contributors": [
Expand Down
19 changes: 6 additions & 13 deletions src/nodefony/bundles/mongoose-bundle/services/mongooseService.js
@@ -1,3 +1,4 @@
/* eslint-disable default-case */
/**
* SERVIVE Mongoose
*/
Expand All @@ -10,9 +11,7 @@ const defaultconfigServer = {
};

const defaultConfigConnection = {
socketTimeoutMS: 0,
keepAlive: true
// useNewUrlParser: true,
socketTimeoutMS: 0
// replicaSet: 'rs'
};

Expand Down Expand Up @@ -214,20 +213,13 @@ class mongoose extends nodefony.Orm {
throw new Error(`Entity : ${entityName} not found`);
}
const {db} = entity;
const session = await db.startSession.call(db);
const session = await db.startSession();
await session.startTransaction();
return session;
}

async getTransaction (entityName) {
const entity = this.getNodefonyEntity(entityName);
if (!entity) {
throw new Error(`Entity : ${entityName} not found`);
}
const {db} = entity;
const session = await db.startSession.bind(db);
await session.startTransaction();
return session;
getTransaction (entityName) {
return this.startTransaction(entityName);
}

getConnectorsList (name = null) {
Expand Down Expand Up @@ -295,6 +287,7 @@ class mongoose extends nodefony.Orm {
return obj;
}

// eslint-disable-next-line complexity
getOpenApiSchema (entity) {
const attr = {
type: "object",
Expand Down
@@ -1,5 +1,6 @@
/* eslint-disable max-lines-per-function */
nodefony.register.call(nodefony.session.storage, "mongoose", () => {
const finderGC = function (msMaxlifetime, contextSession) {
const finderGC = function finderGC (msMaxlifetime, contextSession) {
const where = {
context: contextSession,
updatedAt: {
Expand Down Expand Up @@ -70,7 +71,7 @@ nodefony.register.call(nodefony.session.storage, "mongoose", () => {
})
.then((result) => {
if (result) {
return result.remove({
return result.deleteOne({
force: true
}).then((session) => {
this.manager.logger(`DB DESTROY SESSION context : ${session.context} ID : ${session.session_id} DELETED`);
Expand Down Expand Up @@ -112,7 +113,7 @@ nodefony.register.call(nodefony.session.storage, "mongoose", () => {
};
}
return this.entity.findOne(where)
.populate("username")
.populate("user")
.then((result) => {
if (result) {
return {
Expand Down Expand Up @@ -140,7 +141,7 @@ nodefony.register.call(nodefony.session.storage, "mongoose", () => {
const myuser = await this.userEntity.findOne({
username: data.username.username
});
data.username = myuser._id;
data.user = myuser._id;
}
return this.entity.updateOne({
session_id: id,
Expand Down
@@ -1,3 +1,4 @@
/* eslint-disable default-case */
module.exports = {
Query: {
// provides all functions for each API endpoint
Expand Down Expand Up @@ -42,14 +43,13 @@ module.exports = {
});
case "mongoose":
return requestsEntity.find({})
.populate("username")
.populate("user")
.then((results) =>
// console.log(results)
JSON.stringify({
count: Object.keys(results).length,
rows: results
}));
break;
}
},

Expand Down Expand Up @@ -77,6 +77,15 @@ module.exports = {
context.log(error, "ERROR");
throw new nodefony.Error(error);
});
case "mongoose":
return requestsEntity.findById(id)
.populate("user")
.lean({versionKey: false})
.then((result) => JSON.stringify(result))
.catch((error) => {
context.log(error, "ERROR");
throw new nodefony.Error(error);
});
default:
return JSON.stringify([]);
}
Expand Down

0 comments on commit 931dd18

Please sign in to comment.