@elysiumjs/styx 0.5.0 → 0.6.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/package.json +5 -4
- package/styx.js +62 -60
- package/src/commands/make_command.ts +0 -172
- package/src/commands/make_controller.ts +0 -277
- package/src/commands/make_job.ts +0 -183
- package/src/commands/make_middleware.ts +0 -143
- package/src/commands/make_model.ts +0 -205
- package/src/commands/make_repository.ts +0 -225
- package/src/commands/make_service.ts +0 -201
- package/src/commands/make_validator.ts +0 -147
- package/src/commands/migration_generate.ts +0 -80
- package/src/commands/migration_run.ts +0 -153
- package/src/commands/module_new.ts +0 -112
- package/src/commands/module_rename.ts +0 -78
- package/src/config.ts +0 -101
- package/src/styx.ts +0 -54
- package/src/utils.ts +0 -148
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { PromptObject } from 'prompts';
|
|
16
|
-
|
|
17
|
-
import { join } from 'node:path';
|
|
18
|
-
|
|
19
|
-
import { Command, CommandArgumentType } from '@elysiumjs/core';
|
|
20
|
-
import prompts from 'prompts';
|
|
21
|
-
import { snake } from 'radash';
|
|
22
|
-
import formatter from 'string-template';
|
|
23
|
-
|
|
24
|
-
import { getModulePath, parseProjectConfig } from '../config';
|
|
25
|
-
import { getRootPath } from '../utils';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Maker command for creating Elysium services.
|
|
29
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
30
|
-
*/
|
|
31
|
-
export class MakeServiceCommand extends Command {
|
|
32
|
-
public static readonly command: string = 'make:service';
|
|
33
|
-
public static readonly description: string = 'Creates a new service.';
|
|
34
|
-
|
|
35
|
-
@Command.arg({
|
|
36
|
-
description: 'The name of the service to create',
|
|
37
|
-
type: CommandArgumentType.STRING
|
|
38
|
-
})
|
|
39
|
-
private name?: string;
|
|
40
|
-
|
|
41
|
-
@Command.arg({
|
|
42
|
-
description: 'The module where the service will be created',
|
|
43
|
-
type: CommandArgumentType.STRING
|
|
44
|
-
})
|
|
45
|
-
private module?: string;
|
|
46
|
-
|
|
47
|
-
@Command.arg({
|
|
48
|
-
description: 'The alias of the service to create',
|
|
49
|
-
type: CommandArgumentType.STRING
|
|
50
|
-
})
|
|
51
|
-
private alias?: string;
|
|
52
|
-
|
|
53
|
-
@Command.arg({
|
|
54
|
-
description: 'Create a factory service',
|
|
55
|
-
type: CommandArgumentType.BOOLEAN,
|
|
56
|
-
default: false
|
|
57
|
-
})
|
|
58
|
-
private factory: boolean = false;
|
|
59
|
-
|
|
60
|
-
@Command.arg({
|
|
61
|
-
description: 'Create a singleton service',
|
|
62
|
-
type: CommandArgumentType.BOOLEAN,
|
|
63
|
-
default: false
|
|
64
|
-
})
|
|
65
|
-
private singleton: boolean = false;
|
|
66
|
-
|
|
67
|
-
public async run(): Promise<void> {
|
|
68
|
-
if (!this.name) {
|
|
69
|
-
return this.setup();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const config = await parseProjectConfig();
|
|
73
|
-
|
|
74
|
-
const answers: Record<string, any> = {
|
|
75
|
-
module: this.module,
|
|
76
|
-
name: this.name,
|
|
77
|
-
alias: this.alias,
|
|
78
|
-
scope: this.factory ? 'FACTORY' : this.singleton ? 'SINGLETON' : 'SINGLETON'
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
if (!answers.module && !config.mono) {
|
|
82
|
-
const module = await prompts({
|
|
83
|
-
type: 'select',
|
|
84
|
-
name: 'module',
|
|
85
|
-
message: 'Module:',
|
|
86
|
-
choices: Object.keys(config.modules ?? {}).map((moduleName) => ({
|
|
87
|
-
title: moduleName,
|
|
88
|
-
value: moduleName
|
|
89
|
-
}))
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
answers.module = module.module;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return this.build(answers);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
private async setup(): Promise<void> {
|
|
99
|
-
const config = await parseProjectConfig();
|
|
100
|
-
|
|
101
|
-
const items: PromptObject[] = [
|
|
102
|
-
{
|
|
103
|
-
type() {
|
|
104
|
-
return config.mono ? null : 'select';
|
|
105
|
-
},
|
|
106
|
-
name: 'module',
|
|
107
|
-
message: 'Module:',
|
|
108
|
-
choices() {
|
|
109
|
-
return Object.keys(config.modules ?? {}).map((moduleName) => ({
|
|
110
|
-
title: moduleName,
|
|
111
|
-
value: moduleName
|
|
112
|
-
}));
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
type: 'text',
|
|
117
|
-
name: 'name',
|
|
118
|
-
message: 'Service Name:',
|
|
119
|
-
initial: 'UserService',
|
|
120
|
-
validate(value: string) {
|
|
121
|
-
if (value.length < 1) {
|
|
122
|
-
return 'Service name cannot be empty';
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
type: 'text',
|
|
130
|
-
name: 'alias',
|
|
131
|
-
message: 'Service Alias:',
|
|
132
|
-
initial(_, values) {
|
|
133
|
-
return values.name;
|
|
134
|
-
},
|
|
135
|
-
validate(value: string) {
|
|
136
|
-
if (value.length < 1) {
|
|
137
|
-
return 'Service alias cannot be empty';
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
type: 'select',
|
|
145
|
-
name: 'scope',
|
|
146
|
-
message: 'Service Scope:',
|
|
147
|
-
choices: [
|
|
148
|
-
{
|
|
149
|
-
title: 'SINGLETON',
|
|
150
|
-
value: 'SINGLETON',
|
|
151
|
-
description: 'A single instance of the service is created.'
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
title: 'FACTORY',
|
|
155
|
-
value: 'FACTORY',
|
|
156
|
-
description: 'A new instance of the service is created each time it is injected.'
|
|
157
|
-
}
|
|
158
|
-
]
|
|
159
|
-
}
|
|
160
|
-
];
|
|
161
|
-
|
|
162
|
-
const answers = await prompts(items, {
|
|
163
|
-
onCancel: () => {
|
|
164
|
-
this.error('Operation cancelled.');
|
|
165
|
-
process.exit(0);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
return this.build(answers);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private async build(answers: Record<string, any>): Promise<void> {
|
|
173
|
-
if (!answers.name) {
|
|
174
|
-
this.error('Operation cancelled.');
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (!answers.name.endsWith('Service')) {
|
|
179
|
-
answers.name += 'Service';
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (!answers.alias) {
|
|
183
|
-
answers.alias = answers.name;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Get stub file
|
|
187
|
-
const stubFile = Bun.file(join(getRootPath(), 'stubs/service.stub'));
|
|
188
|
-
|
|
189
|
-
// Format the stub content
|
|
190
|
-
const stub = formatter(await stubFile.text(), answers);
|
|
191
|
-
|
|
192
|
-
const path = answers.module ? await getModulePath(answers.module) : './src';
|
|
193
|
-
|
|
194
|
-
// Write to file
|
|
195
|
-
const name = snake(answers.name.replace('Service', ''));
|
|
196
|
-
const file = Bun.file(`${path}/services/${name}.service.ts`);
|
|
197
|
-
await file.write(stub);
|
|
198
|
-
|
|
199
|
-
this.success(`Service ${this.bold(file.name!)} created successfully.`);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { PromptObject } from 'prompts';
|
|
16
|
-
|
|
17
|
-
import { join } from 'node:path';
|
|
18
|
-
|
|
19
|
-
import { Command, CommandArgumentType } from '@elysiumjs/core';
|
|
20
|
-
import prompts from 'prompts';
|
|
21
|
-
import { snake } from 'radash';
|
|
22
|
-
import formatter from 'string-template';
|
|
23
|
-
|
|
24
|
-
import { getModulePath, parseProjectConfig } from '../config';
|
|
25
|
-
import { getRootPath } from '../utils';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Maker command for creating Elysium validators.
|
|
29
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
30
|
-
*/
|
|
31
|
-
export class MakeValidatorCommand extends Command {
|
|
32
|
-
public static readonly command: string = 'make:validator';
|
|
33
|
-
public static readonly description: string = 'Creates a new validator.';
|
|
34
|
-
|
|
35
|
-
@Command.arg({
|
|
36
|
-
description: 'The name of the validator to create',
|
|
37
|
-
type: CommandArgumentType.STRING
|
|
38
|
-
})
|
|
39
|
-
private name?: string;
|
|
40
|
-
|
|
41
|
-
@Command.arg({
|
|
42
|
-
description: 'The module where the validator will be created',
|
|
43
|
-
type: CommandArgumentType.STRING
|
|
44
|
-
})
|
|
45
|
-
private module?: string;
|
|
46
|
-
|
|
47
|
-
public async run(): Promise<void> {
|
|
48
|
-
if (!this.name) {
|
|
49
|
-
return this.setup();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const config = await parseProjectConfig();
|
|
53
|
-
|
|
54
|
-
const answers: Record<string, any> = {
|
|
55
|
-
module: this.module,
|
|
56
|
-
name: this.name
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
if (!answers.module && !config.mono) {
|
|
60
|
-
const module = await prompts({
|
|
61
|
-
type: 'select',
|
|
62
|
-
name: 'module',
|
|
63
|
-
message: 'Module:',
|
|
64
|
-
choices: Object.keys(config.modules ?? {}).map((moduleName) => ({
|
|
65
|
-
title: moduleName,
|
|
66
|
-
value: moduleName
|
|
67
|
-
}))
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
answers.module = module.module;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return this.build(answers);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
private async setup(): Promise<void> {
|
|
77
|
-
const config = await parseProjectConfig();
|
|
78
|
-
|
|
79
|
-
const items: PromptObject[] = [
|
|
80
|
-
{
|
|
81
|
-
type() {
|
|
82
|
-
return config.mono ? null : 'select';
|
|
83
|
-
},
|
|
84
|
-
name: 'module',
|
|
85
|
-
message: 'Module:',
|
|
86
|
-
choices() {
|
|
87
|
-
return Object.keys(config.modules ?? {}).map((moduleName) => ({
|
|
88
|
-
title: moduleName,
|
|
89
|
-
value: moduleName
|
|
90
|
-
}));
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
type: 'text',
|
|
95
|
-
name: 'name',
|
|
96
|
-
message: 'Validator Name:',
|
|
97
|
-
initial: 'LoginValidator',
|
|
98
|
-
validate(value: string) {
|
|
99
|
-
if (value.length < 1) {
|
|
100
|
-
return 'Validator name cannot be empty';
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
];
|
|
107
|
-
|
|
108
|
-
const answers = await prompts(items, {
|
|
109
|
-
onCancel: () => {
|
|
110
|
-
this.error('Operation cancelled.');
|
|
111
|
-
process.exit(0);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
return this.build(answers);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private async build(answers: Record<string, any>): Promise<void> {
|
|
119
|
-
if (!answers.name) {
|
|
120
|
-
this.error('Operation cancelled.');
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (!answers.name.endsWith('Validator')) {
|
|
125
|
-
answers.name += 'Validator';
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (!answers.request_name) {
|
|
129
|
-
answers.request_name = answers.name.replace('Validator', 'Request');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Get stub file
|
|
133
|
-
const stubFile = Bun.file(join(getRootPath(), 'stubs/validator.stub'));
|
|
134
|
-
|
|
135
|
-
// Format the stub content
|
|
136
|
-
const stub = formatter(await stubFile.text(), answers);
|
|
137
|
-
|
|
138
|
-
const path = answers.module ? await getModulePath(answers.module) : './src';
|
|
139
|
-
|
|
140
|
-
// Write to file
|
|
141
|
-
const name = snake(answers.name.replace('Validator', ''));
|
|
142
|
-
const file = Bun.file(`${path}/validators/${name}.validator.ts`);
|
|
143
|
-
await file.write(stub);
|
|
144
|
-
|
|
145
|
-
this.success(`Validator ${this.bold(file.name!)} created successfully.`);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { exists } from 'node:fs/promises';
|
|
16
|
-
|
|
17
|
-
import { Command, CommandArgumentType } from '@elysiumjs/core';
|
|
18
|
-
import prompts from 'prompts';
|
|
19
|
-
|
|
20
|
-
import { getProjectPath } from '../utils';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Generates migration files.
|
|
24
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
25
|
-
*/
|
|
26
|
-
export class MigrationGenerateCommand extends Command {
|
|
27
|
-
public static readonly command: string = 'migration:generate';
|
|
28
|
-
public static readonly description: string = 'Generates migration files.';
|
|
29
|
-
|
|
30
|
-
@Command.arg({
|
|
31
|
-
description: 'The name of the migration to create',
|
|
32
|
-
type: CommandArgumentType.STRING
|
|
33
|
-
})
|
|
34
|
-
private name?: string;
|
|
35
|
-
|
|
36
|
-
public async run(): Promise<void> {
|
|
37
|
-
if (!this.name) {
|
|
38
|
-
const answers = await prompts(
|
|
39
|
-
[
|
|
40
|
-
{
|
|
41
|
-
type: 'text',
|
|
42
|
-
name: 'name',
|
|
43
|
-
message: 'Migration Name:',
|
|
44
|
-
initial: 'create_users_table',
|
|
45
|
-
validate(value: string) {
|
|
46
|
-
if (value.length < 1) {
|
|
47
|
-
return 'Migration name cannot be empty';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
],
|
|
54
|
-
{
|
|
55
|
-
onCancel: () => {
|
|
56
|
-
this.error('Operation cancelled.');
|
|
57
|
-
process.exit(0);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
this.name = answers.name;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const projectRoot = getProjectPath();
|
|
66
|
-
let schemaPath = `${projectRoot}/src/database/schemas`;
|
|
67
|
-
if (await exists(`${projectRoot}/src/database/schema.ts`)) {
|
|
68
|
-
schemaPath = `${projectRoot}/src/database/schema.ts`;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
await Bun.$`bunx --bun drizzle-kit generate --dialect postgresql --schema ${schemaPath} --out ${projectRoot}/src/database/migrations --prefix timestamp --name ${this.name}`;
|
|
72
|
-
|
|
73
|
-
const sqlFiles = new Bun.Glob(`${projectRoot}/src/database/migrations/**/*.sql`).scan();
|
|
74
|
-
for await (const path of sqlFiles) {
|
|
75
|
-
const file = Bun.file(path);
|
|
76
|
-
const content = await file.text();
|
|
77
|
-
await file.write(content.replace(/"public"\./gm, ''));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { basename } from 'node:path';
|
|
16
|
-
|
|
17
|
-
import { Command, CommandArgumentType, Database } from '@elysiumjs/core';
|
|
18
|
-
import prompts from 'prompts';
|
|
19
|
-
import { alphabetical } from 'radash';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Execute migration files.
|
|
23
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
24
|
-
*/
|
|
25
|
-
export class MigrationRunCommand extends Command {
|
|
26
|
-
public static readonly command: string = 'migration:run';
|
|
27
|
-
public static readonly description: string = 'Generates migration files.';
|
|
28
|
-
|
|
29
|
-
@Command.arg({
|
|
30
|
-
description: 'The name of the tenant in which the migrations will be executed.',
|
|
31
|
-
type: CommandArgumentType.STRING,
|
|
32
|
-
required: true
|
|
33
|
-
})
|
|
34
|
-
private tenant: string = '';
|
|
35
|
-
|
|
36
|
-
@Command.arg({
|
|
37
|
-
description: 'The name of the connection to use.',
|
|
38
|
-
type: CommandArgumentType.STRING,
|
|
39
|
-
default: 'default'
|
|
40
|
-
})
|
|
41
|
-
private connection: string = 'default';
|
|
42
|
-
|
|
43
|
-
@Command.arg({
|
|
44
|
-
description: 'Drop the entire schema before running the migrations.',
|
|
45
|
-
type: CommandArgumentType.BOOLEAN,
|
|
46
|
-
default: false
|
|
47
|
-
})
|
|
48
|
-
private fresh: boolean = false;
|
|
49
|
-
|
|
50
|
-
public async run(): Promise<void> {
|
|
51
|
-
if (!Database.connectionExists(this.connection)) {
|
|
52
|
-
this.error(`Connection ${this.bold(this.connection)} does not exist.`);
|
|
53
|
-
return process.exit(1);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const sql = Database.getConnection(this.connection).$client;
|
|
57
|
-
await sql.connect();
|
|
58
|
-
|
|
59
|
-
await sql.begin(async (sql) => {
|
|
60
|
-
if (this.fresh) {
|
|
61
|
-
await sql`DROP SCHEMA IF EXISTS ${sql(this.tenant)} CASCADE;`;
|
|
62
|
-
await sql`CREATE SCHEMA ${sql(this.tenant)};`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const [{ schema_exists }] = await sql`SELECT EXISTS (
|
|
66
|
-
SELECT schema_name
|
|
67
|
-
FROM information_schema.schemata
|
|
68
|
-
WHERE schema_name = ${this.tenant}
|
|
69
|
-
) AS schema_exists;`;
|
|
70
|
-
|
|
71
|
-
if (!schema_exists) {
|
|
72
|
-
const answers = await prompts(
|
|
73
|
-
[
|
|
74
|
-
{
|
|
75
|
-
type: 'confirm',
|
|
76
|
-
name: 'create',
|
|
77
|
-
message: `Schema ${this.tenant} does not exist. Do you want to create it?`,
|
|
78
|
-
initial: true
|
|
79
|
-
}
|
|
80
|
-
],
|
|
81
|
-
{
|
|
82
|
-
onCancel: () => {
|
|
83
|
-
this.error('Operation cancelled.');
|
|
84
|
-
process.exit(0);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
if (answers.create) {
|
|
90
|
-
await sql`CREATE SCHEMA ${sql(this.tenant)};`;
|
|
91
|
-
} else {
|
|
92
|
-
this.error(`Schema ${this.bold(this.tenant)} creation cancelled.`);
|
|
93
|
-
return process.exit(1);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
await sql`SET search_path TO ${sql(this.tenant)};`;
|
|
98
|
-
|
|
99
|
-
const [{ table_exists }] = await sql`SELECT EXISTS (
|
|
100
|
-
SELECT *
|
|
101
|
-
FROM information_schema.tables
|
|
102
|
-
WHERE table_name = 'elysium_migrations'
|
|
103
|
-
AND table_schema = ${this.tenant}
|
|
104
|
-
) AS table_exists;`;
|
|
105
|
-
|
|
106
|
-
if (!table_exists) {
|
|
107
|
-
await sql`CREATE TABLE elysium_migrations (
|
|
108
|
-
id SERIAL PRIMARY KEY,
|
|
109
|
-
name TEXT NOT NULL,
|
|
110
|
-
batch INTEGER NOT NULL,
|
|
111
|
-
applied_at TIMESTAMP NOT NULL
|
|
112
|
-
);`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const migrations: { name: string }[] = await sql`SELECT name FROM elysium_migrations;`;
|
|
116
|
-
const completedMigrations = migrations.map((row) => row.name);
|
|
117
|
-
|
|
118
|
-
const filesIt = new Bun.Glob(`./src/database/migrations/**/*.sql`).scan({
|
|
119
|
-
absolute: true,
|
|
120
|
-
onlyFiles: true
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
const files = await Array.fromAsync(filesIt);
|
|
124
|
-
const sqlFiles = alphabetical(
|
|
125
|
-
files.filter((file) => !completedMigrations.includes(basename(file))),
|
|
126
|
-
(file) => file
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
if (sqlFiles.length === 0) {
|
|
130
|
-
this.success('No new migrations to apply.');
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const s = this.spinner(`Running migrations for tenant ${this.bold(this.tenant)}...`);
|
|
135
|
-
|
|
136
|
-
const [{ max }]: [{ max?: number }] =
|
|
137
|
-
await sql`SELECT MAX(batch) + 1 AS max FROM elysium_migrations;`;
|
|
138
|
-
|
|
139
|
-
const batch = max ?? 0;
|
|
140
|
-
|
|
141
|
-
for (const path of sqlFiles) {
|
|
142
|
-
await sql.file(path);
|
|
143
|
-
const [{ name }] =
|
|
144
|
-
await sql`INSERT INTO elysium_migrations (name, batch, applied_at) VALUES (${basename(path)}, ${batch}, ${new Date()}) RETURNING name;`;
|
|
145
|
-
s.pause(`Migration ${this.bold(name)} applied successfully.`);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
s.complete(`Migrations for tenant ${this.bold(this.tenant)} applied successfully.`);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
await sql.close();
|
|
152
|
-
}
|
|
153
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { exists, rmdir } from 'node:fs/promises';
|
|
16
|
-
import { join } from 'node:path';
|
|
17
|
-
|
|
18
|
-
import { Command, CommandArgumentType } from '@elysiumjs/core';
|
|
19
|
-
import prompts from 'prompts';
|
|
20
|
-
import { snake } from 'radash';
|
|
21
|
-
|
|
22
|
-
import { parseProjectConfig } from '../config';
|
|
23
|
-
import { createModule, getProjectPath } from '../utils';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Command for creating Elysium.js modules.
|
|
27
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
28
|
-
*/
|
|
29
|
-
export class ModuleNewCommand extends Command {
|
|
30
|
-
public static readonly command: string = 'module:new';
|
|
31
|
-
public static readonly description: string = 'Creates a new module.';
|
|
32
|
-
|
|
33
|
-
@Command.arg({
|
|
34
|
-
description: 'The name of the module to create',
|
|
35
|
-
type: CommandArgumentType.STRING
|
|
36
|
-
})
|
|
37
|
-
private name?: string;
|
|
38
|
-
|
|
39
|
-
private module?: string;
|
|
40
|
-
private convert: boolean = false;
|
|
41
|
-
|
|
42
|
-
public override async run(): Promise<void> {
|
|
43
|
-
const config = await parseProjectConfig();
|
|
44
|
-
|
|
45
|
-
// 1. Prompt for new module name if not provided
|
|
46
|
-
if (!this.name) {
|
|
47
|
-
const name = await prompts(
|
|
48
|
-
{
|
|
49
|
-
type: 'text',
|
|
50
|
-
name: 'name',
|
|
51
|
-
message: 'New module name:',
|
|
52
|
-
initial: 'module',
|
|
53
|
-
validate(value) {
|
|
54
|
-
return value.length > 0 ? true : 'Module name cannot be empty';
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
onCancel: () => process.exit(0)
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
this.name = snake(name.name);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 2. If mono-module, prompt for conversion and current module name
|
|
66
|
-
if (config.mono) {
|
|
67
|
-
const { module, convert } = await prompts(
|
|
68
|
-
[
|
|
69
|
-
{
|
|
70
|
-
type: 'confirm',
|
|
71
|
-
name: 'convert',
|
|
72
|
-
message:
|
|
73
|
-
'This project is a mono-module project. Do you want to convert it to a multi-module project?',
|
|
74
|
-
initial: false
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
type: (convert) => (convert ? 'text' : null),
|
|
78
|
-
name: 'module',
|
|
79
|
-
message: 'Current module name:',
|
|
80
|
-
initial: 'main',
|
|
81
|
-
validate(value) {
|
|
82
|
-
return value.length > 0 ? true : 'Module name cannot be empty';
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
],
|
|
86
|
-
{
|
|
87
|
-
onCancel: () => process.exit(0)
|
|
88
|
-
}
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
if (!convert) return this.error('Operation cancelled. The project is left unchanged.');
|
|
92
|
-
|
|
93
|
-
this.module = snake(module as string);
|
|
94
|
-
this.convert = convert;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 3. If multi-module conversion is enabled, convert the project to a multi-module project
|
|
98
|
-
if (this.convert) {
|
|
99
|
-
const modulesDir = join(getProjectPath(), 'src', 'modules');
|
|
100
|
-
if (await exists(modulesDir)) await rmdir(modulesDir, { recursive: true });
|
|
101
|
-
|
|
102
|
-
await createModule(this.module!, 'root');
|
|
103
|
-
|
|
104
|
-
this.success(`Project converted to multi-module.`);
|
|
105
|
-
this.success(`Module '${this.module}' created.`);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// 4. Create the new module
|
|
109
|
-
await createModule(this.name);
|
|
110
|
-
this.success(`Module '${this.name}' created.`);
|
|
111
|
-
}
|
|
112
|
-
}
|