Skip to content

Commit

Permalink
Add db config and mongoose models
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandoagf committed May 21, 2021
1 parent 30578e5 commit a7e0961
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/config/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { connect } from 'mongoose'
import { config } from 'dotenv'

config()

export default async function connectDB() {
try {
await connect(
process.env.MONGODB_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
}
)

console.log('MongoDB Connected')
} catch (err) {
console.error(err.message)
process.exit(1)
}
}
14 changes: 14 additions & 0 deletions src/models/LedgerUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { model, Schema, Model, Document } from "mongoose";

interface ILedgerUpdate extends Document {
modifiedAt: number
}

const LedgerUpdateSchema: Schema = new Schema({
modifiedAt: { type: Number }
}, {
versionKey: false
});

const LedgerUpdate: Model<ILedgerUpdate> = model("LedgerUpdate", LedgerUpdateSchema);
export default LedgerUpdate;
27 changes: 27 additions & 0 deletions src/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { model, Schema, Model, Document } from "mongoose";

export interface IUser extends Document {
discordId: string,
address: string,
username: string,
github: string,
discourse: string,
modifiedAt: number,
createdAt: number
}

const UserSchema: Schema = new Schema({
discordId: { type: String, unique: true, required: true, dropDups: true },
username: { type: String, unique: true, required: true, dropDups: true },
discourse: { type: String },
github: { type: String },
address: { type: String, unique: true },
modifiedAt: { type: Number },
createdAt: { type: Number, default: Date.now() }
}, {
versionKey: false,
_id: false
});

const User: Model<IUser> = model("User", UserSchema);
export default User;

0 comments on commit a7e0961

Please sign in to comment.