Migrate to shortid for easier links

This commit is contained in:
oitsjustjose 2020-07-05 09:46:18 -05:00
commit 9e8fdcac95
6 changed files with 37 additions and 7 deletions

18
package-lock.json generated
View file

@ -166,6 +166,11 @@
"@types/mime": "*" "@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": { "@types/ws": {
"version": "7.2.6", "version": "7.2.6",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.2.6.tgz", "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", "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz",
"integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" "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": { "nanomatch": {
"version": "1.2.13", "version": "1.2.13",
"resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz", "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", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" "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": { "sift": {
"version": "7.0.1", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz", "resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz",

View file

@ -21,6 +21,7 @@
"@types/mongoose": "^5.7.21", "@types/mongoose": "^5.7.21",
"@types/passport": "^1.0.3", "@types/passport": "^1.0.3",
"@types/passport-local": "^1.0.33", "@types/passport-local": "^1.0.33",
"@types/shortid": "0.0.29",
"bcrypt": "^3.0.8", "bcrypt": "^3.0.8",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"cookie-session": "^1.4.0", "cookie-session": "^1.4.0",
@ -33,6 +34,7 @@
"mongoose": "^5.9.13", "mongoose": "^5.9.13",
"passport": "^0.4.1", "passport": "^0.4.1",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"shortid": "^2.2.15",
"typescript": "^3.9.3" "typescript": "^3.9.3"
}, },
"author": "", "author": "",

View file

@ -1,11 +1,18 @@
import { Document, model, Schema, Types } from 'mongoose' import { Document, model, Schema, Types } from 'mongoose'
import shortid from 'shortid'
const ConfigSchema = new Schema({ const ConfigSchema = new Schema({
json: Object, json: Object,
shortid: {
type: String,
unique: true,
default: shortid.generate
}
}) })
export type ConfigModel = Document & { export type ConfigModel = Document & {
json: object, json: object,
shortid: string
} }
export default model<ConfigModel>('configs', ConfigSchema, 'configs') export default model<ConfigModel>('configs', ConfigSchema, 'configs')

View file

@ -6,7 +6,7 @@ const UserSchema = new Schema({
password: String, password: String,
configs: [{ configs: [{
name: String, name: String,
conf: Types.ObjectId, conf: String
}] }]
}) })
@ -26,7 +26,7 @@ UserSchema.methods.changePassword = function (password: string, newPassword: str
interface INamedConfig { interface INamedConfig {
name: string, name: string,
conf: Types.ObjectId conf: string
} }
export type UserModel = Document & { export type UserModel = Document & {

View file

@ -1,8 +1,8 @@
import { Application } from "express" import { Application } from "express"
import { PassportStatic } from "passport" import { PassportStatic } from "passport"
import * as Auth from './auth' import shortid from 'shortid'
import ConfigDB from '../models/config.model' import ConfigDB from '../models/config.model'
import * as Auth from './auth'
export const init = (app: Application, passport: PassportStatic) => { export const init = (app: Application, passport: PassportStatic) => {
app.get('/', async (req, res) => { app.get('/', async (req, res) => {
@ -15,12 +15,13 @@ export const init = (app: Application, passport: PassportStatic) => {
try { try {
const conf = new ConfigDB() const conf = new ConfigDB()
conf.json = req.body.json conf.json = req.body.json
await conf.save() await conf.save()
if (req.isAuthenticated() && req.user) { if (req.isAuthenticated() && req.user) {
req.user.configs.push({ req.user.configs.push({
name: req.body.name, name: req.body.name,
conf: conf._id conf: conf.shortid
}) })
} }
res.status(200).send(conf) res.status(200).send(conf)
@ -32,7 +33,9 @@ export const init = (app: Application, passport: PassportStatic) => {
}) })
app.get('/configs/:id', async (req, res) => { 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) { if (conf) {
res.json(conf.json) res.json(conf.json)
} else { } else {

View file

@ -45,7 +45,7 @@ export const read = async () => {
<center> <center>
You can view your JSON at You can view your JSON at
<br> <br>
<a href="${location.origin}/configs/${newDoc._id}">${location.origin}/configs/${newDoc._id}</a> <a href="${location.origin}/configs/${newDoc.shortid}">${location.origin}/configs/${newDoc.shortid}</a>
</center> </center>
`); `);
}; };