Adding data model/ Schema

| fullurl | shorturl | clicks |

+ shortid-library to shorten url/get id from url
This commit is contained in:
Sunny Dhoke
2020-10-08 11:51:08 +05:30
parent 6f36ae8680
commit 3869894394
3 changed files with 44 additions and 1 deletions

29
models/shortUrl.js Normal file
View File

@@ -0,0 +1,29 @@
//to connect to mongodb
const mongoose = require("mongoose");
// to create short identifier
//has .generate() function that we will pass to shorturl as default
const shortId = require("shortid");
//db schema
// | fullurl | shorturl | clicks |
const shortUrlSchema = new mongoose.Schema({
fullurl: {
type: String,
required: true,
},
shorturl: {
type: String,
required: true,
default: shortId.generate,
},
clicks: {
type: Number,
required: true,
default: 0,
},
});
//now exporting this to hook model with database
// model name, schema name
modules.exports = mongoose.model("shortUrl", shortUrlSchema);

13
package-lock.json generated
View File

@@ -991,6 +991,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"nanoid": {
"version": "2.1.11",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-2.1.11.tgz",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
},
"negotiator": {
"version": "0.6.2",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
@@ -1342,6 +1347,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",

View File

@@ -23,7 +23,8 @@
"dependencies": {
"ejs": "^3.1.5",
"express": "^4.17.1",
"mongoose": "^5.10.8"
"mongoose": "^5.10.8",
"shortid": "^2.2.15"
},
"devDependencies": {
"nodemon": "^2.0.4"