@bejibun/database 0.1.0
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 +27 -0
- package/LICENSE +21 -0
- package/README.md +106 -0
- package/builders/DatabaseBuilder.d.ts +5 -0
- package/builders/DatabaseBuilder.js +17 -0
- package/commands/db/DbSeedCommand.d.ts +27 -0
- package/commands/db/DbSeedCommand.js +51 -0
- package/commands/migrate/MigrateFreshCommand.d.ts +27 -0
- package/commands/migrate/MigrateFreshCommand.js +67 -0
- package/commands/migrate/MigrateLatestCommand.d.ts +27 -0
- package/commands/migrate/MigrateLatestCommand.js +50 -0
- package/commands/migrate/MigrateRollbackCommand.d.ts +27 -0
- package/commands/migrate/MigrateRollbackCommand.js +65 -0
- package/commands/migrate/MigrateStatusCommand.d.ts +27 -0
- package/commands/migrate/MigrateStatusCommand.js +59 -0
- package/config/database.d.ts +3 -0
- package/config/database.js +24 -0
- package/configure.d.ts +1 -0
- package/configure.js +14 -0
- package/facades/Database.d.ts +4 -0
- package/facades/Database.js +6 -0
- package/facades/index.d.ts +1 -0
- package/facades/index.js +1 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +45 -0
- package/tsconfig.json +17 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## [v0.1.0](https://github.com/crenata/bejibun-database/compare/v0.1.0...v0.1.0) - 2025-10-22
|
|
7
|
+
|
|
8
|
+
### 🩹 Fixes
|
|
9
|
+
|
|
10
|
+
### 📖 Changes
|
|
11
|
+
What's New :
|
|
12
|
+
- Adding `Database.knex()` for init database connection
|
|
13
|
+
- Adding `config/database.ts` configuration file
|
|
14
|
+
- Adding commands directory structure
|
|
15
|
+
|
|
16
|
+
Available Commands :
|
|
17
|
+
- `db:seed` Run database seeders
|
|
18
|
+
- `migrate:fresh` Rollback all migrations and re-run migrations
|
|
19
|
+
- `migrate:latest` Run latest migration
|
|
20
|
+
- `migrate:rollback` Rollback the latest migrations
|
|
21
|
+
- `migrate:status` List migrations status
|
|
22
|
+
|
|
23
|
+
### ❤️Contributors
|
|
24
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
25
|
+
- Ghulje ([@ghulje](https://github.com/ghulje))
|
|
26
|
+
|
|
27
|
+
**Full Changelog**: https://github.com/crenata/bejibun-database/blob/master/CHANGELOG.md
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Havea Crenata
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
# Database for Bejibun
|
|
12
|
+
Database package with Built-in Bun for Bejibun Framework.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Installation
|
|
17
|
+
Install the package.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Using Bun
|
|
21
|
+
bun add @bejibun/database
|
|
22
|
+
|
|
23
|
+
# Using Bejibun
|
|
24
|
+
bun ace install @bejibun/database
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Configuration
|
|
28
|
+
Add `database.ts` inside config directory on your project
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
config/database.ts
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const config: Record<string, any> = {
|
|
36
|
+
default: "local",
|
|
37
|
+
|
|
38
|
+
connections: {
|
|
39
|
+
local: {
|
|
40
|
+
host: "127.0.0.1",
|
|
41
|
+
port: 6379,
|
|
42
|
+
password: "",
|
|
43
|
+
database: 0,
|
|
44
|
+
maxRetries: 10
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export default config;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
You can pass the value with environment variables.
|
|
53
|
+
|
|
54
|
+
### How to Use
|
|
55
|
+
How to use tha package.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import type {DatabasePipeline} from "@bejibun/database/types";
|
|
59
|
+
import BaseController from "@bejibun/core/bases/BaseController";
|
|
60
|
+
import Logger from "@bejibun/logger";
|
|
61
|
+
import Database from "@bejibun/database";
|
|
62
|
+
|
|
63
|
+
export default class TestController extends BaseController {
|
|
64
|
+
public async database(request: Bun.BunRequest): Promise<Response> {
|
|
65
|
+
await Database.set("database", {hello: "world"});
|
|
66
|
+
const database = await Database.get("database");
|
|
67
|
+
|
|
68
|
+
await Database.connection("local").set("connection", "This is using custom connection.");
|
|
69
|
+
const connection = await Database.connection("local").get("connection");
|
|
70
|
+
|
|
71
|
+
const pipeline = await Database.pipeline((pipe: DatabasePipeline) => {
|
|
72
|
+
pipe.set("database-pipeline-1", "This is database pipeline 1");
|
|
73
|
+
pipe.set("database-pipeline-2", "This is database pipeline 2");
|
|
74
|
+
|
|
75
|
+
pipe.get("database-pipeline-1");
|
|
76
|
+
pipe.get("database-pipeline-2");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const subscriber = await Database.subscribe("database-subscribe", (message: string, channel: string) => {
|
|
80
|
+
Logger.setContext(channel).debug(message);
|
|
81
|
+
});
|
|
82
|
+
await Database.publish("database-subscribe", "Hai database subscriber!");
|
|
83
|
+
setTimeout(async () => {
|
|
84
|
+
await subscriber.unsubscribe();
|
|
85
|
+
}, 500);
|
|
86
|
+
|
|
87
|
+
return super.response.setData({database, connection, pipeline}).send();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Contributors
|
|
93
|
+
- [Havea Crenata](mailto:havea.crenata@gmail.com)
|
|
94
|
+
|
|
95
|
+
## ☕ Support / Donate
|
|
96
|
+
|
|
97
|
+
If you find this project helpful and want to support it, you can donate via PayPal :
|
|
98
|
+
|
|
99
|
+
[](https://paypal.me/hafiizhghulam)
|
|
100
|
+
|
|
101
|
+
Or if you are prefer using crypto :
|
|
102
|
+
|
|
103
|
+
| EVM | Solana |
|
|
104
|
+
| --- | ------ |
|
|
105
|
+
| <img src="https://github.com/crenata/bejibun/blob/master/public/images/EVM.png?raw=true" width="150" /> | <img src="https://github.com/crenata/bejibun/blob/master/public/images/SOL.png?raw=true" width="150" /> |
|
|
106
|
+
| 0xdABe8750061410D35cE52EB2a418c8cB004788B3 | GAnoyvy9p3QFyxikWDh9hA3fmSk2uiPLNWyQ579cckMn |
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import App from "@bejibun/app";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import knex from "knex";
|
|
4
|
+
import DatabaseConfig from "../config/database";
|
|
5
|
+
export default class DatabaseBuilder {
|
|
6
|
+
database;
|
|
7
|
+
knex() {
|
|
8
|
+
const configPath = App.Path.configPath("database.ts");
|
|
9
|
+
let config;
|
|
10
|
+
if (fs.existsSync(configPath))
|
|
11
|
+
config = require(configPath).default;
|
|
12
|
+
else
|
|
13
|
+
config = DatabaseConfig;
|
|
14
|
+
this.database = knex(config);
|
|
15
|
+
return this.database;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default class DbSeedCommand {
|
|
2
|
+
/**
|
|
3
|
+
* The name and signature of the console command.
|
|
4
|
+
*
|
|
5
|
+
* @var $signature string
|
|
6
|
+
*/
|
|
7
|
+
protected $signature: string;
|
|
8
|
+
/**
|
|
9
|
+
* The console command description.
|
|
10
|
+
*
|
|
11
|
+
* @var $description string
|
|
12
|
+
*/
|
|
13
|
+
protected $description: string;
|
|
14
|
+
/**
|
|
15
|
+
* The options or optional flag of the console command.
|
|
16
|
+
*
|
|
17
|
+
* @var $options Array<Array<any>>
|
|
18
|
+
*/
|
|
19
|
+
protected $options: Array<Array<any>>;
|
|
20
|
+
/**
|
|
21
|
+
* The arguments of the console command.
|
|
22
|
+
*
|
|
23
|
+
* @var $arguments Array<Array<string>>
|
|
24
|
+
*/
|
|
25
|
+
protected $arguments: Array<Array<string>>;
|
|
26
|
+
handle(options: any, args: Array<string>): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
+
import ora from "ora";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import Database from "../../facades/Database";
|
|
5
|
+
export default class DbSeedCommand {
|
|
6
|
+
/**
|
|
7
|
+
* The name and signature of the console command.
|
|
8
|
+
*
|
|
9
|
+
* @var $signature string
|
|
10
|
+
*/
|
|
11
|
+
$signature = "db:seed";
|
|
12
|
+
/**
|
|
13
|
+
* The console command description.
|
|
14
|
+
*
|
|
15
|
+
* @var $description string
|
|
16
|
+
*/
|
|
17
|
+
$description = "Run database seeders";
|
|
18
|
+
/**
|
|
19
|
+
* The options or optional flag of the console command.
|
|
20
|
+
*
|
|
21
|
+
* @var $options Array<Array<any>>
|
|
22
|
+
*/
|
|
23
|
+
$options = [];
|
|
24
|
+
/**
|
|
25
|
+
* The arguments of the console command.
|
|
26
|
+
*
|
|
27
|
+
* @var $arguments Array<Array<string>>
|
|
28
|
+
*/
|
|
29
|
+
$arguments = [];
|
|
30
|
+
async handle(options, args) {
|
|
31
|
+
const database = Database.knex();
|
|
32
|
+
const spinner = ora(Chalk.setValue("Seeding...")
|
|
33
|
+
.info()
|
|
34
|
+
.show()).start();
|
|
35
|
+
try {
|
|
36
|
+
const logs = (await database.seed.run()).flat();
|
|
37
|
+
spinner.succeed("Seeding finished");
|
|
38
|
+
if (logs.length > 0)
|
|
39
|
+
logs.forEach((seeder) => spinner.succeed(path.basename(seeder)));
|
|
40
|
+
else
|
|
41
|
+
spinner.succeed("No seeders were run.");
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
spinner.fail(`Seeding failed : ${error.message}`);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
await database.destroy();
|
|
48
|
+
spinner.stop();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default class MigrateFreshCommand {
|
|
2
|
+
/**
|
|
3
|
+
* The name and signature of the console command.
|
|
4
|
+
*
|
|
5
|
+
* @var $signature string
|
|
6
|
+
*/
|
|
7
|
+
protected $signature: string;
|
|
8
|
+
/**
|
|
9
|
+
* The console command description.
|
|
10
|
+
*
|
|
11
|
+
* @var $description string
|
|
12
|
+
*/
|
|
13
|
+
protected $description: string;
|
|
14
|
+
/**
|
|
15
|
+
* The options or optional flag of the console command.
|
|
16
|
+
*
|
|
17
|
+
* @var $options Array<Array<any>>
|
|
18
|
+
*/
|
|
19
|
+
protected $options: Array<Array<any>>;
|
|
20
|
+
/**
|
|
21
|
+
* The arguments of the console command.
|
|
22
|
+
*
|
|
23
|
+
* @var $arguments Array<Array<string>>
|
|
24
|
+
*/
|
|
25
|
+
protected $arguments: Array<Array<string>>;
|
|
26
|
+
handle(options: any, args: Array<string>): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import Logger from "@bejibun/logger";
|
|
2
|
+
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
3
|
+
import { ask, isNotEmpty } from "@bejibun/utils";
|
|
4
|
+
import ora from "ora";
|
|
5
|
+
import Database from "../../facades/Database";
|
|
6
|
+
export default class MigrateFreshCommand {
|
|
7
|
+
/**
|
|
8
|
+
* The name and signature of the console command.
|
|
9
|
+
*
|
|
10
|
+
* @var $signature string
|
|
11
|
+
*/
|
|
12
|
+
$signature = "migrate:fresh";
|
|
13
|
+
/**
|
|
14
|
+
* The console command description.
|
|
15
|
+
*
|
|
16
|
+
* @var $description string
|
|
17
|
+
*/
|
|
18
|
+
$description = "Rollback all migrations and re-run migrations";
|
|
19
|
+
/**
|
|
20
|
+
* The options or optional flag of the console command.
|
|
21
|
+
*
|
|
22
|
+
* @var $options Array<Array<any>>
|
|
23
|
+
*/
|
|
24
|
+
$options = [
|
|
25
|
+
["-f, --force", "Skip command confirmation"]
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* The arguments of the console command.
|
|
29
|
+
*
|
|
30
|
+
* @var $arguments Array<Array<string>>
|
|
31
|
+
*/
|
|
32
|
+
$arguments = [];
|
|
33
|
+
async handle(options, args) {
|
|
34
|
+
const database = Database.knex();
|
|
35
|
+
const bypass = isNotEmpty(options.force);
|
|
36
|
+
let confirm = "Y";
|
|
37
|
+
if (!bypass)
|
|
38
|
+
confirm = await ask(Chalk.setValue("This will DROP ALL tables and re-run ALL migrations. Are you want to continue? (Y/N): ")
|
|
39
|
+
.inline()
|
|
40
|
+
.error()
|
|
41
|
+
.show());
|
|
42
|
+
if (confirm.toUpperCase() === "Y") {
|
|
43
|
+
if (!bypass)
|
|
44
|
+
Logger.empty();
|
|
45
|
+
const spinner = ora(Chalk.setValue("Rollback...")
|
|
46
|
+
.info()
|
|
47
|
+
.show()).start();
|
|
48
|
+
try {
|
|
49
|
+
await database.migrate.rollback({}, true);
|
|
50
|
+
spinner.succeed("Rolled back all migrations");
|
|
51
|
+
const [batchNo, logs] = await database.migrate.latest();
|
|
52
|
+
spinner.succeed(`Batch ${batchNo} finished`);
|
|
53
|
+
if (logs.length > 0)
|
|
54
|
+
logs.forEach((migration) => spinner.succeed(migration));
|
|
55
|
+
else
|
|
56
|
+
spinner.succeed("No migrations were run.");
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
spinner.fail(`Migration failed : ${error.message}`);
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
await database.destroy();
|
|
63
|
+
spinner.stop();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default class MigrateLatestCommand {
|
|
2
|
+
/**
|
|
3
|
+
* The name and signature of the console command.
|
|
4
|
+
*
|
|
5
|
+
* @var $signature string
|
|
6
|
+
*/
|
|
7
|
+
protected $signature: string;
|
|
8
|
+
/**
|
|
9
|
+
* The console command description.
|
|
10
|
+
*
|
|
11
|
+
* @var $description string
|
|
12
|
+
*/
|
|
13
|
+
protected $description: string;
|
|
14
|
+
/**
|
|
15
|
+
* The options or optional flag of the console command.
|
|
16
|
+
*
|
|
17
|
+
* @var $options Array<Array<any>>
|
|
18
|
+
*/
|
|
19
|
+
protected $options: Array<Array<any>>;
|
|
20
|
+
/**
|
|
21
|
+
* The arguments of the console command.
|
|
22
|
+
*
|
|
23
|
+
* @var $arguments Array<Array<string>>
|
|
24
|
+
*/
|
|
25
|
+
protected $arguments: Array<Array<string>>;
|
|
26
|
+
handle(options: any, args: Array<string>): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
2
|
+
import ora from "ora";
|
|
3
|
+
import Database from "../../facades/Database";
|
|
4
|
+
export default class MigrateLatestCommand {
|
|
5
|
+
/**
|
|
6
|
+
* The name and signature of the console command.
|
|
7
|
+
*
|
|
8
|
+
* @var $signature string
|
|
9
|
+
*/
|
|
10
|
+
$signature = "migrate:latest";
|
|
11
|
+
/**
|
|
12
|
+
* The console command description.
|
|
13
|
+
*
|
|
14
|
+
* @var $description string
|
|
15
|
+
*/
|
|
16
|
+
$description = "Run latest migration";
|
|
17
|
+
/**
|
|
18
|
+
* The options or optional flag of the console command.
|
|
19
|
+
*
|
|
20
|
+
* @var $options Array<Array<any>>
|
|
21
|
+
*/
|
|
22
|
+
$options = [];
|
|
23
|
+
/**
|
|
24
|
+
* The arguments of the console command.
|
|
25
|
+
*
|
|
26
|
+
* @var $arguments Array<Array<string>>
|
|
27
|
+
*/
|
|
28
|
+
$arguments = [];
|
|
29
|
+
async handle(options, args) {
|
|
30
|
+
const database = Database.knex();
|
|
31
|
+
const spinner = ora(Chalk.setValue("Migrating...")
|
|
32
|
+
.info()
|
|
33
|
+
.show()).start();
|
|
34
|
+
try {
|
|
35
|
+
const [batchNo, logs] = await database.migrate.latest();
|
|
36
|
+
spinner.succeed(`Batch ${batchNo} finished`);
|
|
37
|
+
if (logs.length > 0)
|
|
38
|
+
logs.forEach((migration) => spinner.succeed(migration));
|
|
39
|
+
else
|
|
40
|
+
spinner.succeed("No migrations were run.");
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
spinner.fail(`Migration failed : ${error.message}`);
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
await database.destroy();
|
|
47
|
+
spinner.stop();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default class MigrateRollbackCommand {
|
|
2
|
+
/**
|
|
3
|
+
* The name and signature of the console command.
|
|
4
|
+
*
|
|
5
|
+
* @var $signature string
|
|
6
|
+
*/
|
|
7
|
+
protected $signature: string;
|
|
8
|
+
/**
|
|
9
|
+
* The console command description.
|
|
10
|
+
*
|
|
11
|
+
* @var $description string
|
|
12
|
+
*/
|
|
13
|
+
protected $description: string;
|
|
14
|
+
/**
|
|
15
|
+
* The options or optional flag of the console command.
|
|
16
|
+
*
|
|
17
|
+
* @var $options Array<Array<any>>
|
|
18
|
+
*/
|
|
19
|
+
protected $options: Array<Array<any>>;
|
|
20
|
+
/**
|
|
21
|
+
* The arguments of the console command.
|
|
22
|
+
*
|
|
23
|
+
* @var $arguments Array<Array<string>>
|
|
24
|
+
*/
|
|
25
|
+
protected $arguments: Array<Array<string>>;
|
|
26
|
+
handle(options: any, args: Array<string>): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import Logger from "@bejibun/logger";
|
|
2
|
+
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
3
|
+
import { ask, isNotEmpty } from "@bejibun/utils";
|
|
4
|
+
import ora from "ora";
|
|
5
|
+
import Database from "../../facades/Database";
|
|
6
|
+
export default class MigrateRollbackCommand {
|
|
7
|
+
/**
|
|
8
|
+
* The name and signature of the console command.
|
|
9
|
+
*
|
|
10
|
+
* @var $signature string
|
|
11
|
+
*/
|
|
12
|
+
$signature = "migrate:rollback";
|
|
13
|
+
/**
|
|
14
|
+
* The console command description.
|
|
15
|
+
*
|
|
16
|
+
* @var $description string
|
|
17
|
+
*/
|
|
18
|
+
$description = "Rollback the latest migrations";
|
|
19
|
+
/**
|
|
20
|
+
* The options or optional flag of the console command.
|
|
21
|
+
*
|
|
22
|
+
* @var $options Array<Array<any>>
|
|
23
|
+
*/
|
|
24
|
+
$options = [
|
|
25
|
+
["-f, --force", "Skip command confirmation"]
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* The arguments of the console command.
|
|
29
|
+
*
|
|
30
|
+
* @var $arguments Array<Array<string>>
|
|
31
|
+
*/
|
|
32
|
+
$arguments = [];
|
|
33
|
+
async handle(options, args) {
|
|
34
|
+
const database = Database.knex();
|
|
35
|
+
const bypass = isNotEmpty(options.force);
|
|
36
|
+
let confirm = "Y";
|
|
37
|
+
if (!bypass)
|
|
38
|
+
confirm = await ask(Chalk.setValue("This will ROLLBACK latest migrations. Are you want to continue? (Y/N): ")
|
|
39
|
+
.inline()
|
|
40
|
+
.error()
|
|
41
|
+
.show());
|
|
42
|
+
if (confirm.toUpperCase() === "Y") {
|
|
43
|
+
if (!bypass)
|
|
44
|
+
Logger.empty();
|
|
45
|
+
const spinner = ora(Chalk.setValue("Rollback...")
|
|
46
|
+
.info()
|
|
47
|
+
.show()).start();
|
|
48
|
+
try {
|
|
49
|
+
const [batchNo, logs] = await database.migrate.rollback();
|
|
50
|
+
spinner.succeed(`Batch ${batchNo} finished`);
|
|
51
|
+
if (logs.length > 0)
|
|
52
|
+
logs.forEach((migration) => spinner.succeed(migration));
|
|
53
|
+
else
|
|
54
|
+
spinner.succeed("No migrations were rolled back.");
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
spinner.fail(`Rollback failed : ${error.message}`);
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
await database.destroy();
|
|
61
|
+
spinner.stop();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default class MigrateStatusCommand {
|
|
2
|
+
/**
|
|
3
|
+
* The name and signature of the console command.
|
|
4
|
+
*
|
|
5
|
+
* @var $signature string
|
|
6
|
+
*/
|
|
7
|
+
protected $signature: string;
|
|
8
|
+
/**
|
|
9
|
+
* The console command description.
|
|
10
|
+
*
|
|
11
|
+
* @var $description string
|
|
12
|
+
*/
|
|
13
|
+
protected $description: string;
|
|
14
|
+
/**
|
|
15
|
+
* The options or optional flag of the console command.
|
|
16
|
+
*
|
|
17
|
+
* @var $options Array<Array<any>>
|
|
18
|
+
*/
|
|
19
|
+
protected $options: Array<Array<any>>;
|
|
20
|
+
/**
|
|
21
|
+
* The arguments of the console command.
|
|
22
|
+
*
|
|
23
|
+
* @var $arguments Array<Array<string>>
|
|
24
|
+
*/
|
|
25
|
+
protected $arguments: Array<Array<string>>;
|
|
26
|
+
handle(options: any, args: Array<string>): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import Logger from "@bejibun/logger";
|
|
2
|
+
import Chalk from "@bejibun/logger/facades/Chalk";
|
|
3
|
+
import ora from "ora";
|
|
4
|
+
import Database from "../../facades/Database";
|
|
5
|
+
export default class MigrateStatusCommand {
|
|
6
|
+
/**
|
|
7
|
+
* The name and signature of the console command.
|
|
8
|
+
*
|
|
9
|
+
* @var $signature string
|
|
10
|
+
*/
|
|
11
|
+
$signature = "migrate:status";
|
|
12
|
+
/**
|
|
13
|
+
* The console command description.
|
|
14
|
+
*
|
|
15
|
+
* @var $description string
|
|
16
|
+
*/
|
|
17
|
+
$description = "List migrations status";
|
|
18
|
+
/**
|
|
19
|
+
* The options or optional flag of the console command.
|
|
20
|
+
*
|
|
21
|
+
* @var $options Array<Array<any>>
|
|
22
|
+
*/
|
|
23
|
+
$options = [
|
|
24
|
+
["-f, --force", "Skip command confirmation"]
|
|
25
|
+
];
|
|
26
|
+
/**
|
|
27
|
+
* The arguments of the console command.
|
|
28
|
+
*
|
|
29
|
+
* @var $arguments Array<Array<string>>
|
|
30
|
+
*/
|
|
31
|
+
$arguments = [];
|
|
32
|
+
async handle(options, args) {
|
|
33
|
+
const database = Database.knex();
|
|
34
|
+
const spinner = ora(Chalk.setValue("Fetching...")
|
|
35
|
+
.info()
|
|
36
|
+
.show()).start();
|
|
37
|
+
try {
|
|
38
|
+
const [completed, pending] = await database.migrate.list();
|
|
39
|
+
spinner.succeed("Completed Migrations :");
|
|
40
|
+
if (completed.length > 0)
|
|
41
|
+
completed.forEach((migration) => spinner.succeed(migration.name));
|
|
42
|
+
else
|
|
43
|
+
spinner.succeed("No migrations were completed.");
|
|
44
|
+
Logger.empty();
|
|
45
|
+
spinner.succeed("Pending Migrations :");
|
|
46
|
+
if (pending.length > 0)
|
|
47
|
+
pending.forEach((migration) => spinner.succeed(migration.file));
|
|
48
|
+
else
|
|
49
|
+
spinner.succeed("No migrations were pending.");
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
spinner.fail(`Fetching failed : ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
await database.destroy();
|
|
56
|
+
spinner.stop();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const config = {
|
|
2
|
+
client: "pg",
|
|
3
|
+
connection: {
|
|
4
|
+
host: "127.0.0.1",
|
|
5
|
+
port: 5432,
|
|
6
|
+
user: "postgres",
|
|
7
|
+
password: "",
|
|
8
|
+
database: "bejibun"
|
|
9
|
+
},
|
|
10
|
+
migrations: {
|
|
11
|
+
extension: "ts",
|
|
12
|
+
directory: "./database/migrations",
|
|
13
|
+
tableName: "migrations"
|
|
14
|
+
},
|
|
15
|
+
pool: {
|
|
16
|
+
min: 0,
|
|
17
|
+
max: 1
|
|
18
|
+
},
|
|
19
|
+
seeds: {
|
|
20
|
+
extension: "ts",
|
|
21
|
+
directory: "./database/seeders"
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export default config;
|
package/configure.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/configure.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import App from "@bejibun/app";
|
|
2
|
+
import Logger from "@bejibun/logger";
|
|
3
|
+
import path from "path";
|
|
4
|
+
const configPath = path.resolve(__dirname, "config");
|
|
5
|
+
const regex = /\.(m?js|ts)$/;
|
|
6
|
+
const configs = Array.from(new Bun.Glob("**/*").scanSync({
|
|
7
|
+
cwd: configPath
|
|
8
|
+
})).filter(value => (regex.test(value) &&
|
|
9
|
+
!value.endsWith(".d.ts")));
|
|
10
|
+
for (const config of configs) {
|
|
11
|
+
const destination = config.replace(regex, ".ts");
|
|
12
|
+
await Bun.write(App.Path.configPath(destination), await Bun.file(path.resolve(configPath, config)).text());
|
|
13
|
+
Logger.setContext("CONFIGURE").info(`Copying ${config} into config/${destination}`);
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../facades/Database";
|
package/facades/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../facades/Database";
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bejibun/database",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Havea Crenata <havea.crenata@gmail.com>",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/crenata/bejibun-database.git"
|
|
8
|
+
},
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"module": "index.js",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/bun": "latest",
|
|
13
|
+
"tsc-alias": "^1.8.16"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/crenata/bejibun-database/issues"
|
|
17
|
+
},
|
|
18
|
+
"description": "Database for Bejibun Framework",
|
|
19
|
+
"homepage": "https://github.com/crenata/bejibun-database#readme",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"bun",
|
|
22
|
+
"bun framework",
|
|
23
|
+
"database",
|
|
24
|
+
"bejibun",
|
|
25
|
+
"typescript"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"alias": "bunx tsc-alias -p tsconfig.json",
|
|
30
|
+
"rsync": "rsync -a dist/ ./",
|
|
31
|
+
"clean": "rm -rf dist",
|
|
32
|
+
"build": "bunx tsc -p tsconfig.json && bun run alias && bun run rsync && bun run clean",
|
|
33
|
+
"deploy": "bun run build && bun publish --access public"
|
|
34
|
+
},
|
|
35
|
+
"type": "module",
|
|
36
|
+
"types": "index.d.ts",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@bejibun/app": "^0.1.19",
|
|
39
|
+
"@bejibun/logger": "^0.1.18",
|
|
40
|
+
"@bejibun/utils": "^0.1.14",
|
|
41
|
+
"knex": "^3.1.0",
|
|
42
|
+
"ora": "^9.0.0",
|
|
43
|
+
"pg": "^8.16.3"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"baseUrl": ".",
|
|
13
|
+
"paths": {
|
|
14
|
+
"@/*": ["src/*"]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|