@friggframework/core 2.0.0--canary.707.df80bfd.0 → 2.0.0--canary.680.d670696.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 +3 -0
- package/CLAUDE.md +0 -28
- package/README.md +0 -105
- package/application/commands/integration-commands.js +2 -2
- package/application/commands/scheduler-commands.js +7 -11
- package/application/commands/user-commands.js +5 -17
- package/core/Worker.js +7 -31
- package/core/create-handler.js +0 -9
- package/credential/use-cases/credential-operations-for-user.js +25 -110
- package/credential/use-cases/reauthorize-credential-for-user.js +8 -15
- package/database/config.js +24 -48
- package/database/documentdb-encryption-service.js +0 -1
- package/database/documentdb-utils.js +1 -6
- package/database/encryption/encryption-config.js +44 -13
- package/database/encryption/encryption-schema-registry.js +31 -88
- package/database/prisma.js +2 -4
- package/generated/prisma-mongodb/edge.js +4 -27
- package/generated/prisma-mongodb/index-browser.js +1 -24
- package/generated/prisma-mongodb/index.d.ts +48 -3228
- package/generated/prisma-mongodb/index.js +4 -27
- package/generated/prisma-mongodb/package.json +1 -1
- package/generated/prisma-mongodb/schema.prisma +0 -65
- package/generated/prisma-mongodb/wasm.js +4 -27
- package/generated/prisma-postgresql/edge.js +4 -27
- package/generated/prisma-postgresql/index-browser.js +1 -24
- package/generated/prisma-postgresql/index.d.ts +51 -3503
- package/generated/prisma-postgresql/index.js +4 -27
- package/generated/prisma-postgresql/package.json +1 -1
- package/generated/prisma-postgresql/schema.prisma +0 -61
- package/generated/prisma-postgresql/wasm.js +4 -27
- package/handlers/app-definition-loader.js +12 -110
- package/handlers/routers/auth.js +12 -40
- package/handlers/routers/health.js +2 -4
- package/handlers/routers/integration-defined-routers.js +92 -144
- package/handlers/routers/integration-webhook-routers.js +64 -102
- package/index.js +1 -64
- package/infrastructure/scheduler/index.js +6 -19
- package/infrastructure/scheduler/scheduler-service-factory.js +7 -55
- package/integrations/index.js +0 -5
- package/integrations/integration-router.js +73 -115
- package/integrations/repositories/integration-repository-documentdb.js +3 -35
- package/integrations/repositories/integration-repository-interface.js +1 -17
- package/integrations/repositories/integration-repository-mongo.js +76 -59
- package/integrations/repositories/integration-repository-postgres.js +76 -62
- package/integrations/tests/doubles/test-integration-repository.js +1 -12
- package/integrations/use-cases/create-integration.js +20 -55
- package/integrations/use-cases/get-integrations-for-user.js +40 -80
- package/integrations/use-cases/index.js +1 -7
- package/integrations/use-cases/integration-operations-for-user.js +24 -55
- package/integrations/utils/map-integration-dto.js +1 -44
- package/management-api/management-api-config.js +2 -71
- package/management-api/meta-router.js +2 -21
- package/management-api/openapi/components.js +3 -50
- package/management-api/openapi/v2-operations.js +5 -17
- package/management-api/route-registry.js +6 -123
- package/management-api/v2/dependencies.js +15 -40
- package/management-api/v2/dto.js +0 -20
- package/management-api/v2/handlers/authorization.js +0 -1
- package/management-api/v2/handlers/credentials.js +2 -6
- package/management-api/v2/handlers/entities.js +7 -10
- package/management-api/v2/handlers/integrations.js +3 -6
- package/management-api/v2/request.js +1 -32
- package/modules/index.js +0 -8
- package/modules/module-factory.js +0 -12
- package/modules/module.js +2 -53
- package/modules/proxy/proxy-errors.js +1 -12
- package/modules/proxy/proxy-path.js +3 -7
- package/modules/proxy/proxy-policy.js +5 -34
- package/modules/repositories/module-repository-documentdb.js +6 -51
- package/modules/repositories/module-repository-interface.js +2 -19
- package/modules/repositories/module-repository-mongo.js +74 -67
- package/modules/repositories/module-repository-postgres.js +74 -66
- package/modules/repositories/module-repository.js +1 -2
- package/modules/requester/proxy-request.js +1 -2
- package/modules/requester/requester.js +3 -8
- package/modules/use-cases/entity-operations-for-user.js +13 -50
- package/modules/use-cases/entity-types.js +7 -31
- package/modules/use-cases/execute-entity-proxy-request.js +15 -107
- package/modules/use-cases/get-authorization-requirements.js +2 -2
- package/modules/use-cases/get-entities-for-user.js +13 -26
- package/modules/use-cases/get-entity-module-for-user.js +7 -34
- package/modules/use-cases/get-entity-options-by-id.js +4 -10
- package/modules/use-cases/get-entity-options-by-type.js +1 -1
- package/modules/use-cases/get-module-instance-from-type.js +1 -1
- package/modules/use-cases/get-module.js +4 -10
- package/modules/use-cases/multi-step-authorization.js +13 -40
- package/modules/use-cases/process-authorization-callback.js +19 -53
- package/modules/use-cases/refresh-entity-options.js +4 -10
- package/modules/use-cases/test-module-auth.js +4 -10
- package/package.json +5 -6
- package/prisma-mongodb/schema.prisma +0 -65
- package/prisma-postgresql/schema.prisma +0 -61
- package/queues/index.js +0 -25
- package/types/index.d.ts +0 -1
- package/types/modules/index.d.ts +1 -157
- package/user/repositories/user-repository-documentdb.js +2 -27
- package/user/repositories/user-repository-interface.js +0 -18
- package/user/repositories/user-repository-mongo.js +0 -24
- package/user/repositories/user-repository-postgres.js +0 -18
- package/user/use-cases/authenticate-user.js +1 -7
- package/user/use-cases/get-user-from-adopter-jwt.js +124 -95
- package/user/use-cases/get-user-from-x-frigg-headers.js +80 -83
- package/user/user.js +1 -64
- package/database/organization-where.js +0 -18
- package/database/unique-violation.js +0 -25
- package/extensions/bootstrap-runner.js +0 -43
- package/extensions/extension-loader.js +0 -230
- package/extensions/index.js +0 -44
- package/extensions/initialize-app.js +0 -123
- package/extensions/prisma-composer.js +0 -270
- package/extensions/route-mounter.js +0 -103
- package/extensions/schema-composer.js +0 -188
- package/infrastructure/scheduler/netlify-scheduler-adapter.js +0 -216
- package/infrastructure/scheduler/scheduled-job-repository.js +0 -118
- package/integrations/repositories/integration-not-found.js +0 -14
- package/integrations/schedule-config.js +0 -80
- package/integrations/use-cases/dispatch-ongoing-syncs.js +0 -100
- package/integrations/use-cases/resolve-integration-ownership.js +0 -177
- package/modules/entity-scope.js +0 -15
- package/modules/proxy/proxy-bases.js +0 -169
- package/modules/repositories/entity-not-found.js +0 -18
- package/modules/scope-config.js +0 -192
- package/modules/scope-policy.js +0 -112
- package/modules/scope-resolution.js +0 -288
- package/modules/scope-visibility.js +0 -247
- package/modules/tests/doubles/ownership-matching.js +0 -18
- package/modules/use-cases/authorize-entity-for-user.js +0 -61
- package/modules/use-cases/create-entity-from-credential.js +0 -113
- package/modules/use-cases/manage-access.js +0 -21
- package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +0 -26
- package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +0 -25
- package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +0 -28
- package/providers/index.js +0 -23
- package/providers/resolve-provider.js +0 -93
- package/providers/runtime-detection.js +0 -124
- package/queues/providers/index.js +0 -11
- package/queues/providers/netlify-background-provider.js +0 -150
- package/queues/providers/qstash-queue-provider.js +0 -219
- package/queues/providers/sqs-queue-provider.js +0 -70
- package/queues/queue-client-interface.js +0 -55
- package/queues/queue-message-auth.js +0 -193
- package/queues/queue-provider-factory.js +0 -101
- package/queues/queue-provider.js +0 -65
- package/queues/sqs-queue-client.js +0 -81
- package/types/user/index.d.ts +0 -134
- package/user/adopter-jwt-config.js +0 -177
- package/user/adopter-jwt-errors.js +0 -59
- package/user/adopter-token-verifier.js +0 -122
- package/user/app-user-helpers.js +0 -97
- package/user/create-user-authentication.js +0 -64
- package/user/membership.js +0 -51
- package/user/repositories/organization-membership-record.js +0 -19
- package/user/repositories/organization-membership-repository-documentdb.js +0 -106
- package/user/repositories/organization-membership-repository-factory.js +0 -42
- package/user/repositories/organization-membership-repository-interface.js +0 -57
- package/user/repositories/organization-membership-repository-mongo.js +0 -13
- package/user/repositories/organization-membership-repository-postgres.js +0 -29
- package/user/repositories/organization-membership-repository-prisma.js +0 -81
- package/user/tests/doubles/adopter-jwt-fixtures.js +0 -220
- package/user/use-cases/ensure-organization-membership.js +0 -92
- package/user/use-cases/find-individual-users-by-organization.js +0 -54
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extension Loader
|
|
3
|
-
*
|
|
4
|
-
* Validates and normalizes the core extensions of the app definition
|
|
5
|
-
* (`appDefinition.coreExtensions`, an array). Core extensions add custom
|
|
6
|
-
* Prisma models, encryption schemas, admin routes and bootstrap logic to a
|
|
7
|
-
* Frigg app. The key is `coreExtensions`, not `extensions`: ADR-017 keeps
|
|
8
|
-
* `appDefinition.extensions` for its keyed object form.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* const extensions = loadExtensions(appDefinition);
|
|
12
|
-
* // extensions is a normalized array, safe to iterate
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const path = require('path');
|
|
16
|
-
const {
|
|
17
|
-
coreExtensionRoutePathError,
|
|
18
|
-
} = require('../management-api/route-registry');
|
|
19
|
-
|
|
20
|
-
const SCHEMA_DB_TYPES = ['postgresql', 'mongodb'];
|
|
21
|
-
|
|
22
|
-
function schemaErrors(schema, label) {
|
|
23
|
-
if (typeof schema === 'string') {
|
|
24
|
-
return schema.endsWith('.prisma') ? [] : [`${label}: must end with .prisma`];
|
|
25
|
-
}
|
|
26
|
-
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
|
|
27
|
-
return [`${label}: must be a string (path to .prisma file) or { postgresql, mongodb } paths`];
|
|
28
|
-
}
|
|
29
|
-
const errors = [];
|
|
30
|
-
const keys = Object.keys(schema);
|
|
31
|
-
if (keys.length === 0) errors.push(`${label}: names no database type`);
|
|
32
|
-
for (const key of keys) {
|
|
33
|
-
if (!SCHEMA_DB_TYPES.includes(key)) {
|
|
34
|
-
errors.push(`${label}.${key}: unknown database type (expected ${SCHEMA_DB_TYPES.join(' or ')})`);
|
|
35
|
-
} else if (typeof schema[key] !== 'string' || !schema[key].endsWith('.prisma')) {
|
|
36
|
-
errors.push(`${label}.${key}: must be a path ending in .prisma`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return errors;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Validates a single extension definition.
|
|
44
|
-
* @param {Object} ext - Raw extension config from app definition
|
|
45
|
-
* @param {number} index - Position in the coreExtensions array (for error messages)
|
|
46
|
-
* @returns {{valid: boolean, errors: string[]}}
|
|
47
|
-
*/
|
|
48
|
-
function validateExtension(ext, index) {
|
|
49
|
-
const prefix = `coreExtensions[${index}]`;
|
|
50
|
-
const errors = [];
|
|
51
|
-
|
|
52
|
-
if (!ext || typeof ext !== 'object') {
|
|
53
|
-
errors.push(`${prefix}: must be an object`);
|
|
54
|
-
return { valid: false, errors };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!ext.name || typeof ext.name !== 'string') {
|
|
58
|
-
errors.push(`${prefix}.name: required string`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// schema is optional: a .prisma file for every database, or one per
|
|
62
|
-
// database type ({ postgresql, mongodb }), since a model written for one
|
|
63
|
-
// (an autoincrement Int id, a String[] column) does not compile for the other.
|
|
64
|
-
if (ext.schema !== undefined) {
|
|
65
|
-
errors.push(...schemaErrors(ext.schema, `${prefix}.schema`));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// migrations is optional: a directory of Prisma migration folders
|
|
69
|
-
// (<timestamp>_<name>/migration.sql) for the extension's PostgreSQL models.
|
|
70
|
-
// They are applied with the core migrations. MongoDB has no migrations.
|
|
71
|
-
if (ext.migrations !== undefined && (typeof ext.migrations !== 'string' || ext.migrations === '')) {
|
|
72
|
-
errors.push(`${prefix}.migrations: must be a string (path to a migrations directory)`);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// encryption is optional — object with model names → { fields: [...] }
|
|
76
|
-
if (ext.encryption !== undefined) {
|
|
77
|
-
if (!ext.encryption || typeof ext.encryption !== 'object') {
|
|
78
|
-
errors.push(`${prefix}.encryption: must be an object`);
|
|
79
|
-
} else {
|
|
80
|
-
for (const [model, config] of Object.entries(ext.encryption)) {
|
|
81
|
-
if (!config || !Array.isArray(config.fields)) {
|
|
82
|
-
errors.push(
|
|
83
|
-
`${prefix}.encryption.${model}: must have a "fields" array`
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// routes is optional — { path, handler }
|
|
91
|
-
if (ext.routes !== undefined) {
|
|
92
|
-
if (!ext.routes || typeof ext.routes !== 'object') {
|
|
93
|
-
errors.push(`${prefix}.routes: must be an object with { path, handler }`);
|
|
94
|
-
} else {
|
|
95
|
-
if (!ext.routes.path || typeof ext.routes.path !== 'string') {
|
|
96
|
-
errors.push(`${prefix}.routes.path: required string`);
|
|
97
|
-
} else {
|
|
98
|
-
const pathError = coreExtensionRoutePathError(ext.routes.path);
|
|
99
|
-
if (pathError) {
|
|
100
|
-
errors.push(`${prefix}.routes.path: ${pathError}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
if (!ext.routes.handler) {
|
|
104
|
-
errors.push(`${prefix}.routes.handler: required (function or module path)`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// bootstrap is optional — function or module path
|
|
110
|
-
if (ext.bootstrap !== undefined) {
|
|
111
|
-
if (typeof ext.bootstrap !== 'function' && typeof ext.bootstrap !== 'string') {
|
|
112
|
-
errors.push(
|
|
113
|
-
`${prefix}.bootstrap: must be a function or a string (module path)`
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return { valid: errors.length === 0, errors };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Loads and validates extensions from the app definition.
|
|
123
|
-
*
|
|
124
|
-
* @param {Object} appDefinition - The full app definition
|
|
125
|
-
* @returns {Array<Object>} Normalized extensions array (empty if none)
|
|
126
|
-
* @throws {Error} If any extension fails validation
|
|
127
|
-
*/
|
|
128
|
-
function loadExtensions(appDefinition) {
|
|
129
|
-
if (!appDefinition) return [];
|
|
130
|
-
|
|
131
|
-
// The array this loader reads used to be `appDefinition.extensions`;
|
|
132
|
-
// that key now belongs to ADR-017's keyed object. An array there is the
|
|
133
|
-
// old spelling: stop at load rather than silently dropping the extensions.
|
|
134
|
-
if (Array.isArray(appDefinition.extensions)) {
|
|
135
|
-
throw new Error(
|
|
136
|
-
'appDefinition.extensions (array) was renamed to appDefinition.coreExtensions; ' +
|
|
137
|
-
'move the array to coreExtensions'
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const raw = appDefinition.coreExtensions;
|
|
142
|
-
if (!raw) return [];
|
|
143
|
-
|
|
144
|
-
if (!Array.isArray(raw)) {
|
|
145
|
-
throw new Error('appDefinition.coreExtensions must be an array');
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (raw.length === 0) return [];
|
|
149
|
-
|
|
150
|
-
const allErrors = [];
|
|
151
|
-
|
|
152
|
-
const normalized = raw.map((ext, index) => {
|
|
153
|
-
const { valid, errors } = validateExtension(ext, index);
|
|
154
|
-
if (!valid) {
|
|
155
|
-
allErrors.push(...errors);
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Normalize bootstrap — resolve string paths to functions
|
|
160
|
-
let bootstrapFn = null;
|
|
161
|
-
if (typeof ext.bootstrap === 'function') {
|
|
162
|
-
bootstrapFn = ext.bootstrap;
|
|
163
|
-
} else if (typeof ext.bootstrap === 'string') {
|
|
164
|
-
bootstrapFn = require(ext.bootstrap);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Normalize route handler — resolve string paths to functions
|
|
168
|
-
let routeHandler = null;
|
|
169
|
-
if (ext.routes) {
|
|
170
|
-
if (typeof ext.routes.handler === 'function') {
|
|
171
|
-
routeHandler = ext.routes.handler;
|
|
172
|
-
} else if (typeof ext.routes.handler === 'string') {
|
|
173
|
-
routeHandler = require(ext.routes.handler);
|
|
174
|
-
} else {
|
|
175
|
-
routeHandler = ext.routes.handler;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return {
|
|
180
|
-
name: ext.name,
|
|
181
|
-
schema: ext.schema || null,
|
|
182
|
-
migrations: ext.migrations || null,
|
|
183
|
-
encryption: ext.encryption || null,
|
|
184
|
-
routes: ext.routes
|
|
185
|
-
? { path: ext.routes.path, handler: routeHandler }
|
|
186
|
-
: null,
|
|
187
|
-
bootstrap: bootstrapFn,
|
|
188
|
-
};
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
if (allErrors.length > 0) {
|
|
192
|
-
throw new Error(
|
|
193
|
-
`Invalid extension configuration:\n- ${allErrors.join('\n- ')}`
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return normalized.filter(Boolean);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* The .prisma file an extension adds for one database type.
|
|
202
|
-
* @param {Object} ext - Normalized extension
|
|
203
|
-
* @param {'postgresql'|'mongodb'} [dbType] - Omitted: a string schema only
|
|
204
|
-
* @returns {string|null}
|
|
205
|
-
*/
|
|
206
|
-
function extensionSchemaPath(ext, dbType) {
|
|
207
|
-
if (!ext?.schema) return null;
|
|
208
|
-
if (typeof ext.schema === 'string') return ext.schema;
|
|
209
|
-
return dbType ? ext.schema[dbType] || null : null;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Get extension schema file paths from loaded extensions.
|
|
214
|
-
* @param {Array<Object>} extensions - Normalized extensions
|
|
215
|
-
* @param {'postgresql'|'mongodb'} [dbType] - Database the paths are for. A
|
|
216
|
-
* per-database schema ({ postgresql, mongodb }) only answers when given one.
|
|
217
|
-
* @returns {string[]} Array of absolute paths to .prisma schema files
|
|
218
|
-
*/
|
|
219
|
-
function getExtensionSchemaPaths(extensions, dbType) {
|
|
220
|
-
return extensions
|
|
221
|
-
.map((ext) => extensionSchemaPath(ext, dbType))
|
|
222
|
-
.filter(Boolean);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
module.exports = {
|
|
226
|
-
loadExtensions,
|
|
227
|
-
validateExtension,
|
|
228
|
-
getExtensionSchemaPaths,
|
|
229
|
-
extensionSchemaPath,
|
|
230
|
-
};
|
package/extensions/index.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Frigg Extension System
|
|
3
|
-
*
|
|
4
|
-
* Extensions allow integration developers to extend Frigg Core with:
|
|
5
|
-
* - Custom Prisma models (schema composition)
|
|
6
|
-
* - Encrypted fields (encryption registry merge)
|
|
7
|
-
* - Admin API routes (route mounting)
|
|
8
|
-
* - Bootstrap lifecycle hooks (credential loading, etc.)
|
|
9
|
-
*
|
|
10
|
-
* @module @friggframework/core/extensions
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const { loadExtensions, validateExtension, getExtensionSchemaPaths } = require('./extension-loader');
|
|
14
|
-
const { composeSchemas, extractModelBlocks, getSchemaFilePaths } = require('./schema-composer');
|
|
15
|
-
const { composeExtensionPrisma, resolvePrismaDbTypes } = require('./prisma-composer');
|
|
16
|
-
const {
|
|
17
|
-
mountExtensionRoutes,
|
|
18
|
-
createExtensionRoutesRouter,
|
|
19
|
-
} = require('./route-mounter');
|
|
20
|
-
const { runExtensionBootstraps } = require('./bootstrap-runner');
|
|
21
|
-
const { initializeApp, resetInitialization } = require('./initialize-app');
|
|
22
|
-
|
|
23
|
-
module.exports = {
|
|
24
|
-
// Extension loading & validation
|
|
25
|
-
loadExtensions,
|
|
26
|
-
validateExtension,
|
|
27
|
-
getExtensionSchemaPaths,
|
|
28
|
-
|
|
29
|
-
// Schema composition
|
|
30
|
-
composeSchemas,
|
|
31
|
-
extractModelBlocks,
|
|
32
|
-
getSchemaFilePaths,
|
|
33
|
-
composeExtensionPrisma,
|
|
34
|
-
resolvePrismaDbTypes,
|
|
35
|
-
|
|
36
|
-
// Route mounting
|
|
37
|
-
mountExtensionRoutes,
|
|
38
|
-
createExtensionRoutesRouter,
|
|
39
|
-
|
|
40
|
-
// Bootstrap lifecycle
|
|
41
|
-
runExtensionBootstraps,
|
|
42
|
-
initializeApp,
|
|
43
|
-
resetInitialization,
|
|
44
|
-
};
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Application Initialization with Extension Lifecycle
|
|
3
|
-
*
|
|
4
|
-
* Provides an async initialization function that runs extension bootstraps
|
|
5
|
-
* after database connection but before router creation. This is the integration
|
|
6
|
-
* point for providers (Lambda, Netlify) to hook into the extension lifecycle.
|
|
7
|
-
*
|
|
8
|
-
* Call `initializeApp()` during the async startup phase of your handler/function.
|
|
9
|
-
* It is safe to call multiple times — subsequent calls are no-ops.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* const { initializeApp } = require('@friggframework/core/extensions/initialize-app');
|
|
13
|
-
*
|
|
14
|
-
* // In a Lambda handler or Netlify function:
|
|
15
|
-
* await initializeApp();
|
|
16
|
-
* // Now extensions are bootstrapped and router can be created
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const { loadExtensions } = require('./extension-loader');
|
|
20
|
-
const { runExtensionBootstraps } = require('./bootstrap-runner');
|
|
21
|
-
|
|
22
|
-
let _initialized = false;
|
|
23
|
-
let _initPromise = null;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Initializes extensions by running bootstrap hooks.
|
|
27
|
-
*
|
|
28
|
-
* This function:
|
|
29
|
-
* 1. Loads the app definition
|
|
30
|
-
* 2. Loads and validates extensions
|
|
31
|
-
* 3. Runs extension bootstrap hooks in sequence
|
|
32
|
-
*
|
|
33
|
-
* Bootstrap hooks receive (prisma, appDefinition) and can:
|
|
34
|
-
* - Load credentials from database into integration Definition.env
|
|
35
|
-
* - Register services or middleware
|
|
36
|
-
* - Perform any one-time async initialization
|
|
37
|
-
*
|
|
38
|
-
* @param {Object} [options]
|
|
39
|
-
* @param {Object} [options.appDefinition] - Override app definition (skips loadAppDefinition)
|
|
40
|
-
* @param {Object} [options.prisma] - Override Prisma client instance
|
|
41
|
-
* @param {boolean} [options.force=false] - Force re-initialization
|
|
42
|
-
* @returns {Promise<{extensions: Array}>} The loaded extensions
|
|
43
|
-
*/
|
|
44
|
-
async function initializeApp(options = {}) {
|
|
45
|
-
const { force = false } = options;
|
|
46
|
-
|
|
47
|
-
// Return cached promise if already initializing (prevent race conditions)
|
|
48
|
-
if (_initPromise && !force) {
|
|
49
|
-
return _initPromise;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (_initialized && !force) {
|
|
53
|
-
return { extensions: [] };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
_initPromise = _doInitialize(options);
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
const result = await _initPromise;
|
|
60
|
-
_initialized = true;
|
|
61
|
-
return result;
|
|
62
|
-
} catch (error) {
|
|
63
|
-
_initPromise = null;
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function _doInitialize(options = {}) {
|
|
69
|
-
let { appDefinition, prisma } = options;
|
|
70
|
-
|
|
71
|
-
// Load app definition if not provided
|
|
72
|
-
if (!appDefinition) {
|
|
73
|
-
try {
|
|
74
|
-
const { loadAppDefinition } = require('../handlers/app-definition-loader');
|
|
75
|
-
const loaded = loadAppDefinition();
|
|
76
|
-
appDefinition = loaded.appDefinition;
|
|
77
|
-
} catch {
|
|
78
|
-
// No app definition available — skip extension bootstrap
|
|
79
|
-
return { extensions: [] };
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (!appDefinition) {
|
|
84
|
-
return { extensions: [] };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Load extensions
|
|
88
|
-
const extensions = loadExtensions(appDefinition);
|
|
89
|
-
|
|
90
|
-
if (extensions.length === 0) {
|
|
91
|
-
return { extensions };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Get Prisma client if not provided
|
|
95
|
-
if (!prisma) {
|
|
96
|
-
try {
|
|
97
|
-
const db = require('../database/prisma');
|
|
98
|
-
prisma = db.prisma;
|
|
99
|
-
} catch {
|
|
100
|
-
// Prisma not available — skip bootstrap
|
|
101
|
-
console.warn('Prisma client not available, skipping extension bootstraps');
|
|
102
|
-
return { extensions };
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Run bootstrap hooks
|
|
107
|
-
await runExtensionBootstraps(extensions, { prisma, appDefinition });
|
|
108
|
-
|
|
109
|
-
return { extensions };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Reset initialization state (for testing).
|
|
114
|
-
*/
|
|
115
|
-
function resetInitialization() {
|
|
116
|
-
_initialized = false;
|
|
117
|
-
_initPromise = null;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
module.exports = {
|
|
121
|
-
initializeApp,
|
|
122
|
-
resetInitialization,
|
|
123
|
-
};
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Prisma composer: puts core extensions' models into the Prisma client.
|
|
3
|
-
*
|
|
4
|
-
* Core ships one generated client per database (`generated/prisma-<db>`),
|
|
5
|
-
* built from `prisma-<db>/schema.prisma`. A core extension that declares a
|
|
6
|
-
* schema (`coreExtensions[].schema`) needs its models in that client, and on
|
|
7
|
-
* PostgreSQL its tables need migrations (`coreExtensions[].migrations`).
|
|
8
|
-
* Without this step the extension's models exist nowhere: every call to
|
|
9
|
-
* `prisma.<model>` is undefined, and the table is never created.
|
|
10
|
-
*
|
|
11
|
-
* At build time, for each database the app uses:
|
|
12
|
-
* 1. The pristine core schema is kept as `schema.base.prisma` (taken from
|
|
13
|
-
* `schema.prisma` the first time), so composing again never appends twice.
|
|
14
|
-
* 2. `schema.prisma` becomes base + the extensions' models. Everything that
|
|
15
|
-
* reads the schema (prisma generate, the migration runner, the Lambda
|
|
16
|
-
* layer, which copies the generated client's schema) sees the composed one.
|
|
17
|
-
* 3. PostgreSQL: each extension migration folder is copied into
|
|
18
|
-
* `prisma-postgresql/migrations`, so `prisma migrate deploy` applies it in
|
|
19
|
-
* timestamp order with the core migrations.
|
|
20
|
-
* 4. `prisma generate` rebuilds `generated/prisma-<db>` from the composed
|
|
21
|
-
* schema, only when the schema changed or the client is missing.
|
|
22
|
-
*
|
|
23
|
-
* Removing every extension restores the base schema and regenerates. Copied
|
|
24
|
-
* migrations are left in place: a migration that may already be applied must
|
|
25
|
-
* not disappear from the history.
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
const fs = require('fs');
|
|
29
|
-
const path = require('path');
|
|
30
|
-
const { execFileSync } = require('child_process');
|
|
31
|
-
const {
|
|
32
|
-
loadExtensions,
|
|
33
|
-
getExtensionSchemaPaths,
|
|
34
|
-
} = require('./extension-loader');
|
|
35
|
-
const { extractModelBlocks } = require('./schema-composer');
|
|
36
|
-
|
|
37
|
-
const DB_TYPES = ['postgresql', 'mongodb'];
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The Prisma databases an app definition uses. DocumentDB uses the MongoDB client.
|
|
41
|
-
* @param {Object} appDefinition
|
|
42
|
-
* @returns {Array<'postgresql'|'mongodb'>}
|
|
43
|
-
*/
|
|
44
|
-
function resolvePrismaDbTypes(appDefinition) {
|
|
45
|
-
const db = appDefinition?.database || {};
|
|
46
|
-
const types = [];
|
|
47
|
-
if (db.postgres?.enable) types.push('postgresql');
|
|
48
|
-
if (db.mongoDB?.enable || db.documentDB?.enable) types.push('mongodb');
|
|
49
|
-
return types;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function blockNames(schemaContent) {
|
|
53
|
-
const names = [];
|
|
54
|
-
const re = /^\s*(model|enum|type|view)\s+(\w+)\s*\{/gm;
|
|
55
|
-
let match;
|
|
56
|
-
while ((match = re.exec(schemaContent)) !== null) names.push(match[2]);
|
|
57
|
-
return names;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Base schema + the extensions' model blocks. Throws when an extension
|
|
62
|
-
* declares a model, enum or type the base (or another extension) already has.
|
|
63
|
-
*/
|
|
64
|
-
function buildComposedSchema(baseContent, extensionSchemaPaths) {
|
|
65
|
-
const seen = new Map(
|
|
66
|
-
blockNames(baseContent).map((name) => [name, 'the core schema'])
|
|
67
|
-
);
|
|
68
|
-
const sections = [];
|
|
69
|
-
|
|
70
|
-
for (const extPath of extensionSchemaPaths) {
|
|
71
|
-
if (!fs.existsSync(extPath)) {
|
|
72
|
-
throw new Error(`Extension schema not found: ${extPath}`);
|
|
73
|
-
}
|
|
74
|
-
const blocks = extractModelBlocks(fs.readFileSync(extPath, 'utf-8'));
|
|
75
|
-
if (!blocks) continue;
|
|
76
|
-
for (const name of blockNames(blocks)) {
|
|
77
|
-
if (seen.has(name)) {
|
|
78
|
-
throw new Error(
|
|
79
|
-
`Extension schema ${extPath} declares "${name}", already declared by ${seen.get(
|
|
80
|
-
name
|
|
81
|
-
)}`
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
seen.set(name, extPath);
|
|
85
|
-
}
|
|
86
|
-
sections.push(`// --- Extension schema: ${extPath} ---`, blocks, '');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (sections.length === 0) return baseContent;
|
|
90
|
-
|
|
91
|
-
return [
|
|
92
|
-
baseContent.trimEnd(),
|
|
93
|
-
'',
|
|
94
|
-
'// =========================================================================',
|
|
95
|
-
'// Extension models (composed by @friggframework/core at build; do not edit)',
|
|
96
|
-
'// =========================================================================',
|
|
97
|
-
'',
|
|
98
|
-
...sections,
|
|
99
|
-
].join('\n');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Copies each extension's migration folders into the core migrations
|
|
104
|
-
* directory. A folder already there with the same migration.sql is skipped;
|
|
105
|
-
* a different one under the same name is an error (it would silently be a
|
|
106
|
-
* different migration with an applied migration's name).
|
|
107
|
-
* @returns {string[]} names of the folders copied
|
|
108
|
-
*/
|
|
109
|
-
function copyExtensionMigrations(extensions, migrationsDir) {
|
|
110
|
-
const copied = [];
|
|
111
|
-
for (const ext of extensions) {
|
|
112
|
-
if (!ext.migrations) continue;
|
|
113
|
-
if (
|
|
114
|
-
!fs.existsSync(ext.migrations) ||
|
|
115
|
-
!fs.statSync(ext.migrations).isDirectory()
|
|
116
|
-
) {
|
|
117
|
-
throw new Error(
|
|
118
|
-
`Extension "${ext.name}" migrations directory not found: ${ext.migrations}`
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
const folders = fs
|
|
122
|
-
.readdirSync(ext.migrations, { withFileTypes: true })
|
|
123
|
-
.filter((entry) => entry.isDirectory())
|
|
124
|
-
.map((entry) => entry.name)
|
|
125
|
-
.sort();
|
|
126
|
-
for (const folder of folders) {
|
|
127
|
-
const source = path.join(ext.migrations, folder, 'migration.sql');
|
|
128
|
-
if (!fs.existsSync(source)) {
|
|
129
|
-
throw new Error(
|
|
130
|
-
`Extension "${ext.name}" migration ${folder} has no migration.sql`
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
const sql = fs.readFileSync(source, 'utf-8');
|
|
134
|
-
const destDir = path.join(migrationsDir, folder);
|
|
135
|
-
const dest = path.join(destDir, 'migration.sql');
|
|
136
|
-
if (fs.existsSync(dest)) {
|
|
137
|
-
if (fs.readFileSync(dest, 'utf-8') !== sql) {
|
|
138
|
-
throw new Error(
|
|
139
|
-
`Migration ${folder} from extension "${ext.name}" conflicts with an existing migration of the same name`
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
fs.mkdirSync(destDir, { recursive: true });
|
|
145
|
-
fs.writeFileSync(dest, sql, 'utf-8');
|
|
146
|
-
copied.push(folder);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return copied;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Runs `prisma generate` with the Prisma CLI installed beside core (the app's
|
|
154
|
-
* own `prisma`), falling back to npx.
|
|
155
|
-
*/
|
|
156
|
-
function defaultGenerate({ schemaPath, corePath }) {
|
|
157
|
-
let cli;
|
|
158
|
-
try {
|
|
159
|
-
cli = require.resolve('prisma/build/index.js', {
|
|
160
|
-
paths: [corePath, process.cwd()],
|
|
161
|
-
});
|
|
162
|
-
} catch {
|
|
163
|
-
cli = null;
|
|
164
|
-
}
|
|
165
|
-
const [command, args] = cli
|
|
166
|
-
? [process.execPath, [cli, 'generate', `--schema=${schemaPath}`]]
|
|
167
|
-
: ['npx', ['prisma', 'generate', `--schema=${schemaPath}`]];
|
|
168
|
-
execFileSync(command, args, { cwd: corePath, stdio: 'inherit' });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Composes the extensions' schemas and migrations into core's Prisma schema
|
|
173
|
-
* and regenerates the client when it changed.
|
|
174
|
-
*
|
|
175
|
-
* @param {Object} params
|
|
176
|
-
* @param {Object} params.appDefinition
|
|
177
|
-
* @param {string} params.corePath - Directory of the installed @friggframework/core
|
|
178
|
-
* @param {Array<'postgresql'|'mongodb'>} [params.dbTypes] - Defaults to the app definition's databases
|
|
179
|
-
* @param {Function} [params.generate] - ({ schemaPath, corePath, dbType }) => void
|
|
180
|
-
* @param {Function} [params.log]
|
|
181
|
-
* @returns {{ changed: boolean, results: Array<Object> }}
|
|
182
|
-
*/
|
|
183
|
-
function composeExtensionPrisma({
|
|
184
|
-
appDefinition,
|
|
185
|
-
corePath,
|
|
186
|
-
dbTypes,
|
|
187
|
-
generate = defaultGenerate,
|
|
188
|
-
log = (message) => console.log(message),
|
|
189
|
-
}) {
|
|
190
|
-
if (!corePath) throw new Error('corePath is required');
|
|
191
|
-
const extensions = loadExtensions(appDefinition);
|
|
192
|
-
const types = (dbTypes || resolvePrismaDbTypes(appDefinition)).filter((t) =>
|
|
193
|
-
DB_TYPES.includes(t)
|
|
194
|
-
);
|
|
195
|
-
const results = [];
|
|
196
|
-
|
|
197
|
-
for (const dbType of types) {
|
|
198
|
-
const schemaDir = path.join(corePath, `prisma-${dbType}`);
|
|
199
|
-
const schemaPath = path.join(schemaDir, 'schema.prisma');
|
|
200
|
-
const basePath = path.join(schemaDir, 'schema.base.prisma');
|
|
201
|
-
if (!fs.existsSync(schemaPath)) {
|
|
202
|
-
throw new Error(`Core Prisma schema not found: ${schemaPath}`);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const extensionSchemas = getExtensionSchemaPaths(extensions, dbType);
|
|
206
|
-
// Migrations do not depend on a schema: an extension may ship only
|
|
207
|
-
// SQL (a table it reads with raw queries), and it must still be applied.
|
|
208
|
-
const migrations =
|
|
209
|
-
dbType === 'postgresql'
|
|
210
|
-
? copyExtensionMigrations(
|
|
211
|
-
extensions,
|
|
212
|
-
path.join(schemaDir, 'migrations')
|
|
213
|
-
)
|
|
214
|
-
: [];
|
|
215
|
-
if (migrations.length > 0) {
|
|
216
|
-
log(`[Frigg] Added extension migrations: ${migrations.join(', ')}`);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// No schema to compose and none composed before: the schema and the
|
|
220
|
-
// client stay exactly as core shipped them.
|
|
221
|
-
if (extensionSchemas.length === 0 && !fs.existsSync(basePath)) {
|
|
222
|
-
results.push({
|
|
223
|
-
dbType,
|
|
224
|
-
changed: migrations.length > 0,
|
|
225
|
-
migrations,
|
|
226
|
-
});
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (!fs.existsSync(basePath)) fs.copyFileSync(schemaPath, basePath);
|
|
231
|
-
const target = buildComposedSchema(
|
|
232
|
-
fs.readFileSync(basePath, 'utf-8'),
|
|
233
|
-
extensionSchemas
|
|
234
|
-
);
|
|
235
|
-
const current = fs.readFileSync(schemaPath, 'utf-8');
|
|
236
|
-
const schemaChanged = current !== target;
|
|
237
|
-
if (schemaChanged) fs.writeFileSync(schemaPath, target, 'utf-8');
|
|
238
|
-
|
|
239
|
-
const clientPath = path.join(
|
|
240
|
-
corePath,
|
|
241
|
-
'generated',
|
|
242
|
-
`prisma-${dbType}`,
|
|
243
|
-
'schema.prisma'
|
|
244
|
-
);
|
|
245
|
-
const clientStale =
|
|
246
|
-
!fs.existsSync(clientPath) ||
|
|
247
|
-
fs.readFileSync(clientPath, 'utf-8') !== target;
|
|
248
|
-
|
|
249
|
-
if (schemaChanged || clientStale) {
|
|
250
|
-
log(
|
|
251
|
-
`[Frigg] Composing ${extensionSchemas.length} extension schema(s) into the ${dbType} Prisma client`
|
|
252
|
-
);
|
|
253
|
-
generate({ schemaPath, corePath, dbType });
|
|
254
|
-
}
|
|
255
|
-
results.push({
|
|
256
|
-
dbType,
|
|
257
|
-
changed: schemaChanged || clientStale || migrations.length > 0,
|
|
258
|
-
migrations,
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return { changed: results.some((r) => r.changed), results };
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
module.exports = {
|
|
266
|
-
composeExtensionPrisma,
|
|
267
|
-
resolvePrismaDbTypes,
|
|
268
|
-
buildComposedSchema,
|
|
269
|
-
copyExtensionMigrations,
|
|
270
|
-
};
|