@aventoapp/framework 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/dist/index.js +6 -0
- package/dist/index.js.map +7 -0
- package/dist/native/index.js +2522 -0
- package/dist/native/index.js.map +7 -0
- package/dist/server/index.js +2776 -0
- package/dist/server/index.js.map +7 -0
- package/dist/server/workflow/index.js +537 -0
- package/dist/server/workflow/index.js.map +7 -0
- package/dist/shared/index.js +62 -0
- package/dist/shared/index.js.map +7 -0
- package/dist/web/index.js +7323 -0
- package/dist/web/index.js.map +7 -0
- package/package.json +81 -0
- package/schemas/Workflow.json +14 -0
- package/schemas/WorkflowHistory.json +22 -0
- package/schemas/WorkflowInstance.json +16 -0
- package/schemas/WorkflowState.json +18 -0
- package/schemas/WorkflowTransition.json +27 -0
- package/src/index.js +8 -0
- package/src/native/components/NativeAuthProvider.jsx +249 -0
- package/src/native/components/NativeDetail.jsx +270 -0
- package/src/native/components/NativeForm.jsx +543 -0
- package/src/native/components/NativeGrid.jsx +90 -0
- package/src/native/components/NativeJsonGrid.jsx +213 -0
- package/src/native/components/NativeJsonView.jsx +454 -0
- package/src/native/components/NativeJsonViewContext.js +17 -0
- package/src/native/components/NativeShell.jsx +68 -0
- package/src/native/components/NativeViewComponent.jsx +351 -0
- package/src/native/extensions/NativeExtensionRegistry.js +81 -0
- package/src/native/hooks/useNativeStore.js +72 -0
- package/src/native/index.js +15 -0
- package/src/native/layouts/NativeStackLayout.jsx +30 -0
- package/src/native/layouts/NativeTabLayout.jsx +96 -0
- package/src/native/storage.js +13 -0
- package/src/native/stores/NativeBaseStore.js +348 -0
- package/src/native/stores/NativeStoreContext.jsx +53 -0
- package/src/server/ai/capabilities-routes.js +106 -0
- package/src/server/ai/index.js +1 -0
- package/src/server/cache/MemoryCache.js +153 -0
- package/src/server/cache/index.js +2 -0
- package/src/server/cache/middleware.js +12 -0
- package/src/server/db/index.js +14 -0
- package/src/server/db/mongo.js +137 -0
- package/src/server/db/postgres.js +144 -0
- package/src/server/index.js +38 -0
- package/src/server/middleware/auth.js +682 -0
- package/src/server/middleware/can-flags.js +132 -0
- package/src/server/middleware/entityRoles.js +105 -0
- package/src/server/middleware/index.js +58 -0
- package/src/server/middleware/permissions.js +240 -0
- package/src/server/middleware/tenant.js +203 -0
- package/src/server/realtime/EventBus.js +45 -0
- package/src/server/realtime/WebSocketServer.js +105 -0
- package/src/server/realtime/index.js +3 -0
- package/src/server/realtime/middleware.js +18 -0
- package/src/server/services/email-templates.js +157 -0
- package/src/server/services/email.js +78 -0
- package/src/server/services/index.js +15 -0
- package/src/server/services/storage.js +119 -0
- package/src/server/utils/index.js +16 -0
- package/src/server/utils/paginate.js +65 -0
- package/src/server/utils/query-builder.js +293 -0
- package/src/server/utils/route-loader.js +61 -0
- package/src/server/workflow/WorkflowEngine.js +494 -0
- package/src/server/workflow/index.js +2 -0
- package/src/server/workflow/workflow-routes.js +174 -0
- package/src/shared/index.js +10 -0
- package/src/shared/utils/field-accessor.js +108 -0
- package/src/shared/utils/index.js +7 -0
- package/src/shared/utils/validation-utils.js +83 -0
- package/src/web/components/AppRouter/AppRouter.jsx +130 -0
- package/src/web/components/AppRouter/index.js +1 -0
- package/src/web/components/AppShell/AppShell.jsx +101 -0
- package/src/web/components/AppShell/DefaultUserMenu.jsx +54 -0
- package/src/web/components/AppShell/ShellConfig.js +53 -0
- package/src/web/components/AppShell/index.js +3 -0
- package/src/web/components/AuthProvider.jsx +426 -0
- package/src/web/components/CardList/CardList.jsx +272 -0
- package/src/web/components/CardList/index.js +2 -0
- package/src/web/components/DetailView/DetailView.jsx +281 -0
- package/src/web/components/DetailView/index.js +2 -0
- package/src/web/components/Form/Form.jsx +300 -0
- package/src/web/components/Form/FormField.jsx +226 -0
- package/src/web/components/Form/index.js +3 -0
- package/src/web/components/Grid/Grid.jsx +304 -0
- package/src/web/components/Grid/index.js +2 -0
- package/src/web/components/IconRegistry/IconRegistry.jsx +127 -0
- package/src/web/components/IconRegistry/index.js +1 -0
- package/src/web/components/JsonView/JsonView.jsx +647 -0
- package/src/web/components/JsonView/ViewComponent.jsx +1045 -0
- package/src/web/components/JsonView/ViewToolbar.jsx +141 -0
- package/src/web/components/JsonView/index.js +14 -0
- package/src/web/components/JsonView/layouts/SingleLayout.jsx +41 -0
- package/src/web/components/JsonView/layouts/SplitLayout.jsx +122 -0
- package/src/web/components/JsonView/layouts/TabLayout.jsx +112 -0
- package/src/web/components/JsonView/layouts/index.js +8 -0
- package/src/web/components/Layout.jsx +265 -0
- package/src/web/components/MobileLayout/MobileLayout.jsx +385 -0
- package/src/web/components/MobileLayout/index.js +1 -0
- package/src/web/components/Navigation/Navigation.jsx +248 -0
- package/src/web/components/Navigation/index.js +1 -0
- package/src/web/components/NotificationProvider.jsx +55 -0
- package/src/web/components/Pagination/InfiniteScroll.jsx +82 -0
- package/src/web/components/Pagination/Pagination.jsx +73 -0
- package/src/web/components/Pagination/index.js +8 -0
- package/src/web/components/SimpleList/SimpleList.jsx +177 -0
- package/src/web/components/SimpleList/index.js +2 -0
- package/src/web/components/TenantProvider.jsx +121 -0
- package/src/web/components/WorkflowActions/WorkflowActions.jsx +71 -0
- package/src/web/components/WorkflowActions/index.js +1 -0
- package/src/web/components/WorkflowField/WorkflowField.jsx +176 -0
- package/src/web/components/WorkflowField/index.js +2 -0
- package/src/web/components/WorkflowField/workflowColors.js +32 -0
- package/src/web/components/auth/DevTenantSelector.jsx +90 -0
- package/src/web/components/auth/ForgotPasswordForm.jsx +94 -0
- package/src/web/components/auth/RegisterForm.jsx +168 -0
- package/src/web/components/auth/ResetPasswordForm.jsx +121 -0
- package/src/web/components/auth/VerifyEmailView.jsx +85 -0
- package/src/web/components/auth/index.js +5 -0
- package/src/web/components/index.js +39 -0
- package/src/web/components/layouts/CardListLayout.jsx +68 -0
- package/src/web/components/layouts/FullscreenLayout.jsx +30 -0
- package/src/web/components/layouts/MobileStackLayout.jsx +28 -0
- package/src/web/components/layouts/WizardLayout.jsx +86 -0
- package/src/web/components/layouts/index.js +4 -0
- package/src/web/components/mobile/Badge.jsx +105 -0
- package/src/web/components/mobile/BadgeThemeEditor.jsx +104 -0
- package/src/web/components/mobile/EmptyState.jsx +30 -0
- package/src/web/components/mobile/QuickActionButton.jsx +77 -0
- package/src/web/components/mobile/ServerAutocomplete.jsx +123 -0
- package/src/web/components/mobile/StarRating.jsx +42 -0
- package/src/web/components/mobile/StatCard.jsx +65 -0
- package/src/web/components/mobile/index.js +7 -0
- package/src/web/extensions/ExtensionRegistry.js +79 -0
- package/src/web/extensions/index.js +7 -0
- package/src/web/hooks/index.js +15 -0
- package/src/web/hooks/useApi.js +76 -0
- package/src/web/hooks/useAuth.js +37 -0
- package/src/web/hooks/useDebouncedSave.js +158 -0
- package/src/web/hooks/useIsMobile.js +14 -0
- package/src/web/hooks/useOfflineQueue.js +99 -0
- package/src/web/hooks/useSelectionModel.js +143 -0
- package/src/web/hooks/useServerAutocomplete.js +146 -0
- package/src/web/hooks/useStore.js +107 -0
- package/src/web/hooks/useSubscription.js +130 -0
- package/src/web/hooks/useTenant.js +39 -0
- package/src/web/hooks/useTenantResolver.js +65 -0
- package/src/web/hooks/useUndoRedo.js +77 -0
- package/src/web/hooks/useViews.js +113 -0
- package/src/web/hooks/useWorkflow.js +190 -0
- package/src/web/index.js +117 -0
- package/src/web/stores/BaseStore.js +439 -0
- package/src/web/stores/StoreContext.jsx +93 -0
- package/src/web/stores/index.js +3 -0
- package/src/web/theme/index.js +3 -0
- package/src/web/theme/mui-theme.js +228 -0
- package/src/web/utils/formatTimeAgo.js +22 -0
- package/src/web/utils/index.js +1 -0
- package/src/web/views/ViewRegistry.js +235 -0
- package/src/web/views/index.js +2 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Storage Service
|
|
3
|
+
*
|
|
4
|
+
* Provides a storage abstraction with local filesystem (dev) and Cloudflare R2 (production) backends.
|
|
5
|
+
* R2 activates automatically when R2_BUCKET_NAME and R2_ACCESS_KEY_ID env vars are set.
|
|
6
|
+
* Falls back to local filesystem under server/uploads/ when R2 is not configured.
|
|
7
|
+
*
|
|
8
|
+
* Environment variables (all optional - omit for local filesystem):
|
|
9
|
+
* R2_ACCOUNT_ID - Cloudflare account ID
|
|
10
|
+
* R2_ACCESS_KEY_ID - R2 API token access key
|
|
11
|
+
* R2_SECRET_ACCESS_KEY - R2 API token secret key
|
|
12
|
+
* R2_BUCKET_NAME - R2 bucket name
|
|
13
|
+
* R2_PUBLIC_URL - Public URL prefix for the bucket (e.g. https://cdn.myapp.com)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import fs from 'fs';
|
|
17
|
+
import path from 'path';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a storage service instance.
|
|
21
|
+
*
|
|
22
|
+
* Call once during server startup (async because it lazy-loads @aws-sdk/client-s3 only when R2 is configured).
|
|
23
|
+
*
|
|
24
|
+
* @param {object} options
|
|
25
|
+
* @param {string} options.uploadsDir - Absolute path to local uploads directory (used in local mode)
|
|
26
|
+
* @param {object} [options.r2] - R2 configuration (optional, overrides env vars)
|
|
27
|
+
* @param {string} [options.r2.accountId]
|
|
28
|
+
* @param {string} [options.r2.accessKeyId]
|
|
29
|
+
* @param {string} [options.r2.secretAccessKey]
|
|
30
|
+
* @param {string} [options.r2.bucketName]
|
|
31
|
+
* @param {string} [options.r2.publicUrl]
|
|
32
|
+
* @returns {Promise<{ put, del, getUrl, isR2Enabled }>}
|
|
33
|
+
*/
|
|
34
|
+
export async function createStorageService(options = {}) {
|
|
35
|
+
const uploadsDir = options.uploadsDir;
|
|
36
|
+
|
|
37
|
+
const r2Config = {
|
|
38
|
+
accountId: options.r2?.accountId || process.env.R2_ACCOUNT_ID,
|
|
39
|
+
accessKeyId: options.r2?.accessKeyId || process.env.R2_ACCESS_KEY_ID,
|
|
40
|
+
secretAccessKey: options.r2?.secretAccessKey || process.env.R2_SECRET_ACCESS_KEY,
|
|
41
|
+
bucketName: options.r2?.bucketName || process.env.R2_BUCKET_NAME,
|
|
42
|
+
publicUrl: options.r2?.publicUrl || process.env.R2_PUBLIC_URL,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const r2Enabled = !!(r2Config.bucketName && r2Config.accessKeyId);
|
|
46
|
+
|
|
47
|
+
let s3, PutObjectCommand, DeleteObjectCommand;
|
|
48
|
+
if (r2Enabled) {
|
|
49
|
+
const sdk = await import('@aws-sdk/client-s3');
|
|
50
|
+
PutObjectCommand = sdk.PutObjectCommand;
|
|
51
|
+
DeleteObjectCommand = sdk.DeleteObjectCommand;
|
|
52
|
+
s3 = new sdk.S3Client({
|
|
53
|
+
region: 'auto',
|
|
54
|
+
endpoint: `https://${r2Config.accountId}.r2.cloudflarestorage.com`,
|
|
55
|
+
credentials: {
|
|
56
|
+
accessKeyId: r2Config.accessKeyId,
|
|
57
|
+
secretAccessKey: r2Config.secretAccessKey,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Upload a file to storage.
|
|
64
|
+
* @param {string} key - Relative path, e.g. "courses/abc_123.jpg"
|
|
65
|
+
* @param {Buffer} buffer - File contents
|
|
66
|
+
* @param {string} contentType - MIME type
|
|
67
|
+
* @returns {Promise<string>} The URL to access the file
|
|
68
|
+
*/
|
|
69
|
+
async function put(key, buffer, contentType) {
|
|
70
|
+
if (r2Enabled) {
|
|
71
|
+
await s3.send(new PutObjectCommand({
|
|
72
|
+
Bucket: r2Config.bucketName,
|
|
73
|
+
Key: key,
|
|
74
|
+
Body: buffer,
|
|
75
|
+
ContentType: contentType,
|
|
76
|
+
}));
|
|
77
|
+
return `${r2Config.publicUrl}/${key}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const filePath = path.join(uploadsDir, key);
|
|
81
|
+
const dir = path.dirname(filePath);
|
|
82
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
83
|
+
fs.writeFileSync(filePath, buffer);
|
|
84
|
+
return `/uploads/${key}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Delete a file from storage.
|
|
89
|
+
* @param {string} key - Relative path, e.g. "courses/abc_123.jpg"
|
|
90
|
+
*/
|
|
91
|
+
async function del(key) {
|
|
92
|
+
if (r2Enabled) {
|
|
93
|
+
await s3.send(new DeleteObjectCommand({
|
|
94
|
+
Bucket: r2Config.bucketName,
|
|
95
|
+
Key: key,
|
|
96
|
+
}));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
const filePath = path.join(uploadsDir, key);
|
|
102
|
+
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
|
|
103
|
+
} catch {}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get the public URL for a stored file.
|
|
108
|
+
* @param {string} key - Relative path, e.g. "courses/abc_123.jpg"
|
|
109
|
+
* @returns {string}
|
|
110
|
+
*/
|
|
111
|
+
function getUrl(key) {
|
|
112
|
+
if (r2Enabled) {
|
|
113
|
+
return `${r2Config.publicUrl}/${key}`;
|
|
114
|
+
}
|
|
115
|
+
return `/uploads/${key}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return { put, del, getUrl, isR2Enabled: r2Enabled };
|
|
119
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
QueryBuilder,
|
|
3
|
+
createQueryBuilder,
|
|
4
|
+
query
|
|
5
|
+
} from './query-builder.js';
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
parsePaginationParams,
|
|
9
|
+
paginatedResponse,
|
|
10
|
+
getSqlPagination
|
|
11
|
+
} from './paginate.js';
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
mountGeneratedRoutes,
|
|
15
|
+
defineRoute
|
|
16
|
+
} from './route-loader.js';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server Pagination Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for parsing pagination parameters and formatting paginated responses.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parse pagination parameters from request query string
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} req - Express request object
|
|
11
|
+
* @returns {Object} Parsed pagination params
|
|
12
|
+
*/
|
|
13
|
+
export function parsePaginationParams(req) {
|
|
14
|
+
const page = parseInt(req.query.page) || 1;
|
|
15
|
+
const requestedPageSize = parseInt(req.query.pageSize) || 25;
|
|
16
|
+
// Cap page size at 100 to prevent excessive queries
|
|
17
|
+
const pageSize = Math.min(requestedPageSize, 100);
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
page,
|
|
21
|
+
pageSize,
|
|
22
|
+
offset: (page - 1) * pageSize,
|
|
23
|
+
sort: req.query.sort || null,
|
|
24
|
+
sortDirection: req.query.sortDirection || 'asc'
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a paginated response object
|
|
30
|
+
*
|
|
31
|
+
* @param {Array} data - Array of items for current page
|
|
32
|
+
* @param {number} total - Total count of all items
|
|
33
|
+
* @param {number} page - Current page number
|
|
34
|
+
* @param {number} pageSize - Items per page
|
|
35
|
+
* @returns {Object} Paginated response object
|
|
36
|
+
*/
|
|
37
|
+
export function paginatedResponse(data, total, page, pageSize) {
|
|
38
|
+
const totalPages = Math.ceil(total / pageSize);
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
data,
|
|
42
|
+
pagination: {
|
|
43
|
+
page,
|
|
44
|
+
pageSize,
|
|
45
|
+
total,
|
|
46
|
+
totalPages,
|
|
47
|
+
hasNext: page < totalPages,
|
|
48
|
+
hasPrev: page > 1
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Apply pagination to a SQL query builder
|
|
55
|
+
* Returns the offset and limit values for SQL queries
|
|
56
|
+
*
|
|
57
|
+
* @param {Object} params - Pagination params from parsePaginationParams
|
|
58
|
+
* @returns {Object} SQL pagination values { limit, offset }
|
|
59
|
+
*/
|
|
60
|
+
export function getSqlPagination(params) {
|
|
61
|
+
return {
|
|
62
|
+
limit: params.pageSize,
|
|
63
|
+
offset: params.offset
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tenant-scoped SQL Query Builder
|
|
3
|
+
*
|
|
4
|
+
* Automatically adds tenant_id to all queries to ensure
|
|
5
|
+
* multi-tenant data isolation.
|
|
6
|
+
*/
|
|
7
|
+
export class QueryBuilder {
|
|
8
|
+
constructor(db, tenantId) {
|
|
9
|
+
if (!tenantId) {
|
|
10
|
+
throw new Error('QueryBuilder requires tenantId for multi-tenant isolation');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
this.db = db;
|
|
14
|
+
this.tenantId = tenantId;
|
|
15
|
+
this._table = null;
|
|
16
|
+
this._select = ['*'];
|
|
17
|
+
this._where = [];
|
|
18
|
+
this._whereParams = [];
|
|
19
|
+
this._orderBy = [];
|
|
20
|
+
this._limit = null;
|
|
21
|
+
this._offset = null;
|
|
22
|
+
this._joins = [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Set the table to query
|
|
27
|
+
*/
|
|
28
|
+
table(tableName) {
|
|
29
|
+
this._table = tableName;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Set columns to select
|
|
35
|
+
*/
|
|
36
|
+
select(...columns) {
|
|
37
|
+
this._select = columns.length > 0 ? columns : ['*'];
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Add WHERE condition
|
|
43
|
+
*/
|
|
44
|
+
where(column, operator, value) {
|
|
45
|
+
// Handle simple equality: where('name', 'John')
|
|
46
|
+
if (value === undefined) {
|
|
47
|
+
value = operator;
|
|
48
|
+
operator = '=';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this._whereParams.push(value);
|
|
52
|
+
this._where.push(`${column} ${operator} $${this._whereParams.length + 1}`);
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Add WHERE IN condition
|
|
58
|
+
*/
|
|
59
|
+
whereIn(column, values) {
|
|
60
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const placeholders = values.map((_, i) => `$${this._whereParams.length + i + 2}`);
|
|
65
|
+
this._whereParams.push(...values);
|
|
66
|
+
this._where.push(`${column} IN (${placeholders.join(', ')})`);
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Add WHERE NULL condition
|
|
72
|
+
*/
|
|
73
|
+
whereNull(column) {
|
|
74
|
+
this._where.push(`${column} IS NULL`);
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Add WHERE NOT NULL condition
|
|
80
|
+
*/
|
|
81
|
+
whereNotNull(column) {
|
|
82
|
+
this._where.push(`${column} IS NOT NULL`);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Add ORDER BY clause
|
|
88
|
+
*/
|
|
89
|
+
orderBy(column, direction = 'ASC') {
|
|
90
|
+
this._orderBy.push(`${column} ${direction.toUpperCase()}`);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Set LIMIT
|
|
96
|
+
*/
|
|
97
|
+
limit(count) {
|
|
98
|
+
this._limit = count;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Set OFFSET
|
|
104
|
+
*/
|
|
105
|
+
offset(count) {
|
|
106
|
+
this._offset = count;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Add JOIN clause
|
|
112
|
+
*/
|
|
113
|
+
join(table, condition, type = 'INNER') {
|
|
114
|
+
this._joins.push(`${type} JOIN ${table} ON ${condition}`);
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Build SELECT query
|
|
120
|
+
*/
|
|
121
|
+
_buildSelect() {
|
|
122
|
+
if (!this._table) {
|
|
123
|
+
throw new Error('Table name is required');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const parts = [`SELECT ${this._select.join(', ')} FROM ${this._table}`];
|
|
127
|
+
|
|
128
|
+
// Add joins
|
|
129
|
+
if (this._joins.length > 0) {
|
|
130
|
+
parts.push(this._joins.join(' '));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Always include tenant_id in WHERE (critical for isolation)
|
|
134
|
+
const whereConditions = [`${this._table}.tenant_id = $1`, ...this._where];
|
|
135
|
+
parts.push(`WHERE ${whereConditions.join(' AND ')}`);
|
|
136
|
+
|
|
137
|
+
// Add ORDER BY
|
|
138
|
+
if (this._orderBy.length > 0) {
|
|
139
|
+
parts.push(`ORDER BY ${this._orderBy.join(', ')}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Add LIMIT and OFFSET
|
|
143
|
+
if (this._limit !== null) {
|
|
144
|
+
parts.push(`LIMIT ${this._limit}`);
|
|
145
|
+
}
|
|
146
|
+
if (this._offset !== null) {
|
|
147
|
+
parts.push(`OFFSET ${this._offset}`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
text: parts.join(' '),
|
|
152
|
+
params: [this.tenantId, ...this._whereParams]
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Execute SELECT and return all rows
|
|
158
|
+
*/
|
|
159
|
+
async get() {
|
|
160
|
+
const query = this._buildSelect();
|
|
161
|
+
const result = await this.db.query(query.text, query.params);
|
|
162
|
+
return result.rows;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Execute SELECT and return first row
|
|
167
|
+
*/
|
|
168
|
+
async first() {
|
|
169
|
+
this._limit = 1;
|
|
170
|
+
const rows = await this.get();
|
|
171
|
+
return rows[0] || null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Execute SELECT and return count
|
|
176
|
+
*/
|
|
177
|
+
async count() {
|
|
178
|
+
const originalSelect = this._select;
|
|
179
|
+
this._select = ['COUNT(*) as count'];
|
|
180
|
+
|
|
181
|
+
const query = this._buildSelect();
|
|
182
|
+
const result = await this.db.query(query.text, query.params);
|
|
183
|
+
|
|
184
|
+
this._select = originalSelect;
|
|
185
|
+
return parseInt(result.rows[0]?.count || 0, 10);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Find by ID (with tenant scoping)
|
|
190
|
+
*/
|
|
191
|
+
async find(id) {
|
|
192
|
+
return this.where('id', id).first();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Insert a new record
|
|
197
|
+
*/
|
|
198
|
+
async insert(data) {
|
|
199
|
+
if (!this._table) {
|
|
200
|
+
throw new Error('Table name is required');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Always include tenant_id
|
|
204
|
+
const dataWithTenant = { tenant_id: this.tenantId, ...data };
|
|
205
|
+
const columns = Object.keys(dataWithTenant);
|
|
206
|
+
const values = Object.values(dataWithTenant);
|
|
207
|
+
const placeholders = columns.map((_, i) => `$${i + 1}`);
|
|
208
|
+
|
|
209
|
+
const query = `
|
|
210
|
+
INSERT INTO ${this._table} (${columns.join(', ')})
|
|
211
|
+
VALUES (${placeholders.join(', ')})
|
|
212
|
+
RETURNING *
|
|
213
|
+
`;
|
|
214
|
+
|
|
215
|
+
const result = await this.db.query(query, values);
|
|
216
|
+
return result.rows[0];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Update records
|
|
221
|
+
*/
|
|
222
|
+
async update(data) {
|
|
223
|
+
if (!this._table) {
|
|
224
|
+
throw new Error('Table name is required');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Add updated_at timestamp
|
|
228
|
+
const dataWithTimestamp = { ...data, updated_at: new Date() };
|
|
229
|
+
const columns = Object.keys(dataWithTimestamp);
|
|
230
|
+
const values = Object.values(dataWithTimestamp);
|
|
231
|
+
|
|
232
|
+
// Build SET clause (params start at $2, $1 is tenant_id)
|
|
233
|
+
const setClause = columns.map((col, i) => `${col} = $${i + 2}`).join(', ');
|
|
234
|
+
|
|
235
|
+
// Build WHERE clause
|
|
236
|
+
const whereConditions = [`tenant_id = $1`, ...this._where];
|
|
237
|
+
|
|
238
|
+
const query = `
|
|
239
|
+
UPDATE ${this._table}
|
|
240
|
+
SET ${setClause}
|
|
241
|
+
WHERE ${whereConditions.join(' AND ')}
|
|
242
|
+
RETURNING *
|
|
243
|
+
`;
|
|
244
|
+
|
|
245
|
+
// Adjust where params (they now start after set params)
|
|
246
|
+
const adjustedWhereParams = this._whereParams.map((_, i) =>
|
|
247
|
+
this._where[i].replace(/\$(\d+)/g, (match, num) =>
|
|
248
|
+
`$${parseInt(num) + columns.length}`
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const result = await this.db.query(query, [this.tenantId, ...values, ...this._whereParams]);
|
|
253
|
+
return result.rows;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Delete records
|
|
258
|
+
*/
|
|
259
|
+
async delete() {
|
|
260
|
+
if (!this._table) {
|
|
261
|
+
throw new Error('Table name is required');
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Build WHERE clause (always includes tenant_id)
|
|
265
|
+
const whereConditions = [`tenant_id = $1`, ...this._where];
|
|
266
|
+
|
|
267
|
+
const query = `
|
|
268
|
+
DELETE FROM ${this._table}
|
|
269
|
+
WHERE ${whereConditions.join(' AND ')}
|
|
270
|
+
RETURNING *
|
|
271
|
+
`;
|
|
272
|
+
|
|
273
|
+
const result = await this.db.query(query, [this.tenantId, ...this._whereParams]);
|
|
274
|
+
return result.rows;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Create a query builder for a request (uses req.tenant)
|
|
280
|
+
*/
|
|
281
|
+
export function createQueryBuilder(db, req) {
|
|
282
|
+
if (!req.tenant || !req.tenant.id) {
|
|
283
|
+
throw new Error('Request must have tenant context');
|
|
284
|
+
}
|
|
285
|
+
return new QueryBuilder(db, req.tenant.id);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Query builder factory function
|
|
290
|
+
*/
|
|
291
|
+
export function query(db, tenantId) {
|
|
292
|
+
return new QueryBuilder(db, tenantId);
|
|
293
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Loader Utility
|
|
3
|
+
*
|
|
4
|
+
* Loads and mounts generated routes from a routes index file.
|
|
5
|
+
* This allows the generator to maintain route registrations without
|
|
6
|
+
* modifying the server entry point directly.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Mount routes from a generated routes module
|
|
11
|
+
*
|
|
12
|
+
* @param {Express} app - Express application instance
|
|
13
|
+
* @param {Object} routesModule - Module containing route definitions
|
|
14
|
+
* @param {string} basePath - Base path for all routes (default: '/api')
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // In server/index.js:
|
|
18
|
+
* import { mountGeneratedRoutes } from '@avento/framework/server';
|
|
19
|
+
* import * as generatedRoutes from './routes/generated/index.js';
|
|
20
|
+
*
|
|
21
|
+
* mountGeneratedRoutes(app, generatedRoutes);
|
|
22
|
+
*
|
|
23
|
+
* // The generated routes module exports:
|
|
24
|
+
* // export const routes = [
|
|
25
|
+
* // { path: '/tasks', router: taskRoutes },
|
|
26
|
+
* // { path: '/contacts', router: contactRoutes },
|
|
27
|
+
* // ];
|
|
28
|
+
*/
|
|
29
|
+
export function mountGeneratedRoutes(app, routesModule, basePath = '/api') {
|
|
30
|
+
const routes = routesModule.routes || [];
|
|
31
|
+
|
|
32
|
+
if (!Array.isArray(routes)) {
|
|
33
|
+
console.warn('[RouteLoader] routes module does not export a routes array');
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
for (const route of routes) {
|
|
38
|
+
if (!route.path || !route.router) {
|
|
39
|
+
console.warn('[RouteLoader] Invalid route definition:', route);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const fullPath = `${basePath}${route.path}`;
|
|
44
|
+
app.use(fullPath, route.router);
|
|
45
|
+
console.log(`[RouteLoader] Mounted ${fullPath}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log(`[RouteLoader] Loaded ${routes.length} route(s)`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Create a route registration function for use in generated files
|
|
53
|
+
* This is a helper used by generated code.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} path - Route path (e.g., '/tasks')
|
|
56
|
+
* @param {Router} router - Express router instance
|
|
57
|
+
* @returns {Object} Route definition object
|
|
58
|
+
*/
|
|
59
|
+
export function defineRoute(path, router) {
|
|
60
|
+
return { path, router };
|
|
61
|
+
}
|