@bejibun/core 0.1.39 → 0.1.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/bases/BaseController.d.ts +1 -2
- package/bases/BaseModel.js +3 -3
- package/builders/RouterBuilder.d.ts +4 -0
- package/builders/RouterBuilder.js +30 -7
- package/commands/Kernel.d.ts +1 -0
- package/commands/Kernel.js +28 -13
- package/commands/{DbSeedCommand.d.ts → db/DbSeedCommand.d.ts} +2 -2
- package/commands/{DbSeedCommand.js → db/DbSeedCommand.js} +2 -2
- package/commands/maintenance/MaintenanceDownCommand.d.ts +27 -0
- package/commands/maintenance/MaintenanceDownCommand.js +40 -0
- package/commands/{MigrateFreshCommand.d.ts → migrate/MigrateFreshCommand.d.ts} +2 -2
- package/commands/{MigrateFreshCommand.js → migrate/MigrateFreshCommand.js} +5 -4
- package/commands/{MigrateLatestCommand.d.ts → migrate/MigrateLatestCommand.d.ts} +2 -2
- package/commands/{MigrateLatestCommand.js → migrate/MigrateLatestCommand.js} +2 -2
- package/commands/{MigrateRollbackCommand.d.ts → migrate/MigrateRollbackCommand.d.ts} +2 -2
- package/commands/{MigrateRollbackCommand.js → migrate/MigrateRollbackCommand.js} +5 -4
- package/commands/{MigrateStatusCommand.d.ts → migrate/MigrateStatusCommand.d.ts} +2 -2
- package/commands/{MigrateStatusCommand.js → migrate/MigrateStatusCommand.js} +5 -4
- package/config/database.js +2 -2
- package/exceptions/ModelNotFoundException.js +2 -0
- package/exceptions/RouterInvalidException.js +2 -0
- package/exceptions/ValidatorException.js +2 -0
- package/facades/Router.d.ts +1 -0
- package/facades/Router.js +5 -14
- package/package.json +6 -5
- package/types/router.d.ts +1 -1
- package/bun.lock +0 -266
- package/src/ace.ts +0 -22
- package/src/bases/BaseController.ts +0 -139
- package/src/bases/BaseModel.ts +0 -108
- package/src/bases/BaseValidator.ts +0 -8
- package/src/bases/index.ts +0 -3
- package/src/bootstrap.ts +0 -5
- package/src/builders/ResponseBuilder.ts +0 -54
- package/src/builders/RouterBuilder.ts +0 -173
- package/src/commands/DbSeedCommand.ts +0 -57
- package/src/commands/Kernel.ts +0 -49
- package/src/commands/MigrateFreshCommand.ts +0 -76
- package/src/commands/MigrateLatestCommand.ts +0 -56
- package/src/commands/MigrateRollbackCommand.ts +0 -72
- package/src/commands/MigrateStatusCommand.ts +0 -64
- package/src/config/database.ts +0 -41
- package/src/exceptions/ModelNotFoundException.ts +0 -15
- package/src/exceptions/RouterInvalidException.ts +0 -15
- package/src/exceptions/ValidatorException.ts +0 -15
- package/src/exceptions/index.ts +0 -3
- package/src/facades/Response.ts +0 -15
- package/src/facades/Router.ts +0 -89
- package/src/facades/SoftDeletes.ts +0 -66
- package/src/facades/index.ts +0 -3
- package/src/index.ts +0 -5
- package/src/types/index.d.ts +0 -4
- package/src/types/middleware.d.ts +0 -7
- package/src/types/router.d.ts +0 -5
- package/src/types/validator.d.ts +0 -3
- package/src/types/vine.d.ts +0 -13
- package/src/utils/vine.ts +0 -2
- package/src/utils/vines/exists.ts +0 -41
- package/src/utils/vines/unique.ts +0 -41
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import Cors from "@bejibun/cors";
|
|
2
|
-
|
|
3
|
-
export default class ResponseBuilder {
|
|
4
|
-
protected data?: any;
|
|
5
|
-
protected message: string;
|
|
6
|
-
protected status: number;
|
|
7
|
-
|
|
8
|
-
public constructor() {
|
|
9
|
-
this.data = null;
|
|
10
|
-
this.message = "Success";
|
|
11
|
-
this.status = 200;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public setData(data?: any): ResponseBuilder {
|
|
15
|
-
this.data = data;
|
|
16
|
-
|
|
17
|
-
return this;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public setMessage(message: string): ResponseBuilder {
|
|
21
|
-
this.message = message;
|
|
22
|
-
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public setStatus(status: number): ResponseBuilder {
|
|
27
|
-
this.status = status;
|
|
28
|
-
|
|
29
|
-
return this;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public send(): globalThis.Response {
|
|
33
|
-
return globalThis.Response.json({
|
|
34
|
-
data: this.data,
|
|
35
|
-
message: this.message,
|
|
36
|
-
status: this.status
|
|
37
|
-
}, {
|
|
38
|
-
headers: {
|
|
39
|
-
...Cors.init
|
|
40
|
-
},
|
|
41
|
-
status: this.status
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public stream(options: ResponseInit = {}): globalThis.Response {
|
|
46
|
-
return new globalThis.Response(Bun.file(this.data), {
|
|
47
|
-
...options,
|
|
48
|
-
headers: {
|
|
49
|
-
...Cors.init
|
|
50
|
-
},
|
|
51
|
-
status: this.status
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import {isEmpty} from "@bejibun/utils";
|
|
2
|
-
import HttpMethodEnum from "@bejibun/utils/enums/HttpMethodEnum";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import type {IMiddleware} from "@/types/middleware";
|
|
5
|
-
import type {HandlerType, ResourceAction, RouterGroup} from "@/types/router";
|
|
6
|
-
import RouterInvalidException from "@/exceptions/RouterInvalidException";
|
|
7
|
-
|
|
8
|
-
export interface ResourceOptions {
|
|
9
|
-
only?: Array<ResourceAction>;
|
|
10
|
-
except?: Array<ResourceAction>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default class RouterBuilder {
|
|
14
|
-
private basePath: string = "";
|
|
15
|
-
private middlewares: Array<IMiddleware> = [];
|
|
16
|
-
|
|
17
|
-
public prefix(basePath: string): RouterBuilder {
|
|
18
|
-
this.basePath = basePath;
|
|
19
|
-
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public middleware(...middlewares: Array<IMiddleware>): RouterBuilder {
|
|
24
|
-
this.middlewares.push(...middlewares);
|
|
25
|
-
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public group(routes: RouterGroup | Array<RouterGroup>): RouterGroup {
|
|
30
|
-
const routeList = Array.isArray(routes) ? routes : [routes];
|
|
31
|
-
const newRoutes: RouterGroup = {};
|
|
32
|
-
|
|
33
|
-
for (const route of routeList) {
|
|
34
|
-
for (const path in route) {
|
|
35
|
-
const fullPath = this.joinPaths(this.basePath, path);
|
|
36
|
-
const routeHandlers = route[path];
|
|
37
|
-
const wrappedHandlers: Record<string, HandlerType> = {};
|
|
38
|
-
|
|
39
|
-
for (const method in routeHandlers) {
|
|
40
|
-
let handler = routeHandlers[method];
|
|
41
|
-
|
|
42
|
-
for (const middleware of this.middlewares) {
|
|
43
|
-
handler = middleware.handle(handler);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
wrappedHandlers[method] = handler;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (isEmpty(newRoutes[fullPath])) newRoutes[fullPath] = {};
|
|
50
|
-
|
|
51
|
-
Object.assign(newRoutes[fullPath], wrappedHandlers);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return newRoutes;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public resources(controller: Record<string, HandlerType>, options?: ResourceOptions): RouterGroup {
|
|
59
|
-
const allRoutes: Record<string, Record<string, ResourceAction>> = {
|
|
60
|
-
"": {
|
|
61
|
-
GET: "index",
|
|
62
|
-
POST: "store"
|
|
63
|
-
},
|
|
64
|
-
":id": {
|
|
65
|
-
GET: "show",
|
|
66
|
-
PUT: "update",
|
|
67
|
-
DELETE: "destroy"
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const includedActions = this.resolveIncludedActions(options);
|
|
72
|
-
|
|
73
|
-
const filteredRoutes: RouterGroup = {};
|
|
74
|
-
|
|
75
|
-
for (const path in allRoutes) {
|
|
76
|
-
const methods = allRoutes[path];
|
|
77
|
-
const methodHandlers: Record<string, HandlerType> = {};
|
|
78
|
-
|
|
79
|
-
for (const method in methods) {
|
|
80
|
-
const action = methods[method];
|
|
81
|
-
if (includedActions.has(action) && controller[action]) {
|
|
82
|
-
methodHandlers[method] = controller[action];
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (Object.keys(methodHandlers).length > 0) {
|
|
87
|
-
filteredRoutes[path] = methodHandlers;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return this.group(filteredRoutes);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public buildSingle(method: HttpMethodEnum, path: string, handler: string | HandlerType): RouterGroup {
|
|
95
|
-
const cleanPath = this.joinPaths(this.basePath, path);
|
|
96
|
-
|
|
97
|
-
let resolvedHandler: HandlerType = typeof handler === "string" ?
|
|
98
|
-
this.resolveControllerString(handler) :
|
|
99
|
-
handler;
|
|
100
|
-
|
|
101
|
-
for (const middleware of this.middlewares) {
|
|
102
|
-
resolvedHandler = middleware.handle(resolvedHandler);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return {
|
|
106
|
-
[cleanPath]: {
|
|
107
|
-
[method]: resolvedHandler
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private joinPaths(base: string, path: string): string {
|
|
113
|
-
base = base.replace(/\/+$/, "");
|
|
114
|
-
path = path.replace(/^\/+/, "");
|
|
115
|
-
|
|
116
|
-
return "/" + [base, path].filter(Boolean).join("/");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private resolveControllerString(definition: string): HandlerType {
|
|
120
|
-
const [controllerName, methodName] = definition.split("@");
|
|
121
|
-
|
|
122
|
-
if (isEmpty(controllerName) || isEmpty(methodName)) {
|
|
123
|
-
throw new RouterInvalidException(`[RouterInvalidException]: Invalid router controller definition: ${definition}.`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const controllerPath = path.resolve(process.cwd(), "app/controllers");
|
|
127
|
-
const location = `${controllerPath}/${controllerName}`;
|
|
128
|
-
|
|
129
|
-
let ControllerClass: any;
|
|
130
|
-
|
|
131
|
-
try {
|
|
132
|
-
ControllerClass = require(location).default;
|
|
133
|
-
} catch {
|
|
134
|
-
return async (...args: any[]) => {
|
|
135
|
-
const module = await import(location);
|
|
136
|
-
const ESMController = module.default;
|
|
137
|
-
const instance = new ESMController();
|
|
138
|
-
|
|
139
|
-
if (typeof instance[methodName] !== "function") {
|
|
140
|
-
throw new RouterInvalidException(`[RouterInvalidException]: Method "${methodName}" not found in ${controllerName}.`);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return instance[methodName](...args);
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (isEmpty(ControllerClass)) {
|
|
148
|
-
throw new RouterInvalidException(`[RouterInvalidException]: Controller not found: ${controllerName}.`);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const instance = new ControllerClass();
|
|
152
|
-
|
|
153
|
-
if (typeof instance[methodName] !== "function") {
|
|
154
|
-
throw new RouterInvalidException(`[RouterInvalidException]: Method "${methodName}" not found in ${controllerName}.`);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return instance[methodName].bind(instance);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private resolveIncludedActions(options?: ResourceOptions): Set<ResourceAction> {
|
|
161
|
-
const all: Array<ResourceAction> = ["index", "store", "show", "update", "destroy"];
|
|
162
|
-
|
|
163
|
-
if (options?.only) {
|
|
164
|
-
return new Set(options.only);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (options?.except) {
|
|
168
|
-
return new Set(all.filter(action => !options.except!.includes(action)));
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return new Set(all);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
-
import ora from "ora";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import {initDatabase} from "@/config/database";
|
|
5
|
-
|
|
6
|
-
export default class DbSeedCommand {
|
|
7
|
-
/**
|
|
8
|
-
* The name and signature of the console command.
|
|
9
|
-
*
|
|
10
|
-
* @var $signature string
|
|
11
|
-
*/
|
|
12
|
-
protected $signature: string = "db:seed";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The console command description.
|
|
16
|
-
*
|
|
17
|
-
* @var $description string
|
|
18
|
-
*/
|
|
19
|
-
protected $description: string = "Run database seeders";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The options or optional flag of the console command.
|
|
23
|
-
*
|
|
24
|
-
* @var $options Array<Array<string>>
|
|
25
|
-
*/
|
|
26
|
-
protected $options: Array<Array<string>> = [];
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The arguments of the console command.
|
|
30
|
-
*
|
|
31
|
-
* @var $arguments Array<Array<string>>
|
|
32
|
-
*/
|
|
33
|
-
protected $arguments: Array<Array<string>> = [];
|
|
34
|
-
|
|
35
|
-
public async handle(options: any, args: Array<string>): Promise<void> {
|
|
36
|
-
const database = initDatabase();
|
|
37
|
-
|
|
38
|
-
const spinner = ora(
|
|
39
|
-
Chalk.setValue("Seeding...")
|
|
40
|
-
.info()
|
|
41
|
-
.show()
|
|
42
|
-
).start();
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
const logs = (await database.seed.run()).flat();
|
|
46
|
-
spinner.succeed("Seeding finished");
|
|
47
|
-
|
|
48
|
-
if (logs.length > 0) logs.forEach((seeder: string) => spinner.succeed(path.basename(seeder)));
|
|
49
|
-
else spinner.succeed("No seeders were run.");
|
|
50
|
-
} catch (error: any) {
|
|
51
|
-
spinner.fail(`Seeding failed : ${error.message}`);
|
|
52
|
-
} finally {
|
|
53
|
-
await database.destroy();
|
|
54
|
-
spinner.stop();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
package/src/commands/Kernel.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type {Command} from "commander";
|
|
2
|
-
import {defineValue, isEmpty} from "@bejibun/utils";
|
|
3
|
-
import {readdirSync} from "fs";
|
|
4
|
-
import path from "path";
|
|
5
|
-
|
|
6
|
-
export default class Kernel {
|
|
7
|
-
public static registerCommands(program: Command): void {
|
|
8
|
-
const commandsDir = path.resolve(__dirname);
|
|
9
|
-
const files = readdirSync(commandsDir).filter((file: string) => {
|
|
10
|
-
return (
|
|
11
|
-
/\.(m?js|ts)$/.test(file) &&
|
|
12
|
-
!file.endsWith(".d.ts") &&
|
|
13
|
-
!file.includes("Kernel")
|
|
14
|
-
)
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
for (const file of files) {
|
|
18
|
-
const modulePath = path.join(commandsDir, file);
|
|
19
|
-
const {default: CommandClass} = require(modulePath);
|
|
20
|
-
|
|
21
|
-
const instance = new CommandClass();
|
|
22
|
-
|
|
23
|
-
if (isEmpty(instance.$signature) || typeof instance.handle !== "function") continue;
|
|
24
|
-
|
|
25
|
-
const cmd = program
|
|
26
|
-
.command(instance.$signature)
|
|
27
|
-
.description(defineValue(instance.$description, ""))
|
|
28
|
-
.action(instance.handle);
|
|
29
|
-
|
|
30
|
-
if (Array.isArray(instance.$options)) {
|
|
31
|
-
for (const option of instance.$options) {
|
|
32
|
-
cmd.option(...(option as [string, string?, any?]));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (Array.isArray(instance.$arguments)) {
|
|
37
|
-
for (const argument of instance.$arguments) {
|
|
38
|
-
cmd.argument(...(argument as [string, string?, unknown?]));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
cmd.action(async (...args: any[]) => {
|
|
43
|
-
const lastArg = args[args.length - 1];
|
|
44
|
-
const options = defineValue(lastArg, {});
|
|
45
|
-
await instance.handle(options, args.slice(0, -1));
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
-
import {ask, isNotEmpty} from "@bejibun/utils";
|
|
3
|
-
import ora from "ora";
|
|
4
|
-
import {initDatabase} from "@/config/database";
|
|
5
|
-
|
|
6
|
-
export default class MigrateFreshCommand {
|
|
7
|
-
/**
|
|
8
|
-
* The name and signature of the console command.
|
|
9
|
-
*
|
|
10
|
-
* @var $signature string
|
|
11
|
-
*/
|
|
12
|
-
protected $signature: string = "migrate:fresh";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The console command description.
|
|
16
|
-
*
|
|
17
|
-
* @var $description string
|
|
18
|
-
*/
|
|
19
|
-
protected $description: string = "Rollback all migrations and re-run migrations";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The options or optional flag of the console command.
|
|
23
|
-
*
|
|
24
|
-
* @var $options Array<Array<string>>
|
|
25
|
-
*/
|
|
26
|
-
protected $options: Array<Array<string>> = [
|
|
27
|
-
["-f, --force", "Skip command confirmation."]
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The arguments of the console command.
|
|
32
|
-
*
|
|
33
|
-
* @var $arguments Array<Array<string>>
|
|
34
|
-
*/
|
|
35
|
-
protected $arguments: Array<Array<string>> = [];
|
|
36
|
-
|
|
37
|
-
public async handle(options: any, args: Array<string>): Promise<void> {
|
|
38
|
-
const database = initDatabase();
|
|
39
|
-
|
|
40
|
-
const bypass = isNotEmpty(options.force);
|
|
41
|
-
|
|
42
|
-
let confirm = "Y";
|
|
43
|
-
if (!bypass) confirm = await ask(
|
|
44
|
-
Chalk.setValue("This will DROP ALL tables and re-run ALL migrations. Are you want to continue? (Y/N): ")
|
|
45
|
-
.inline()
|
|
46
|
-
.error()
|
|
47
|
-
.show()
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
if (confirm.toUpperCase() === "Y") {
|
|
51
|
-
if (!bypass) console.log();
|
|
52
|
-
|
|
53
|
-
const spinner = ora(
|
|
54
|
-
Chalk.setValue("Rollback...")
|
|
55
|
-
.info()
|
|
56
|
-
.show()
|
|
57
|
-
).start();
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
await database.migrate.rollback({}, true);
|
|
61
|
-
spinner.succeed("Rolled back all migrations");
|
|
62
|
-
|
|
63
|
-
const [batchNo, logs] = await database.migrate.latest();
|
|
64
|
-
spinner.succeed(`Batch ${batchNo} finished`);
|
|
65
|
-
|
|
66
|
-
if (logs.length > 0) logs.forEach((migration: string) => spinner.succeed(migration));
|
|
67
|
-
else spinner.succeed("No migrations were run.");
|
|
68
|
-
} catch (error: any) {
|
|
69
|
-
spinner.fail(`Migration failed : ${error.message}`);
|
|
70
|
-
} finally {
|
|
71
|
-
await database.destroy();
|
|
72
|
-
spinner.stop();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
-
import ora from "ora";
|
|
3
|
-
import {initDatabase} from "@/config/database";
|
|
4
|
-
|
|
5
|
-
export default class MigrateLatestCommand {
|
|
6
|
-
/**
|
|
7
|
-
* The name and signature of the console command.
|
|
8
|
-
*
|
|
9
|
-
* @var $signature string
|
|
10
|
-
*/
|
|
11
|
-
protected $signature: string = "migrate:latest";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The console command description.
|
|
15
|
-
*
|
|
16
|
-
* @var $description string
|
|
17
|
-
*/
|
|
18
|
-
protected $description: string = "Run latest migration";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The options or optional flag of the console command.
|
|
22
|
-
*
|
|
23
|
-
* @var $options Array<Array<string>>
|
|
24
|
-
*/
|
|
25
|
-
protected $options: Array<Array<string>> = [];
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* The arguments of the console command.
|
|
29
|
-
*
|
|
30
|
-
* @var $arguments Array<Array<string>>
|
|
31
|
-
*/
|
|
32
|
-
protected $arguments: Array<Array<string>> = [];
|
|
33
|
-
|
|
34
|
-
public async handle(options: any, args: Array<string>): Promise<void> {
|
|
35
|
-
const database = initDatabase();
|
|
36
|
-
|
|
37
|
-
const spinner = ora(
|
|
38
|
-
Chalk.setValue("Migrating...")
|
|
39
|
-
.info()
|
|
40
|
-
.show()
|
|
41
|
-
).start();
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
const [batchNo, logs] = await database.migrate.latest();
|
|
45
|
-
spinner.succeed(`Batch ${batchNo} finished`);
|
|
46
|
-
|
|
47
|
-
if (logs.length > 0) logs.forEach((migration: string) => spinner.succeed(migration));
|
|
48
|
-
else spinner.succeed("No migrations were run.");
|
|
49
|
-
} catch (error: any) {
|
|
50
|
-
spinner.fail(`Migration failed : ${error.message}`);
|
|
51
|
-
} finally {
|
|
52
|
-
await database.destroy();
|
|
53
|
-
spinner.stop();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
-
import {ask, isNotEmpty} from "@bejibun/utils";
|
|
3
|
-
import ora from "ora";
|
|
4
|
-
import {initDatabase} from "@/config/database";
|
|
5
|
-
|
|
6
|
-
export default class MigrateRollbackCommand {
|
|
7
|
-
/**
|
|
8
|
-
* The name and signature of the console command.
|
|
9
|
-
*
|
|
10
|
-
* @var $signature string
|
|
11
|
-
*/
|
|
12
|
-
protected $signature: string = "migrate:rollback";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The console command description.
|
|
16
|
-
*
|
|
17
|
-
* @var $description string
|
|
18
|
-
*/
|
|
19
|
-
protected $description: string = "Rollback the latest migrations";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The options or optional flag of the console command.
|
|
23
|
-
*
|
|
24
|
-
* @var $options Array<Array<string>>
|
|
25
|
-
*/
|
|
26
|
-
protected $options: Array<Array<string>> = [
|
|
27
|
-
["-f, --force", "Skip command confirmation."]
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The arguments of the console command.
|
|
32
|
-
*
|
|
33
|
-
* @var $arguments Array<Array<string>>
|
|
34
|
-
*/
|
|
35
|
-
protected $arguments: Array<Array<string>> = [];
|
|
36
|
-
|
|
37
|
-
public async handle(options: any, args: Array<string>): Promise<void> {
|
|
38
|
-
const database = initDatabase();
|
|
39
|
-
|
|
40
|
-
const bypass = isNotEmpty(options.force);
|
|
41
|
-
|
|
42
|
-
let confirm = "Y";
|
|
43
|
-
if (!bypass) confirm = await ask(
|
|
44
|
-
Chalk.setValue("This will ROLLBACK latest migrations. Are you want to continue? (Y/N): ")
|
|
45
|
-
.inline()
|
|
46
|
-
.error()
|
|
47
|
-
.show()
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
if (confirm.toUpperCase() === "Y") {
|
|
51
|
-
if (!bypass) console.log();
|
|
52
|
-
|
|
53
|
-
const spinner = ora(
|
|
54
|
-
Chalk.setValue("Rollback...")
|
|
55
|
-
.info()
|
|
56
|
-
.show()
|
|
57
|
-
).start();
|
|
58
|
-
try {
|
|
59
|
-
const [batchNo, logs] = await database.migrate.rollback();
|
|
60
|
-
spinner.succeed(`Batch ${batchNo} finished`);
|
|
61
|
-
|
|
62
|
-
if (logs.length > 0) logs.forEach((migration: string) => spinner.succeed(migration));
|
|
63
|
-
else spinner.succeed("No migrations were rolled back.");
|
|
64
|
-
} catch (error: any) {
|
|
65
|
-
spinner.fail(`Rollback failed : ${error.message}`);
|
|
66
|
-
} finally {
|
|
67
|
-
await database.destroy();
|
|
68
|
-
spinner.stop();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
-
import ora from "ora";
|
|
3
|
-
import {initDatabase} from "@/config/database";
|
|
4
|
-
|
|
5
|
-
export default class MigrateStatusCommand {
|
|
6
|
-
/**
|
|
7
|
-
* The name and signature of the console command.
|
|
8
|
-
*
|
|
9
|
-
* @var $signature string
|
|
10
|
-
*/
|
|
11
|
-
protected $signature: string = "migrate:status";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The console command description.
|
|
15
|
-
*
|
|
16
|
-
* @var $description string
|
|
17
|
-
*/
|
|
18
|
-
protected $description: string = "List migrations status";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The options or optional flag of the console command.
|
|
22
|
-
*
|
|
23
|
-
* @var $options Array<Array<string>>
|
|
24
|
-
*/
|
|
25
|
-
protected $options: Array<Array<string>> = [
|
|
26
|
-
["-f, --force", "Skip command confirmation."]
|
|
27
|
-
];
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The arguments of the console command.
|
|
31
|
-
*
|
|
32
|
-
* @var $arguments Array<Array<string>>
|
|
33
|
-
*/
|
|
34
|
-
protected $arguments: Array<Array<string>> = [];
|
|
35
|
-
|
|
36
|
-
public async handle(options: any, args: Array<string>): Promise<void> {
|
|
37
|
-
const database = initDatabase();
|
|
38
|
-
|
|
39
|
-
const spinner = ora(
|
|
40
|
-
Chalk.setValue("Fetching...")
|
|
41
|
-
.info()
|
|
42
|
-
.show()
|
|
43
|
-
).start();
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const [completed, pending] = await database.migrate.list();
|
|
47
|
-
|
|
48
|
-
spinner.succeed("Completed Migrations :");
|
|
49
|
-
if (completed.length > 0) completed.forEach((migration: { name: string }) => spinner.succeed(migration.name));
|
|
50
|
-
else spinner.succeed("No migrations were completed.");
|
|
51
|
-
|
|
52
|
-
console.log();
|
|
53
|
-
|
|
54
|
-
spinner.succeed("Pending Migrations :");
|
|
55
|
-
if (pending.length > 0) pending.forEach((migration: { file: string, directory: string }) => spinner.succeed(migration.file));
|
|
56
|
-
else spinner.succeed("No migrations were pending.");
|
|
57
|
-
} catch (error: any) {
|
|
58
|
-
spinner.fail(`Fetching failed : ${error.message}`);
|
|
59
|
-
} finally {
|
|
60
|
-
await database.destroy();
|
|
61
|
-
spinner.stop();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
package/src/config/database.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type {Knex} from "knex";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import knex from "knex";
|
|
4
|
-
import path from "path";
|
|
5
|
-
|
|
6
|
-
const config: Knex.Config = {
|
|
7
|
-
client: "pg",
|
|
8
|
-
connection: {
|
|
9
|
-
host: "127.0.0.1",
|
|
10
|
-
port: 5432,
|
|
11
|
-
user: "postgres",
|
|
12
|
-
password: "",
|
|
13
|
-
database: "bejibun"
|
|
14
|
-
},
|
|
15
|
-
migrations: {
|
|
16
|
-
extension: "ts",
|
|
17
|
-
directory: "./database/migrations",
|
|
18
|
-
tableName: "migrations"
|
|
19
|
-
},
|
|
20
|
-
pool: {
|
|
21
|
-
min: 0,
|
|
22
|
-
max: 1
|
|
23
|
-
},
|
|
24
|
-
seeds: {
|
|
25
|
-
extension: "ts",
|
|
26
|
-
directory: "./database/seeders"
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const initDatabase = (): Knex => {
|
|
31
|
-
const configPath = path.resolve(process.cwd(), "config/database.ts");
|
|
32
|
-
|
|
33
|
-
let _config: any;
|
|
34
|
-
|
|
35
|
-
if (fs.existsSync(configPath)) _config = require(configPath).default;
|
|
36
|
-
else _config = config;
|
|
37
|
-
|
|
38
|
-
return knex(_config);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export default config;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {defineValue} from "@bejibun/utils";
|
|
2
|
-
|
|
3
|
-
export default class ModelNotFoundException extends Error {
|
|
4
|
-
public code: number;
|
|
5
|
-
|
|
6
|
-
public constructor(message?: string, code?: number) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "ModelNotFoundException";
|
|
9
|
-
this.code = defineValue(code, 404);
|
|
10
|
-
|
|
11
|
-
if (Error.captureStackTrace) {
|
|
12
|
-
Error.captureStackTrace(this, ModelNotFoundException);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {defineValue} from "@bejibun/utils";
|
|
2
|
-
|
|
3
|
-
export default class RouterInvalidException extends Error {
|
|
4
|
-
public code: number;
|
|
5
|
-
|
|
6
|
-
public constructor(message?: string, code?: number) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "RouterInvalidException";
|
|
9
|
-
this.code = defineValue(code, 500);
|
|
10
|
-
|
|
11
|
-
if (Error.captureStackTrace) {
|
|
12
|
-
Error.captureStackTrace(this, RouterInvalidException);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {defineValue} from "@bejibun/utils";
|
|
2
|
-
|
|
3
|
-
export default class ValidatorException extends Error {
|
|
4
|
-
public code: number;
|
|
5
|
-
|
|
6
|
-
public constructor(message?: string, code?: number) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "ValidatorException";
|
|
9
|
-
this.code = defineValue(code, 422);
|
|
10
|
-
|
|
11
|
-
if (Error.captureStackTrace) {
|
|
12
|
-
Error.captureStackTrace(this, ValidatorException);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|