@murumets-ee/entity 0.1.4 → 0.1.6
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/dist/admin/index.d.mts +452 -0
- package/dist/admin/index.d.mts.map +1 -0
- package/dist/admin/index.mjs +2 -0
- package/dist/admin/index.mjs.map +1 -0
- package/dist/index.d.mts +903 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +65 -0
- package/dist/index.mjs.map +1 -0
- package/dist/query/index.d.mts +362 -0
- package/dist/query/index.d.mts.map +1 -0
- package/dist/query/index.mjs +7 -0
- package/dist/query/index.mjs.map +1 -0
- package/dist/refs/index.d.mts +249 -0
- package/dist/refs/index.d.mts.map +1 -0
- package/dist/refs/index.mjs +2 -0
- package/dist/refs/index.mjs.map +1 -0
- package/package.json +13 -13
- package/dist/admin/index.d.ts +0 -510
- package/dist/admin/index.js +0 -1
- package/dist/index.d.ts +0 -1027
- package/dist/index.js +0 -64
- package/dist/query/index.d.ts +0 -417
- package/dist/query/index.js +0 -6
- package/dist/refs/index.d.ts +0 -263
- package/dist/refs/index.js +0 -1
package/dist/refs/index.d.ts
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
|
-
import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Generic entity usage lookup — "where is this entity referenced?"
|
|
6
|
-
*
|
|
7
|
-
* One indexed query on entity_refs instead of scanning every table.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
interface EntityUsage {
|
|
11
|
-
sourceEntity: string;
|
|
12
|
-
sourceId: string;
|
|
13
|
-
sourceField: string;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Find all entities that reference a given entity.
|
|
17
|
-
*
|
|
18
|
-
* @param targetEntity - Entity type name (e.g. 'media', 'category')
|
|
19
|
-
* @param targetId - UUID of the referenced entity
|
|
20
|
-
* @param db - Database connection
|
|
21
|
-
* @returns Array of usage records (empty if unreferenced)
|
|
22
|
-
*/
|
|
23
|
-
declare function findEntityUsages(targetEntity: string, targetId: string, db: PostgresJsDatabase): Promise<EntityUsage[]>;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Error thrown when attempting to delete an entity that is still referenced.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
declare class ReferencedEntityError extends Error {
|
|
30
|
-
readonly entityName: string;
|
|
31
|
-
readonly entityId: string;
|
|
32
|
-
readonly usages: EntityUsage[];
|
|
33
|
-
constructor(entityName: string, entityId: string, usages: EntityUsage[]);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Field type definitions
|
|
38
|
-
* These define the structure and configuration for all field types
|
|
39
|
-
*/
|
|
40
|
-
interface BaseFieldConfig {
|
|
41
|
-
required?: boolean;
|
|
42
|
-
default?: unknown;
|
|
43
|
-
translatable?: boolean;
|
|
44
|
-
indexed?: boolean;
|
|
45
|
-
unique?: boolean;
|
|
46
|
-
access?: {
|
|
47
|
-
view?: string;
|
|
48
|
-
edit?: string;
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
interface IdField extends BaseFieldConfig {
|
|
52
|
-
type: 'id';
|
|
53
|
-
}
|
|
54
|
-
interface TextField extends BaseFieldConfig {
|
|
55
|
-
type: 'text';
|
|
56
|
-
maxLength?: number;
|
|
57
|
-
minLength?: number;
|
|
58
|
-
pattern?: RegExp;
|
|
59
|
-
}
|
|
60
|
-
interface NumberField extends BaseFieldConfig {
|
|
61
|
-
type: 'number';
|
|
62
|
-
min?: number;
|
|
63
|
-
max?: number;
|
|
64
|
-
integer?: boolean;
|
|
65
|
-
}
|
|
66
|
-
interface BooleanField extends BaseFieldConfig {
|
|
67
|
-
type: 'boolean';
|
|
68
|
-
}
|
|
69
|
-
interface DateField extends BaseFieldConfig {
|
|
70
|
-
type: 'date';
|
|
71
|
-
minDate?: Date;
|
|
72
|
-
maxDate?: Date;
|
|
73
|
-
}
|
|
74
|
-
interface SelectField extends BaseFieldConfig {
|
|
75
|
-
type: 'select';
|
|
76
|
-
options: readonly string[];
|
|
77
|
-
}
|
|
78
|
-
interface ReferenceField extends BaseFieldConfig {
|
|
79
|
-
type: 'reference';
|
|
80
|
-
entity: string;
|
|
81
|
-
cardinality: 'one' | 'many';
|
|
82
|
-
onDelete?: 'cascade' | 'set-null' | 'restrict';
|
|
83
|
-
}
|
|
84
|
-
interface MediaField extends BaseFieldConfig {
|
|
85
|
-
type: 'media';
|
|
86
|
-
accept?: string[];
|
|
87
|
-
maxSize?: number;
|
|
88
|
-
}
|
|
89
|
-
interface RichTextField extends BaseFieldConfig {
|
|
90
|
-
type: 'richtext';
|
|
91
|
-
blocks?: string[];
|
|
92
|
-
}
|
|
93
|
-
interface SlugField extends BaseFieldConfig {
|
|
94
|
-
type: 'slug';
|
|
95
|
-
from: string;
|
|
96
|
-
unique?: boolean;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Minimal block definition reference — used by BlocksField to type-check allowed blocks.
|
|
100
|
-
* The full BlockDefinition (with label, etc.) lives in @murumets-ee/content.
|
|
101
|
-
*/
|
|
102
|
-
interface BlockDefinitionRef {
|
|
103
|
-
slug: string;
|
|
104
|
-
fields: Record<string, FieldConfig>;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Blocks field — ordered array of typed content blocks.
|
|
108
|
-
* Each block instance stores its data in {entity}_layout table.
|
|
109
|
-
*
|
|
110
|
-
* @property blocks - Allowed block definitions
|
|
111
|
-
* @property min/max - Block count constraints
|
|
112
|
-
* @property localized - true = per-locale layouts, false = shared layout with translated content (default)
|
|
113
|
-
*/
|
|
114
|
-
interface JsonField extends BaseFieldConfig {
|
|
115
|
-
type: 'json';
|
|
116
|
-
}
|
|
117
|
-
interface BlocksField extends BaseFieldConfig {
|
|
118
|
-
type: 'blocks';
|
|
119
|
-
blocks: readonly BlockDefinitionRef[];
|
|
120
|
-
min?: number;
|
|
121
|
-
max?: number;
|
|
122
|
-
localized?: boolean;
|
|
123
|
-
}
|
|
124
|
-
type FieldConfig = IdField | TextField | NumberField | BooleanField | DateField | SelectField | ReferenceField | MediaField | RichTextField | SlugField | JsonField | BlocksField;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Schema-aware reference extraction.
|
|
128
|
-
*
|
|
129
|
-
* Walks entity field definitions and data to find all outgoing references.
|
|
130
|
-
* Handles:
|
|
131
|
-
* - field.media() → target = 'media', single UUID
|
|
132
|
-
* - field.reference() → target = config.entity, single UUID or UUID[]
|
|
133
|
-
* - field.blocks() → inspects block instance data for media/reference fields
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
interface ExtractedRef {
|
|
137
|
-
sourceField: string;
|
|
138
|
-
targetEntity: string;
|
|
139
|
-
targetId: string;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Extract all outgoing references from entity data.
|
|
143
|
-
*
|
|
144
|
-
* @param allFields - The entity's complete field map (entity.allFields)
|
|
145
|
-
* @param data - The entity data (full on create, partial on update)
|
|
146
|
-
* @returns Deduplicated array of extracted references
|
|
147
|
-
*/
|
|
148
|
-
declare function extractRefs(allFields: Record<string, FieldConfig>, data: Record<string, unknown>): ExtractedRef[];
|
|
149
|
-
/**
|
|
150
|
-
* Get the names of all ref-bearing fields in the entity.
|
|
151
|
-
* Used to scope partial-update ref deletion.
|
|
152
|
-
*/
|
|
153
|
-
declare function getRefBearingFields(allFields: Record<string, FieldConfig>): string[];
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* entity_refs — Universal reference tracking table.
|
|
157
|
-
*
|
|
158
|
-
* Every reference between entities (field.media(), field.reference(), block media fields)
|
|
159
|
-
* gets a row here at write time. This enables:
|
|
160
|
-
* - Instant "where is this used?" queries (one indexed lookup)
|
|
161
|
-
* - Universal delete protection (can't delete referenced entities)
|
|
162
|
-
* - Correct results (schema-aware, no LIKE text search)
|
|
163
|
-
*/
|
|
164
|
-
declare const entityRefs: drizzle_orm_pg_core.PgTableWithColumns<{
|
|
165
|
-
name: "entity_refs";
|
|
166
|
-
schema: undefined;
|
|
167
|
-
columns: {
|
|
168
|
-
sourceEntity: drizzle_orm_pg_core.PgColumn<{
|
|
169
|
-
name: "source_entity";
|
|
170
|
-
tableName: "entity_refs";
|
|
171
|
-
dataType: "string";
|
|
172
|
-
columnType: "PgVarchar";
|
|
173
|
-
data: string;
|
|
174
|
-
driverParam: string;
|
|
175
|
-
notNull: true;
|
|
176
|
-
hasDefault: false;
|
|
177
|
-
isPrimaryKey: false;
|
|
178
|
-
isAutoincrement: false;
|
|
179
|
-
hasRuntimeDefault: false;
|
|
180
|
-
enumValues: [string, ...string[]];
|
|
181
|
-
baseColumn: never;
|
|
182
|
-
identity: undefined;
|
|
183
|
-
generated: undefined;
|
|
184
|
-
}, {}, {
|
|
185
|
-
length: 100;
|
|
186
|
-
}>;
|
|
187
|
-
sourceId: drizzle_orm_pg_core.PgColumn<{
|
|
188
|
-
name: "source_id";
|
|
189
|
-
tableName: "entity_refs";
|
|
190
|
-
dataType: "string";
|
|
191
|
-
columnType: "PgUUID";
|
|
192
|
-
data: string;
|
|
193
|
-
driverParam: string;
|
|
194
|
-
notNull: true;
|
|
195
|
-
hasDefault: false;
|
|
196
|
-
isPrimaryKey: false;
|
|
197
|
-
isAutoincrement: false;
|
|
198
|
-
hasRuntimeDefault: false;
|
|
199
|
-
enumValues: undefined;
|
|
200
|
-
baseColumn: never;
|
|
201
|
-
identity: undefined;
|
|
202
|
-
generated: undefined;
|
|
203
|
-
}, {}, {}>;
|
|
204
|
-
sourceField: drizzle_orm_pg_core.PgColumn<{
|
|
205
|
-
name: "source_field";
|
|
206
|
-
tableName: "entity_refs";
|
|
207
|
-
dataType: "string";
|
|
208
|
-
columnType: "PgVarchar";
|
|
209
|
-
data: string;
|
|
210
|
-
driverParam: string;
|
|
211
|
-
notNull: true;
|
|
212
|
-
hasDefault: false;
|
|
213
|
-
isPrimaryKey: false;
|
|
214
|
-
isAutoincrement: false;
|
|
215
|
-
hasRuntimeDefault: false;
|
|
216
|
-
enumValues: [string, ...string[]];
|
|
217
|
-
baseColumn: never;
|
|
218
|
-
identity: undefined;
|
|
219
|
-
generated: undefined;
|
|
220
|
-
}, {}, {
|
|
221
|
-
length: 100;
|
|
222
|
-
}>;
|
|
223
|
-
targetEntity: drizzle_orm_pg_core.PgColumn<{
|
|
224
|
-
name: "target_entity";
|
|
225
|
-
tableName: "entity_refs";
|
|
226
|
-
dataType: "string";
|
|
227
|
-
columnType: "PgVarchar";
|
|
228
|
-
data: string;
|
|
229
|
-
driverParam: string;
|
|
230
|
-
notNull: true;
|
|
231
|
-
hasDefault: false;
|
|
232
|
-
isPrimaryKey: false;
|
|
233
|
-
isAutoincrement: false;
|
|
234
|
-
hasRuntimeDefault: false;
|
|
235
|
-
enumValues: [string, ...string[]];
|
|
236
|
-
baseColumn: never;
|
|
237
|
-
identity: undefined;
|
|
238
|
-
generated: undefined;
|
|
239
|
-
}, {}, {
|
|
240
|
-
length: 100;
|
|
241
|
-
}>;
|
|
242
|
-
targetId: drizzle_orm_pg_core.PgColumn<{
|
|
243
|
-
name: "target_id";
|
|
244
|
-
tableName: "entity_refs";
|
|
245
|
-
dataType: "string";
|
|
246
|
-
columnType: "PgUUID";
|
|
247
|
-
data: string;
|
|
248
|
-
driverParam: string;
|
|
249
|
-
notNull: true;
|
|
250
|
-
hasDefault: false;
|
|
251
|
-
isPrimaryKey: false;
|
|
252
|
-
isAutoincrement: false;
|
|
253
|
-
hasRuntimeDefault: false;
|
|
254
|
-
enumValues: undefined;
|
|
255
|
-
baseColumn: never;
|
|
256
|
-
identity: undefined;
|
|
257
|
-
generated: undefined;
|
|
258
|
-
}, {}, {}>;
|
|
259
|
-
};
|
|
260
|
-
dialect: "pg";
|
|
261
|
-
}>;
|
|
262
|
-
|
|
263
|
-
export { type EntityUsage, type ExtractedRef, ReferencedEntityError, entityRefs, extractRefs, findEntityUsages, getRefBearingFields };
|
package/dist/refs/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var p=class extends Error{entityName;entityId;usages;constructor(i,s,r){let o=r.length;super(`Cannot delete ${i} '${s}': referenced by ${o} other entit${o===1?"y":"ies"}`),this.name="ReferencedEntityError",this.entityName=i,this.entityId=s,this.usages=r}};var h=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function I(e,i){let s=[],r=new Set;function o(n,d,t){if(!h.test(t))return;let c=`${n}|${d}|${t}`;r.has(c)||(r.add(c),s.push({sourceField:n,targetEntity:d,targetId:t}))}for(let[n,d]of Object.entries(e)){let t=i[n];if(t!=null)switch(d.type){case"media":{typeof t=="string"&&o(n,"media",t);break}case"reference":{let c=d;if(c.cardinality==="many"&&Array.isArray(t))for(let g of t)typeof g=="string"&&o(n,c.entity,g);else typeof t=="string"&&o(n,c.entity,t);break}case"blocks":{let c=d;if(!Array.isArray(t))break;for(let g of t){let b=g,E=b._block;if(!E)continue;let x=c.blocks.find(l=>l.slug===E);if(x)for(let[l,y]of Object.entries(x.fields)){let a=b[l];if(a!=null&&(y.type==="media"&&typeof a=="string"&&o(n,"media",a),y.type==="reference")){let u=y;if(u.cardinality==="many"&&Array.isArray(a))for(let k of a)typeof k=="string"&&o(n,u.entity,k);else typeof a=="string"&&o(n,u.entity,a)}}}break}}}return s}function U(e){let i=[];for(let[s,r]of Object.entries(e))(r.type==="media"||r.type==="reference"||r.type==="blocks")&&i.push(s);return i}import{and as w,eq as _}from"drizzle-orm";import{index as R,pgTable as N,unique as j,uuid as F,varchar as m}from"drizzle-orm/pg-core";var f=N("entity_refs",{sourceEntity:m("source_entity",{length:100}).notNull(),sourceId:F("source_id").notNull(),sourceField:m("source_field",{length:100}).notNull(),targetEntity:m("target_entity",{length:100}).notNull(),targetId:F("target_id").notNull()},e=>[j("uq_entity_refs").on(e.sourceEntity,e.sourceId,e.sourceField,e.targetEntity,e.targetId),R("idx_entity_refs_target").on(e.targetEntity,e.targetId),R("idx_entity_refs_source").on(e.sourceEntity,e.sourceId)]);async function C(e,i,s){return await s.select({sourceEntity:f.sourceEntity,sourceId:f.sourceId,sourceField:f.sourceField}).from(f).where(w(_(f.targetEntity,e),_(f.targetId,i)))}export{p as ReferencedEntityError,f as entityRefs,I as extractRefs,C as findEntityUsages,U as getRefBearingFields};
|