@elysiumjs/styx 0.4.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.
@@ -1,196 +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
-
164
- return this.build(answers);
165
- }
166
-
167
- private async build(answers: Record<string, any>): Promise<void> {
168
- if (!answers.name) {
169
- this.error('Operation cancelled.');
170
- return;
171
- }
172
-
173
- if (!answers.name.endsWith('Service')) {
174
- answers.name += 'Service';
175
- }
176
-
177
- if (!answers.alias) {
178
- answers.alias = answers.name;
179
- }
180
-
181
- // Get stub file
182
- const stubFile = Bun.file(join(getRootPath(), 'stubs/service.stub'));
183
-
184
- // Format the stub content
185
- const stub = formatter(await stubFile.text(), answers);
186
-
187
- const path = answers.module ? await getModulePath(answers.module) : './src';
188
-
189
- // Write to file
190
- const name = snake(answers.name.replace('Service', ''));
191
- const file = Bun.file(`${path}/services/${name}.service.ts`);
192
- await file.write(stub);
193
-
194
- this.success(`Service ${this.bold(file.name!)} created successfully.`);
195
- }
196
- }
@@ -1,142 +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
-
110
- return this.build(answers);
111
- }
112
-
113
- private async build(answers: Record<string, any>): Promise<void> {
114
- if (!answers.name) {
115
- this.error('Operation cancelled.');
116
- return;
117
- }
118
-
119
- if (!answers.name.endsWith('Validator')) {
120
- answers.name += 'Validator';
121
- }
122
-
123
- if (!answers.request_name) {
124
- answers.request_name = answers.name.replace('Validator', 'Request');
125
- }
126
-
127
- // Get stub file
128
- const stubFile = Bun.file(join(getRootPath(), 'stubs/validator.stub'));
129
-
130
- // Format the stub content
131
- const stub = formatter(await stubFile.text(), answers);
132
-
133
- const path = answers.module ? await getModulePath(answers.module) : './src';
134
-
135
- // Write to file
136
- const name = snake(answers.name.replace('Validator', ''));
137
- const file = Bun.file(`${path}/validators/${name}.validator.ts`);
138
- await file.write(stub);
139
-
140
- this.success(`Validator ${this.bold(file.name!)} created successfully.`);
141
- }
142
- }
@@ -1,69 +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
- /**
21
- * Generates migration files.
22
- * @author Axel Nana <axel.nana@workbud.com>
23
- */
24
- export class MigrationGenerateCommand extends Command {
25
- public static readonly command: string = 'migration:generate';
26
- public static readonly description: string = 'Generates migration files.';
27
-
28
- @Command.arg({
29
- description: 'The name of the migration to create',
30
- type: CommandArgumentType.STRING
31
- })
32
- private name?: string;
33
-
34
- public async run(): Promise<void> {
35
- if (!this.name) {
36
- const answers = await prompts([
37
- {
38
- type: 'text',
39
- name: 'name',
40
- message: 'Migration Name:',
41
- initial: 'create_users_table',
42
- validate(value: string) {
43
- if (value.length < 1) {
44
- return 'Migration name cannot be empty';
45
- }
46
-
47
- return true;
48
- }
49
- }
50
- ]);
51
-
52
- this.name = answers.name;
53
- }
54
-
55
- let schemaPath = `./src/database/schemas`;
56
- if (await exists(`${process.cwd()}/src/database/schema.ts`)) {
57
- schemaPath = `./src/database/schema.ts`;
58
- }
59
-
60
- await Bun.$`bunx --bun drizzle-kit generate --dialect postgresql --schema ${schemaPath} --out ./src/database/migrations --prefix timestamp --name ${this.name}`;
61
-
62
- const sqlFiles = new Bun.Glob(`${process.cwd()}/src/database/migrations/**/*.sql`).scan();
63
- for await (const path of sqlFiles) {
64
- const file = Bun.file(path);
65
- const content = await file.text();
66
- await file.write(content.replace(/"public"./gm, ''));
67
- }
68
- }
69
- }
@@ -1,145 +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
- type: 'confirm',
75
- name: 'create',
76
- message: `Schema ${this.tenant} does not exist. Do you want to create it?`,
77
- initial: true
78
- }
79
- ]);
80
-
81
- if (answers.create) {
82
- await sql`CREATE SCHEMA ${sql(this.tenant)};`;
83
- } else {
84
- this.error(`Schema ${this.bold(this.tenant)} creation cancelled.`);
85
- return process.exit(1);
86
- }
87
- }
88
-
89
- await sql`SET search_path TO ${sql(this.tenant)};`;
90
-
91
- const [{ table_exists }] = await sql`SELECT EXISTS (
92
- SELECT *
93
- FROM information_schema.tables
94
- WHERE table_name = 'elysium_migrations'
95
- AND table_schema = ${this.tenant}
96
- ) AS table_exists;`;
97
-
98
- if (!table_exists) {
99
- await sql`CREATE TABLE elysium_migrations (
100
- id SERIAL PRIMARY KEY,
101
- name TEXT NOT NULL,
102
- batch INTEGER NOT NULL,
103
- applied_at TIMESTAMP NOT NULL
104
- );`;
105
- }
106
-
107
- const migrations: { name: string }[] = await sql`SELECT name FROM elysium_migrations;`;
108
- const completedMigrations = migrations.map((row) => row.name);
109
-
110
- const filesIt = new Bun.Glob(`./src/database/migrations/**/*.sql`).scan({
111
- absolute: true,
112
- onlyFiles: true
113
- });
114
-
115
- const files = await Array.fromAsync(filesIt);
116
- const sqlFiles = alphabetical(
117
- files.filter((file) => !completedMigrations.includes(basename(file))),
118
- (file) => file
119
- );
120
-
121
- if (sqlFiles.length === 0) {
122
- this.success('No new migrations to apply.');
123
- return;
124
- }
125
-
126
- const s = this.spinner(`Running migrations for tenant ${this.bold(this.tenant)}...`);
127
-
128
- const [{ max }]: [{ max?: number }] =
129
- await sql`SELECT MAX(batch) + 1 AS max FROM elysium_migrations;`;
130
-
131
- const batch = max ?? 0;
132
-
133
- for (const path of sqlFiles) {
134
- await sql.file(path);
135
- const [{ name }] =
136
- await sql`INSERT INTO elysium_migrations (name, batch, applied_at) VALUES (${basename(path)}, ${batch}, ${new Date()}) RETURNING name;`;
137
- s.pause(`Migration ${this.bold(name)} applied successfully.`);
138
- }
139
-
140
- s.complete(`Migrations for tenant ${this.bold(this.tenant)} applied successfully.`);
141
- });
142
-
143
- await sql.close();
144
- }
145
- }
package/src/config.ts DELETED
@@ -1,99 +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 { resolve } from 'node:path';
16
-
17
- /**
18
- * Describes the Elysium project's configuration.
19
- * @author Axel Nana <axel.nana@workbud.com>
20
- */
21
- export interface ProjectConfig {
22
- /**
23
- * The name of the Elysium project.
24
- */
25
- projectName: string;
26
-
27
- /**
28
- * Whether the Elysium project is a mono module.
29
- */
30
- mono: boolean;
31
-
32
- /**
33
- * A map of module names to their paths.
34
- *
35
- * Only used if the Elysium project is not a mono module.
36
- */
37
- modules?: Record<string, string>;
38
- }
39
-
40
- /**
41
- * Parses the Elysium project's configuration file.
42
- * @author Axel Nana <axel.nana@workbud.com>
43
- * @throws If the Elysium config file is not found.
44
- * @returns The Elysium project's configuration.
45
- */
46
- export const parseProjectConfig = async (): Promise<ProjectConfig> => {
47
- const config = Bun.file(resolve('.elysiumrc'));
48
- if (!config.exists()) {
49
- throw new Error('Elysium config file not found.');
50
- }
51
-
52
- return JSON.parse(await config.text());
53
- };
54
-
55
- /**
56
- * Writes the Elysium project's configuration file.
57
- * @author Axel Nana <axel.nana@workbud.com>
58
- * @param config The Elysium project's configuration.
59
- */
60
- export const writeProjectConfig = async (config: ProjectConfig) => {
61
- const file = Bun.file(resolve('.elysiumrc'));
62
- await file.write(JSON.stringify(config, null, 2));
63
- };
64
-
65
- /**
66
- * Checks if the Elysium project is a mono-module project.
67
- * @author Axel Nana <axel.nana@workbud.com>
68
- * @returns Whether the Elysium project is a mono module.
69
- */
70
- export const isMonoModule = async () => {
71
- const config = await parseProjectConfig();
72
- return config.mono;
73
- };
74
-
75
- /**
76
- * Gets the path of a module.
77
- * @author Axel Nana <axel.nana@workbud.com>
78
- * @param moduleName The name of the module.
79
- * @returns The path of the module.
80
- */
81
- export const getModulePath = async (moduleName: string) => {
82
- const config = await parseProjectConfig();
83
- if (config.mono) {
84
- return './src';
85
- }
86
-
87
- return config.modules![moduleName];
88
- };
89
-
90
- /**
91
- * Checks if a module exists.
92
- * @author Axel Nana <axel.nana@workbud.com>
93
- * @param moduleName The name of the module.
94
- * @returns Whether the module exists.
95
- */
96
- export const moduleExists = async (moduleName: string) => {
97
- const path = await getModulePath(moduleName);
98
- return Bun.file(path).exists();
99
- };
package/src/utils.ts DELETED
@@ -1,23 +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 { dirname } from 'node:path';
16
-
17
- /**
18
- * Gets the root path of the styx package.
19
- * @author Axel Nana <axel.nana@workbud.com>
20
- */
21
- export const getRootPath = () => {
22
- return dirname(Bun.fileURLToPath(import.meta.url));
23
- };