Skip to content

remove 'I' prefix from the interface - community request #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/auth/authUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tokens } from 'app-request';
import { AuthFailureError, InternalError } from '../core/ApiError';
import JWT, { JwtPayload } from '../core/JWT';
import { Types } from 'mongoose';
import { IUser } from '../database/model/User';
import User from '../database/model/User';
import { tokenInfo } from '../config';

export const validateTokenData = async (payload: JwtPayload, userId: Types.ObjectId): Promise<JwtPayload> => {
Expand All @@ -14,7 +14,7 @@ export const validateTokenData = async (payload: JwtPayload, userId: Types.Objec
return payload;
};

export const createTokens = async (user: IUser, accessTokenKey: string, refreshTokenKey: string)
export const createTokens = async (user: User, accessTokenKey: string, refreshTokenKey: string)
: Promise<Tokens> => {
const accessToken = await JWT.encode(
new JwtPayload(
Expand Down
6 changes: 2 additions & 4 deletions src/database/model/ApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Schema, model, Document, Types } from 'mongoose';
export const DOCUMENT_NAME = 'ApiKey';
export const COLLECTION_NAME = 'api_keys';

export interface IApiKey extends Document {
export default interface ApiKey extends Document {
key: string;
version: number;
metadata: string;
Expand Down Expand Up @@ -49,6 +49,4 @@ const schema = new Schema(
versionKey: false
});

const ApiKey = model<IApiKey>(DOCUMENT_NAME, schema, COLLECTION_NAME);

export default ApiKey;
export const ApiKeyModel = model<ApiKey>(DOCUMENT_NAME, schema, COLLECTION_NAME);
15 changes: 6 additions & 9 deletions src/database/model/Blog.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Schema, model, Document } from 'mongoose';
import { IUser } from './User';
import Logger from '../../core/Logger';
import User from './User';

export const DOCUMENT_NAME = 'Blog';
export const COLLECTION_NAME = 'blogs';

export interface IBlog extends Document {
export default interface Blog extends Document {
title: string;
description: string;
text?: string;
draftText: string;
tags: string[];
author: IUser;
author: User;
imgUrl?: string;
blogUrl: string;
likes?: number;
Expand All @@ -21,8 +20,8 @@ export interface IBlog extends Document {
isPublished: boolean;
status?: boolean;
publishedAt?: Date;
createdBy?: IUser;
updatedBy?: IUser;
createdBy?: User;
updatedBy?: User;
createdAt?: Date;
updatedAt?: Date;
}
Expand Down Expand Up @@ -145,6 +144,4 @@ const schema = new Schema(
{ weights: { title: 3, description: 1 }, background: false }
);

const Blog = model<IBlog>(DOCUMENT_NAME, schema, COLLECTION_NAME);

export default Blog;
export const BlogModel = model<Blog>(DOCUMENT_NAME, schema, COLLECTION_NAME);
10 changes: 4 additions & 6 deletions src/database/model/Keystore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Schema, model, Document } from 'mongoose';
import { IUser } from './User';
import User from './User';

export const DOCUMENT_NAME = 'Keystore';
export const COLLECTION_NAME = 'keystores';

export interface IKeystore extends Document {
client: IUser;
export default interface Keystore extends Document {
client: User;
primaryKey: string;
secondaryKey: string;
status?: boolean;
Expand Down Expand Up @@ -50,6 +50,4 @@ const schema = new Schema(
versionKey: false
});

const Keystore = model<IKeystore>(DOCUMENT_NAME, schema, COLLECTION_NAME);

export default Keystore;
export const KeystoreModel = model<Keystore>(DOCUMENT_NAME, schema, COLLECTION_NAME);
6 changes: 2 additions & 4 deletions src/database/model/Role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const enum RoleCode {
ADMIN = 'ADMIN',
}

export interface IRole extends Document {
export default interface Role extends Document {
code: string;
status?: boolean;
createdAt?: Date;
Expand Down Expand Up @@ -48,6 +48,4 @@ const schema = new Schema(
versionKey: false
});

const Role = model<IRole>(DOCUMENT_NAME, schema, COLLECTION_NAME);

export default Role;
export const RoleModel = model<Role>(DOCUMENT_NAME, schema, COLLECTION_NAME);
10 changes: 4 additions & 6 deletions src/database/model/User.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { model, Schema, Document, Types } from 'mongoose';
import { IRole } from './Role';
import Role from './Role';

export const DOCUMENT_NAME = 'User';
export const COLLECTION_NAME = 'users';

export interface IUser extends Document {
export default interface User extends Document {
name: string;
email?: string;
password?: string;
profilePicUrl?: string;
roles: IRole[];
roles: Role[];
verified?: boolean;
status?: boolean;
createdAt?: Date;
Expand Down Expand Up @@ -70,6 +70,4 @@ const schema = new Schema(
versionKey: false
});

const User = model<IUser>(DOCUMENT_NAME, schema, COLLECTION_NAME);

export default User;
export const UserModel = model<User>(DOCUMENT_NAME, schema, COLLECTION_NAME);
6 changes: 3 additions & 3 deletions src/database/repository/ApiKeyRepo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ApiKey, { IApiKey } from '../model/ApiKey';
import ApiKey, { ApiKeyModel } from '../model/ApiKey';

export default class ApiRepo {

public static async findByKey(key: string): Promise<IApiKey> {
return ApiKey.findOne({ key: key, status: true }).lean<IApiKey>().exec();
public static async findByKey(key: string): Promise<ApiKey> {
return ApiKeyModel.findOne({ key: key, status: true }).lean<ApiKey>().exec();
}
}
102 changes: 51 additions & 51 deletions src/database/repository/BlogRepo.ts
Original file line number Diff line number Diff line change
@@ -1,135 +1,135 @@
import Blog, { IBlog } from '../model/Blog';
import Blog, { BlogModel } from '../model/Blog';
import { Types } from 'mongoose';
import { IUser } from '../model/User';
import User from '../model/User';

export default class BlogRepo {

private static AUTHOR_DETAIL = 'name profilePicUrl';
private static BLOG_INFO_ADDITIONAL = '+isSubmitted +isDraft +isPublished +createdBy +updatedBy';
private static BLOG_ALL_DATA = '+text +draftText +isSubmitted +isDraft +isPublished +status +createdBy +updatedBy';

public static async create(blog: IBlog): Promise<IBlog> {
public static async create(blog: Blog): Promise<Blog> {
const now = new Date();
blog.createdAt = now;
blog.updatedAt = now;
const createdBlog = await Blog.create(blog);
const createdBlog = await BlogModel.create(blog);
return createdBlog.toObject();
}

public static update(blog: IBlog): Promise<any> {
public static update(blog: Blog): Promise<any> {
blog.updatedAt = new Date();
return Blog.updateOne({ _id: blog._id }, { $set: { ...blog } }).lean<IBlog>().exec();
return BlogModel.updateOne({ _id: blog._id }, { $set: { ...blog } }).lean<Blog>().exec();
}

public static findInfoById(id: Types.ObjectId): Promise<IBlog> {
return Blog.findOne({ _id: id, status: true })
public static findInfoById(id: Types.ObjectId): Promise<Blog> {
return BlogModel.findOne({ _id: id, status: true })
.populate('author', this.AUTHOR_DETAIL)
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findInfoWithTextById(id: Types.ObjectId): Promise<IBlog> {
return Blog.findOne({ _id: id, status: true })
public static findInfoWithTextById(id: Types.ObjectId): Promise<Blog> {
return BlogModel.findOne({ _id: id, status: true })
.select('+text')
.populate('author', this.AUTHOR_DETAIL)
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findInfoWithTextAndDraftTextById(id: Types.ObjectId): Promise<IBlog> {
return Blog.findOne({ _id: id, status: true })
public static findInfoWithTextAndDraftTextById(id: Types.ObjectId): Promise<Blog> {
return BlogModel.findOne({ _id: id, status: true })
.select('+text +draftText +isSubmitted +isDraft +isPublished +status')
.populate('author', this.AUTHOR_DETAIL)
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findBlogAllDataById(id: Types.ObjectId): Promise<IBlog> {
return Blog.findOne({ _id: id, status: true })
public static findBlogAllDataById(id: Types.ObjectId): Promise<Blog> {
return BlogModel.findOne({ _id: id, status: true })
.select(this.BLOG_ALL_DATA)
.populate('author', this.AUTHOR_DETAIL)
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findByUrl(blogUrl: string): Promise<IBlog> {
return Blog.findOne({ blogUrl: blogUrl, status: true })
public static findByUrl(blogUrl: string): Promise<Blog> {
return BlogModel.findOne({ blogUrl: blogUrl, status: true })
.select('+text')
.populate('author', this.AUTHOR_DETAIL)
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findUrlIfExists(blogUrl: string): Promise<IBlog> {
return Blog.findOne({ blogUrl: blogUrl }).lean<IBlog>().exec();
public static findUrlIfExists(blogUrl: string): Promise<Blog> {
return BlogModel.findOne({ blogUrl: blogUrl }).lean<Blog>().exec();
}

public static findByTagAndPaginated(tag: string, pageNumber: number, limit: number): Promise<IBlog[]> {
return Blog.find({ tags: tag, status: true, isPublished: true })
public static findByTagAndPaginated(tag: string, pageNumber: number, limit: number): Promise<Blog[]> {
return BlogModel.find({ tags: tag, status: true, isPublished: true })
.skip(limit * (pageNumber - 1))
.limit(limit)
.populate('author', this.AUTHOR_DETAIL)
.sort({ updatedAt: -1 })
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findAllPublishedForAuthor(user: IUser): Promise<IBlog[]> {
return Blog.find({ author: user, status: true, isPublished: true })
public static findAllPublishedForAuthor(user: User): Promise<Blog[]> {
return BlogModel.find({ author: user, status: true, isPublished: true })
.populate('author', this.AUTHOR_DETAIL)
.sort({ updatedAt: -1 })
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findAllDrafts(): Promise<IBlog[]> {
public static findAllDrafts(): Promise<Blog[]> {
return this.findDetailedBlogs({ isDraft: true, status: true });
}

public static findAllSubmissions(): Promise<IBlog[]> {
public static findAllSubmissions(): Promise<Blog[]> {
return this.findDetailedBlogs({ isSubmitted: true, status: true });
}

public static findAllPublished(): Promise<IBlog[]> {
public static findAllPublished(): Promise<Blog[]> {
return this.findDetailedBlogs({ isPublished: true, status: true });
}

public static findAllSubmissionsForWriter(user: IUser): Promise<IBlog[]> {
public static findAllSubmissionsForWriter(user: User): Promise<Blog[]> {
return this.findDetailedBlogs({ author: user, status: true, isSubmitted: true });
}

public static findAllPublishedForWriter(user: IUser): Promise<IBlog[]> {
public static findAllPublishedForWriter(user: User): Promise<Blog[]> {
return this.findDetailedBlogs({ author: user, status: true, isPublished: true });
}

public static findAllDraftsForWriter(user: IUser): Promise<IBlog[]> {
public static findAllDraftsForWriter(user: User): Promise<Blog[]> {
return this.findDetailedBlogs({ author: user, status: true, isDraft: true });
}

private static findDetailedBlogs(query: Object): Promise<IBlog[]> {
return Blog.find(query)
private static findDetailedBlogs(query: Object): Promise<Blog[]> {
return BlogModel.find(query)
.select(this.BLOG_INFO_ADDITIONAL)
.populate('author', this.AUTHOR_DETAIL)
.populate('createdBy', this.AUTHOR_DETAIL)
.populate('updatedBy', this.AUTHOR_DETAIL)
.sort({ updatedAt: -1 })
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static findLatestBlogs(pageNumber: number, limit: number): Promise<IBlog[]> {
return Blog.find({ status: true, isPublished: true })
public static findLatestBlogs(pageNumber: number, limit: number): Promise<Blog[]> {
return BlogModel.find({ status: true, isPublished: true })
.skip(limit * (pageNumber - 1))
.limit(limit)
.populate('author', this.AUTHOR_DETAIL)
.sort({ publishedAt: -1 })
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static searchSimilarBlogs(blog: IBlog, limit: number)
: Promise<IBlog[]> {
return Blog.find(
public static searchSimilarBlogs(blog: Blog, limit: number)
: Promise<Blog[]> {
return BlogModel.find(
{
$text: { $search: blog.title, $caseSensitive: false },
status: true,
Expand All @@ -143,12 +143,12 @@ export default class BlogRepo {
.sort({ updatedAt: -1 })
.limit(limit)
.sort({ similarity: { $meta: 'textScore' } })
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static search(query: string, limit: number): Promise<IBlog[]> {
return Blog.find(
public static search(query: string, limit: number): Promise<Blog[]> {
return BlogModel.find(
{
$text: { $search: query, $caseSensitive: false },
status: true,
Expand All @@ -160,12 +160,12 @@ export default class BlogRepo {
.select('-status -description')
.limit(limit)
.sort({ similarity: { $meta: 'textScore' } })
.lean<IBlog>()
.lean<Blog>()
.exec();
}

public static searchLike(query: string, limit: number): Promise<IBlog[]> {
return Blog.find(
public static searchLike(query: string, limit: number): Promise<Blog[]> {
return BlogModel.find(
{
title: { $regex: `.*${query}.*`, $options: 'i' },
status: true,
Expand All @@ -174,7 +174,7 @@ export default class BlogRepo {
.select('-status -description')
.limit(limit)
.sort({ score: -1 })
.lean<IBlog>()
.lean<Blog>()
.exec();
}
}
Loading