@crombolo/data 0.40.0 → 0.41.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.40.0",
3
+ "version": "0.41.1",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.40.0",
31
+ "@crombolo/core": "0.41.1",
32
32
  "kysely": "0.29.6"
33
33
  }
34
34
  }
package/src/index.ts CHANGED
@@ -60,6 +60,7 @@ export * from './schema/built-in-media-types.ts'
60
60
  export * from './schema/built-in-member-types.ts'
61
61
  export * from './schema/bundle-grants.ts'
62
62
  export * from './schema/bundle-package.ts'
63
+ export * from './schema/bundle-schema-picks.ts'
63
64
  export * from './schema/bundles-disabled.ts'
64
65
  export * from './schema/bundles-rename.ts'
65
66
  export * from './schema/change-report.ts'
@@ -139,6 +139,25 @@ const hydrate = (row: Row): Bundle => ({
139
139
  updateDate: DbDate.fromDb(row.update_date) ?? new Date(0),
140
140
  })
141
141
 
142
+ /** What a bundle picks beyond Umbraco's definition (story 226), by alias. */
143
+ export interface BundleSchemaPicks {
144
+ forms: string[]
145
+ workflows: string[]
146
+ userGroups: string[]
147
+ /** What a pick brought and its author unticked: `<kind>:<alias>`. */
148
+ excluded: string[]
149
+ }
150
+
151
+ export const NO_SCHEMA_PICKS: BundleSchemaPicks = {
152
+ forms: [],
153
+ workflows: [],
154
+ userGroups: [],
155
+ excluded: [],
156
+ }
157
+
158
+ const strings = (value: unknown): string[] =>
159
+ Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []
160
+
142
161
  export class BundleRepository {
143
162
  #db: Db
144
163
  constructor(db: Db) {
@@ -172,6 +191,38 @@ export class BundleRepository {
172
191
  return row ? hydrate(row) : undefined
173
192
  }
174
193
 
194
+ /** The forms, workflows and user groups it picks; none when it picks none. */
195
+ async schemaPicks(id: string): Promise<BundleSchemaPicks> {
196
+ const row = await this.#db.q
197
+ .selectFrom('bundle')
198
+ .select('schema_picks')
199
+ .where('id', '=', id)
200
+ .executeTakeFirst()
201
+ if (!row?.schema_picks) return { ...NO_SCHEMA_PICKS }
202
+ try {
203
+ const parsed = JSON.parse(row.schema_picks) as Record<string, unknown>
204
+ return {
205
+ forms: strings(parsed.forms),
206
+ workflows: strings(parsed.workflows),
207
+ userGroups: strings(parsed.userGroups),
208
+ excluded: strings(parsed.excluded),
209
+ }
210
+ } catch {
211
+ return { ...NO_SCHEMA_PICKS }
212
+ }
213
+ }
214
+
215
+ /** Replaces them; false when there is no such bundle. */
216
+ async setSchemaPicks(id: string, picks: BundleSchemaPicks): Promise<boolean> {
217
+ if (!(await this.byId(id))) return false
218
+ await this.#db.q
219
+ .updateTable('bundle')
220
+ .set({ schema_picks: JSON.stringify(picks), update_date: DbDate.toDb(new Date()) })
221
+ .where('id', '=', id)
222
+ .execute()
223
+ return true
224
+ }
225
+
175
226
  async byName(name: string): Promise<Bundle | undefined> {
176
227
  const row = await this.#db.q
177
228
  .selectFrom('bundle')
@@ -53,6 +53,9 @@ export class TagRepository {
53
53
  .where('pv.is_current', '=', this.#db.dialect.boolValue(true))
54
54
  .where('d.editor_alias', '=', 'Umbraco.Tags')
55
55
  .where('n.trashed', '=', this.#db.dialect.boolValue(false))
56
+ // The earliest page's spelling names a tag, whatever order the engine reads in.
57
+ .orderBy('pv.node_id')
58
+ .orderBy('pv.id')
56
59
  .execute()
57
60
  const tags = new Map<string, TagRow & { nodes: Set<number> }>()
58
61
  for (const row of rows) {
@@ -97,6 +97,33 @@ export interface UserGroupInput {
97
97
  granular: GranularPermission[]
98
98
  }
99
99
 
100
+ /** A group as a site's files declare it (story 228). */
101
+ export interface DeclaredUserGroup {
102
+ alias: string
103
+ name: string
104
+ description?: string
105
+ icon?: string
106
+ sections: readonly string[]
107
+ permissions: readonly string[]
108
+ languages: readonly string[] | 'all'
109
+ }
110
+
111
+ const sameSet = (a: readonly string[], b: readonly string[]) =>
112
+ a.length === new Set(b).size && a.every((item) => b.includes(item))
113
+
114
+ /** Whether a group already holds what its file says. */
115
+ function sameDeclared(existing: UserGroupRecord, input: UserGroupInput): boolean {
116
+ return (
117
+ existing.name === input.name &&
118
+ (existing.description ?? null) === input.description &&
119
+ (existing.icon ?? null) === input.icon &&
120
+ existing.hasAccessToAllLanguages === input.hasAccessToAllLanguages &&
121
+ sameSet(existing.sections, input.sections) &&
122
+ sameSet(existing.permissions, input.permissions) &&
123
+ sameSet(existing.languages, input.languages)
124
+ )
125
+ }
126
+
100
127
  export interface UserInput {
101
128
  key: string
102
129
  name: string
@@ -945,6 +972,47 @@ export class UserGroupRepository {
945
972
  })
946
973
  }
947
974
 
975
+ /**
976
+ * Creates or updates the groups a site declares in files (story 228), by
977
+ * alias: their name, description, icon, sections, verbs and languages. Start
978
+ * nodes, per-page permissions and members are the environment's, and kept.
979
+ */
980
+ async syncDeclared(groups: readonly DeclaredUserGroup[]): Promise<{
981
+ created: string[]
982
+ updated: string[]
983
+ }> {
984
+ const created: string[] = []
985
+ const updated: string[] = []
986
+ const none: GroupStartNode = { root: false, key: null }
987
+ for (const group of groups) {
988
+ const existing = await this.byAlias(group.alias)
989
+ const languages = group.languages === 'all' ? [] : [...group.languages]
990
+ const input: UserGroupInput = {
991
+ key: existing?.key ?? crypto.randomUUID(),
992
+ alias: group.alias,
993
+ name: group.name,
994
+ description: group.description ?? null,
995
+ icon: group.icon ?? null,
996
+ sections: [...group.sections],
997
+ languages,
998
+ hasAccessToAllLanguages: group.languages === 'all',
999
+ startNodes: existing?.startNodes ?? { document: none, media: none, element: none },
1000
+ permissions: [...group.permissions],
1001
+ granular: existing?.granular ?? [],
1002
+ }
1003
+ if (existing && sameDeclared(existing, input)) continue
1004
+ const saved = await this.save(input)
1005
+ if (saved === 'missing-language')
1006
+ throw new Error(
1007
+ `The user group "${group.alias}" names a language the site does not have: ${languages.join(', ')}.`,
1008
+ )
1009
+ if (saved !== 'saved')
1010
+ throw new Error(`The user group "${group.alias}" keeps a start node that no longer exists.`)
1011
+ ;(existing ? updated : created).push(group.alias)
1012
+ }
1013
+ return { created, updated }
1014
+ }
1015
+
948
1016
  async delete(key: string): Promise<void> {
949
1017
  const group = await this.byKey(key)
950
1018
  if (!group) return
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Migration 058: what a created bundle picks beyond Umbraco's definition (story
3
+ * 226): its forms, approval workflows and user groups, and what its author
4
+ * unticked of what they bring. JSON text, as the definition's other lists are.
5
+ */
6
+ import type { Db } from '../database.ts'
7
+ import type { Migration } from '../migrations.ts'
8
+ import { schemaOps } from '../schema-ops.ts'
9
+ import { STATE_SITE_SETTINGS_TYPES } from './site-settings-types.ts'
10
+
11
+ export const STATE_BUNDLE_SCHEMA_PICKS = '{4b198f00-0058-4ca8-bc39-97d92fca0058}'
12
+
13
+ export const bundleSchemaPicksMigration: Migration = {
14
+ from: STATE_SITE_SETTINGS_TYPES,
15
+ to: STATE_BUNDLE_SCHEMA_PICKS,
16
+ name: 'AddBundleSchemaPicks',
17
+ kind: 'expand',
18
+ release: '0.41.1',
19
+ async up(db: Db) {
20
+ await schemaOps(db).addColumn('bundle', 'schema_picks', db.dialect.types.text)
21
+ },
22
+ }
@@ -20,6 +20,7 @@ import { auditLogMigration } from './audit-log.ts'
20
20
  import { bundleComponentsMigration } from './bundle-components.ts'
21
21
  import { bundleGrantsMigration } from './bundle-grants.ts'
22
22
  import { bundlePackageMigration } from './bundle-package.ts'
23
+ import { bundleSchemaPicksMigration } from './bundle-schema-picks.ts'
23
24
  import { bundlesDisabledMigration } from './bundles-disabled.ts'
24
25
  import { bundlesRenameMigration } from './bundles-rename.ts'
25
26
  import { changeReportMigration } from './change-report.ts'
@@ -129,4 +130,5 @@ export const cromboloPlan = new MigrationPlan([
129
130
  jobsMigration,
130
131
  bundlesDisabledMigration,
131
132
  siteSettingsTypesMigration,
133
+ bundleSchemaPicksMigration,
132
134
  ])
package/src/tables.gen.ts CHANGED
@@ -107,6 +107,7 @@ export interface CoreTables {
107
107
  name: string
108
108
  package_name: string | null
109
109
  package_version: string | null
110
+ schema_picks: string | null
110
111
  scripts: string
111
112
  stylesheets: string
112
113
  update_date: Timestamp