schematic-pg 0.1.2 → 0.1.4
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/README.md +158 -19
- package/dist/api/middleware/validate.d.ts +10 -0
- package/dist/api/middleware/validate.js +3 -0
- package/dist/api/utils/list-query.d.ts +16 -0
- package/dist/api/utils/list-query.js +99 -0
- package/dist/api/utils/omit-fields.d.ts +2 -0
- package/dist/api/utils/omit-fields.js +16 -0
- package/dist/api-generator/route-generator.d.ts +2 -0
- package/dist/api-generator/route-generator.js +59 -25
- package/dist/api-generator/utils/api-fields.d.ts +8 -0
- package/dist/api-generator/utils/api-fields.js +25 -0
- package/dist/api-generator/utils/filter-operators.d.ts +14 -0
- package/dist/api-generator/utils/filter-operators.js +92 -0
- package/dist/api-generator/zod-schema-generator.d.ts +2 -0
- package/dist/api-generator/zod-schema-generator.js +55 -0
- package/dist/cli/init.js +27 -1
- package/dist/cli/templates.d.ts +1 -0
- package/dist/cli/templates.js +10 -2
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +5 -0
- package/dist/db/db-client-generator.js +14 -5
- package/dist/db/include/executor.d.ts +3 -0
- package/dist/db/include/executor.js +90 -0
- package/dist/db/include/hydrator.d.ts +4 -0
- package/dist/db/include/hydrator.js +34 -0
- package/dist/db/include/json-agg.d.ts +5 -0
- package/dist/db/include/json-agg.js +101 -0
- package/dist/db/include/load.d.ts +8 -0
- package/dist/db/include/load.js +46 -0
- package/dist/db/include/planner.d.ts +16 -0
- package/dist/db/include/planner.js +48 -0
- package/dist/db/include/types.d.ts +14 -0
- package/dist/db/include/types.js +1 -0
- package/dist/db/model-client.d.ts +14 -12
- package/dist/db/model-client.js +35 -21
- package/dist/db/model-meta.d.ts +13 -0
- package/dist/db/model-meta.js +6 -0
- package/dist/db/type-generator.d.ts +2 -0
- package/dist/db/type-generator.js +16 -0
- package/dist/db/utils/relations.d.ts +3 -0
- package/dist/db/utils/relations.js +95 -0
- package/package.json +1 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { getFieldAttribute, getIdentifierNames, getModelNames, getOptionalKvPair, getPrimaryKey, } from '../../sql-generator/utils/ast-helpers.js';
|
|
2
|
+
export function buildRelations(model, schema) {
|
|
3
|
+
const modelNames = getModelNames(schema);
|
|
4
|
+
const relations = [];
|
|
5
|
+
for (const field of model.fields) {
|
|
6
|
+
if (!modelNames.has(field.type.name)) {
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
const parsed = parseRelationField(field);
|
|
10
|
+
if (parsed?.localFields && parsed.foreignFields) {
|
|
11
|
+
relations.push({
|
|
12
|
+
name: field.name,
|
|
13
|
+
kind: resolveDirectRelationKind(field),
|
|
14
|
+
targetModel: field.type.name,
|
|
15
|
+
localKey: parsed.localFields[0],
|
|
16
|
+
foreignKey: parsed.foreignFields[0],
|
|
17
|
+
unique: !field.type.array,
|
|
18
|
+
relationName: parsed.relationName,
|
|
19
|
+
});
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const inverse = findInverseRelation(model, field, schema, parsed?.relationName);
|
|
23
|
+
if (!inverse) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
relations.push({
|
|
27
|
+
name: field.name,
|
|
28
|
+
kind: field.type.array ? 'hasMany' : 'hasOne',
|
|
29
|
+
targetModel: field.type.name,
|
|
30
|
+
localKey: inverse.localKey,
|
|
31
|
+
foreignKey: inverse.foreignKey,
|
|
32
|
+
unique: !field.type.array,
|
|
33
|
+
relationName: parsed?.relationName,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return relations;
|
|
37
|
+
}
|
|
38
|
+
function parseRelationField(field) {
|
|
39
|
+
const relation = getFieldAttribute(field, 'relation');
|
|
40
|
+
if (!relation?.args || relation.args.kind !== 'KeyValueArgs') {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const namePair = getOptionalKvPair(relation.args, 'name');
|
|
44
|
+
const fieldsPair = getOptionalKvPair(relation.args, 'fields');
|
|
45
|
+
const referencesPair = getOptionalKvPair(relation.args, 'references');
|
|
46
|
+
return {
|
|
47
|
+
relationName: namePair ? readIdentifierValue(namePair.value) : undefined,
|
|
48
|
+
localFields: fieldsPair ? getIdentifierNames(fieldsPair.value) : undefined,
|
|
49
|
+
foreignFields: referencesPair ? getIdentifierNames(referencesPair.value) : undefined,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function readIdentifierValue(value) {
|
|
53
|
+
if (value.kind === 'Identifier') {
|
|
54
|
+
return value.name;
|
|
55
|
+
}
|
|
56
|
+
if (value.kind === 'StringLiteral' && 'value' in value) {
|
|
57
|
+
return value.value;
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
function resolveDirectRelationKind(field) {
|
|
62
|
+
if (field.type.array) {
|
|
63
|
+
return 'hasMany';
|
|
64
|
+
}
|
|
65
|
+
return 'belongsTo';
|
|
66
|
+
}
|
|
67
|
+
function findInverseRelation(sourceModel, field, schema, sourceRelationName) {
|
|
68
|
+
const targetModel = schema.models.find((candidate) => candidate.name === field.type.name);
|
|
69
|
+
if (!targetModel) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const sourcePrimaryKey = getPrimaryKey(sourceModel)?.fields ?? ['id'];
|
|
73
|
+
for (const targetField of targetModel.fields) {
|
|
74
|
+
if (targetField.type.name !== sourceModel.name) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const targetRelation = parseRelationField(targetField);
|
|
78
|
+
if (!targetRelation?.localFields || !targetRelation.foreignFields) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (sourceRelationName && targetRelation.relationName !== sourceRelationName) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (!sourceRelationName && targetRelation.relationName) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const localKey = targetRelation.foreignFields[0];
|
|
88
|
+
const foreignKey = targetRelation.localFields[0];
|
|
89
|
+
if (!sourcePrimaryKey.includes(localKey)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
return { localKey, foreignKey };
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|