@crombolo/data 0.37.0 → 0.38.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 +3 -2
- package/src/audit.ts +163 -133
- package/src/change-report.ts +66 -43
- package/src/cluster.ts +119 -59
- package/src/content-query.ts +205 -200
- package/src/{bundle-tables → custom-tables}/builder.ts +104 -12
- package/src/custom-tables/chain.ts +150 -0
- package/src/custom-tables/ddl.ts +159 -0
- package/src/custom-tables/handle.ts +141 -0
- package/src/{bundle-tables → custom-tables}/index.ts +3 -2
- package/src/custom-tables/names.ts +94 -0
- package/src/custom-tables/owners.ts +299 -0
- package/src/{bundle-tables → custom-tables}/schema.ts +263 -90
- package/src/custom-tables/values.ts +181 -0
- package/src/database.ts +40 -5
- package/src/dialect.ts +43 -16
- package/src/index.ts +8 -1
- package/src/migrations.ts +130 -88
- package/src/query.ts +158 -0
- package/src/relations.ts +49 -28
- package/src/repositories/approvals.ts +130 -89
- package/src/repositories/bundle-grants.ts +74 -0
- package/src/repositories/bundles.ts +85 -76
- package/src/repositories/content-applied.ts +47 -28
- package/src/repositories/content-events.ts +114 -67
- package/src/repositories/content-notifications.ts +102 -74
- package/src/repositories/content-types.ts +449 -320
- package/src/repositories/data-types.ts +105 -56
- package/src/repositories/delivery-member-tokens.ts +37 -28
- package/src/repositories/dictionary.ts +77 -58
- package/src/repositories/documents.ts +956 -580
- package/src/repositories/domains.ts +28 -16
- package/src/repositories/dynamic-roots.ts +32 -17
- package/src/repositories/external-logins.ts +38 -24
- package/src/repositories/folders.ts +7 -4
- package/src/repositories/form-entries.ts +120 -103
- package/src/repositories/form-workflows.ts +87 -62
- package/src/repositories/jobs.ts +296 -0
- package/src/repositories/languages.ts +51 -38
- package/src/repositories/log-searches.ts +7 -5
- package/src/repositories/media-access.ts +173 -123
- package/src/repositories/media-listing.ts +155 -92
- package/src/repositories/members.ts +189 -162
- package/src/repositories/nodes.ts +227 -181
- package/src/repositories/notifications.ts +22 -17
- package/src/repositories/public-access.ts +77 -61
- package/src/repositories/redirects.ts +113 -70
- package/src/repositories/references.ts +55 -44
- package/src/repositories/remote-jobs.ts +45 -35
- package/src/repositories/tags.ts +11 -10
- package/src/repositories/templates.ts +24 -14
- package/src/repositories/transfer-answers.ts +58 -50
- package/src/repositories/transfer-runs.ts +93 -75
- package/src/repositories/two-factor.ts +74 -55
- package/src/repositories/user-data.ts +54 -36
- package/src/repositories/users.ts +345 -300
- package/src/repositories/webhooks.ts +196 -137
- package/src/schema/bundle-grants.ts +32 -0
- package/src/schema/jobs.ts +56 -0
- package/src/schema/plan.ts +4 -0
- package/src/schema-ops.ts +15 -12
- package/src/schema-state.ts +53 -38
- package/src/search.ts +175 -129
- package/src/setup.ts +17 -13
- package/src/site-search.ts +134 -83
- package/src/table-types.ts +152 -0
- package/src/tables.gen.ts +859 -0
- package/src/tree-invariants.ts +21 -12
- package/src/url-options.ts +17 -13
- package/src/workflow-store.ts +17 -13
- package/src/bundle-tables/capability.ts +0 -377
- package/src/bundle-tables/chain.ts +0 -105
- package/src/bundle-tables/ddl.ts +0 -124
- package/src/bundle-tables/guard.ts +0 -116
- package/src/bundle-tables/names.ts +0 -58
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { normaliseUuid } from '@crombolo/core'
|
|
7
7
|
import type { Db } from '../database.ts'
|
|
8
8
|
import { DbDate } from '../dialect.ts'
|
|
9
|
+
import { sql } from '../query.ts'
|
|
9
10
|
|
|
10
11
|
export interface TwoFactorLogin {
|
|
11
12
|
ownerKey: string
|
|
@@ -23,10 +24,12 @@ export class TwoFactorRepository {
|
|
|
23
24
|
|
|
24
25
|
/** The providers an owner has turned on. */
|
|
25
26
|
async providers(ownerKey: string): Promise<string[]> {
|
|
26
|
-
const rows = await this.#db.
|
|
27
|
-
'
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const rows = await this.#db.q
|
|
28
|
+
.selectFrom('two_factor_login')
|
|
29
|
+
.select('provider_name')
|
|
30
|
+
.where('owner_key', '=', normaliseUuid(ownerKey))
|
|
31
|
+
.orderBy('provider_name')
|
|
32
|
+
.execute()
|
|
30
33
|
return rows.map((row) => String(row.provider_name))
|
|
31
34
|
}
|
|
32
35
|
|
|
@@ -34,19 +37,22 @@ export class TwoFactorRepository {
|
|
|
34
37
|
async enabledAmong(ownerKeys: readonly string[]): Promise<Set<string>> {
|
|
35
38
|
if (ownerKeys.length === 0) return new Set()
|
|
36
39
|
const keys = ownerKeys.map(normaliseUuid)
|
|
37
|
-
const rows = await this.#db.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
const rows = await this.#db.q
|
|
41
|
+
.selectFrom('two_factor_login')
|
|
42
|
+
.select('owner_key')
|
|
43
|
+
.distinct()
|
|
44
|
+
.where('owner_key', 'in', keys)
|
|
45
|
+
.execute()
|
|
41
46
|
return new Set(rows.map((row) => normaliseUuid(String(row.owner_key))))
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
async get(ownerKey: string, providerName: string): Promise<TwoFactorLogin | undefined> {
|
|
45
|
-
const
|
|
46
|
-
'
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
const row = await this.#db.q
|
|
51
|
+
.selectFrom('two_factor_login')
|
|
52
|
+
.selectAll()
|
|
53
|
+
.where('owner_key', '=', normaliseUuid(ownerKey))
|
|
54
|
+
.where('provider_name', '=', providerName)
|
|
55
|
+
.executeTakeFirst()
|
|
50
56
|
if (!row) return undefined
|
|
51
57
|
return {
|
|
52
58
|
ownerKey: normaliseUuid(String(row.owner_key)),
|
|
@@ -69,69 +75,82 @@ export class TwoFactorRepository {
|
|
|
69
75
|
): Promise<void> {
|
|
70
76
|
const owner = normaliseUuid(ownerKey)
|
|
71
77
|
await this.#db.transaction(async (tx) => {
|
|
72
|
-
await tx.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
await tx.q
|
|
79
|
+
.deleteFrom('two_factor_login')
|
|
80
|
+
.where('owner_key', '=', owner)
|
|
81
|
+
.where('provider_name', '=', providerName)
|
|
82
|
+
.execute()
|
|
83
|
+
await tx.q
|
|
84
|
+
.insertInto('two_factor_login')
|
|
85
|
+
.values({
|
|
86
|
+
owner_key: owner,
|
|
87
|
+
provider_name: providerName,
|
|
88
|
+
secret: sealedSecret,
|
|
89
|
+
last_used_step: acceptedStep,
|
|
90
|
+
create_date: DbDate.toDb(new Date()),
|
|
91
|
+
})
|
|
92
|
+
.execute()
|
|
93
|
+
await tx.q.deleteFrom('two_factor_recovery_code').where('owner_key', '=', owner).execute()
|
|
82
94
|
for (const hash of recoveryHashes)
|
|
83
|
-
await tx.
|
|
84
|
-
|
|
85
|
-
hash
|
|
86
|
-
|
|
95
|
+
await tx.q
|
|
96
|
+
.insertInto('two_factor_recovery_code')
|
|
97
|
+
.values({ owner_key: owner, code_hash: hash })
|
|
98
|
+
.execute()
|
|
87
99
|
})
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
/** Records a step a code was accepted for, refusing one at or before the last; true when recorded. */
|
|
91
103
|
async useStep(ownerKey: string, providerName: string, step: number): Promise<boolean> {
|
|
92
|
-
const rows = await this.#db.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
const rows = await this.#db.q
|
|
105
|
+
.updateTable('two_factor_login')
|
|
106
|
+
.set({ last_used_step: step })
|
|
107
|
+
.where('owner_key', '=', normaliseUuid(ownerKey))
|
|
108
|
+
.where('provider_name', '=', providerName)
|
|
109
|
+
.where((eb) => eb.or([eb('last_used_step', 'is', null), eb('last_used_step', '<', step)]))
|
|
110
|
+
.returning('owner_key')
|
|
111
|
+
.execute()
|
|
98
112
|
return rows.length > 0
|
|
99
113
|
}
|
|
100
114
|
|
|
101
115
|
/** Spends a recovery code by its hash; true when it was there and unused. */
|
|
102
116
|
async useRecoveryCode(ownerKey: string, hash: string): Promise<boolean> {
|
|
103
|
-
const rows = await this.#db.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
117
|
+
const rows = await this.#db.q
|
|
118
|
+
.updateTable('two_factor_recovery_code')
|
|
119
|
+
.set({ used_date: DbDate.toDb(new Date()) })
|
|
120
|
+
.where('owner_key', '=', normaliseUuid(ownerKey))
|
|
121
|
+
.where('code_hash', '=', hash)
|
|
122
|
+
.where('used_date', 'is', null)
|
|
123
|
+
.returning('id')
|
|
124
|
+
.execute()
|
|
108
125
|
return rows.length > 0
|
|
109
126
|
}
|
|
110
127
|
|
|
111
128
|
async recoveryCodesLeft(ownerKey: string): Promise<number> {
|
|
112
|
-
const
|
|
113
|
-
'
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
129
|
+
const row = await this.#db.q
|
|
130
|
+
.selectFrom('two_factor_recovery_code')
|
|
131
|
+
.select(sql<number>`count(*)`.as('n'))
|
|
132
|
+
.where('owner_key', '=', normaliseUuid(ownerKey))
|
|
133
|
+
.where('used_date', 'is', null)
|
|
134
|
+
.executeTakeFirst()
|
|
135
|
+
return Number(row?.n ?? 0)
|
|
117
136
|
}
|
|
118
137
|
|
|
119
138
|
/** Turns a provider off, or every provider when none is named, with the recovery codes once none is left. */
|
|
120
139
|
async disable(ownerKey: string, providerName?: string): Promise<void> {
|
|
121
140
|
const owner = normaliseUuid(ownerKey)
|
|
122
141
|
await this.#db.transaction(async (tx) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (Number(left
|
|
134
|
-
await tx.
|
|
142
|
+
await tx.q
|
|
143
|
+
.deleteFrom('two_factor_login')
|
|
144
|
+
.where('owner_key', '=', owner)
|
|
145
|
+
.$if(!!providerName, (q) => q.where('provider_name', '=', providerName ?? ''))
|
|
146
|
+
.execute()
|
|
147
|
+
const left = await tx.q
|
|
148
|
+
.selectFrom('two_factor_login')
|
|
149
|
+
.select(sql<number>`count(*)`.as('n'))
|
|
150
|
+
.where('owner_key', '=', owner)
|
|
151
|
+
.executeTakeFirst()
|
|
152
|
+
if (Number(left?.n ?? 0) === 0)
|
|
153
|
+
await tx.q.deleteFrom('two_factor_recovery_code').where('owner_key', '=', owner).execute()
|
|
135
154
|
})
|
|
136
155
|
}
|
|
137
156
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Per-user key/value data the backoffice keeps between sessions (Umbraco's `umbracoUserData`). */
|
|
2
2
|
import { normaliseUuid } from '@crombolo/core'
|
|
3
3
|
import type { Db } from '../database.ts'
|
|
4
|
+
import { sql } from '../query.ts'
|
|
4
5
|
|
|
5
6
|
export interface UserDataEntry {
|
|
6
7
|
key: string
|
|
@@ -31,60 +32,77 @@ export class UserDataRepository {
|
|
|
31
32
|
skip: number,
|
|
32
33
|
take: number,
|
|
33
34
|
): Promise<{ total: number; items: UserDataEntry[] }> {
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const rows = await this.#db.query(
|
|
45
|
-
`SELECT key, data_group, identifier, value FROM user_data WHERE ${where.join(' AND ')}
|
|
46
|
-
ORDER BY data_group, identifier`,
|
|
47
|
-
params,
|
|
48
|
-
)
|
|
35
|
+
const { groups, identifiers } = filter
|
|
36
|
+
const rows = await this.#db.q
|
|
37
|
+
.selectFrom('user_data')
|
|
38
|
+
.select(['key', 'data_group', 'identifier', 'value'])
|
|
39
|
+
.where('user_id', '=', userId)
|
|
40
|
+
.$if(!!groups?.length, (q) => q.where('data_group', 'in', groups ?? []))
|
|
41
|
+
.$if(!!identifiers?.length, (q) => q.where('identifier', 'in', identifiers ?? []))
|
|
42
|
+
.orderBy('data_group')
|
|
43
|
+
.orderBy('identifier')
|
|
44
|
+
.execute()
|
|
49
45
|
return { total: rows.length, items: rows.slice(skip, skip + take).map(map) }
|
|
50
46
|
}
|
|
51
47
|
|
|
52
48
|
async get(userId: number, key: string): Promise<UserDataEntry | undefined> {
|
|
53
|
-
const
|
|
54
|
-
'
|
|
55
|
-
[
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
const row = await this.#db.q
|
|
50
|
+
.selectFrom('user_data')
|
|
51
|
+
.select(['key', 'data_group', 'identifier', 'value'])
|
|
52
|
+
.where('user_id', '=', userId)
|
|
53
|
+
.where('key', '=', normaliseUuid(key))
|
|
54
|
+
.executeTakeFirst()
|
|
55
|
+
return row ? map(row) : undefined
|
|
58
56
|
}
|
|
59
57
|
|
|
60
58
|
async create(userId: number, entry: UserDataEntry): Promise<UserDataResult> {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
const key = normaliseUuid(entry.key)
|
|
60
|
+
const clash = await this.#db.q
|
|
61
|
+
.selectFrom('user_data')
|
|
62
|
+
.select(sql<number>`1`.as('one'))
|
|
63
|
+
.where((eb) =>
|
|
64
|
+
eb.or([
|
|
65
|
+
eb('key', '=', key),
|
|
66
|
+
eb.and([
|
|
67
|
+
eb('user_id', '=', userId),
|
|
68
|
+
eb('data_group', '=', entry.group),
|
|
69
|
+
eb('identifier', '=', entry.identifier),
|
|
70
|
+
]),
|
|
71
|
+
]),
|
|
72
|
+
)
|
|
73
|
+
.execute()
|
|
65
74
|
if (clash.length > 0) return 'AlreadyExists'
|
|
66
|
-
await this.#db.
|
|
67
|
-
'
|
|
68
|
-
|
|
69
|
-
|
|
75
|
+
await this.#db.q
|
|
76
|
+
.insertInto('user_data')
|
|
77
|
+
.values({
|
|
78
|
+
key,
|
|
79
|
+
user_id: userId,
|
|
80
|
+
data_group: entry.group,
|
|
81
|
+
identifier: entry.identifier,
|
|
82
|
+
value: entry.value,
|
|
83
|
+
})
|
|
84
|
+
.execute()
|
|
70
85
|
return 'Success'
|
|
71
86
|
}
|
|
72
87
|
|
|
73
88
|
async update(userId: number, entry: UserDataEntry): Promise<UserDataResult> {
|
|
74
89
|
if (!(await this.get(userId, entry.key))) return 'NotFound'
|
|
75
|
-
await this.#db.
|
|
76
|
-
'
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
await this.#db.q
|
|
91
|
+
.updateTable('user_data')
|
|
92
|
+
.set({ data_group: entry.group, identifier: entry.identifier, value: entry.value })
|
|
93
|
+
.where('user_id', '=', userId)
|
|
94
|
+
.where('key', '=', normaliseUuid(entry.key))
|
|
95
|
+
.execute()
|
|
79
96
|
return 'Success'
|
|
80
97
|
}
|
|
81
98
|
|
|
82
99
|
async delete(userId: number, key: string): Promise<UserDataResult> {
|
|
83
100
|
if (!(await this.get(userId, key))) return 'NotFound'
|
|
84
|
-
await this.#db.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
101
|
+
await this.#db.q
|
|
102
|
+
.deleteFrom('user_data')
|
|
103
|
+
.where('user_id', '=', userId)
|
|
104
|
+
.where('key', '=', normaliseUuid(key))
|
|
105
|
+
.execute()
|
|
88
106
|
return 'Success'
|
|
89
107
|
}
|
|
90
108
|
}
|