adula 0.0.0 → 2.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.
Files changed (210) hide show
  1. package/LICENSE.md +10 -0
  2. package/README.md +38 -2
  3. package/build/agent/AGENTS.template.md +28 -0
  4. package/build/agent/capabilities.md +60 -0
  5. package/build/agent/skills/adula-frontend-design/SKILL.md +59 -0
  6. package/build/agent/skills/idea-review/SKILL.md +31 -0
  7. package/build/agent/skills/module-review/SKILL.md +22 -0
  8. package/build/agent/skills/perf-review/SKILL.md +24 -0
  9. package/build/agent/skills/schema-review/SKILL.md +22 -0
  10. package/build/agent/skills/security-review/SKILL.md +24 -0
  11. package/build/agent/skills/ui-review/SKILL.md +22 -0
  12. package/build/commands/capabilities.d.ts +10 -0
  13. package/build/commands/capabilities.js +40 -0
  14. package/build/commands/doctor.d.ts +9 -0
  15. package/build/commands/doctor.js +72 -0
  16. package/build/commands/gaps.d.ts +12 -0
  17. package/build/commands/gaps.js +62 -0
  18. package/build/commands/install.d.ts +9 -0
  19. package/build/commands/install.js +105 -0
  20. package/build/commands/main.d.ts +8 -0
  21. package/build/commands/main.js +33 -0
  22. package/build/commands/module_add.d.ts +8 -0
  23. package/build/commands/module_add.js +57 -0
  24. package/build/commands/module_remove.d.ts +10 -0
  25. package/build/commands/module_remove.js +94 -0
  26. package/build/commands/resource.d.ts +8 -0
  27. package/build/commands/resource.js +29 -0
  28. package/build/commands/resource_snapshot.d.ts +15 -0
  29. package/build/commands/resource_snapshot.js +61 -0
  30. package/build/commands/storage_migrate.d.ts +12 -0
  31. package/build/commands/storage_migrate.js +39 -0
  32. package/build/commands/ui.d.ts +11 -0
  33. package/build/commands/ui.js +210 -0
  34. package/build/commands/upgrade.d.ts +15 -0
  35. package/build/commands/upgrade.js +34 -0
  36. package/build/configure.d.ts +2 -0
  37. package/build/configure.js +79 -0
  38. package/build/database/migrations/1770000000000_core.d.ts +5 -0
  39. package/build/database/migrations/1770000000000_core.js +10 -0
  40. package/build/database/migrations/1770000000001_attachments.d.ts +5 -0
  41. package/build/database/migrations/1770000000001_attachments.js +10 -0
  42. package/build/database/migrations/1770000000002_saved_views.d.ts +5 -0
  43. package/build/database/migrations/1770000000002_saved_views.js +10 -0
  44. package/build/database/migrations/1770000000003_user_invitations.d.ts +5 -0
  45. package/build/database/migrations/1770000000003_user_invitations.js +18 -0
  46. package/build/database/migrations/1770000000004_collaboration.d.ts +5 -0
  47. package/build/database/migrations/1770000000004_collaboration.js +10 -0
  48. package/build/database/migrations/1770000000005_assignments.d.ts +5 -0
  49. package/build/database/migrations/1770000000005_assignments.js +10 -0
  50. package/build/database/migrations/1770000000006_messaging.d.ts +5 -0
  51. package/build/database/migrations/1770000000006_messaging.js +10 -0
  52. package/build/database/migrations/1770000000007_webhooks.d.ts +5 -0
  53. package/build/database/migrations/1770000000007_webhooks.js +10 -0
  54. package/build/database/migrations/1770000000008_imports.d.ts +5 -0
  55. package/build/database/migrations/1770000000008_imports.js +10 -0
  56. package/build/database/migrations/1770000000010_workflows.d.ts +5 -0
  57. package/build/database/migrations/1770000000010_workflows.js +10 -0
  58. package/build/database/migrations/1770000000011_managed_assignments.d.ts +5 -0
  59. package/build/database/migrations/1770000000011_managed_assignments.js +10 -0
  60. package/build/database/migrations/1770000000012_role_keys.d.ts +5 -0
  61. package/build/database/migrations/1770000000012_role_keys.js +10 -0
  62. package/build/database/migrations/1770000000013_notification_targets.d.ts +5 -0
  63. package/build/database/migrations/1770000000013_notification_targets.js +10 -0
  64. package/build/database/migrations/1770000000014_upload_grants.d.ts +5 -0
  65. package/build/database/migrations/1770000000014_upload_grants.js +10 -0
  66. package/build/database/migrations/1770000000015_inbound_webhooks.d.ts +5 -0
  67. package/build/database/migrations/1770000000015_inbound_webhooks.js +10 -0
  68. package/build/database/migrations/1770000000016_adulajs_names.d.ts +6 -0
  69. package/build/database/migrations/1770000000016_adulajs_names.js +11 -0
  70. package/build/index.d.ts +80 -0
  71. package/build/index.js +49 -0
  72. package/build/providers/adula_provider.d.ts +6 -0
  73. package/build/providers/adula_provider.js +10 -0
  74. package/build/src/admin/contracts.d.ts +19 -0
  75. package/build/src/admin/contracts.js +115 -0
  76. package/build/src/admin/controller.d.ts +32 -0
  77. package/build/src/admin/controller.js +167 -0
  78. package/build/src/admin/errors.d.ts +5 -0
  79. package/build/src/admin/errors.js +9 -0
  80. package/build/src/admin/presentation.d.ts +28 -0
  81. package/build/src/admin/presentation.js +1 -0
  82. package/build/src/admin/record_title.d.ts +14 -0
  83. package/build/src/admin/record_title.js +50 -0
  84. package/build/src/admin/resource_service.d.ts +376 -0
  85. package/build/src/admin/resource_service.js +1448 -0
  86. package/build/src/attachments/attachment_service.d.ts +100 -0
  87. package/build/src/attachments/attachment_service.js +270 -0
  88. package/build/src/attachments/doctor.d.ts +4 -0
  89. package/build/src/attachments/doctor.js +28 -0
  90. package/build/src/attachments/storage_migrate.d.ts +39 -0
  91. package/build/src/attachments/storage_migrate.js +64 -0
  92. package/build/src/attachments/upload_grants.d.ts +44 -0
  93. package/build/src/attachments/upload_grants.js +105 -0
  94. package/build/src/auth/ability.d.ts +24 -0
  95. package/build/src/auth/ability.js +34 -0
  96. package/build/src/auth/actor_store.d.ts +14 -0
  97. package/build/src/auth/actor_store.js +70 -0
  98. package/build/src/auth/conditions.d.ts +26 -0
  99. package/build/src/auth/conditions.js +117 -0
  100. package/build/src/auth/packed_rules.d.ts +4 -0
  101. package/build/src/auth/packed_rules.js +13 -0
  102. package/build/src/auth/sql.d.ts +11 -0
  103. package/build/src/auth/sql.js +118 -0
  104. package/build/src/collaboration/assignments.d.ts +129 -0
  105. package/build/src/collaboration/assignments.js +333 -0
  106. package/build/src/collaboration/record_collaboration.d.ts +86 -0
  107. package/build/src/collaboration/record_collaboration.js +348 -0
  108. package/build/src/commands/agent_assets.d.ts +7 -0
  109. package/build/src/commands/agent_assets.js +66 -0
  110. package/build/src/commands/capabilities.d.ts +19 -0
  111. package/build/src/commands/capabilities.js +179 -0
  112. package/build/src/commands/doctor.d.ts +42 -0
  113. package/build/src/commands/doctor.js +375 -0
  114. package/build/src/commands/gap_report.d.ts +56 -0
  115. package/build/src/commands/gap_report.js +110 -0
  116. package/build/src/commands/generator.d.ts +1 -0
  117. package/build/src/commands/generator.js +70 -0
  118. package/build/src/commands/snapshot.d.ts +28 -0
  119. package/build/src/commands/snapshot.js +48 -0
  120. package/build/src/commands/source_markers.d.ts +11 -0
  121. package/build/src/commands/source_markers.js +41 -0
  122. package/build/src/core/activity.d.ts +43 -0
  123. package/build/src/core/activity.js +77 -0
  124. package/build/src/core/administration_guard.d.ts +3 -0
  125. package/build/src/core/administration_guard.js +39 -0
  126. package/build/src/core/health.d.ts +42 -0
  127. package/build/src/core/health.js +44 -0
  128. package/build/src/core/mail_test.d.ts +18 -0
  129. package/build/src/core/mail_test.js +88 -0
  130. package/build/src/core/message_templates.d.ts +83 -0
  131. package/build/src/core/message_templates.js +293 -0
  132. package/build/src/core/module_seed.d.ts +15 -0
  133. package/build/src/core/module_seed.js +31 -0
  134. package/build/src/core/notifications.d.ts +42 -0
  135. package/build/src/core/notifications.js +91 -0
  136. package/build/src/core/org_units.d.ts +30 -0
  137. package/build/src/core/org_units.js +123 -0
  138. package/build/src/core/roles.d.ts +98 -0
  139. package/build/src/core/roles.js +447 -0
  140. package/build/src/core/saved_views.d.ts +37 -0
  141. package/build/src/core/saved_views.js +164 -0
  142. package/build/src/core/settings.d.ts +29 -0
  143. package/build/src/core/settings.js +113 -0
  144. package/build/src/core/setup.d.ts +14 -0
  145. package/build/src/core/setup.js +89 -0
  146. package/build/src/core/ui_preferences.d.ts +12 -0
  147. package/build/src/core/ui_preferences.js +30 -0
  148. package/build/src/core/user_invitations.d.ts +23 -0
  149. package/build/src/core/user_invitations.js +144 -0
  150. package/build/src/core/users.d.ts +44 -0
  151. package/build/src/core/users.js +182 -0
  152. package/build/src/database/configure.d.ts +3 -0
  153. package/build/src/database/configure.js +41 -0
  154. package/build/src/database/schema.d.ts +52 -0
  155. package/build/src/database/schema.js +538 -0
  156. package/build/src/eslint/index.d.ts +3 -0
  157. package/build/src/eslint/index.js +90 -0
  158. package/build/src/events/adonis_jobs.d.ts +9 -0
  159. package/build/src/events/adonis_jobs.js +13 -0
  160. package/build/src/events/outbox.d.ts +19 -0
  161. package/build/src/events/outbox.js +33 -0
  162. package/build/src/events/record_mutation.d.ts +24 -0
  163. package/build/src/events/record_mutation.js +62 -0
  164. package/build/src/integrations/imports.d.ts +74 -0
  165. package/build/src/integrations/imports.js +333 -0
  166. package/build/src/integrations/inbound_webhooks.d.ts +94 -0
  167. package/build/src/integrations/inbound_webhooks.js +276 -0
  168. package/build/src/integrations/openapi.d.ts +39 -0
  169. package/build/src/integrations/openapi.js +323 -0
  170. package/build/src/integrations/print.d.ts +37 -0
  171. package/build/src/integrations/print.js +124 -0
  172. package/build/src/integrations/webhooks.d.ts +98 -0
  173. package/build/src/integrations/webhooks.js +298 -0
  174. package/build/src/mcp/tools.d.ts +8 -0
  175. package/build/src/mcp/tools.js +156 -0
  176. package/build/src/org/org_service.d.ts +2 -0
  177. package/build/src/org/org_service.js +16 -0
  178. package/build/src/resource/define_resource.d.ts +5 -0
  179. package/build/src/resource/define_resource.js +68 -0
  180. package/build/src/resource/registry.d.ts +12 -0
  181. package/build/src/resource/registry.js +101 -0
  182. package/build/src/resource/types.d.ts +155 -0
  183. package/build/src/resource/types.js +1 -0
  184. package/build/src/resource/values.d.ts +5 -0
  185. package/build/src/resource/values.js +88 -0
  186. package/build/src/services/backup.d.ts +14 -0
  187. package/build/src/services/backup.js +59 -0
  188. package/build/src/services/settings.d.ts +21 -0
  189. package/build/src/services/settings.js +30 -0
  190. package/build/src/upgrade/legacy.d.ts +27 -0
  191. package/build/src/upgrade/legacy.js +58 -0
  192. package/build/src/upgrade/rename.d.ts +17 -0
  193. package/build/src/upgrade/rename.js +94 -0
  194. package/build/src/version.d.ts +1 -0
  195. package/build/src/version.js +2 -0
  196. package/build/src/workflows/define_workflow.d.ts +124 -0
  197. package/build/src/workflows/define_workflow.js +123 -0
  198. package/build/src/workflows/engine.d.ts +141 -0
  199. package/build/src/workflows/engine.js +752 -0
  200. package/build/stubs/README.md +6 -0
  201. package/build/stubs/controller.stub +8 -0
  202. package/build/stubs/main.d.ts +5 -0
  203. package/build/stubs/main.js +5 -0
  204. package/build/stubs/main.ts +5 -0
  205. package/build/stubs/modules.stub +6 -0
  206. package/build/stubs/resource_contract.stub +1 -0
  207. package/build/stubs/resource_contract.txt +452 -0
  208. package/build/stubs/routes.stub +16 -0
  209. package/build/stubs/service.stub +9 -0
  210. package/package.json +95 -6
@@ -0,0 +1,6 @@
1
+ # The stubs directory
2
+
3
+ The `stubs` directory stores all the stubs needed by your package. It could be config files you will publish during the initial setup or stubs you want to use within the scaffolding commands.
4
+
5
+ - Inside the `package.json` file, we have defined a `copy:templates` script that copies the `stubs` folder to the `build` folder.
6
+ - Ensure the `build/stubs` are always published to npm via the `files` array inside the `package.json` file.
@@ -0,0 +1,8 @@
1
+ {{{ exports({ to: app.makePath('app/controllers/resources_controller.ts') }) }}}
2
+ import { createResourceController } from 'adula'
3
+ import { adula } from '#services/adula'
4
+
5
+ export default createResourceController(async (ctx) => {
6
+ const runtime = adula()
7
+ return { ...runtime, actor: await runtime.actors.load(ctx.auth.getUserOrFail().id) }
8
+ })
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Path to the root directory where the stubs are stored. We use
3
+ * this path within commands and the configure hook
4
+ */
5
+ export declare const stubsRoot: string;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Path to the root directory where the stubs are stored. We use
3
+ * this path within commands and the configure hook
4
+ */
5
+ export const stubsRoot = import.meta.dirname;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Path to the root directory where the stubs are stored. We use
3
+ * this path within commands and the configure hook
4
+ */
5
+ export const stubsRoot = import.meta.dirname
@@ -0,0 +1,6 @@
1
+ {{{ exports({ to: app.makePath('start/modules.ts') }) }}}
2
+ import { ResourceRegistry } from 'adula'
3
+ import type { Module } from 'adula'
4
+ // adula:imports
5
+ export const modules: Module[] = [/* adula:modules */]
6
+ export const registry = new ResourceRegistry().register(modules)
@@ -0,0 +1 @@
1
+ {{{ exports({ to: app.makePath('tests/helpers/resource_contract.ts') }) }}}
@@ -0,0 +1,452 @@
1
+ import { test } from '@japa/runner'
2
+ import User from '#models/user'
3
+ import app from '@adonisjs/core/services/app'
4
+ import db from '@adonisjs/lucid/services/db'
5
+ import { adula } from '#services/adula'
6
+ import { randomUUID } from 'node:crypto'
7
+ import { columnName } from 'adula'
8
+ import type { Action, Field, RecordData, Resource, SerializedRecord } from 'adula'
9
+
10
+ export type FixtureContext = { userId: number; orgUnitId: number; unique: string }
11
+ export type ContractFixture = {
12
+ input: RecordData
13
+ /** Expected public values after validation; relations have separate tests. */
14
+ expected: SerializedRecord
15
+ /** Normalized database values for writable fields excluded from the response. */
16
+ stored?: RecordData
17
+ /** All live child rows after creation, ordered by ID, using public field names. */
18
+ inline?: Record<string, RecordData[]>
19
+ /**
20
+ * A valid update for this same record, including required validator fields. Required
21
+ * when the resource declares the `update` action.
22
+ */
23
+ update?: RecordData
24
+ updated?: SerializedRecord
25
+ updatedStored?: RecordData
26
+ updatedInline?: Record<string, RecordData[]>
27
+ }
28
+ export type ResourceFixture = (
29
+ context: FixtureContext
30
+ ) => Promise<ContractFixture> | ContractFixture
31
+
32
+ function storedValue(field: Field, value: unknown) {
33
+ if (!(value instanceof Date)) return value
34
+ return field.type === 'date'
35
+ ? [
36
+ value.getFullYear(),
37
+ String(value.getMonth() + 1).padStart(2, '0'),
38
+ String(value.getDate()).padStart(2, '0'),
39
+ ].join('-')
40
+ : value.toISOString()
41
+ }
42
+
43
+ /** Fixtures belong to the application and exercise its real validators and HTTP routes. */
44
+ export function resourceContract(name: string, fixture: ResourceFixture) {
45
+ test.group(`HTTP security contract: ${name}`, (group) => {
46
+ let denied: User
47
+ let writer: User
48
+ let reader: User
49
+ let outsider: User
50
+ let orgUnitId: number
51
+ let resource: Resource
52
+ const base = `/resources/${name}`
53
+ const fresh = () => fixture({ userId: writer.id, orgUnitId, unique: randomUUID() })
54
+ const input = (values: RecordData) => ({
55
+ ...values,
56
+ ...(resource.scoped && !resource.scope ? { orgUnitId } : {}),
57
+ })
58
+ const version = (row: SerializedRecord) => (resource.version ? { version: row.version } : {})
59
+ // Actions the resource does not declare are refused even to a role that manages all.
60
+ const allows = (action: Action) => resource.actions.includes(action)
61
+
62
+ group.setup(async () => {
63
+ const knex = db.connection().getWriteClient()
64
+ const databaseInfo = await knex.raw('SELECT current_database() AS name')
65
+ const database = databaseInfo.rows[0].name
66
+ if (!app.inTest || !database.endsWith('_test'))
67
+ throw new Error('Resource contracts require a dedicated *_test database')
68
+ resource = adula().registry.get(name)
69
+ const suffix = randomUUID()
70
+ const users = []
71
+ for (const label of ['denied', 'writer', 'reader', 'outside']) {
72
+ users.push(
73
+ await User.create({
74
+ fullName: 'مستخدم الاختبار',
75
+ email: `${label}-${suffix}@example.test`,
76
+ password: 'a-long-test-password-123',
77
+ })
78
+ )
79
+ }
80
+ ;[denied, writer, reader, outsider] = users
81
+ const [org, outside] = await knex('org_units')
82
+ .insert([
83
+ { name: 'نطاق الاختبار', type: 'root', path: `contract_${suffix.replaceAll('-', '_')}` },
84
+ { name: 'نطاق آخر', type: 'root', path: `outside_${suffix.replaceAll('-', '_')}` },
85
+ ])
86
+ .returning('id')
87
+ orgUnitId = org.id
88
+ const [writeRole, readRole] = await knex('roles')
89
+ .insert([
90
+ { name: `writer-${suffix}`, permission_level: 1 },
91
+ { name: `reader-${suffix}`, permission_level: 0 },
92
+ ])
93
+ .returning('id')
94
+ await knex('role_rules').insert([
95
+ { role_id: writeRole.id, subject: 'all', action: 'manage' },
96
+ { role_id: readRole.id, subject: 'all', action: 'view' },
97
+ ])
98
+ await knex('user_roles').insert([
99
+ { user_id: writer.id, role_id: writeRole.id },
100
+ { user_id: outsider.id, role_id: writeRole.id },
101
+ { user_id: reader.id, role_id: readRole.id },
102
+ ])
103
+ await knex('user_org_units').insert([
104
+ { user_id: writer.id, org_unit_id: orgUnitId },
105
+ { user_id: reader.id, org_unit_id: orgUnitId },
106
+ { user_id: outsider.id, org_unit_id: outside.id },
107
+ ])
108
+ })
109
+
110
+ for (const [method, suffix] of [
111
+ ['get', ''],
112
+ ['get', '/create'],
113
+ ['get', '/999999/edit'],
114
+ ['get', '/999999'],
115
+ ['post', ''],
116
+ ['patch', '/999999'],
117
+ ['delete', '/999999'],
118
+ ['post', '/999999/submit'],
119
+ ['post', '/999999/cancel'],
120
+ ] as const) {
121
+ test(`${method.toUpperCase()} ${suffix || '/'} returns 403 without roles`, async ({
122
+ client,
123
+ }) => {
124
+ const response = await client[method](`${base}${suffix}`)
125
+ .loginAs(denied)
126
+ .withCsrfToken()
127
+ .header('Accept', 'application/json')
128
+ response.assertStatus(403)
129
+ })
130
+ }
131
+ test('resource route is protected from anonymous access', async ({ client }) => {
132
+ const response = await client.get(base).header('Accept', 'application/json')
133
+ response.assertStatus(401)
134
+ })
135
+ test('undeclared actions are refused to a role that manages all resources', async ({
136
+ client,
137
+ }) => {
138
+ for (const [action, method, suffix] of [
139
+ ['create', 'post', ''],
140
+ ['update', 'patch', '/999999'],
141
+ ['delete', 'delete', '/999999'],
142
+ ] as const) {
143
+ if (allows(action)) continue
144
+ const response = await client[method](`${base}${suffix}`)
145
+ .loginAs(writer)
146
+ .withCsrfToken()
147
+ .header('Accept', 'application/json')
148
+ response.assertStatus(403)
149
+ }
150
+ })
151
+ test('existing records enforce scope on reads and writes; central resources stay shared', async ({
152
+ client,
153
+ assert,
154
+ }) => {
155
+ if (!allows('create')) return
156
+ const values = await fresh()
157
+ const created = await client
158
+ .post(base)
159
+ .loginAs(writer)
160
+ .withCsrfToken()
161
+ .header('Accept', 'application/json')
162
+ .json(input(values.input))
163
+ created.assertStatus(201)
164
+ const row = created.body().data
165
+ const visible = await client
166
+ .get(`${base}/${row.id}`)
167
+ .loginAs(writer)
168
+ .header('Accept', 'application/json')
169
+ visible.assertStatus(200)
170
+ assert.equal(visible.body().data.id, row.id)
171
+ const outside = await client
172
+ .get(`${base}/${row.id}`)
173
+ .loginAs(outsider)
174
+ .header('Accept', 'application/json')
175
+ outside.assertStatus(resource.scoped ? 404 : 200)
176
+ if (!resource.scoped) return
177
+ const listing = await client.get(base).loginAs(outsider).header('Accept', 'application/json')
178
+ listing.assertStatus(200)
179
+ assert.notInclude(
180
+ listing.body().data.map((entry: SerializedRecord) => entry.id),
181
+ row.id
182
+ )
183
+ const edit = await client
184
+ .get(`${base}/${row.id}/edit`)
185
+ .loginAs(outsider)
186
+ .header('Accept', 'application/json')
187
+ edit.assertStatus(allows('update') ? 404 : 403)
188
+ const changed = await client
189
+ .patch(`${base}/${row.id}`)
190
+ .loginAs(outsider)
191
+ .withCsrfToken()
192
+ .header('Accept', 'application/json')
193
+ .json({ ...(values.update ?? values.input), ...version(row) })
194
+ changed.assertStatus(allows('update') ? 404 : 403)
195
+ const deleted = await client
196
+ .delete(`${base}/${row.id}`)
197
+ .loginAs(outsider)
198
+ .withCsrfToken()
199
+ .header('Accept', 'application/json')
200
+ .json(version(row))
201
+ deleted.assertStatus(allows('delete') ? 404 : 403)
202
+ const after = await client
203
+ .get(`${base}/${row.id}`)
204
+ .loginAs(writer)
205
+ .header('Accept', 'application/json')
206
+ after.assertStatus(200)
207
+ assert.deepEqual(after.body().data, row)
208
+ })
209
+ test('fixture fields round-trip, private values stay hidden, updates and soft deletion persist', async ({
210
+ client,
211
+ assert,
212
+ }) => {
213
+ if (!allows('create')) return
214
+ const values = await fresh()
215
+ for (const key of resource.form)
216
+ assert.property(values.input, key, `Missing ${name}.${key} fixture`)
217
+ const created = await client
218
+ .post(base)
219
+ .loginAs(writer)
220
+ .withCsrfToken()
221
+ .header('Accept', 'application/json')
222
+ .json(input(values.input))
223
+ created.assertStatus(201)
224
+ const row = created.body().data
225
+ assert.isNumber(row.id)
226
+ for (const [key, value] of Object.entries(values.expected))
227
+ assert.deepEqual(row[key], value, key)
228
+ const exposed = new Set([
229
+ 'id',
230
+ ...(resource.serialize ?? [...resource.list, ...resource.show]),
231
+ ...(resource.scoped ? ['orgUnitId'] : []),
232
+ ...(resource.version ? ['version'] : []),
233
+ ...(resource.submittable ? ['docStatus'] : []),
234
+ ])
235
+ for (const key of Object.keys(row))
236
+ assert.isTrue(exposed.has(key), `Unexpected serialized field ${key}`)
237
+ const assertStored = async (
238
+ submitted: RecordData,
239
+ expected: SerializedRecord,
240
+ stored: RecordData = {},
241
+ inline: Record<string, RecordData[]> = {}
242
+ ) => {
243
+ const persistedRow = await db
244
+ .connection()
245
+ .getWriteClient()(name)
246
+ .where('id', row.id)
247
+ .first()
248
+ const expectations = { ...expected, ...stored }
249
+ for (const key of Object.keys(submitted)) {
250
+ if (!resource.form.includes(key)) continue
251
+ const field = resource.fields[key]
252
+ if (field.type === 'hasMany') {
253
+ assert.property(inline, key, `Missing expected ${name}.${key} child rows`)
254
+ const child = adula().registry.get(field.resource)
255
+ const foreignKey = child.fields[field.foreignKey].column ?? columnName(field.foreignKey)
256
+ const rows = await db
257
+ .connection()
258
+ .getWriteClient()(child.name)
259
+ .where(foreignKey, row.id)
260
+ .whereNull('deleted_at')
261
+ .orderBy('id')
262
+ assert.lengthOf(rows, inline[key].length)
263
+ for (const [index, entry] of inline[key].entries()) {
264
+ for (const childKey of child.form.filter((entryKey) => entryKey !== field.foreignKey))
265
+ assert.property(entry, childKey, `Missing expected ${field.resource}.${childKey}`)
266
+ for (const [childKey, value] of Object.entries(entry)) {
267
+ const childField = child.fields[childKey]
268
+ assert.exists(childField, `Unknown expected child field ${childKey}`)
269
+ assert.deepEqual(
270
+ storedValue(childField, rows[index][childField.column ?? columnName(childKey)]),
271
+ value
272
+ )
273
+ }
274
+ }
275
+ } else {
276
+ assert.property(expectations, key, `Missing stored ${name}.${key} expectation`)
277
+ if (exposed.has(key))
278
+ assert.property(expected, key, `Missing public ${name}.${key} expectation`)
279
+ assert.deepEqual(
280
+ storedValue(field, persistedRow[field.column ?? columnName(key)]),
281
+ expectations[key],
282
+ key
283
+ )
284
+ }
285
+ }
286
+ }
287
+ await assertStored(values.input, values.expected, values.stored, values.inline)
288
+ const shown = await client
289
+ .get(`${base}/${row.id}`)
290
+ .loginAs(reader)
291
+ .header('Accept', 'application/json')
292
+ shown.assertStatus(200)
293
+ for (const [key, field] of Object.entries(resource.fields)) {
294
+ if (field.permissionLevel || resource.hidden?.includes(key))
295
+ assert.notProperty(shown.body().data, key)
296
+ }
297
+ const stored = async () => db.connection().getWriteClient()(name).where('id', row.id).first()
298
+ const inserted = await stored()
299
+ assert.equal(inserted.created_by, writer.id)
300
+ if (!allows('update')) {
301
+ const refused = await client
302
+ .patch(`${base}/${row.id}`)
303
+ .loginAs(writer)
304
+ .withCsrfToken()
305
+ .header('Accept', 'application/json')
306
+ .json({ ...values.input, ...version(row) })
307
+ refused.assertStatus(403)
308
+ }
309
+ const current = allows('update') ? await updateRecord() : row
310
+ if (!allows('delete')) {
311
+ const refused = await client
312
+ .delete(`${base}/${row.id}`)
313
+ .loginAs(writer)
314
+ .withCsrfToken()
315
+ .header('Accept', 'application/json')
316
+ .json(version(current))
317
+ refused.assertStatus(403)
318
+ const kept = await client
319
+ .get(`${base}/${row.id}`)
320
+ .loginAs(writer)
321
+ .header('Accept', 'application/json')
322
+ kept.assertStatus(200)
323
+ assert.deepEqual(kept.body().data, current)
324
+ return
325
+ }
326
+ const removed = await client
327
+ .delete(`${base}/${row.id}`)
328
+ .loginAs(writer)
329
+ .withCsrfToken()
330
+ .header('Accept', 'application/json')
331
+ .json(version(current))
332
+ removed.assertStatus(200)
333
+ const deleted = await stored()
334
+ assert.exists(deleted.deleted_at)
335
+ const missing = await client
336
+ .get(`${base}/${row.id}`)
337
+ .loginAs(writer)
338
+ .header('Accept', 'application/json')
339
+ missing.assertStatus(404)
340
+
341
+ async function updateRecord() {
342
+ assert.exists(values.update, `Missing ${name} update fixture`)
343
+ assert.exists(values.updated, `Missing ${name} updated fixture`)
344
+ const update = values.update!
345
+ for (const key of ['createdBy', 'updatedBy', 'deletedAt', 'orgPath', 'searchVector']) {
346
+ const invalid = await client
347
+ .patch(`${base}/${row.id}`)
348
+ .loginAs(writer)
349
+ .withCsrfToken()
350
+ .header('Accept', 'application/json')
351
+ .json({ ...update, ...version(row), [key]: writer.id })
352
+ invalid.assertStatus(422)
353
+ assert.equal(invalid.body().error.code, 'E_FIELD_NOT_WRITABLE')
354
+ }
355
+ const updated = await client
356
+ .patch(`${base}/${row.id}`)
357
+ .loginAs(writer)
358
+ .withCsrfToken()
359
+ .header('Accept', 'application/json')
360
+ .json({ ...update, ...version(row) })
361
+ updated.assertStatus(200)
362
+ for (const [key, value] of Object.entries(values.updated!))
363
+ assert.deepEqual(updated.body().data[key], value, key)
364
+ await assertStored(update, values.updated!, values.updatedStored, values.updatedInline)
365
+ if (resource.version) {
366
+ assert.equal(updated.body().data.version, Number(row.version) + 1)
367
+ const stale = await client
368
+ .patch(`${base}/${row.id}`)
369
+ .loginAs(writer)
370
+ .withCsrfToken()
371
+ .header('Accept', 'application/json')
372
+ .json({ ...update, ...version(row) })
373
+ stale.assertStatus(409)
374
+ }
375
+ const persisted = await client
376
+ .get(`${base}/${row.id}`)
377
+ .loginAs(writer)
378
+ .header('Accept', 'application/json')
379
+ persisted.assertStatus(200)
380
+ assert.deepEqual(persisted.body().data, updated.body().data)
381
+ return updated.body().data as SerializedRecord
382
+ }
383
+ })
384
+ test('unique constraints reject duplicates and allow reuse after soft deletion', async ({
385
+ client,
386
+ assert,
387
+ }) => {
388
+ const keys = Object.entries(resource.fields).filter(([, field]) => field.unique)
389
+ if (!keys.length || !allows('create')) return
390
+ const values = await fresh()
391
+ const created = await client
392
+ .post(base)
393
+ .loginAs(writer)
394
+ .withCsrfToken()
395
+ .header('Accept', 'application/json')
396
+ .json(input(values.input))
397
+ created.assertStatus(201)
398
+ const row = created.body().data
399
+ const knex = db.connection().getWriteClient()
400
+ const stored = await knex(name).where('id', row.id).first()
401
+ const distinct = await fresh()
402
+ const second = await client
403
+ .post(base)
404
+ .loginAs(writer)
405
+ .withCsrfToken()
406
+ .header('Accept', 'application/json')
407
+ .json(input(distinct.input))
408
+ second.assertStatus(201)
409
+ // Read-only sequence values also require real PostgreSQL uniqueness assertions.
410
+ for (const [key, field] of keys) {
411
+ const column = field.column ?? columnName(key)
412
+ assert.isNotNull(stored[column], `Missing unique fixture ${key}`)
413
+ await assert.rejects(
414
+ () =>
415
+ knex(name)
416
+ .where('id', second.body().data.id)
417
+ .update({ [column]: stored[column] }),
418
+ /duplicate key/
419
+ )
420
+ }
421
+ for (const [key, field] of keys) {
422
+ if (field.sequence || !resource.form.includes(key)) continue
423
+ const other = await fresh()
424
+ const rejected = await client
425
+ .post(base)
426
+ .loginAs(writer)
427
+ .withCsrfToken()
428
+ .header('Accept', 'application/json')
429
+ .json(input({ ...other.input, [key]: values.input[key] }))
430
+ rejected.assertStatus(409)
431
+ assert.equal(rejected.body().error.code, 'E_DUPLICATE')
432
+ }
433
+ if (!allows('delete')) return
434
+ const removed = await client
435
+ .delete(`${base}/${row.id}`)
436
+ .loginAs(writer)
437
+ .withCsrfToken()
438
+ .header('Accept', 'application/json')
439
+ .json(version(row))
440
+ removed.assertStatus(200)
441
+ for (const [key, field] of keys) {
442
+ const column = field.column ?? columnName(key)
443
+ assert.equal(
444
+ await knex(name)
445
+ .where('id', second.body().data.id)
446
+ .update({ [column]: stored[column] }),
447
+ 1
448
+ )
449
+ }
450
+ })
451
+ })
452
+ }
@@ -0,0 +1,16 @@
1
+ {{{ exports({ to: app.makePath('start/adula_routes.ts') }) }}}
2
+ import router from '@adonisjs/core/services/router'
3
+ import { middleware } from '#start/kernel'
4
+ const ResourcesController = () => import('#controllers/resources_controller')
5
+
6
+ router.group(() => {
7
+ router.get('/resources/:resource', [ResourcesController, 'index'])
8
+ router.get('/resources/:resource/create', [ResourcesController, 'create'])
9
+ router.get('/resources/:resource/:id/edit', [ResourcesController, 'edit'])
10
+ router.get('/resources/:resource/:id', [ResourcesController, 'show'])
11
+ router.post('/resources/:resource', [ResourcesController, 'store'])
12
+ router.patch('/resources/:resource/:id', [ResourcesController, 'update'])
13
+ router.delete('/resources/:resource/:id', [ResourcesController, 'destroy'])
14
+ router.post('/resources/:resource/:id/submit', [ResourcesController, 'submit'])
15
+ router.post('/resources/:resource/:id/cancel', [ResourcesController, 'cancel'])
16
+ }).use(middleware.auth())
@@ -0,0 +1,9 @@
1
+ {{{ exports({ to: app.makePath('app/services/adula.ts') }) }}}
2
+ import db from '@adonisjs/lucid/services/db'
3
+ import { ActorStore, ResourceService } from 'adula'
4
+ import { registry } from '#start/modules'
5
+
6
+ export function adula() {
7
+ const knex = db.connection().getWriteClient()
8
+ return { registry, resources: new ResourceService(knex, registry), actors: new ActorStore(knex, registry) }
9
+ }
package/package.json CHANGED
@@ -1,9 +1,98 @@
1
1
  {
2
2
  "name": "adula",
3
- "version": "0.0.0",
4
- "description": "AdulaJS — resource-driven business applications for AdonisJS. Placeholder; the first release is in preparation.",
3
+ "description": "Resource-driven business applications for AdonisJS 7",
4
+ "version": "2.1.0",
5
+ "engines": {
6
+ "node": ">=24.0.0"
7
+ },
8
+ "type": "module",
9
+ "files": [
10
+ "build",
11
+ "!build/bin",
12
+ "!build/tests"
13
+ ],
14
+ "main": "build/index.js",
15
+ "exports": {
16
+ ".": "./build/index.js",
17
+ "./types": "./build/src/resource/types.js",
18
+ "./auth": "./build/src/auth/ability.js",
19
+ "./mcp": "./build/src/mcp/tools.js",
20
+ "./provider": "./build/providers/adula_provider.js",
21
+ "./commands": "./build/commands/main.js",
22
+ "./eslint": "./build/src/eslint/index.js",
23
+ "./migrations/*": "./build/database/migrations/*.js",
24
+ "./package.json": "./package.json"
25
+ },
26
+ "keywords": [
27
+ "adonisjs",
28
+ "business-applications",
29
+ "arabic",
30
+ "rtl",
31
+ "postgresql",
32
+ "authorization"
33
+ ],
34
+ "author": "adula contributors",
5
35
  "license": "MIT",
6
- "author": "adula",
7
- "repository": { "type": "git", "url": "git+https://github.com/adulash/adula-kit.git" },
8
- "files": ["README.md"]
9
- }
36
+ "devDependencies": {
37
+ "@adonisjs/assembler": "8.5.0",
38
+ "@adonisjs/core": "7.5.0",
39
+ "@adonisjs/drive": "4.0.0",
40
+ "@adonisjs/eslint-config": "3.1.0",
41
+ "@adonisjs/lucid": "22.4.2",
42
+ "@adonisjs/tsconfig": "2.0.0",
43
+ "@japa/assert": "4.2.0",
44
+ "@japa/runner": "5.3.0",
45
+ "@jrmc/adonis-mcp": "2.0.0",
46
+ "@poppinss/ts-exec": "1.4.4",
47
+ "@types/node": "26.2.0",
48
+ "@types/papaparse": "5.5.2",
49
+ "eslint": "10.9.0",
50
+ "pg": "8.16.3",
51
+ "typescript": "6.0.3"
52
+ },
53
+ "peerDependencies": {
54
+ "@adonisjs/assembler": "^8.5.0",
55
+ "@adonisjs/core": "^7.0.0",
56
+ "@adonisjs/drive": "^4.0.0",
57
+ "@adonisjs/lucid": "^22.0.0",
58
+ "@jrmc/adonis-mcp": "^2.0.0"
59
+ },
60
+ "peerDependenciesMeta": {
61
+ "@jrmc/adonis-mcp": {
62
+ "optional": true
63
+ },
64
+ "@adonisjs/assembler": {
65
+ "optional": true
66
+ },
67
+ "@adonisjs/drive": {
68
+ "optional": true
69
+ }
70
+ },
71
+ "publishConfig": {
72
+ "access": "public",
73
+ "provenance": true
74
+ },
75
+ "prettier": "@adonisjs/prettier-config",
76
+ "dependencies": {
77
+ "@casl/ability": "7.0.1",
78
+ "knex": "3.3.0",
79
+ "papaparse": "5.7.0",
80
+ "xstate": "5.33.2"
81
+ },
82
+ "repository": {
83
+ "type": "git",
84
+ "url": "git+https://github.com/adulash/adulajs.git",
85
+ "directory": "packages/adula"
86
+ },
87
+ "homepage": "https://github.com/adulash/adulajs/tree/main/packages/adula#readme",
88
+ "bugs": {
89
+ "url": "https://github.com/adulash/adulajs/issues"
90
+ },
91
+ "scripts": {
92
+ "build": "node ../../scripts/clean-core-build.mjs && tsc -p tsconfig.build.json && node ../../scripts/copy-core-assets.mjs",
93
+ "typecheck": "tsc --noEmit",
94
+ "test": "node --import=@poppinss/ts-exec bin/test.ts",
95
+ "lint": "eslint src providers commands database tests index.ts",
96
+ "capabilities": "node --import=@poppinss/ts-exec bin/generate_capabilities.ts"
97
+ }
98
+ }