diff --git a/package-lock.json b/package-lock.json index 0fd7cbf..55a0363 100644 --- a/package-lock.json +++ b/package-lock.json @@ -166,6 +166,11 @@ "@types/mime": "*" } }, + "@types/shortid": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/shortid/-/shortid-0.0.29.tgz", + "integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=" + }, "@types/ws": { "version": "7.2.6", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.2.6.tgz", @@ -2377,6 +2382,11 @@ "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" }, + "nanoid": { + "version": "2.1.11", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.11.tgz", + "integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==" + }, "nanomatch": { "version": "1.2.13", "resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz", @@ -3187,6 +3197,14 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" }, + "shortid": { + "version": "2.2.15", + "resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.15.tgz", + "integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==", + "requires": { + "nanoid": "^2.1.0" + } + }, "sift": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz", diff --git a/package.json b/package.json index f8aabcf..36959d1 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@types/mongoose": "^5.7.21", "@types/passport": "^1.0.3", "@types/passport-local": "^1.0.33", + "@types/shortid": "0.0.29", "bcrypt": "^3.0.8", "body-parser": "^1.19.0", "cookie-session": "^1.4.0", @@ -33,6 +34,7 @@ "mongoose": "^5.9.13", "passport": "^0.4.1", "passport-local": "^1.0.0", + "shortid": "^2.2.15", "typescript": "^3.9.3" }, "author": "", diff --git a/src/app/models/config.model.ts b/src/app/models/config.model.ts index 52ac738..a6c202d 100644 --- a/src/app/models/config.model.ts +++ b/src/app/models/config.model.ts @@ -1,11 +1,18 @@ import { Document, model, Schema, Types } from 'mongoose' +import shortid from 'shortid' const ConfigSchema = new Schema({ json: Object, + shortid: { + type: String, + unique: true, + default: shortid.generate + } }) export type ConfigModel = Document & { json: object, + shortid: string } export default model('configs', ConfigSchema, 'configs') \ No newline at end of file diff --git a/src/app/models/user.model.ts b/src/app/models/user.model.ts index f1471bb..55e55bc 100644 --- a/src/app/models/user.model.ts +++ b/src/app/models/user.model.ts @@ -6,7 +6,7 @@ const UserSchema = new Schema({ password: String, configs: [{ name: String, - conf: Types.ObjectId, + conf: String }] }) @@ -26,7 +26,7 @@ UserSchema.methods.changePassword = function (password: string, newPassword: str interface INamedConfig { name: string, - conf: Types.ObjectId + conf: string } export type UserModel = Document & { diff --git a/src/app/routes/routes.ts b/src/app/routes/routes.ts index 9364b50..335262c 100644 --- a/src/app/routes/routes.ts +++ b/src/app/routes/routes.ts @@ -1,8 +1,8 @@ import { Application } from "express" import { PassportStatic } from "passport" -import * as Auth from './auth' +import shortid from 'shortid' import ConfigDB from '../models/config.model' - +import * as Auth from './auth' export const init = (app: Application, passport: PassportStatic) => { app.get('/', async (req, res) => { @@ -15,12 +15,13 @@ export const init = (app: Application, passport: PassportStatic) => { try { const conf = new ConfigDB() conf.json = req.body.json + await conf.save() if (req.isAuthenticated() && req.user) { req.user.configs.push({ name: req.body.name, - conf: conf._id + conf: conf.shortid }) } res.status(200).send(conf) @@ -32,7 +33,9 @@ export const init = (app: Application, passport: PassportStatic) => { }) app.get('/configs/:id', async (req, res) => { - const conf = await ConfigDB.findById(req.params.id) + const conf = await ConfigDB.findOne({ + shortid: req.params.id + }) if (conf) { res.json(conf.json) } else { diff --git a/src/public/assets/js/modules/parser/ore.module.js b/src/public/assets/js/modules/parser/ore.module.js index 24ea030..d40b94a 100644 --- a/src/public/assets/js/modules/parser/ore.module.js +++ b/src/public/assets/js/modules/parser/ore.module.js @@ -45,7 +45,7 @@ export const read = async () => {
You can view your JSON at
- ${location.origin}/configs/${newDoc._id} + ${location.origin}/configs/${newDoc.shortid}
`); };