-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtable-meta-builder.ts
More file actions
163 lines (147 loc) · 5.04 KB
/
Copy pathtable-meta-builder.ts
File metadata and controls
163 lines (147 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {
buildForeignKeyConstraints,
buildIndexes,
buildPrimaryKey,
buildUniqueConstraints,
} from './constraint-meta-builders';
import { buildInflectionMeta, buildQueryMeta, resolveTableType } from './name-meta-builders';
import {
buildBelongsToRelations,
buildManyToManyRelations,
buildReverseRelations,
} from './relation-meta-builders';
import { buildStorageMeta, buildSearchMeta, buildI18nMeta, buildRealtimeMeta } from './storage-search-meta-builders';
import { buildFieldMeta } from './type-mappings';
import {
createBuildContext,
type BuildContext,
type TableResourceWithCodec,
} from './table-meta-context';
import {
getConfiguredSchemas,
getRelations,
getSchemaName,
getUniques,
isTableResource,
} from './table-resource-utils';
import type {
ConstraintsMeta,
MetaBuild,
PgCodec,
TableMeta,
RelationsMeta,
} from './types';
function buildTableMeta(
resource: TableResourceWithCodec,
schemaName: string,
context: BuildContext,
): TableMeta {
const codec = resource.codec;
const attributes = codec.attributes;
const uniques = getUniques(resource);
const relations = getRelations(resource);
// Compute PK and FK attribute name sets for field metadata
const pkAttrNames = new Set<string>();
const fkAttrNames = new Set<string>();
for (const unique of uniques) {
if (unique.isPrimary) {
for (const attrName of unique.attributes) pkAttrNames.add(attrName);
}
}
for (const relation of Object.values(relations)) {
if (relation.isReferencee) continue;
for (const attrName of relation.localAttributes || []) fkAttrNames.add(attrName);
}
const fields = Object.entries(attributes).map(([attrName, attr]) =>
buildFieldMeta(context.inflectAttr(attrName, codec), attr, context.build, {
isPrimaryKey: pkAttrNames.has(attrName),
isForeignKey: fkAttrNames.has(attrName),
}),
);
const indexes = buildIndexes(codec, attributes, uniques, context);
const primaryKey = buildPrimaryKey(codec, attributes, uniques, context);
const uniqueConstraints = buildUniqueConstraints(codec, attributes, uniques, context);
const foreignKeyConstraints = buildForeignKeyConstraints(
codec,
attributes,
relations,
context,
);
const constraints: ConstraintsMeta = {
primaryKey,
unique: uniqueConstraints,
foreignKey: foreignKeyConstraints,
};
const belongsTo = buildBelongsToRelations(codec, attributes, uniques, relations, context);
const { hasOne, hasMany } = buildReverseRelations(codec, attributes, relations, context);
const manyToMany = buildManyToManyRelations(resource, codec, context);
const relationsMeta: RelationsMeta = {
belongsTo,
has: [...hasOne, ...hasMany],
hasOne,
hasMany,
manyToMany,
};
const tableType = resolveTableType(context.build, codec);
const storage = buildStorageMeta(codec);
const search = buildSearchMeta(codec, context.build, context.inflectAttr);
const i18n = buildI18nMeta(codec, context.build, context.inflectAttr);
const realtime = buildRealtimeMeta(codec, context.build);
return {
name: tableType,
schemaName,
fields,
indexes,
constraints,
foreignKeyConstraints,
primaryKeyConstraints: primaryKey ? [primaryKey] : [],
uniqueConstraints,
relations: relationsMeta,
inflection: buildInflectionMeta(resource, tableType, context.build),
query: buildQueryMeta(resource, uniques, tableType, context.build),
storage,
search,
i18n,
realtime,
};
}
/**
* Strip the tenant hash prefix (`<dashed-dbname>-<8hex>-`) from a physical
* schema name, returning the logical name; names without a hash segment are
* returned unchanged. Local duplicate of the server-side helper to avoid a
* cross-package dependency.
*/
function stripSchemaHashPrefix(name: string): string {
const match = /-[0-9a-f]{8}-/.exec(name);
if (!match) return name;
return name.slice(match.index + match[0].length);
}
export function collectTablesMeta(build: MetaBuild): TableMeta[] {
const configuredSchemas = getConfiguredSchemas(build);
const context = createBuildContext(build);
const seenCodecs = new Set<PgCodec>();
const tablesMeta: TableMeta[] = [];
// Shared (pooled) instances serve many tenants: reporting the build-time
// PHYSICAL schema name would leak the representative tenant's hashed schema
// identifier to every other tenant via _meta. When schema.constructivePooled
// is set, report the logical (hash-stripped) name instead.
const pooled = !!(build.options as any)?.constructivePooled;
for (const resource of Object.values(build.input.pgRegistry.pgResources || {})) {
if (!isTableResource(resource)) continue;
const codec = resource.codec;
if (seenCodecs.has(codec)) continue;
seenCodecs.add(codec);
const schemaName = getSchemaName(resource);
if (!schemaName) continue;
if (
configuredSchemas.length > 0 &&
!configuredSchemas.includes(schemaName)
) {
continue;
}
tablesMeta.push(
buildTableMeta(resource, pooled ? stripSchemaHashPrefix(schemaName) : schemaName, context)
);
}
return tablesMeta;
}