Skip to content

Commit 9743672

Browse files
committed
add template
1 parent ba54242 commit 9743672

File tree

6 files changed

+209
-24
lines changed

6 files changed

+209
-24
lines changed

.templates/database/model/Sample.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Schema, model, Types } from 'mongoose';
2+
3+
export const DOCUMENT_NAME = 'Sample';
4+
export const COLLECTION_NAME = 'samples';
5+
6+
export enum Category {
7+
ABC = 'ABC',
8+
XYZ = 'XYZ',
9+
}
10+
11+
export default interface Sample {
12+
_id: Types.ObjectId;
13+
category: Category;
14+
status?: boolean;
15+
createdAt?: Date;
16+
updatedAt?: Date;
17+
}
18+
19+
const schema = new Schema<Sample>(
20+
{
21+
category: {
22+
type: Schema.Types.String,
23+
required: true,
24+
enum: Object.values(Category),
25+
},
26+
status: {
27+
type: Schema.Types.Boolean,
28+
default: true,
29+
},
30+
createdAt: {
31+
type: Schema.Types.Date,
32+
required: true,
33+
select: false,
34+
},
35+
updatedAt: {
36+
type: Schema.Types.Date,
37+
required: true,
38+
select: false,
39+
},
40+
},
41+
{
42+
versionKey: false,
43+
},
44+
);
45+
46+
export const SampleModel = model<Sample>(DOCUMENT_NAME, schema, COLLECTION_NAME);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Sample, { SampleModel } from '../model/Sample';
2+
import { Types } from 'mongoose';
3+
4+
export default class SampleRepo {
5+
public static findById(id: Types.ObjectId): Promise<Sample | null> {
6+
return SampleModel.findOne({ _id: id, status: true }).lean().exec();
7+
}
8+
9+
public static async create(sample: Sample): Promise<Sample> {
10+
const now = new Date();
11+
sample.createdAt = now;
12+
sample.updatedAt = now;
13+
const created = await SampleModel.create(sample);
14+
return created.toObject();
15+
}
16+
17+
public static update(sample: Sample): Promise<Sample | null> {
18+
sample.updatedAt = new Date();
19+
return SampleModel.findByIdAndUpdate(sample._id, sample, { new: true }).lean().exec();
20+
}
21+
}

.templates/route/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
import express from 'express';
3+
import { ProtectedRequest } from 'app-request';
4+
import { SuccessResponse } from '../../../core/ApiResponse';
5+
import asyncHandler from '../../../helpers/asyncHandler';
6+
import validator, { ValidationSource } from '../../../helpers/validator';
7+
import schema from './schema';
8+
import { BadRequestError } from '../../../core/ApiError';
9+
import role from '../../../helpers/role';
10+
import authentication from '../../../auth/authentication';
11+
import authorization from '../../../auth/authorization';
12+
import { RoleCode } from '../../../database/model/Role';
13+
14+
const router = express.Router();
15+
16+
//----------------------------------------------------------------
17+
router.use(authentication, role(RoleCode.LEARNER), authorization);
18+
//----------------------------------------------------------------
19+
20+
router.post(
21+
'/sample',
22+
validator(schema.sample, ValidationSource.BODY),
23+
asyncHandler(async (req: ProtectedRequest, res) => {
24+
new SuccessResponse('Success', {}).send(res);
25+
}),
26+
);
27+
28+
export default router;
29+
*/

.templates/route/schema.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Joi from 'joi';
2+
3+
export default {
4+
sample: Joi.object().keys({
5+
key: Joi.string().required().min(1),
6+
}),
7+
};

.vscode/launch.json

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "2.0.0",
6-
"configurations": [
7-
{
8-
"type": "node",
9-
"request": "launch",
10-
"name": "Launch Program",
11-
"program": "${workspaceFolder}/src/server.ts",
12-
"preLaunchTask": "tsc: build - tsconfig.json",
13-
"runtimeArgs": [
14-
"-r",
15-
"dotenv/config"
16-
],
17-
"outFiles": [
18-
"${workspaceFolder}/build/*.js",
19-
"${workspaceFolder}/build/**/*.js",
20-
"${workspaceFolder}/build/**/**/*.js",
21-
"${workspaceFolder}/build/**/**/**/*.js"
22-
]
23-
}
24-
]
25-
}
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "2.0.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/build/server.js",
12+
"preLaunchTask": "tsc: build - tsconfig.json",
13+
"runtimeArgs": ["-r", "dotenv/config"],
14+
"outFiles": [
15+
"${workspaceFolder}/build/*.js",
16+
"${workspaceFolder}/build/**/*.js",
17+
"${workspaceFolder}/build/**/**/*.js",
18+
"${workspaceFolder}/build/**/**/**/*.js"
19+
]
20+
}
21+
]
22+
}

.vscode/tasks.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "vscode: Install Plugins",
6+
"type": "shell",
7+
"command": "chmod +x ./.vscode/extensions.list | ./.vscode/extensions.list",
8+
"presentation": {
9+
"reveal": "always",
10+
"panel": "new"
11+
}
12+
},
13+
{
14+
"label": "Install Dependencies",
15+
"type": "shell",
16+
"command": "npm install",
17+
"presentation": {
18+
"reveal": "always",
19+
"panel": "new"
20+
}
21+
},
22+
{
23+
"label": "Start Prod Server",
24+
"type": "npm",
25+
"script": "start"
26+
},
27+
{
28+
"label": "Start Dev Server",
29+
"type": "npm",
30+
"script": "run watch"
31+
},
32+
{
33+
"label": "Template: Generate V1 Route",
34+
"type": "shell",
35+
"command": "cp",
36+
"presentation": {
37+
"reveal": "never",
38+
"panel": "shared"
39+
},
40+
"args": [
41+
"-r",
42+
"${workspaceFolder}/.templates/route",
43+
"${workspaceFolder}/src/routes/v1/${input:routeName}"
44+
]
45+
},
46+
{
47+
"label": "Template: Generate Model",
48+
"type": "shell",
49+
"command": "cp",
50+
"presentation": {
51+
"reveal": "never",
52+
"panel": "shared"
53+
},
54+
"args": [
55+
"${workspaceFolder}/.templates/database/model/Sample.ts",
56+
"${workspaceFolder}/src/database/model/${input:modelName}.ts"
57+
]
58+
},
59+
{
60+
"label": "Template: Generate Repo",
61+
"type": "shell",
62+
"command": "cp",
63+
"presentation": {
64+
"reveal": "never",
65+
"panel": "shared"
66+
},
67+
"args": [
68+
"${workspaceFolder}/.templates/database/repository/SampleRepo.ts",
69+
"${workspaceFolder}/src/database/repository/${input:modelName}Repo.ts"
70+
]
71+
}
72+
],
73+
"inputs": [
74+
{
75+
"type": "promptString",
76+
"id": "routeName",
77+
"description": "Route name to create?"
78+
},
79+
{
80+
"type": "promptString",
81+
"id": "modelName",
82+
"description": "Model name to create?"
83+
}
84+
]
85+
}

0 commit comments

Comments
 (0)