-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtable-meta-builder.ts
More file actions
134 lines (120 loc) · 3.79 KB
/
table-meta-builder.ts
File metadata and controls
134 lines (120 loc) · 3.79 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
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 { 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);
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),
};
}
export function collectTablesMeta(build: MetaBuild): TableMeta[] {
const configuredSchemas = getConfiguredSchemas(build);
const context = createBuildContext(build);
const seenCodecs = new Set<PgCodec>();
const tablesMeta: TableMeta[] = [];
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, schemaName, context));
}
return tablesMeta;
}