@codecks/fetch 0.0.2 → 0.0.3
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/index.js +21 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -85,8 +85,7 @@ var ModelPool = class {
|
|
|
85
85
|
}
|
|
86
86
|
get(model, id$1) {
|
|
87
87
|
const instances = this.data.get(model);
|
|
88
|
-
|
|
89
|
-
return instances.get(id$1) ?? null;
|
|
88
|
+
return instances?.get(id$1) ?? null;
|
|
90
89
|
}
|
|
91
90
|
};
|
|
92
91
|
|
|
@@ -3046,11 +3045,11 @@ const serializeModel = (q, modelDesc) => {
|
|
|
3046
3045
|
rels.forEach((r) => {
|
|
3047
3046
|
switch (r?.type) {
|
|
3048
3047
|
case "count": {
|
|
3049
|
-
list.push(`count
|
|
3048
|
+
list.push(`count:${getRelKey(r, k)}`);
|
|
3050
3049
|
break;
|
|
3051
3050
|
}
|
|
3052
3051
|
case "exists": {
|
|
3053
|
-
list.push(`exists
|
|
3052
|
+
list.push(`exists:${getRelKey(r, k)}`);
|
|
3054
3053
|
break;
|
|
3055
3054
|
}
|
|
3056
3055
|
default:
|
|
@@ -3062,11 +3061,8 @@ const serializeModel = (q, modelDesc) => {
|
|
|
3062
3061
|
if (Object.keys(relObj).length) list.push(relObj);
|
|
3063
3062
|
return list;
|
|
3064
3063
|
};
|
|
3065
|
-
const serializeRelations = (q, modelDesc) => {
|
|
3066
|
-
return Object.fromEntries(Object.entries(q).map(([k, v]) => [k, serializeModel(v, modelDesc.relations[k])]));
|
|
3067
|
-
};
|
|
3068
3064
|
const serializeRootQuery = (q) => {
|
|
3069
|
-
return { _root:
|
|
3065
|
+
return { _root: serializeModel({ relations: q }, _rootDesc) };
|
|
3070
3066
|
};
|
|
3071
3067
|
const serializeInstanceQuery = (q, instances, modelMap$1) => {
|
|
3072
3068
|
return Object.fromEntries(instances.map((instance) => {
|
|
@@ -3077,26 +3073,13 @@ const serializeInstanceQuery = (q, instances, modelMap$1) => {
|
|
|
3077
3073
|
|
|
3078
3074
|
//#endregion
|
|
3079
3075
|
//#region src/reconcile-query.ts
|
|
3080
|
-
const
|
|
3081
|
-
const
|
|
3082
|
-
const responsePart = response._root;
|
|
3083
|
-
for (const [relName, modelFields] of Object.entries(query)) {
|
|
3084
|
-
const relDesc = _rootDesc.relations[relName];
|
|
3085
|
-
const id$1 = responsePart[relName];
|
|
3086
|
-
if (!id$1) {
|
|
3087
|
-
result[relName] = null;
|
|
3088
|
-
continue;
|
|
3089
|
-
} else result[relName] = reconcileInstanceQuery(modelFields, response, modelMap[relDesc.relName], `${id$1}`, pool);
|
|
3090
|
-
}
|
|
3091
|
-
return result;
|
|
3092
|
-
};
|
|
3093
|
-
const reconcileInstanceQuery = (query, response, instanceModel, key, pool) => {
|
|
3094
|
-
const instance = pool.get(instanceModel.name, key);
|
|
3076
|
+
const reconcileInstanceQuery = (query, response, instanceModel, key, store, skipMeta = false) => {
|
|
3077
|
+
const instance = store.get(instanceModel.name, key);
|
|
3095
3078
|
if (!instance) {
|
|
3096
3079
|
console.warn(`no instance found in pool: [${instanceModel.name}, ${key}]`);
|
|
3097
3080
|
return null;
|
|
3098
3081
|
}
|
|
3099
|
-
const result = {
|
|
3082
|
+
const result = skipMeta ? {} : {
|
|
3100
3083
|
"~model": instanceModel.name,
|
|
3101
3084
|
"~key": key
|
|
3102
3085
|
};
|
|
@@ -3112,11 +3095,11 @@ const reconcileInstanceQuery = (query, response, instanceModel, key, pool) => {
|
|
|
3112
3095
|
switch (opts.type) {
|
|
3113
3096
|
case "belongsTo":
|
|
3114
3097
|
result[opts.fk] = instance[relName];
|
|
3115
|
-
result[relName] = opts.fk ? reconcileInstanceQuery(relEntry, response, relModel, `${instance[relName]}`,
|
|
3098
|
+
result[relName] = opts.fk ? reconcileInstanceQuery(relEntry, response, relModel, `${instance[relName]}`, store) : null;
|
|
3116
3099
|
break;
|
|
3117
3100
|
case "hasOne":
|
|
3118
3101
|
result[`~${relName}`] = instance[relName];
|
|
3119
|
-
result[relName] = reconcileInstanceQuery(relEntry, response, relModel, `${instance[relName]}`,
|
|
3102
|
+
result[relName] = reconcileInstanceQuery(relEntry, response, relModel, `${instance[relName]}`, store);
|
|
3120
3103
|
break;
|
|
3121
3104
|
case "hasMany":
|
|
3122
3105
|
const asName = relEntry.as ?? relName;
|
|
@@ -3124,20 +3107,20 @@ const reconcileInstanceQuery = (query, response, instanceModel, key, pool) => {
|
|
|
3124
3107
|
const val = instance[relKey];
|
|
3125
3108
|
switch (relEntry.type) {
|
|
3126
3109
|
case "count": {
|
|
3127
|
-
result[asName] = instance[`count
|
|
3110
|
+
result[asName] = instance[`count:${relKey}`];
|
|
3128
3111
|
break;
|
|
3129
3112
|
}
|
|
3130
3113
|
case "exists": {
|
|
3131
|
-
result[asName] = instance[`exists
|
|
3114
|
+
result[asName] = instance[`exists:${relKey}`];
|
|
3132
3115
|
break;
|
|
3133
3116
|
}
|
|
3134
3117
|
case "first":
|
|
3135
3118
|
result[`~${asName}`] = val;
|
|
3136
|
-
result[asName] = reconcileInstanceQuery(relEntry, response, relModel, `${val}`,
|
|
3119
|
+
result[asName] = reconcileInstanceQuery(relEntry, response, relModel, `${val}`, store);
|
|
3137
3120
|
break;
|
|
3138
3121
|
default: {
|
|
3139
3122
|
result[`~${asName}`] = val;
|
|
3140
|
-
result[asName] = val.map((id$1) => reconcileInstanceQuery(relEntry, response, relModel, `${id$1}`,
|
|
3123
|
+
result[asName] = val.map((id$1) => reconcileInstanceQuery(relEntry, response, relModel, `${id$1}`, store));
|
|
3141
3124
|
}
|
|
3142
3125
|
}
|
|
3143
3126
|
break;
|
|
@@ -3187,20 +3170,19 @@ const buildFetchers = (opts = {}) => {
|
|
|
3187
3170
|
const response = await fetchWithQuery(serializeRootQuery(q));
|
|
3188
3171
|
const pool = new ModelPool(modelMap);
|
|
3189
3172
|
pool.add(response);
|
|
3190
|
-
return
|
|
3173
|
+
return reconcileInstanceQuery({ relations: q }, response, _rootDesc, "", pool, true);
|
|
3191
3174
|
},
|
|
3192
3175
|
fetchFromInstance: _fetchSingleInstance,
|
|
3193
|
-
fetchInstance: async (
|
|
3194
|
-
|
|
3195
|
-
"~model":
|
|
3176
|
+
fetchInstance: async (model, id$1, q) => {
|
|
3177
|
+
return _fetchSingleInstance({
|
|
3178
|
+
"~model": model,
|
|
3196
3179
|
"~key": id$1
|
|
3197
|
-
};
|
|
3198
|
-
return _fetchSingleInstance(instance, q);
|
|
3180
|
+
}, q);
|
|
3199
3181
|
},
|
|
3200
|
-
fetchInstances: async (
|
|
3201
|
-
const modelDesc = modelMap[
|
|
3182
|
+
fetchInstances: async (model, ids, q) => {
|
|
3183
|
+
const modelDesc = modelMap[model];
|
|
3202
3184
|
const instances = ids.map((id$1) => ({
|
|
3203
|
-
"~model":
|
|
3185
|
+
"~model": model,
|
|
3204
3186
|
"~key": id$1
|
|
3205
3187
|
}));
|
|
3206
3188
|
const response = await fetchWithQuery(serializeInstanceQuery(q, instances, modelMap));
|