@bejibun/core 0.1.38 → 0.1.40

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +35 -1
  2. package/bases/BaseController.d.ts +1 -1
  3. package/bases/BaseModel.d.ts +0 -2
  4. package/bases/BaseModel.js +0 -1
  5. package/builders/RouterBuilder.d.ts +4 -0
  6. package/builders/RouterBuilder.js +24 -2
  7. package/bun.lock +11 -8
  8. package/commands/Kernel.d.ts +1 -0
  9. package/commands/Kernel.js +28 -13
  10. package/commands/{DbSeedCommand.d.ts → db/DbSeedCommand.d.ts} +2 -2
  11. package/commands/{DbSeedCommand.js → db/DbSeedCommand.js} +2 -2
  12. package/commands/maintenance/MaintenanceDownCommand.d.ts +27 -0
  13. package/commands/maintenance/MaintenanceDownCommand.js +40 -0
  14. package/commands/{MigrateFreshCommand.d.ts → migrate/MigrateFreshCommand.d.ts} +2 -2
  15. package/commands/{MigrateFreshCommand.js → migrate/MigrateFreshCommand.js} +5 -4
  16. package/commands/{MigrateLatestCommand.d.ts → migrate/MigrateLatestCommand.d.ts} +2 -2
  17. package/commands/{MigrateLatestCommand.js → migrate/MigrateLatestCommand.js} +2 -2
  18. package/commands/{MigrateRollbackCommand.d.ts → migrate/MigrateRollbackCommand.d.ts} +2 -2
  19. package/commands/{MigrateRollbackCommand.js → migrate/MigrateRollbackCommand.js} +5 -4
  20. package/commands/{MigrateStatusCommand.d.ts → migrate/MigrateStatusCommand.d.ts} +2 -2
  21. package/commands/{MigrateStatusCommand.js → migrate/MigrateStatusCommand.js} +5 -4
  22. package/config/database.js +2 -2
  23. package/facades/Router.d.ts +1 -0
  24. package/facades/Router.js +5 -14
  25. package/package.json +5 -4
  26. package/types/router.d.ts +1 -1
  27. package/src/ace.ts +0 -22
  28. package/src/bases/BaseController.ts +0 -139
  29. package/src/bases/BaseModel.ts +0 -110
  30. package/src/bases/BaseValidator.ts +0 -8
  31. package/src/bases/index.ts +0 -3
  32. package/src/bootstrap.ts +0 -4
  33. package/src/builders/ResponseBuilder.ts +0 -54
  34. package/src/builders/RouterBuilder.ts +0 -173
  35. package/src/commands/DbSeedCommand.ts +0 -57
  36. package/src/commands/Kernel.ts +0 -49
  37. package/src/commands/MigrateFreshCommand.ts +0 -76
  38. package/src/commands/MigrateLatestCommand.ts +0 -56
  39. package/src/commands/MigrateRollbackCommand.ts +0 -72
  40. package/src/commands/MigrateStatusCommand.ts +0 -64
  41. package/src/config/database.ts +0 -41
  42. package/src/exceptions/ModelNotFoundException.ts +0 -15
  43. package/src/exceptions/RouterInvalidException.ts +0 -15
  44. package/src/exceptions/ValidatorException.ts +0 -15
  45. package/src/exceptions/index.ts +0 -3
  46. package/src/facades/Response.ts +0 -15
  47. package/src/facades/Router.ts +0 -89
  48. package/src/facades/SoftDeletes.ts +0 -66
  49. package/src/facades/index.ts +0 -3
  50. package/src/index.ts +0 -5
  51. package/src/types/index.d.ts +0 -4
  52. package/src/types/middleware.d.ts +0 -7
  53. package/src/types/router.d.ts +0 -5
  54. package/src/types/validator.d.ts +0 -3
  55. package/src/types/vine.d.ts +0 -13
  56. package/src/utils/vine.ts +0 -2
  57. package/src/utils/vines/exists.ts +0 -41
  58. package/src/utils/vines/unique.ts +0 -41
@@ -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
- }
@@ -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
- }
@@ -1,3 +0,0 @@
1
- export * from "@/exceptions/ModelNotFoundException";
2
- export * from "@/exceptions/RouterInvalidException";
3
- export * from "@/exceptions/ValidatorException";
@@ -1,15 +0,0 @@
1
- import ResponseBuilder from "@/builders/ResponseBuilder";
2
-
3
- export default class Response {
4
- public static setData(data?: any): ResponseBuilder {
5
- return new ResponseBuilder().setData(data);
6
- }
7
-
8
- public static setMessage(message: string): ResponseBuilder {
9
- return new ResponseBuilder().setMessage(message);
10
- }
11
-
12
- public static setStatus(status: number): ResponseBuilder {
13
- return new ResponseBuilder().setStatus(status);
14
- }
15
- }
@@ -1,89 +0,0 @@
1
- import type {IMiddleware} from "@/types/middleware";
2
- import type {HandlerType, RouterGroup} from "@/types/router";
3
- import {isEmpty} from "@bejibun/utils";
4
- import HttpMethodEnum from "@bejibun/utils/enums/HttpMethodEnum";
5
- import Enum from "@bejibun/utils/facades/Enum";
6
- import RouterBuilder, {ResourceOptions} from "@/builders/RouterBuilder";
7
-
8
- export default class Router {
9
- public static prefix(basePath: string): RouterBuilder {
10
- return new RouterBuilder().prefix(basePath);
11
- }
12
-
13
- public static middleware(...middlewares: Array<IMiddleware>): RouterBuilder {
14
- return new RouterBuilder().middleware(...middlewares);
15
- }
16
-
17
- public static resources(
18
- controller: Record<string, HandlerType>,
19
- options?: ResourceOptions
20
- ): RouterGroup {
21
- return new RouterBuilder().resources(controller, options);
22
- }
23
-
24
- public static group(routes: RouterGroup, prefix?: string, middlewares?: Array<IMiddleware>) {
25
- const builder = new RouterBuilder();
26
-
27
- if (prefix) builder.prefix(prefix);
28
- if (middlewares?.length) builder.middleware(...middlewares);
29
-
30
- return builder.group(routes);
31
- }
32
-
33
- public static connect(path: string, handler: string | HandlerType): RouterGroup {
34
- return new RouterBuilder().buildSingle(HttpMethodEnum.Connect, path, handler);
35
- }
36
-
37
- public static delete(path: string, handler: string | HandlerType): RouterGroup {
38
- return new RouterBuilder().buildSingle(HttpMethodEnum.Delete, path, handler);
39
- }
40
-
41
- public static get(path: string, handler: string | HandlerType): RouterGroup {
42
- return new RouterBuilder().buildSingle(HttpMethodEnum.Get, path, handler);
43
- }
44
-
45
- public static head(path: string, handler: string | HandlerType): RouterGroup {
46
- return new RouterBuilder().buildSingle(HttpMethodEnum.Head, path, handler);
47
- }
48
-
49
- public static options(path: string, handler: string | HandlerType): RouterGroup {
50
- return new RouterBuilder().buildSingle(HttpMethodEnum.Options, path, handler);
51
- }
52
-
53
- public static patch(path: string, handler: string | HandlerType): RouterGroup {
54
- return new RouterBuilder().buildSingle(HttpMethodEnum.Patch, path, handler);
55
- }
56
-
57
- public static post(path: string, handler: string | HandlerType): RouterGroup {
58
- return new RouterBuilder().buildSingle(HttpMethodEnum.Post, path, handler);
59
- }
60
-
61
- public static put(path: string, handler: string | HandlerType): RouterGroup {
62
- return new RouterBuilder().buildSingle(HttpMethodEnum.Put, path, handler);
63
- }
64
-
65
- public static trace(path: string, handler: string | HandlerType): RouterGroup {
66
- return new RouterBuilder().buildSingle(HttpMethodEnum.Trace, path, handler);
67
- }
68
-
69
- public static match(methods: Array<HttpMethodEnum>, path: string, handler: string | HandlerType): RouterGroup {
70
- const builder = new RouterBuilder();
71
- const routeMap: RouterGroup = {};
72
-
73
- for (const method of methods) {
74
- const single = builder.buildSingle(method, path, handler);
75
- const fullPath = Object.keys(single)[0];
76
- const handlers = single[fullPath];
77
-
78
- if (isEmpty(routeMap[fullPath])) routeMap[fullPath] = {};
79
-
80
- Object.assign(routeMap[fullPath], handlers);
81
- }
82
-
83
- return routeMap;
84
- }
85
-
86
- public static any(path: string, handler: string | HandlerType): RouterGroup {
87
- return this.match(Enum.setEnums(HttpMethodEnum).toArray(), path, handler);
88
- }
89
- }
@@ -1,66 +0,0 @@
1
- import {DateTime} from "luxon";
2
- import {QueryBuilder, ModelClass, QueryContext, Model} from "objection";
3
-
4
- interface SoftDeleteQueryContext extends QueryContext {
5
- withTrashed?: boolean;
6
- onlyTrashed?: boolean;
7
- }
8
-
9
- export default class SoftDeletes<M extends Model, R = M[]> extends QueryBuilder<M, R> {
10
- private hasFilterApplied = false;
11
-
12
- constructor(modelClass: ModelClass<M>) {
13
- // @ts-ignore
14
- super(modelClass);
15
-
16
- this.onBuild((builder: QueryBuilder<M, R>) => {
17
- const context = this.context() as SoftDeleteQueryContext;
18
-
19
- if (!this.hasFilterApplied) {
20
- const tableName = this.modelClass().tableName;
21
-
22
- if (context.onlyTrashed) {
23
- builder.whereNotNull(`${tableName}.${(this.modelClass() as any).deletedColumn}`);
24
- } else if (!context.withTrashed) {
25
- builder.whereNull(`${tableName}.${(this.modelClass() as any).deletedColumn}`);
26
- }
27
-
28
- this.hasFilterApplied = true;
29
- }
30
- });
31
- }
32
-
33
- withTrashed(): this {
34
- return (this as any).context({
35
- ...(this as any).context(),
36
- withTrashed: true
37
- });
38
- }
39
-
40
- onlyTrashed(): this {
41
- return (this as any).context({
42
- ...(this as any).context(),
43
- onlyTrashed: true
44
- });
45
- }
46
-
47
- delete(): QueryBuilder<M, number> {
48
- return this.update({
49
- [(this.modelClass() as any).deletedColumn]: DateTime.now()
50
- } as any);
51
- }
52
-
53
- del(): QueryBuilder<M, number> {
54
- return this.delete();
55
- }
56
-
57
- forceDelete(): QueryBuilder<M, number> {
58
- return super.delete();
59
- }
60
-
61
- restore(): QueryBuilder<M, number> {
62
- return this.onlyTrashed().update({
63
- [(this.modelClass() as any).deletedColumn]: null
64
- } as any);
65
- }
66
- }
@@ -1,3 +0,0 @@
1
- export * from "@/facades/Response";
2
- export * from "@/facades/Router";
3
- export * from "@/facades/SoftDeletes";
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from "@/bases/index";
2
-
3
- export * from "@/exceptions/index";
4
-
5
- export * from "@/facades/index";
@@ -1,4 +0,0 @@
1
- export * from "@/types/middleware";
2
- export * from "@/types/router";
3
- export * from "@/types/validator";
4
- export * from "@/types/vine";
@@ -1,7 +0,0 @@
1
- import type {HandlerType} from "@/types/router";
2
-
3
- export type MiddlewareType = (handler: HandlerType) => HandlerType;
4
-
5
- export interface IMiddleware {
6
- handle(handler: HandlerType): HandlerType;
7
- }
@@ -1,5 +0,0 @@
1
- import {BunRequest} from "bun";
2
-
3
- export type HandlerType = (request: BunRequest) => Promise<Response>;
4
- export type RouterGroup = Record<string, Record<string, HandlerType>>;
5
- export type ResourceAction = "index" | "store" | "show" | "update" | "destroy";
@@ -1,3 +0,0 @@
1
- import {SchemaTypes, VineValidator} from "@vinejs/vine";
2
-
3
- export type ValidatorType<T extends SchemaTypes = SchemaTypes> = VineValidator<SchemaTypes, Record<string, any> | undefined>;
@@ -1,13 +0,0 @@
1
- declare module "@vinejs/vine" {
2
- interface VineNumber {
3
- exists(tableOrOptions: string | { table: string; column?: string }, column?: string): this;
4
- unique(tableOrOptions: string | { table: string; column?: string }, column?: string): this;
5
- }
6
-
7
- interface VineString {
8
- exists(tableOrOptions: string | { table: string; column?: string }, column?: string): this;
9
- unique(tableOrOptions: string | { table: string; column?: string }, column?: string): this;
10
- }
11
- }
12
-
13
- export {};
package/src/utils/vine.ts DELETED
@@ -1,2 +0,0 @@
1
- import "@/utils/vines/exists";
2
- import "@/utils/vines/unique";
@@ -1,41 +0,0 @@
1
- import {defineValue, isEmpty} from "@bejibun/utils";
2
- import vine, {VineNumber, VineString} from "@vinejs/vine";
3
- import {QueryBuilderType} from "objection";
4
- import BaseModel from "@/bases/BaseModel";
5
-
6
- type Options = {
7
- table: typeof BaseModel;
8
- column?: string;
9
- withTrashed?: boolean;
10
- };
11
-
12
- const exists = async (value: unknown, options: Options, field: any): Promise<void> => {
13
- if (!field.isValid) return;
14
-
15
- const column = defineValue(options.column, field.name);
16
-
17
- let query: any = options.table;
18
- if (options.withTrashed) query = query.withTrashed();
19
- else query = query.query();
20
-
21
- const row = await (query as QueryBuilderType<any>).where(column, value).first();
22
-
23
- if (isEmpty(row)) field.report("The {{ field }} field doesn't exists", "exists", field);
24
- }
25
-
26
- const existsRule = vine.createRule(exists, {isAsync: true});
27
-
28
- const registerExistsMacro = (Type: any): void => {
29
- Type.macro("exists", function (this: typeof Type, tableOrOptions: typeof BaseModel | Options, column?: string, withTrashed?: boolean) {
30
- const isModel = typeof tableOrOptions === "function" && Object.prototype.isPrototypeOf.call(BaseModel, tableOrOptions);
31
-
32
- const options: Options = isModel
33
- ? {table: tableOrOptions as typeof BaseModel, column, withTrashed}
34
- : tableOrOptions as Options;
35
-
36
- return this.use(existsRule(options));
37
- });
38
- };
39
-
40
- registerExistsMacro(VineString);
41
- registerExistsMacro(VineNumber);
@@ -1,41 +0,0 @@
1
- import {defineValue, isNotEmpty} from "@bejibun/utils";
2
- import vine, {VineNumber, VineString} from "@vinejs/vine";
3
- import {QueryBuilderType} from "objection";
4
- import BaseModel from "@/bases/BaseModel";
5
-
6
- type Options = {
7
- table: typeof BaseModel;
8
- column?: string;
9
- withTrashed?: boolean;
10
- };
11
-
12
- const unique = async (value: unknown, options: Options, field: any): Promise<void> => {
13
- if (!field.isValid) return;
14
-
15
- const column = defineValue(options.column, field.name);
16
-
17
- let query: any = options.table;
18
- if (options.withTrashed) query = query.withTrashed();
19
- else query = query.query();
20
-
21
- const row = await (query as QueryBuilderType<any>).where(column, value).first();
22
-
23
- if (isNotEmpty(row)) field.report("The {{ field }} field is already exists", "unique", field);
24
- }
25
-
26
- const uniqueRule = vine.createRule(unique, {isAsync: true});
27
-
28
- const registerUniqueMacro = (Type: any): void => {
29
- Type.macro("unique", function (this: typeof Type, tableOrOptions: typeof BaseModel | Options, column?: string, withTrashed?: boolean) {
30
- const isModel = typeof tableOrOptions === "function" && Object.prototype.isPrototypeOf.call(BaseModel, tableOrOptions);
31
-
32
- const options: Options = isModel
33
- ? {table: tableOrOptions as typeof BaseModel, column, withTrashed}
34
- : tableOrOptions as Options;
35
-
36
- return this.use(uniqueRule(options));
37
- });
38
- };
39
-
40
- registerUniqueMacro(VineString);
41
- registerUniqueMacro(VineNumber);