@opensaas/stack-core 0.20.1 → 0.22.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +334 -0
- package/CLAUDE.md +29 -11
- package/dist/access/access-filter.d.ts +29 -0
- package/dist/access/access-filter.d.ts.map +1 -0
- package/dist/access/access-filter.js +68 -0
- package/dist/access/access-filter.js.map +1 -0
- package/dist/access/engine.d.ts +15 -48
- package/dist/access/engine.d.ts.map +1 -1
- package/dist/access/engine.js +14 -280
- package/dist/access/engine.js.map +1 -1
- package/dist/access/field-access.d.ts +44 -0
- package/dist/access/field-access.d.ts.map +1 -0
- package/dist/access/field-access.js +123 -0
- package/dist/access/field-access.js.map +1 -0
- package/dist/access/field-access.test.d.ts +2 -0
- package/dist/access/field-access.test.d.ts.map +1 -0
- package/dist/access/{engine.test.js → field-access.test.js} +2 -2
- package/dist/access/field-access.test.js.map +1 -0
- package/dist/access/field-visibility.d.ts +13 -0
- package/dist/access/field-visibility.d.ts.map +1 -0
- package/dist/access/field-visibility.js +178 -0
- package/dist/access/field-visibility.js.map +1 -0
- package/dist/access/index.d.ts +4 -1
- package/dist/access/index.d.ts.map +1 -1
- package/dist/access/index.js +8 -1
- package/dist/access/index.js.map +1 -1
- package/dist/access/multi-column-read-write.test.d.ts +2 -0
- package/dist/access/multi-column-read-write.test.d.ts.map +1 -0
- package/dist/access/multi-column-read-write.test.js +149 -0
- package/dist/access/multi-column-read-write.test.js.map +1 -0
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/types.d.ts +334 -5
- package/dist/config/types.d.ts.map +1 -1
- package/dist/context/hook-pipeline.d.ts +49 -0
- package/dist/context/hook-pipeline.d.ts.map +1 -0
- package/dist/context/hook-pipeline.js +75 -0
- package/dist/context/hook-pipeline.js.map +1 -0
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +30 -462
- package/dist/context/index.js.map +1 -1
- package/dist/context/nested-operations.d.ts.map +1 -1
- package/dist/context/nested-operations.js +72 -68
- package/dist/context/nested-operations.js.map +1 -1
- package/dist/context/write-pipeline.d.ts +158 -0
- package/dist/context/write-pipeline.d.ts.map +1 -0
- package/dist/context/write-pipeline.js +306 -0
- package/dist/context/write-pipeline.js.map +1 -0
- package/dist/extend.d.ts +3 -0
- package/dist/extend.d.ts.map +1 -0
- package/dist/extend.js +10 -0
- package/dist/extend.js.map +1 -0
- package/dist/fields/format-prisma-default.d.ts +35 -0
- package/dist/fields/format-prisma-default.d.ts.map +1 -0
- package/dist/fields/format-prisma-default.js +52 -0
- package/dist/fields/format-prisma-default.js.map +1 -0
- package/dist/fields/format-prisma-default.test.d.ts +2 -0
- package/dist/fields/format-prisma-default.test.d.ts.map +1 -0
- package/dist/fields/format-prisma-default.test.js +54 -0
- package/dist/fields/format-prisma-default.test.js.map +1 -0
- package/dist/fields/index.d.ts +1 -0
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +267 -18
- package/dist/fields/index.js.map +1 -1
- package/dist/fields/select.test.js +85 -0
- package/dist/fields/select.test.js.map +1 -1
- package/dist/fields/text-keystone-compat.test.d.ts +2 -0
- package/dist/fields/text-keystone-compat.test.d.ts.map +1 -0
- package/dist/fields/text-keystone-compat.test.js +93 -0
- package/dist/fields/text-keystone-compat.test.js.map +1 -0
- package/dist/hooks/index.d.ts +20 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +246 -0
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.d.ts +6 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -9
- package/dist/index.js.map +1 -1
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/index.test.js +33 -0
- package/dist/index.test.js.map +1 -0
- package/dist/internal.d.ts +8 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +16 -0
- package/dist/internal.js.map +1 -0
- package/dist/mcp/handler.js +0 -1
- package/dist/mcp/handler.js.map +1 -1
- package/dist/validation/field-config.d.ts +55 -0
- package/dist/validation/field-config.d.ts.map +1 -0
- package/dist/validation/field-config.js +100 -0
- package/dist/validation/field-config.js.map +1 -0
- package/dist/validation/field-config.test.d.ts +2 -0
- package/dist/validation/field-config.test.d.ts.map +1 -0
- package/dist/validation/field-config.test.js +159 -0
- package/dist/validation/field-config.test.js.map +1 -0
- package/package.json +11 -3
- package/src/access/access-filter.ts +97 -0
- package/src/access/engine.ts +13 -396
- package/src/access/{engine.test.ts → field-access.test.ts} +1 -1
- package/src/access/field-access.ts +159 -0
- package/src/access/field-visibility.ts +269 -0
- package/src/access/index.ts +7 -4
- package/src/access/multi-column-read-write.test.ts +255 -0
- package/src/config/index.ts +3 -0
- package/src/config/types.ts +342 -4
- package/src/context/hook-pipeline.ts +160 -0
- package/src/context/index.ts +29 -667
- package/src/context/nested-operations.ts +142 -111
- package/src/context/write-pipeline.ts +543 -0
- package/src/extend.ts +19 -0
- package/src/fields/format-prisma-default.test.ts +64 -0
- package/src/fields/format-prisma-default.ts +67 -0
- package/src/fields/index.ts +375 -20
- package/src/fields/select.test.ts +99 -0
- package/src/fields/text-keystone-compat.test.ts +126 -0
- package/src/hooks/index.ts +270 -0
- package/src/index.test.ts +50 -0
- package/src/index.ts +35 -82
- package/src/internal.ts +49 -0
- package/src/mcp/handler.ts +0 -2
- package/src/validation/field-config.test.ts +199 -0
- package/src/validation/field-config.ts +145 -0
- package/tests/access-relationships.test.ts +4 -4
- package/tests/access.test.ts +1 -1
- package/tests/field-hooks.test.ts +410 -0
- package/tests/field-types.test.ts +1 -1
- package/tests/hook-pipeline.test.ts +233 -0
- package/tests/nested-operation-registry.test.ts +206 -0
- package/tests/write-pipeline.test.ts +588 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +43 -1
- package/dist/access/engine.test.d.ts +0 -2
- package/dist/access/engine.test.d.ts.map +0 -1
- package/dist/access/engine.test.js.map +0 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { text, integer } from './index.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Unit coverage for Keystone-compat mode on text() (issue #475).
|
|
6
|
+
*
|
|
7
|
+
* Keystone 6 gives every non-null text column an implicit empty-string default.
|
|
8
|
+
* The `keystoneCompat` flag (db.keystoneCompat) reaches text()'s getPrismaType as
|
|
9
|
+
* the 4th positional argument — the same way provider/listName already do — and
|
|
10
|
+
* emits `@default("")` for a non-null text column that has no explicit default.
|
|
11
|
+
*
|
|
12
|
+
* These tests pin the precise on/off/explicit-default/nullable/non-text matrix
|
|
13
|
+
* from the issue's acceptance criteria.
|
|
14
|
+
*/
|
|
15
|
+
describe('text() Keystone-compat empty-string default', () => {
|
|
16
|
+
const KEYSTONE_COMPAT = true
|
|
17
|
+
|
|
18
|
+
describe('with keystoneCompat ON', () => {
|
|
19
|
+
it('emits @default("") for a required (non-null) text field without an explicit default', () => {
|
|
20
|
+
const field = text({ validation: { isRequired: true } })
|
|
21
|
+
|
|
22
|
+
const result = field.getPrismaType!('name', 'sqlite', 'User', KEYSTONE_COMPAT)
|
|
23
|
+
|
|
24
|
+
expect(result.type).toBe('String')
|
|
25
|
+
expect(result.modifiers).toBe('@default("")')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('emits @default("") for a non-null text field made non-null via db.isNullable: false', () => {
|
|
29
|
+
// Non-null at the DB level even though validation does not mark it required.
|
|
30
|
+
const field = text({ db: { isNullable: false } })
|
|
31
|
+
|
|
32
|
+
const result = field.getPrismaType!('phone', 'sqlite', 'User', KEYSTONE_COMPAT)
|
|
33
|
+
|
|
34
|
+
expect(result.modifiers).toBe('@default("")')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('does NOT emit a default for a nullable text field', () => {
|
|
38
|
+
// Optional → nullable; Keystone-compat must leave it alone.
|
|
39
|
+
const field = text()
|
|
40
|
+
|
|
41
|
+
const result = field.getPrismaType!('bio', 'sqlite', 'User', KEYSTONE_COMPAT)
|
|
42
|
+
|
|
43
|
+
expect(result.modifiers).toBe('?')
|
|
44
|
+
expect(result.modifiers).not.toContain('@default')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('does NOT emit a default for a text field made nullable via db.isNullable: true', () => {
|
|
48
|
+
// Required validation, but explicitly nullable at the DB level.
|
|
49
|
+
const field = text({ validation: { isRequired: true }, db: { isNullable: true } })
|
|
50
|
+
|
|
51
|
+
const result = field.getPrismaType!('note', 'sqlite', 'User', KEYSTONE_COMPAT)
|
|
52
|
+
|
|
53
|
+
expect(result.modifiers).toBe('?')
|
|
54
|
+
expect(result.modifiers).not.toContain('@default')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('lets an explicit defaultValue win over the compat empty-string default', () => {
|
|
58
|
+
const field = text({ validation: { isRequired: true }, defaultValue: 'PLEASE_UPDATE' })
|
|
59
|
+
|
|
60
|
+
const result = field.getPrismaType!('status', 'sqlite', 'Account', KEYSTONE_COMPAT)
|
|
61
|
+
|
|
62
|
+
expect(result.modifiers).toBe('@default("PLEASE_UPDATE")')
|
|
63
|
+
expect(result.modifiers).not.toContain('@default("")')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('honours an explicit empty-string defaultValue without double-emitting', () => {
|
|
67
|
+
const field = text({ validation: { isRequired: true }, defaultValue: '' })
|
|
68
|
+
|
|
69
|
+
const result = field.getPrismaType!('label', 'sqlite', 'Account', KEYSTONE_COMPAT)
|
|
70
|
+
|
|
71
|
+
// A single @default("") — the explicit default, not a duplicate.
|
|
72
|
+
expect(result.modifiers).toBe('@default("")')
|
|
73
|
+
expect(result.modifiers!.match(/@default/g)).toHaveLength(1)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('places @default("") alongside other modifiers in the expected order', () => {
|
|
77
|
+
const field = text({
|
|
78
|
+
validation: { isRequired: true },
|
|
79
|
+
isIndexed: 'unique',
|
|
80
|
+
db: { nativeType: 'Text', map: 'full_name' },
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const result = field.getPrismaType!('fullName', 'postgresql', 'User', KEYSTONE_COMPAT)
|
|
84
|
+
|
|
85
|
+
// nativeType → default → unique → map, matching the builder's modifier order.
|
|
86
|
+
expect(result.modifiers).toBe('@db.Text @default("") @unique @map("full_name")')
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
describe('with keystoneCompat OFF (default)', () => {
|
|
91
|
+
it('emits no default for a required text field when the flag is omitted', () => {
|
|
92
|
+
const field = text({ validation: { isRequired: true } })
|
|
93
|
+
|
|
94
|
+
const result = field.getPrismaType!('name', 'sqlite', 'User')
|
|
95
|
+
|
|
96
|
+
expect(result.modifiers).toBeUndefined()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('emits no default for a required text field when the flag is explicitly false', () => {
|
|
100
|
+
const field = text({ validation: { isRequired: true } })
|
|
101
|
+
|
|
102
|
+
const result = field.getPrismaType!('name', 'sqlite', 'User', false)
|
|
103
|
+
|
|
104
|
+
expect(result.modifiers).toBeUndefined()
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('still honours an explicit defaultValue when the flag is off', () => {
|
|
108
|
+
const field = text({ validation: { isRequired: true }, defaultValue: 'PLEASE_UPDATE' })
|
|
109
|
+
|
|
110
|
+
const result = field.getPrismaType!('status', 'sqlite', 'Account', false)
|
|
111
|
+
|
|
112
|
+
expect(result.modifiers).toBe('@default("PLEASE_UPDATE")')
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('non-text fields are unaffected by the flag', () => {
|
|
117
|
+
it('does not give a required integer field an empty-string default under keystoneCompat', () => {
|
|
118
|
+
const field = integer({ validation: { isRequired: true } })
|
|
119
|
+
|
|
120
|
+
const result = field.getPrismaType!('count', 'sqlite', 'Widget', KEYSTONE_COMPAT)
|
|
121
|
+
|
|
122
|
+
expect(result.type).toBe('Int')
|
|
123
|
+
expect(result.modifiers ?? '').not.toContain('@default')
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
})
|
package/src/hooks/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Hooks } from '../config/types.js'
|
|
|
2
2
|
import type { AccessContext } from '../access/types.js'
|
|
3
3
|
import type { FieldConfig } from '../config/types.js'
|
|
4
4
|
import { validateWithZod } from '../validation/schema.js'
|
|
5
|
+
import { checkFieldAccess } from '../access/field-access.js'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Validation error collection
|
|
@@ -213,6 +214,275 @@ export async function executeAfterOperation<
|
|
|
213
214
|
await hooks.afterOperation(args as Parameters<typeof hooks.afterOperation>[0])
|
|
214
215
|
}
|
|
215
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Execute field-level resolveInput hooks
|
|
219
|
+
* Allows fields to transform their input values before database write
|
|
220
|
+
*/
|
|
221
|
+
export async function executeFieldResolveInputHooks(
|
|
222
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
223
|
+
inputData: Record<string, any>,
|
|
224
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
225
|
+
resolvedData: Record<string, any>,
|
|
226
|
+
fields: Record<string, FieldConfig>,
|
|
227
|
+
operation: 'create' | 'update',
|
|
228
|
+
context: AccessContext,
|
|
229
|
+
listKey: string,
|
|
230
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
231
|
+
item?: any,
|
|
232
|
+
): Promise<Record<string, unknown>> {
|
|
233
|
+
let result = { ...resolvedData }
|
|
234
|
+
|
|
235
|
+
for (const [fieldKey, fieldConfig] of Object.entries(fields)) {
|
|
236
|
+
// Skip if field not in data
|
|
237
|
+
if (!(fieldKey in result)) continue
|
|
238
|
+
|
|
239
|
+
// A field's resolveInput produces its resolved value; for most fields that
|
|
240
|
+
// value is stored back under the same key. Multi-column fields additionally
|
|
241
|
+
// split that value across their physical columns below.
|
|
242
|
+
let resolvedValue: unknown = result[fieldKey]
|
|
243
|
+
|
|
244
|
+
if (fieldConfig.hooks?.resolveInput) {
|
|
245
|
+
// Execute field hook
|
|
246
|
+
// Type assertion is safe here because hooks are typed correctly in field definitions
|
|
247
|
+
// and we're working with runtime values that match those types
|
|
248
|
+
resolvedValue = await fieldConfig.hooks.resolveInput({
|
|
249
|
+
listKey,
|
|
250
|
+
fieldKey,
|
|
251
|
+
operation,
|
|
252
|
+
inputData,
|
|
253
|
+
item,
|
|
254
|
+
resolvedData: { ...result }, // Pass a copy to avoid mutation affecting recorded args
|
|
255
|
+
context,
|
|
256
|
+
} as Parameters<typeof fieldConfig.hooks.resolveInput>[0])
|
|
257
|
+
} else if (!fieldConfig.splitColumns) {
|
|
258
|
+
// No resolveInput and not a multi-column field — nothing to do.
|
|
259
|
+
continue
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (fieldConfig.splitColumns) {
|
|
263
|
+
// Multi-column field (e.g. storage image()/file() in Keystone-parity
|
|
264
|
+
// mode): replace the single logical key with its per-part columns so the
|
|
265
|
+
// write payload targets the live columns instead of a single one.
|
|
266
|
+
//
|
|
267
|
+
// The split removes the logical key from the payload BEFORE the
|
|
268
|
+
// canonical writable-field filter (`filterWritableFields`) runs, and the
|
|
269
|
+
// raw per-part column keys are not in `fieldConfigs` — so that later
|
|
270
|
+
// filter cannot enforce this field's own write access. Enforce it HERE,
|
|
271
|
+
// using the canonical field-access evaluator with the SAME arguments the
|
|
272
|
+
// write pipeline uses. A single-column field denied by `update`/`create`
|
|
273
|
+
// is simply omitted from the write; a denied multi-column field must
|
|
274
|
+
// likewise contribute NONE of its per-part columns. (sudo bypasses via
|
|
275
|
+
// `checkFieldAccess`.)
|
|
276
|
+
const canWrite = await checkFieldAccess(fieldConfig.access, operation, {
|
|
277
|
+
session: context.session,
|
|
278
|
+
item,
|
|
279
|
+
context,
|
|
280
|
+
inputData,
|
|
281
|
+
})
|
|
282
|
+
if (!canWrite) {
|
|
283
|
+
// Denied: drop the logical key and write none of its columns — exactly
|
|
284
|
+
// as filterWritableFields drops a denied single-column field.
|
|
285
|
+
const next = { ...result }
|
|
286
|
+
delete next[fieldKey]
|
|
287
|
+
result = next
|
|
288
|
+
continue
|
|
289
|
+
}
|
|
290
|
+
const columns = fieldConfig.splitColumns(fieldKey, resolvedValue)
|
|
291
|
+
// Drop the logical key (it is not a real column) and merge the columns.
|
|
292
|
+
const next = { ...result, ...columns }
|
|
293
|
+
delete next[fieldKey]
|
|
294
|
+
result = next
|
|
295
|
+
} else {
|
|
296
|
+
// Create new object with updated field to avoid mutating the passed reference
|
|
297
|
+
result = { ...result, [fieldKey]: resolvedValue }
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return result
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Execute field-level validate hooks
|
|
306
|
+
* Allows fields to perform custom validation after resolveInput but before database write
|
|
307
|
+
*/
|
|
308
|
+
export async function executeFieldValidateHooks(
|
|
309
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
310
|
+
inputData: Record<string, any> | undefined,
|
|
311
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
312
|
+
resolvedData: Record<string, any> | undefined,
|
|
313
|
+
fields: Record<string, FieldConfig>,
|
|
314
|
+
operation: 'create' | 'update' | 'delete',
|
|
315
|
+
context: AccessContext,
|
|
316
|
+
listKey: string,
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
318
|
+
item?: any,
|
|
319
|
+
): Promise<void> {
|
|
320
|
+
const errors: string[] = []
|
|
321
|
+
const fieldErrors: Record<string, string> = {}
|
|
322
|
+
|
|
323
|
+
const addValidationError = (fieldKey: string) => (msg: string) => {
|
|
324
|
+
errors.push(msg)
|
|
325
|
+
fieldErrors[fieldKey] = msg
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
for (const [fieldKey, fieldConfig] of Object.entries(fields)) {
|
|
329
|
+
// Support both 'validate' (new) and 'validateInput' (deprecated) for backwards compatibility
|
|
330
|
+
const validateHook = fieldConfig.hooks?.validate ?? fieldConfig.hooks?.validateInput
|
|
331
|
+
if (!validateHook) continue
|
|
332
|
+
|
|
333
|
+
// Execute field hook
|
|
334
|
+
// Type assertion is safe here because hooks are typed correctly in field definitions
|
|
335
|
+
if (operation === 'delete') {
|
|
336
|
+
await validateHook({
|
|
337
|
+
listKey,
|
|
338
|
+
fieldKey,
|
|
339
|
+
operation: 'delete',
|
|
340
|
+
item,
|
|
341
|
+
context,
|
|
342
|
+
addValidationError: addValidationError(fieldKey),
|
|
343
|
+
} as Parameters<typeof validateHook>[0])
|
|
344
|
+
} else if (operation === 'create') {
|
|
345
|
+
await validateHook({
|
|
346
|
+
listKey,
|
|
347
|
+
fieldKey,
|
|
348
|
+
operation: 'create',
|
|
349
|
+
inputData,
|
|
350
|
+
item: undefined,
|
|
351
|
+
resolvedData,
|
|
352
|
+
context,
|
|
353
|
+
addValidationError: addValidationError(fieldKey),
|
|
354
|
+
} as Parameters<typeof validateHook>[0])
|
|
355
|
+
} else {
|
|
356
|
+
// operation === 'update'
|
|
357
|
+
await validateHook({
|
|
358
|
+
listKey,
|
|
359
|
+
fieldKey,
|
|
360
|
+
operation: 'update',
|
|
361
|
+
inputData,
|
|
362
|
+
item,
|
|
363
|
+
resolvedData,
|
|
364
|
+
context,
|
|
365
|
+
addValidationError: addValidationError(fieldKey),
|
|
366
|
+
} as Parameters<typeof validateHook>[0])
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (errors.length > 0) {
|
|
371
|
+
throw new ValidationError(errors, fieldErrors)
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Execute field-level beforeOperation hooks (side effects only)
|
|
377
|
+
* Allows fields to perform side effects before database write
|
|
378
|
+
*/
|
|
379
|
+
export async function executeFieldBeforeOperationHooks(
|
|
380
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
381
|
+
inputData: Record<string, any>,
|
|
382
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
383
|
+
resolvedData: Record<string, any>,
|
|
384
|
+
fields: Record<string, FieldConfig>,
|
|
385
|
+
operation: 'create' | 'update' | 'delete',
|
|
386
|
+
context: AccessContext,
|
|
387
|
+
listKey: string,
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
389
|
+
item?: any,
|
|
390
|
+
): Promise<void> {
|
|
391
|
+
for (const [fieldKey, fieldConfig] of Object.entries(fields)) {
|
|
392
|
+
// Skip if no hooks defined
|
|
393
|
+
if (!fieldConfig.hooks?.beforeOperation) continue
|
|
394
|
+
// Skip if field not in data (for create/update)
|
|
395
|
+
if (operation !== 'delete' && !(fieldKey in resolvedData)) continue
|
|
396
|
+
|
|
397
|
+
// Execute field hook (side effects only, no return value used)
|
|
398
|
+
// Type assertion is safe here because hooks are typed correctly in field definitions
|
|
399
|
+
if (operation === 'delete') {
|
|
400
|
+
await fieldConfig.hooks.beforeOperation({
|
|
401
|
+
listKey,
|
|
402
|
+
fieldKey,
|
|
403
|
+
operation: 'delete',
|
|
404
|
+
item,
|
|
405
|
+
context,
|
|
406
|
+
} as Parameters<typeof fieldConfig.hooks.beforeOperation>[0])
|
|
407
|
+
} else if (operation === 'create') {
|
|
408
|
+
await fieldConfig.hooks.beforeOperation({
|
|
409
|
+
listKey,
|
|
410
|
+
fieldKey,
|
|
411
|
+
operation: 'create',
|
|
412
|
+
inputData,
|
|
413
|
+
resolvedData,
|
|
414
|
+
context,
|
|
415
|
+
} as Parameters<typeof fieldConfig.hooks.beforeOperation>[0])
|
|
416
|
+
} else {
|
|
417
|
+
// operation === 'update'
|
|
418
|
+
await fieldConfig.hooks.beforeOperation({
|
|
419
|
+
listKey,
|
|
420
|
+
fieldKey,
|
|
421
|
+
operation: 'update',
|
|
422
|
+
inputData,
|
|
423
|
+
item,
|
|
424
|
+
resolvedData,
|
|
425
|
+
context,
|
|
426
|
+
} as Parameters<typeof fieldConfig.hooks.beforeOperation>[0])
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Execute field-level afterOperation hooks (side effects only)
|
|
433
|
+
* Allows fields to perform side effects after database operations
|
|
434
|
+
*/
|
|
435
|
+
export async function executeFieldAfterOperationHooks(
|
|
436
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
437
|
+
item: any,
|
|
438
|
+
inputData: Record<string, unknown> | undefined,
|
|
439
|
+
resolvedData: Record<string, unknown> | undefined,
|
|
440
|
+
fields: Record<string, FieldConfig>,
|
|
441
|
+
operation: 'create' | 'update' | 'delete',
|
|
442
|
+
context: AccessContext,
|
|
443
|
+
listKey: string,
|
|
444
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
445
|
+
originalItem?: any,
|
|
446
|
+
): Promise<void> {
|
|
447
|
+
for (const [fieldKey, fieldConfig] of Object.entries(fields)) {
|
|
448
|
+
// Skip if no hooks defined
|
|
449
|
+
if (!fieldConfig.hooks?.afterOperation) continue
|
|
450
|
+
|
|
451
|
+
// Execute field hook (side effects only, no return value used)
|
|
452
|
+
if (operation === 'delete') {
|
|
453
|
+
await fieldConfig.hooks.afterOperation({
|
|
454
|
+
listKey,
|
|
455
|
+
fieldKey,
|
|
456
|
+
operation: 'delete',
|
|
457
|
+
originalItem,
|
|
458
|
+
context,
|
|
459
|
+
} as Parameters<typeof fieldConfig.hooks.afterOperation>[0])
|
|
460
|
+
} else if (operation === 'create') {
|
|
461
|
+
await fieldConfig.hooks.afterOperation({
|
|
462
|
+
listKey,
|
|
463
|
+
fieldKey,
|
|
464
|
+
operation: 'create',
|
|
465
|
+
inputData,
|
|
466
|
+
item,
|
|
467
|
+
resolvedData,
|
|
468
|
+
context,
|
|
469
|
+
} as Parameters<typeof fieldConfig.hooks.afterOperation>[0])
|
|
470
|
+
} else {
|
|
471
|
+
// operation === 'update'
|
|
472
|
+
await fieldConfig.hooks.afterOperation({
|
|
473
|
+
listKey,
|
|
474
|
+
fieldKey,
|
|
475
|
+
operation: 'update',
|
|
476
|
+
inputData,
|
|
477
|
+
originalItem,
|
|
478
|
+
item,
|
|
479
|
+
resolvedData,
|
|
480
|
+
context,
|
|
481
|
+
} as Parameters<typeof fieldConfig.hooks.afterOperation>[0])
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
216
486
|
/**
|
|
217
487
|
* Validate field-level validation rules using Zod
|
|
218
488
|
* Checks isRequired, length constraints, etc.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, it, expect, expectTypeOf } from 'vitest'
|
|
2
|
+
// Import from the package root entry point exactly as the docs / CHANGELOG /
|
|
3
|
+
// migrate-context-calls skill instruct consumers to. If the root `index.ts`
|
|
4
|
+
// stops re-exporting the query API, this file fails to type-check / run,
|
|
5
|
+
// preventing a silent regression (issue #496).
|
|
6
|
+
import { defineFragment, runQuery, runQueryOne } from './index.js'
|
|
7
|
+
import type { ResultOf, RelationSelector, QueryArgs } from './index.js'
|
|
8
|
+
|
|
9
|
+
// ─────────────────────────────────────────────────────────────
|
|
10
|
+
// Stand-in model type (mirrors a Prisma-generated type)
|
|
11
|
+
// ─────────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
type User = {
|
|
14
|
+
id: string
|
|
15
|
+
name: string
|
|
16
|
+
email: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('@opensaas/stack-core root entry point — query API re-exports (issue #496)', () => {
|
|
20
|
+
it('re-exports the runtime query functions from the package root', () => {
|
|
21
|
+
expect(typeof defineFragment).toBe('function')
|
|
22
|
+
expect(typeof runQuery).toBe('function')
|
|
23
|
+
expect(typeof runQueryOne).toBe('function')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('defineFragment imported from the root produces a usable fragment', () => {
|
|
27
|
+
const userFragment = defineFragment<User>()({ id: true, name: true } as const)
|
|
28
|
+
|
|
29
|
+
expect(userFragment._type).toBe('fragment')
|
|
30
|
+
expect(userFragment._fields).toEqual({ id: true, name: true })
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('exposes ResultOf as a usable type alias from the root', () => {
|
|
34
|
+
const userFragment = defineFragment<User>()({ id: true, name: true } as const)
|
|
35
|
+
expect(userFragment._type).toBe('fragment')
|
|
36
|
+
|
|
37
|
+
// Type-level assertion: ResultOf<typeof fragment> narrows to the selection.
|
|
38
|
+
expectTypeOf<ResultOf<typeof userFragment>>().toEqualTypeOf<{ id: string; name: string }>()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('exposes QueryArgs and RelationSelector as usable types from the root', () => {
|
|
42
|
+
// Type-level usage — these annotations only compile if the types resolve
|
|
43
|
+
// from the root entry point.
|
|
44
|
+
const args: QueryArgs = { where: { id: 'abc' }, take: 5 }
|
|
45
|
+
expect(args.take).toBe(5)
|
|
46
|
+
|
|
47
|
+
const selector: RelationSelector<User> = defineFragment<User>()({ id: true } as const)
|
|
48
|
+
expectTypeOf(selector).not.toBeNever()
|
|
49
|
+
})
|
|
50
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -1,105 +1,58 @@
|
|
|
1
|
-
//
|
|
1
|
+
// ───────────────────────────────────────────────────────────────
|
|
2
|
+
// @opensaas/stack-core — consumer entry point
|
|
3
|
+
//
|
|
4
|
+
// The everyday surface for defining a config and using a context.
|
|
5
|
+
// • Field builders → '@opensaas/stack-core/fields'
|
|
6
|
+
// • Plugin / field authoring → '@opensaas/stack-core/extend'
|
|
7
|
+
// • MCP runtime → '@opensaas/stack-core/mcp'
|
|
8
|
+
// Internal plumbing lives on '@opensaas/stack-core/internal' (unstable).
|
|
9
|
+
// ───────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
// Config builders
|
|
2
12
|
export { config, list } from './config/index.js'
|
|
13
|
+
|
|
14
|
+
// Config types a consumer annotates with.
|
|
15
|
+
// Concrete field-config types (TextField, …) live on '@opensaas/stack-core/fields'
|
|
16
|
+
// alongside their builders.
|
|
3
17
|
export type {
|
|
4
18
|
OpenSaasConfig,
|
|
19
|
+
OutputConfig,
|
|
5
20
|
ListConfig,
|
|
21
|
+
DatabaseConfig,
|
|
6
22
|
FieldConfig,
|
|
7
|
-
BaseFieldConfig,
|
|
8
|
-
TextField,
|
|
9
|
-
IntegerField,
|
|
10
|
-
CheckboxField,
|
|
11
|
-
TimestampField,
|
|
12
|
-
PasswordField,
|
|
13
|
-
SelectField,
|
|
14
|
-
RelationshipField,
|
|
15
|
-
JsonField,
|
|
16
|
-
VirtualField,
|
|
17
|
-
TypeDescriptor,
|
|
18
|
-
TypeInfo,
|
|
19
23
|
OperationAccess,
|
|
20
|
-
Hooks,
|
|
21
|
-
FieldHooks,
|
|
22
|
-
FieldsWithTypeInfo,
|
|
23
|
-
DatabaseConfig,
|
|
24
|
-
SessionConfig,
|
|
25
|
-
UIConfig,
|
|
26
|
-
ThemeConfig,
|
|
27
|
-
ThemePreset,
|
|
28
|
-
ThemeColors,
|
|
29
|
-
McpConfig,
|
|
30
|
-
McpToolsConfig,
|
|
31
|
-
McpAuthConfig,
|
|
32
|
-
ListMcpConfig,
|
|
33
|
-
McpCustomTool,
|
|
34
|
-
FileMetadata,
|
|
35
|
-
ImageMetadata,
|
|
36
|
-
ImageTransformationResult,
|
|
37
|
-
// Plugin system types
|
|
38
|
-
Plugin,
|
|
39
|
-
PluginContext,
|
|
40
|
-
GeneratedFiles,
|
|
41
|
-
// List-level hook argument types
|
|
42
|
-
ResolveInputHookArgs,
|
|
43
|
-
ValidateHookArgs,
|
|
44
|
-
BeforeOperationHookArgs,
|
|
45
|
-
AfterOperationHookArgs,
|
|
46
|
-
// Field-level hook argument types
|
|
47
|
-
FieldResolveInputHookArgs,
|
|
48
|
-
FieldValidateHookArgs,
|
|
49
|
-
FieldBeforeOperationHookArgs,
|
|
50
|
-
FieldAfterOperationHookArgs,
|
|
51
|
-
FieldResolveOutputHookArgs,
|
|
52
24
|
} from './config/index.js'
|
|
53
25
|
|
|
54
|
-
// Access control
|
|
26
|
+
// Access control — the types a consumer writes against
|
|
55
27
|
export type {
|
|
56
28
|
AccessControl,
|
|
57
29
|
FieldAccess,
|
|
58
30
|
Session,
|
|
59
31
|
AccessContext,
|
|
60
32
|
PrismaFilter,
|
|
61
|
-
AccessControlledDB,
|
|
62
|
-
StorageUtils,
|
|
63
|
-
AugmentedFindMany,
|
|
64
|
-
AugmentedFindUnique,
|
|
65
|
-
FindManyQueryArgs,
|
|
66
33
|
} from './access/index.js'
|
|
67
34
|
|
|
68
|
-
// Context
|
|
35
|
+
// Context factory
|
|
69
36
|
export { getContext } from './context/index.js'
|
|
70
|
-
export type { PrismaClientLike } from './access/types.js'
|
|
71
|
-
export type { ServerActionProps } from './context/index.js'
|
|
72
37
|
|
|
73
|
-
//
|
|
74
|
-
export {
|
|
75
|
-
getDbKey,
|
|
76
|
-
getUrlKey,
|
|
77
|
-
getListKeyFromUrl,
|
|
78
|
-
pascalToCamel,
|
|
79
|
-
pascalToKebab,
|
|
80
|
-
kebabToPascal,
|
|
81
|
-
kebabToCamel,
|
|
82
|
-
} from './lib/case-utils.js'
|
|
38
|
+
// Naming utilities (documented public helpers; used for URLs and db keys)
|
|
39
|
+
export { getDbKey, getUrlKey, getListKeyFromUrl } from './lib/case-utils.js'
|
|
83
40
|
|
|
84
|
-
//
|
|
41
|
+
// Validation error surfaced by write operations
|
|
85
42
|
export { ValidationError } from './hooks/index.js'
|
|
86
|
-
export { validateWithZod, generateZodSchema } from './validation/schema.js'
|
|
87
43
|
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
} from './utils/password.js'
|
|
44
|
+
// Field self-containment validation — checks each field implements the
|
|
45
|
+
// generation contract (getPrismaType / getTypeScriptType / getZodSchema, or
|
|
46
|
+
// getPrismaRelation for relationships) so a misimplemented field fails early
|
|
47
|
+
// with a clear per-field message instead of deep inside generation.
|
|
48
|
+
export { validateFieldConfig, validateConfigFields } from './validation/field-config.js'
|
|
49
|
+
export type { FieldConfigValidationError } from './validation/field-config.js'
|
|
95
50
|
|
|
96
|
-
//
|
|
51
|
+
// Fragment-based query API — composable, type-safe reads that mirror
|
|
52
|
+
// Keystone's GraphQL fragments without a GraphQL runtime. The migration
|
|
53
|
+
// guide, CHANGELOG, and migrate-context-calls skill all advertise importing
|
|
54
|
+
// these from the root entry point. The internal runtime helpers (isFragment,
|
|
55
|
+
// buildInclude, pickFields) and the Fragment/FieldSelection types stay off the
|
|
56
|
+
// root surface — those live on '@opensaas/stack-core/internal'.
|
|
97
57
|
export { defineFragment, runQuery, runQueryOne } from './query/index.js'
|
|
98
|
-
export type {
|
|
99
|
-
Fragment,
|
|
100
|
-
FieldSelection,
|
|
101
|
-
ResultOf,
|
|
102
|
-
RelationSelector,
|
|
103
|
-
QueryArgs,
|
|
104
|
-
QueryRunnerContext,
|
|
105
|
-
} from './query/index.js'
|
|
58
|
+
export type { ResultOf, RelationSelector, QueryArgs } from './query/index.js'
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// ───────────────────────────────────────────────────────────────
|
|
2
|
+
// @opensaas/stack-core/internal
|
|
3
|
+
//
|
|
4
|
+
// @internal — plumbing shared between the @opensaas/* packages and the
|
|
5
|
+
// code emitted by the generator (`.opensaas/`). NOT a public API: these
|
|
6
|
+
// exports carry NO semver guarantees and may change or disappear in any
|
|
7
|
+
// release. Application authors should never import from this path; use
|
|
8
|
+
// the root entry point, `/fields`, or `/extend` instead.
|
|
9
|
+
// ───────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
// Runtime context internals consumed by generated `.opensaas/` code
|
|
12
|
+
export type { ServerActionProps } from './context/index.js'
|
|
13
|
+
export type {
|
|
14
|
+
PrismaClientLike,
|
|
15
|
+
AccessControlledDB,
|
|
16
|
+
StorageUtils,
|
|
17
|
+
AugmentedFindMany,
|
|
18
|
+
AugmentedFindUnique,
|
|
19
|
+
FindManyQueryArgs,
|
|
20
|
+
} from './access/types.js'
|
|
21
|
+
|
|
22
|
+
// Typed-query internals (Fragment/FieldSelection appear in generated types)
|
|
23
|
+
export type { Fragment, FieldSelection } from './query/index.js'
|
|
24
|
+
|
|
25
|
+
// Password hashing internals (the password field emits HashedPassword into generated types)
|
|
26
|
+
export {
|
|
27
|
+
hashPassword,
|
|
28
|
+
comparePassword,
|
|
29
|
+
isHashedPassword,
|
|
30
|
+
HashedPassword,
|
|
31
|
+
} from './utils/password.js'
|
|
32
|
+
|
|
33
|
+
// Case conversion helpers used by sibling packages
|
|
34
|
+
export { pascalToCamel, pascalToKebab, kebabToPascal, kebabToCamel } from './lib/case-utils.js'
|
|
35
|
+
|
|
36
|
+
// Zod schema helpers used internally for validation
|
|
37
|
+
export { validateWithZod, generateZodSchema } from './validation/schema.js'
|
|
38
|
+
|
|
39
|
+
// Config-shape sub-types consumed by sibling packages (not part of the consumer surface)
|
|
40
|
+
export type {
|
|
41
|
+
DatabaseConfig,
|
|
42
|
+
SessionConfig,
|
|
43
|
+
ThemeConfig,
|
|
44
|
+
ThemePreset,
|
|
45
|
+
ThemeColors,
|
|
46
|
+
FileMetadata,
|
|
47
|
+
ImageMetadata,
|
|
48
|
+
ImageTransformationResult,
|
|
49
|
+
} from './config/index.js'
|
package/src/mcp/handler.ts
CHANGED