🍒 Selective update through findOneAndUpdate for settings

This commit is contained in:
2021-11-23 09:37:45 -08:00
parent 6c431b300b
commit 973bbe93be
2 changed files with 64 additions and 26 deletions

View File

@@ -4,14 +4,15 @@ const paginate = require("mongoose-paginate-v2");
const SettingsScehma = mongoose.Schema({ const SettingsScehma = mongoose.Schema({
directConnect: { directConnect: {
client: { client: {
name: String, host: {
version: String, username: String,
airdcppUserSettings: Object, password: String,
hostname: String, hostname: String,
protocol: String, protocol: String,
username: String, },
password: String, airDCPPUserSettings: Object,
hubs: [{}],
hubs: Array,
}, },
}, },
}); });

View File

@@ -8,7 +8,17 @@ import {
} from "moleculer"; } from "moleculer";
import { DbMixin } from "../mixins/db.mixin"; import { DbMixin } from "../mixins/db.mixin";
import Settings from "../models/settings.model"; import Settings from "../models/settings.model";
import { isEmpty } from "lodash"; import {
isEmpty,
isUndefined,
pick,
pickBy,
identity,
map,
omitBy,
isNil,
} from "lodash";
const ObjectId = require("mongoose").Types.ObjectId;
export default class SettingsService extends Service { export default class SettingsService extends Service {
// @ts-ignore // @ts-ignore
@@ -45,31 +55,58 @@ export default class SettingsService extends Service {
params: {}, params: {},
async handler( async handler(
ctx: Context<{ ctx: Context<{
settingsObject: { settingsPayload: {
hostname: string; host: object;
protocol: string; airDCPPUserSettings: object;
username: string; hubs: [];
password: string;
}; };
airdcppUserSettings: object; settingsObjectId: string;
}> }>
) { ) {
console.log(ctx.params); console.log("varan bhat", ctx.params);
const { settingsObject, airdcppUserSettings } = const { host, airDCPPUserSettings, hubs } =
ctx.params; ctx.params.settingsPayload;
let query = {
host,
airDCPPUserSettings,
hubs,
};
const keysToUpdate = pickBy(query, identity);
let updateQuery = {};
const result = await Settings.create({ map(Object.keys(keysToUpdate), (key) => {
directConnect: { updateQuery[`directConnect.client.${key}`] =
client: { query[key];
...settingsObject,
airdcppUserSettings,
},
},
}); });
console.log("ASDASD", result); const options = {
upsert: true,
new: true,
setDefaultsOnInsert: true,
};
const filter = {
_id: new ObjectId(
ctx.params.settingsObjectId
),
};
const result = Settings.findOneAndUpdate(
filter,
{ $set: updateQuery },
options
);
return result; return result;
}, },
}, },
deleteSettings: {
rest: "POST /deleteSettings",
params: {},
async handler(ctx: Context<{}>) {
return await Settings.remove(
{},
(result) => result
);
},
},
}, },
methods: {}, methods: {},
}, },