@akanjs/server 1.0.5 → 1.0.7-canary.0

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.
@@ -61,15 +61,14 @@ const getProtoModel = (modelRef) => {
61
61
  const allModelRefs = [modelRef, ...childModelRefs];
62
62
  const modelDatas = allModelRefs.map((modelRef2) => {
63
63
  const refName2 = import_constant.constantInfo.getRefName(modelRef2);
64
- const fieldMetas = (0, import_constant.getFieldMetas)(modelRef2);
65
64
  return [
66
65
  refName2,
67
66
  {
68
67
  fields: Object.fromEntries(
69
- fieldMetas.map((fieldMeta, id) => {
70
- const rule = fieldMeta.isArray ? "repeated" : fieldMeta.nullable ? "optional" : "required";
71
- const type = fieldMeta.isClass ? import_constant.constantInfo.getRefName(fieldMeta.modelRef) : fieldMeta.enum ? `${refName2}${(0, import_common.capitalize)(fieldMeta.key)}Enum` : protobufTypeMap.get(fieldMeta.modelRef) ?? "string";
72
- return [fieldMeta.key, { type, id, rule }];
68
+ Object.entries(modelRef2.field).map(([key, field], id) => {
69
+ const rule = field.isArray ? "repeated" : field.nullable ? "optional" : "required";
70
+ const type = field.isClass ? import_constant.constantInfo.getRefName(field.modelRef) : field.enum ? `${refName2}${(0, import_common.capitalize)(key)}Enum` : protobufTypeMap.get(field.modelRef) ?? "string";
71
+ return [key, { type, id, rule }];
73
72
  })
74
73
  )
75
74
  }
@@ -119,23 +118,23 @@ const getProtoEncodeFn = (modelRef) => {
119
118
  const [valueRef] = (0, import_base.getNonArrayModel)(modelRef);
120
119
  return scalarProtoEncodeMap.get(valueRef) ?? ((value) => value);
121
120
  };
122
- const protoEncode = (metadata, value) => {
123
- if (metadata.nullable && (value === null || value === void 0))
121
+ const protoEncode = (field, value) => {
122
+ if (field.nullable && (value === null || value === void 0))
124
123
  return null;
125
- if (metadata.isArray && Array.isArray(value)) {
126
- return value.map((v) => protoEncode(metadata, v));
124
+ if (field.isArray && Array.isArray(value)) {
125
+ return value.map((v) => protoEncode(field, v));
127
126
  }
128
- if (metadata.isMap && metadata.of) {
129
- const protoEncodeFn = getProtoEncodeFn(metadata.of);
127
+ if (field.isMap && field.of) {
128
+ const protoEncodeFn = getProtoEncodeFn(field.of);
130
129
  return Object.fromEntries(
131
130
  [...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, protoEncodeFn)])
132
131
  );
133
132
  }
134
- if (metadata.isClass)
135
- return makeProtoEncode(metadata.modelRef)(value);
136
- if (metadata.enum)
137
- return metadata.enum.indexOf(value);
138
- return getProtoEncodeFn(metadata.modelRef)(value);
133
+ if (field.isClass)
134
+ return makeProtoEncode(field.modelRef)(value);
135
+ if (field.enum)
136
+ return field.enum.indexOf(value);
137
+ return getProtoEncodeFn(field.modelRef)(value);
139
138
  };
140
139
  const getPredefinedProtoEncodeFn = (refName) => {
141
140
  const protoEncode2 = Reflect.getMetadata(refName, ProtoEncodeStorage.prototype);
@@ -149,11 +148,10 @@ const makeProtoEncode = (modelRef) => {
149
148
  const predefinedProtoEncode = getPredefinedProtoEncodeFn(refName);
150
149
  if (predefinedProtoEncode)
151
150
  return predefinedProtoEncode;
152
- const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
153
151
  const protoEncodeFn = (value) => {
154
152
  const result = {};
155
- fieldMetas.forEach((fieldMeta) => {
156
- result[fieldMeta.key] = protoEncode(fieldMeta, value[fieldMeta.key]);
153
+ Object.entries(modelRef.field).forEach(([key, field], id) => {
154
+ result[key] = protoEncode(field.getProps(), value[key]);
157
155
  });
158
156
  return result;
159
157
  };
@@ -193,17 +191,17 @@ const getProtoDecodeFn = (modelRef) => {
193
191
  const [valueRef] = (0, import_base.getNonArrayModel)(modelRef);
194
192
  return scalarProtoDecodeMap.get(valueRef) ?? ((value) => value);
195
193
  };
196
- const protoDecode = (metadata, value) => {
197
- if (metadata.nullable && (value === null || value === void 0))
194
+ const protoDecode = (field, value) => {
195
+ if (field.nullable && (value === null || value === void 0))
198
196
  return null;
199
- if (metadata.isArray) {
197
+ if (field.isArray) {
200
198
  if (value === void 0)
201
199
  return [];
202
200
  if (Array.isArray(value))
203
- return value.map((v) => protoDecode(metadata, v));
201
+ return value.map((v) => protoDecode(field, v));
204
202
  }
205
- if (metadata.isMap && metadata.of) {
206
- const protoDecodeFn = getProtoDecodeFn(metadata.of);
203
+ if (field.isMap && field.of) {
204
+ const protoDecodeFn = getProtoDecodeFn(field.of);
207
205
  return new Map(
208
206
  Object.entries(value).map(([key, val]) => [
209
207
  key,
@@ -211,11 +209,11 @@ const protoDecode = (metadata, value) => {
211
209
  ])
212
210
  );
213
211
  }
214
- if (metadata.isClass)
215
- return makeProtoDecode(metadata.modelRef)(value);
216
- if (metadata.enum)
217
- return metadata.enum.values.at(value);
218
- return getProtoDecodeFn(metadata.modelRef)(value);
212
+ if (field.isClass)
213
+ return makeProtoDecode(field.modelRef)(value);
214
+ if (field.enum)
215
+ return field.enum.values.at(value);
216
+ return getProtoDecodeFn(field.modelRef)(value);
219
217
  };
220
218
  const getPredefinedProtoDecodeFn = (refName) => {
221
219
  const protoDecode2 = Reflect.getMetadata(refName, ProtoDecodeStorage.prototype);
@@ -229,11 +227,10 @@ const makeProtoDecode = (modelRef) => {
229
227
  const predefinedProtoDecode = getPredefinedProtoDecodeFn(refName);
230
228
  if (predefinedProtoDecode)
231
229
  return predefinedProtoDecode;
232
- const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
233
230
  const protoDecodeFn = (value) => {
234
231
  const result = {};
235
- fieldMetas.forEach((fieldMeta) => {
236
- result[fieldMeta.key] = protoDecode(fieldMeta, value[fieldMeta.key]);
232
+ Object.entries(modelRef.field).forEach(([key, field], id) => {
233
+ result[key] = protoDecode(field.getProps(), value[key]);
237
234
  });
238
235
  return result;
239
236
  };
@@ -245,8 +242,7 @@ const decode = (modelRef, buffer) => {
245
242
  const message = protoModel.decode(Buffer.from(buffer));
246
243
  const data = protoModel.toObject(message);
247
244
  const protoDecode2 = makeProtoDecode(modelRef);
248
- const crystalize = (0, import_constant.makeCrystalize)(modelRef);
249
- const result = crystalize(protoDecode2(data));
245
+ const result = new modelRef().set(protoDecode2(data));
250
246
  return result;
251
247
  };
252
248
  // Annotate the CommonJS export names for ESM import in node:
package/cjs/src/gql.js CHANGED
@@ -98,14 +98,16 @@ const gqlNestFieldMap = /* @__PURE__ */ new Map([
98
98
  [import_base.JSON, import_graphql_type_json.default],
99
99
  [Map, import_graphql_type_json.default]
100
100
  ]);
101
- const applyNestField = (model, fieldMeta, type = "object") => {
102
- if (fieldMeta.fieldType === "hidden" && type === "object")
101
+ const applyNestField = (model, field, key, type = "object") => {
102
+ if (field.fieldType === "hidden" && type === "object")
103
103
  return;
104
- const modelRef = fieldMeta.isClass ? type === "object" ? generateGql(fieldMeta.modelRef) : fieldMeta.isScalar ? generateGqlInput(fieldMeta.modelRef) : Nest.ID : gqlNestFieldMap.get(fieldMeta.modelRef) ?? fieldMeta.modelRef;
105
- (0, import_graphql.Field)(() => (0, import_base.arraiedModel)(modelRef, fieldMeta.arrDepth), { nullable: fieldMeta.nullable })(
106
- model.prototype,
107
- fieldMeta.key
108
- );
104
+ const modelRef = field.isClass ? type === "object" ? generateGql(field.modelRef) : field.isScalar ? generateGqlInput(field.modelRef) : Nest.ID : gqlNestFieldMap.get(field.modelRef) ?? field.modelRef;
105
+ (0, import_graphql.Field)(() => (0, import_base.arraiedModel)(modelRef, field.arrDepth), { nullable: field.nullable })(model.prototype, key);
106
+ };
107
+ const gqlClassMap = /* @__PURE__ */ new Map();
108
+ const getGqlClass = (modelRef) => {
109
+ return gqlClassMap.get(modelRef) ?? class GqlClass {
110
+ };
109
111
  };
110
112
  const generateGqlInput = (inputRef) => {
111
113
  const refName = import_constant.constantInfo.getRefName(inputRef);
@@ -114,12 +116,9 @@ const generateGqlInput = (inputRef) => {
114
116
  const predefinedInputGql = getPredefinedInqutGql(gqlName);
115
117
  if (predefinedInputGql)
116
118
  return predefinedInputGql;
117
- const fieldMetas = (0, import_constant.getFieldMetas)(inputRef);
118
- class InputGql {
119
- }
120
- const inputGql = import_constant.constantInfo.isScalar(inputRef) ? InputGql : import_constant.constantInfo.getDatabase(refName).input;
121
- fieldMetas.forEach((fieldMeta) => {
122
- applyNestField(inputGql, fieldMeta, "input");
119
+ const inputGql = getGqlClass(import_constant.constantInfo.isScalar(inputRef) ? inputRef : import_constant.constantInfo.getDatabase(refName).input);
120
+ Object.entries(inputRef.field).forEach(([key, field], id) => {
121
+ applyNestField(inputGql, field.getProps(), key, "input");
123
122
  });
124
123
  (0, import_graphql.InputType)(gqlName)(inputGql);
125
124
  setPredefinedInqutGql(gqlName, inputGql);
@@ -136,12 +135,11 @@ const generateGql = (objectRef) => {
136
135
  const predefinedObjectGql = getPredefinedObjectGql(gqlName);
137
136
  if (predefinedObjectGql)
138
137
  return predefinedObjectGql;
139
- const fieldMetas = (0, import_constant.getFieldMetas)(objectRef);
140
- class ObjectGql {
141
- }
142
- const objectGql = import_constant.constantInfo.isScalar(objectRef) || import_constant.constantInfo.isInsight(objectRef) ? ObjectGql : import_constant.constantInfo.getDatabase(refName).full;
143
- fieldMetas.forEach((fieldMeta) => {
144
- applyNestField(objectGql, fieldMeta);
138
+ const objectGql = getGqlClass(
139
+ import_constant.constantInfo.isScalar(objectRef) || import_constant.constantInfo.isInsight(objectRef) ? objectRef : import_constant.constantInfo.getDatabase(refName).full
140
+ );
141
+ Object.entries(objectRef.field).forEach(([key, field], id) => {
142
+ applyNestField(objectGql, field.getProps(), key);
145
143
  });
146
144
  (0, import_graphql.ObjectType)(gqlName)(objectGql);
147
145
  setPredefinedObjectGql(gqlName, objectGql);
@@ -93,20 +93,19 @@ const resolverOf = (sigRef, allSrvs) => {
93
93
  const resolveFieldKeys = resolveFieldMetas.map((resolveFieldMeta) => resolveFieldMeta.key);
94
94
  if (sigMeta.returns) {
95
95
  const modelRef = sigMeta.returns();
96
- const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
97
- fieldMetas.filter((fieldMeta) => fieldMeta.isClass && !fieldMeta.isScalar && !resolveFieldKeys.includes(fieldMeta.key)).forEach((fieldMeta) => {
98
- const refName = import_constant.constantInfo.getRefName(fieldMeta.modelRef);
96
+ Object.entries(modelRef.field).filter(([key, field]) => field.isClass && !field.isScalar && !resolveFieldKeys.includes(key)).forEach(([key, field]) => {
97
+ const refName = import_constant.constantInfo.getRefName(field.modelRef);
99
98
  const className = (0, import_common.capitalize)(refName);
100
99
  const serviceName = `${refName}Service`;
101
- Rsv.prototype[fieldMeta.key] = async function(parent) {
100
+ Rsv.prototype[key] = async function(parent) {
102
101
  const service = this[serviceName];
103
- return fieldMeta.arrDepth ? await service[`load${className}Many`](parent[fieldMeta.key]) : await service[`load${className}`](parent[fieldMeta.key]);
102
+ return field.arrDepth ? await service[`load${className}Many`](parent[key]) : await service[`load${className}`](parent[key]);
104
103
  };
105
- Nest.Parent()(Rsv.prototype, fieldMeta.key, 0);
106
- Nest.ResolveField(getNestReturn(() => (0, import_base.arraiedModel)(fieldMeta.modelRef, fieldMeta.arrDepth)))(
104
+ Nest.Parent()(Rsv.prototype, key, 0);
105
+ Nest.ResolveField(getNestReturn(() => (0, import_base.arraiedModel)(field.modelRef, field.arrDepth)))(
107
106
  Rsv.prototype,
108
- fieldMeta.key,
109
- Object.getOwnPropertyDescriptor(Rsv.prototype, fieldMeta.key) ?? {}
107
+ key,
108
+ Object.getOwnPropertyDescriptor(Rsv.prototype, key) ?? {}
110
109
  );
111
110
  });
112
111
  }
package/cjs/src/schema.js CHANGED
@@ -60,87 +60,87 @@ const scalarMongoTypeMap = /* @__PURE__ */ new Map([
60
60
  [Boolean, Boolean],
61
61
  [Date, Date]
62
62
  ]);
63
- const applyMongoProp = (schemaProps, fieldMeta) => {
64
- if (["id", "createdAt", "updatedAt"].includes(fieldMeta.key) || fieldMeta.fieldType === "resolve")
63
+ const applyMongoProp = (schemaProps, field, key) => {
64
+ if (["id", "createdAt", "updatedAt"].includes(key) || field.fieldType === "resolve")
65
65
  return;
66
- const type = fieldMeta.isClass ? fieldMeta.isScalar ? createSchema(fieldMeta.modelRef) : import_document.ObjectId : scalarMongoTypeMap.get(fieldMeta.modelRef) ?? fieldMeta.modelRef;
66
+ const type = field.isClass ? field.isScalar ? createSchema(field.modelRef) : import_document.ObjectId : scalarMongoTypeMap.get(field.modelRef) ?? field.modelRef;
67
67
  let prop = {};
68
- if (fieldMeta.optArrDepth) {
68
+ if (field.optArrDepth) {
69
69
  prop.type = type;
70
70
  prop.required = true;
71
- if (fieldMeta.isClass && !fieldMeta.refPath)
72
- prop.ref = import_constant.constantInfo.getRefName(fieldMeta.modelRef);
73
- if (fieldMeta.refPath)
74
- prop.refPath = fieldMeta.refPath;
75
- if (typeof fieldMeta.min === "number")
76
- prop.min = fieldMeta.min;
77
- if (typeof fieldMeta.max === "number")
78
- prop.max = fieldMeta.max;
79
- if (fieldMeta.enum)
80
- prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
81
- if (typeof fieldMeta.minlength === "number")
82
- prop.minlength = fieldMeta.minlength;
83
- if (typeof fieldMeta.maxlength === "number")
84
- prop.maxlength = fieldMeta.maxlength;
85
- if (fieldMeta.validate) {
71
+ if (field.isClass && !field.refPath)
72
+ prop.ref = import_constant.constantInfo.getRefName(field.modelRef);
73
+ if (field.refPath)
74
+ prop.refPath = field.refPath;
75
+ if (typeof field.min === "number")
76
+ prop.min = field.min;
77
+ if (typeof field.max === "number")
78
+ prop.max = field.max;
79
+ if (field.enum)
80
+ prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
81
+ if (typeof field.minlength === "number")
82
+ prop.minlength = field.minlength;
83
+ if (typeof field.maxlength === "number")
84
+ prop.maxlength = field.maxlength;
85
+ if (field.validate) {
86
86
  prop.validate = function(value) {
87
- return fieldMeta.validate?.(fieldMeta.modelRef === Date && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
87
+ return field.validate?.(field.modelRef === Date && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
88
88
  };
89
89
  }
90
- prop = { type: (0, import_base.arraiedModel)(prop, fieldMeta.optArrDepth), default: [], required: true };
91
- if (fieldMeta.modelRef.prototype === Date.prototype) {
90
+ prop = { type: (0, import_base.arraiedModel)(prop, field.optArrDepth), default: [], required: true };
91
+ if (field.modelRef.prototype === Date.prototype) {
92
92
  prop.get = (dates) => (0, import_base.applyFnToArrayObjects)(dates, (date) => (0, import_base.dayjs)(date));
93
93
  prop.set = (days) => (0, import_base.applyFnToArrayObjects)(days, (day) => day.toDate());
94
94
  }
95
- if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === import_base.ID.prototype) {
95
+ if (field.isClass && !field.isScalar || field.modelRef.prototype === import_base.ID.prototype) {
96
96
  prop.get = (ids) => (0, import_base.applyFnToArrayObjects)(ids, (id) => id.toString());
97
97
  prop.set = (ids) => (0, import_base.applyFnToArrayObjects)(ids, (id) => new import_mongoose.Types.ObjectId(id));
98
98
  }
99
99
  } else {
100
- prop.type = (0, import_base.arraiedModel)(type, fieldMeta.arrDepth);
101
- prop.required = !fieldMeta.nullable;
102
- if (fieldMeta.isMap) {
103
- prop.of = scalarMongoTypeMap.get(fieldMeta.of) ?? createSchema(fieldMeta.of);
104
- if (!fieldMeta.default)
100
+ prop.type = (0, import_base.arraiedModel)(type, field.arrDepth);
101
+ prop.required = !field.nullable;
102
+ if (field.isMap) {
103
+ prop.of = scalarMongoTypeMap.get(field.of) ?? createSchema(field.of);
104
+ if (!field.default)
105
105
  prop.default = /* @__PURE__ */ new Map();
106
106
  }
107
- if (fieldMeta.default !== null) {
108
- if (typeof fieldMeta.default === "function")
107
+ if (field.default !== null) {
108
+ if (typeof field.default === "function")
109
109
  prop.default = function() {
110
- const def = fieldMeta.default(this);
110
+ const def = field.default(this);
111
111
  return (0, import_common.isDayjs)(def) ? def.toDate() : def;
112
112
  };
113
113
  else
114
- prop.default = (0, import_common.isDayjs)(fieldMeta.default) ? fieldMeta.default.toDate() : fieldMeta.default;
114
+ prop.default = (0, import_common.isDayjs)(field.default) ? field.default.toDate() : field.default;
115
115
  }
116
- if (typeof fieldMeta.immutable !== "undefined")
117
- prop.immutable = fieldMeta.immutable;
118
- if (fieldMeta.isClass && !fieldMeta.refPath)
119
- prop.ref = import_constant.constantInfo.getRefName(fieldMeta.modelRef);
120
- if (fieldMeta.refPath)
121
- prop.refPath = fieldMeta.refPath;
122
- if (typeof fieldMeta.min === "number")
123
- prop.min = fieldMeta.min;
124
- if (typeof fieldMeta.max === "number")
125
- prop.max = fieldMeta.max;
126
- if (fieldMeta.enum)
127
- prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
128
- if (typeof fieldMeta.select === "boolean")
129
- prop.select = fieldMeta.select;
130
- if (typeof fieldMeta.minlength === "number")
131
- prop.minlength = fieldMeta.minlength;
132
- if (typeof fieldMeta.maxlength === "number")
133
- prop.maxlength = fieldMeta.maxlength;
134
- if (fieldMeta.nullable) {
116
+ if (typeof field.immutable !== "undefined")
117
+ prop.immutable = field.immutable;
118
+ if (field.isClass && !field.refPath)
119
+ prop.ref = import_constant.constantInfo.getRefName(field.modelRef);
120
+ if (field.refPath)
121
+ prop.refPath = field.refPath;
122
+ if (typeof field.min === "number")
123
+ prop.min = field.min;
124
+ if (typeof field.max === "number")
125
+ prop.max = field.max;
126
+ if (field.enum)
127
+ prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
128
+ if (typeof field.select === "boolean")
129
+ prop.select = field.select;
130
+ if (typeof field.minlength === "number")
131
+ prop.minlength = field.minlength;
132
+ if (typeof field.maxlength === "number")
133
+ prop.maxlength = field.maxlength;
134
+ if (field.nullable) {
135
135
  prop.get = (v) => v === void 0 ? void 0 : v;
136
136
  prop.set = (v) => v === null ? void 0 : v;
137
137
  }
138
- if (fieldMeta.modelRef.prototype === Date.prototype) {
138
+ if (field.modelRef.prototype === Date.prototype) {
139
139
  prop.get = (date) => (0, import_base.applyFnToArrayObjects)(date, (date2) => date2 ? (0, import_base.dayjs)(date2) : void 0);
140
140
  prop.set = (day) => (0, import_base.applyFnToArrayObjects)(day, (day2) => day2 ? (0, import_base.dayjs)(day2).toDate() : void 0);
141
141
  }
142
- if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === import_base.ID.prototype) {
143
- if (fieldMeta.arrDepth === 0) {
142
+ if (field.isClass && !field.isScalar || field.modelRef.prototype === import_base.ID.prototype) {
143
+ if (field.arrDepth === 0) {
144
144
  prop.get = (id) => id ? id.toString() : void 0;
145
145
  prop.set = (id) => id ? new import_mongoose.Types.ObjectId(id) : void 0;
146
146
  } else {
@@ -148,26 +148,25 @@ const applyMongoProp = (schemaProps, fieldMeta) => {
148
148
  prop.set = (val) => (0, import_base.applyFnToArrayObjects)(val, (id) => id ? new import_mongoose.Types.ObjectId(id) : void 0);
149
149
  }
150
150
  }
151
- if (fieldMeta.isClass && fieldMeta.isScalar && fieldMeta.default === null && !fieldMeta.nullable) {
152
- prop.default = (0, import_constant.makeDefault)(fieldMeta.modelRef);
151
+ if (field.isClass && field.isScalar && field.default === null && !field.nullable) {
152
+ prop.default = field.modelRef.default;
153
153
  }
154
- if (fieldMeta.validate) {
154
+ if (field.validate) {
155
155
  prop.validate = function(value) {
156
- return fieldMeta.validate?.(fieldMeta.modelRef === Date && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
156
+ return field.validate?.(field.modelRef === Date && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
157
157
  };
158
158
  }
159
159
  }
160
- schemaProps[fieldMeta.key] = prop;
160
+ schemaProps[key] = prop;
161
161
  };
162
162
  const createSchema = (modelRef) => {
163
163
  const refName = import_constant.constantInfo.getRefName(modelRef);
164
164
  const schemaMeta = getScalarSchemaMetaByName(refName);
165
165
  if (schemaMeta)
166
166
  return schemaMeta;
167
- const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
168
167
  const schemaProps = {};
169
- fieldMetas.forEach((fieldMeta) => {
170
- applyMongoProp(schemaProps, fieldMeta);
168
+ Object.entries(modelRef.field).forEach(([key, field]) => {
169
+ applyMongoProp(schemaProps, field.getProps(), key);
171
170
  });
172
171
  const schema = new import_mongoose.Schema(schemaProps);
173
172
  setScalarSchemaMetaByName(refName, schema);
@@ -178,7 +177,6 @@ const schemaOf = (modelRef, docRef, middleware) => {
178
177
  const schemaMeta = getSchemaMetaByName(refName);
179
178
  if (schemaMeta)
180
179
  return schemaMeta;
181
- const fieldMetas = (0, import_constant.getFieldMetas)(docRef);
182
180
  const schemaProps = {
183
181
  createdAt: {
184
182
  type: Date,
@@ -191,8 +189,9 @@ const schemaOf = (modelRef, docRef, middleware) => {
191
189
  set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
192
190
  }
193
191
  };
194
- fieldMetas.forEach((fieldMeta) => {
195
- applyMongoProp(schemaProps, fieldMeta);
192
+ const cnst = import_constant.constantInfo.getDatabase(refName);
193
+ Object.entries(cnst.full.field).forEach(([key, field]) => {
194
+ applyMongoProp(schemaProps, field.getProps(), key);
196
195
  });
197
196
  const schema = new import_mongoose.Schema(schemaProps, (0, import_document.getDefaultSchemaOptions)());
198
197
  schema.methods.refresh = async function() {
@@ -255,7 +254,6 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
255
254
  const modelSchema = Reflect.getMetadata(refName, SchemaStorage.prototype);
256
255
  if (!modelSchema)
257
256
  throw new Error(`Schema of ${refName} not found`);
258
- const fieldMetas = (0, import_constant.getFieldMetas)(docRef);
259
257
  const schemaProps = {
260
258
  createdAt: {
261
259
  type: Date,
@@ -268,13 +266,13 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
268
266
  set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
269
267
  }
270
268
  };
271
- fieldMetas.forEach((fieldMeta) => {
272
- applyMongoProp(schemaProps, fieldMeta);
273
- (0, import__.applyNestField)(originDoc, fieldMeta);
269
+ const cnst = import_constant.constantInfo.getDatabase(refName);
270
+ Object.entries(cnst.full.field).forEach(([key, field]) => {
271
+ applyMongoProp(schemaProps, field.getProps(), key);
272
+ (0, import__.applyNestField)(originDoc, field.getProps(), key);
274
273
  });
275
- const inputFieldMetas = (0, import_constant.getFieldMetas)(inputRef);
276
- inputFieldMetas.forEach((fieldMeta) => {
277
- (0, import__.applyNestField)(originInput, fieldMeta, "input");
274
+ Object.entries(cnst.input.field).forEach(([key, field]) => {
275
+ (0, import__.applyNestField)(originInput, field.getProps(), key, "input");
278
276
  });
279
277
  const schema = new import_mongoose.Schema(schemaProps, (0, import_document.getDefaultSchemaOptions)());
280
278
  modelSchema.add(schema);
@@ -38,38 +38,29 @@ var import_document = require("@akanjs/document");
38
38
  var import_common2 = require("@nestjs/common");
39
39
  var import_mongoose = require("@nestjs/mongoose");
40
40
  const hasTextField = (modelRef) => {
41
- const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
42
- return fieldMetas.some(
43
- (fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
41
+ return Object.entries(modelRef.field).some(
42
+ ([_, field]) => !!field.text || field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)
44
43
  );
45
44
  };
46
45
  const getTextFieldKeys = (modelRef) => {
47
46
  const allSearchFields = [];
48
47
  const allFilterFields = [];
49
- const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
50
- const fieldMetas = [...fieldMetaMap.values()];
51
- const stringTextFields = fieldMetas.filter((fieldMeta) => !!fieldMeta.text).map((fieldMeta) => {
52
- if (fieldMeta.text === "filter")
53
- allFilterFields.push(fieldMeta.key);
54
- else if (fieldMeta.text === "search")
55
- allSearchFields.push(fieldMeta.key);
56
- return fieldMeta.key;
48
+ const stringTextFields = Object.entries(modelRef.field).filter(([_, field]) => !!field.text).map(([key, field]) => {
49
+ if (field.text === "filter")
50
+ allFilterFields.push(key);
51
+ else if (field.text === "search")
52
+ allSearchFields.push(key);
53
+ return key;
57
54
  });
58
- const scalarTextFields = fieldMetas.filter(
59
- (fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
60
- ).map((fieldMeta) => fieldMeta.key);
55
+ const scalarTextFields = Object.entries(modelRef.field).filter(([_, field]) => field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)).map(([key, field]) => key);
61
56
  const deepFields = scalarTextFields.map((key) => {
62
- const fieldMeta = fieldMetaMap.get(key);
63
- if (!fieldMeta)
64
- throw new Error(`No fieldMeta for ${key}`);
65
- const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(
66
- fieldMeta.modelRef
67
- );
68
- allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
69
- allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
57
+ const field = modelRef.field[key];
58
+ const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(field.modelRef);
59
+ allFilterFields2.push(...allSearchFields2.map((field2) => `${key}.${field2}`));
60
+ allSearchFields2.push(...stringTextFields2.map((field2) => `${key}.${field2}`));
70
61
  return [
71
- ...stringTextFields2.map((field) => `${key}.${field}`),
72
- ...allTextFields.map((field) => `${key}.${field}`)
62
+ ...stringTextFields2.map((field2) => `${key}.${field2}`),
63
+ ...allTextFields.map((field2) => `${key}.${field2}`)
73
64
  ];
74
65
  }).flat();
75
66
  return {
@@ -81,7 +72,6 @@ const getTextFieldKeys = (modelRef) => {
81
72
  };
82
73
  };
83
74
  const makeTextFilter = (modelRef) => {
84
- const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
85
75
  const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
86
76
  const filterData = (data, assignObj = {}) => {
87
77
  if (Array.isArray(data))
@@ -90,10 +80,8 @@ const makeTextFilter = (modelRef) => {
90
80
  Object.fromEntries([
91
81
  ...stringTextFields.map((key) => [key, data[key]]),
92
82
  ...scalarTextFields.map((key) => {
93
- const fieldMeta = fieldMetaMap.get(key);
94
- if (!fieldMeta)
95
- throw new Error(`No fieldMeta for ${key}`);
96
- const filterFunc = makeTextFilter(fieldMeta.modelRef);
83
+ const field = modelRef.field[key];
84
+ const filterFunc = makeTextFilter(field.modelRef);
97
85
  return [key, filterFunc(data[key])];
98
86
  })
99
87
  ]),
@@ -11,9 +11,7 @@ import { capitalize, Logger } from "@akanjs/common";
11
11
  import {
12
12
  constantInfo,
13
13
  getChildClassRefs,
14
- getFieldEnumMetas,
15
- getFieldMetas,
16
- makeCrystalize
14
+ getFieldEnumMetas
17
15
  } from "@akanjs/constant";
18
16
  import * as proto from "protobufjs";
19
17
  class ProtoModelStorage {
@@ -42,15 +40,14 @@ const getProtoModel = (modelRef) => {
42
40
  const allModelRefs = [modelRef, ...childModelRefs];
43
41
  const modelDatas = allModelRefs.map((modelRef2) => {
44
42
  const refName2 = constantInfo.getRefName(modelRef2);
45
- const fieldMetas = getFieldMetas(modelRef2);
46
43
  return [
47
44
  refName2,
48
45
  {
49
46
  fields: Object.fromEntries(
50
- fieldMetas.map((fieldMeta, id) => {
51
- const rule = fieldMeta.isArray ? "repeated" : fieldMeta.nullable ? "optional" : "required";
52
- const type = fieldMeta.isClass ? constantInfo.getRefName(fieldMeta.modelRef) : fieldMeta.enum ? `${refName2}${capitalize(fieldMeta.key)}Enum` : protobufTypeMap.get(fieldMeta.modelRef) ?? "string";
53
- return [fieldMeta.key, { type, id, rule }];
47
+ Object.entries(modelRef2.field).map(([key, field], id) => {
48
+ const rule = field.isArray ? "repeated" : field.nullable ? "optional" : "required";
49
+ const type = field.isClass ? constantInfo.getRefName(field.modelRef) : field.enum ? `${refName2}${capitalize(key)}Enum` : protobufTypeMap.get(field.modelRef) ?? "string";
50
+ return [key, { type, id, rule }];
54
51
  })
55
52
  )
56
53
  }
@@ -100,23 +97,23 @@ const getProtoEncodeFn = (modelRef) => {
100
97
  const [valueRef] = getNonArrayModel(modelRef);
101
98
  return scalarProtoEncodeMap.get(valueRef) ?? ((value) => value);
102
99
  };
103
- const protoEncode = (metadata, value) => {
104
- if (metadata.nullable && (value === null || value === void 0))
100
+ const protoEncode = (field, value) => {
101
+ if (field.nullable && (value === null || value === void 0))
105
102
  return null;
106
- if (metadata.isArray && Array.isArray(value)) {
107
- return value.map((v) => protoEncode(metadata, v));
103
+ if (field.isArray && Array.isArray(value)) {
104
+ return value.map((v) => protoEncode(field, v));
108
105
  }
109
- if (metadata.isMap && metadata.of) {
110
- const protoEncodeFn = getProtoEncodeFn(metadata.of);
106
+ if (field.isMap && field.of) {
107
+ const protoEncodeFn = getProtoEncodeFn(field.of);
111
108
  return Object.fromEntries(
112
109
  [...value.entries()].map(([key, val]) => [key, applyFnToArrayObjects(val, protoEncodeFn)])
113
110
  );
114
111
  }
115
- if (metadata.isClass)
116
- return makeProtoEncode(metadata.modelRef)(value);
117
- if (metadata.enum)
118
- return metadata.enum.indexOf(value);
119
- return getProtoEncodeFn(metadata.modelRef)(value);
112
+ if (field.isClass)
113
+ return makeProtoEncode(field.modelRef)(value);
114
+ if (field.enum)
115
+ return field.enum.indexOf(value);
116
+ return getProtoEncodeFn(field.modelRef)(value);
120
117
  };
121
118
  const getPredefinedProtoEncodeFn = (refName) => {
122
119
  const protoEncode2 = Reflect.getMetadata(refName, ProtoEncodeStorage.prototype);
@@ -130,11 +127,10 @@ const makeProtoEncode = (modelRef) => {
130
127
  const predefinedProtoEncode = getPredefinedProtoEncodeFn(refName);
131
128
  if (predefinedProtoEncode)
132
129
  return predefinedProtoEncode;
133
- const fieldMetas = getFieldMetas(modelRef);
134
130
  const protoEncodeFn = (value) => {
135
131
  const result = {};
136
- fieldMetas.forEach((fieldMeta) => {
137
- result[fieldMeta.key] = protoEncode(fieldMeta, value[fieldMeta.key]);
132
+ Object.entries(modelRef.field).forEach(([key, field], id) => {
133
+ result[key] = protoEncode(field.getProps(), value[key]);
138
134
  });
139
135
  return result;
140
136
  };
@@ -174,17 +170,17 @@ const getProtoDecodeFn = (modelRef) => {
174
170
  const [valueRef] = getNonArrayModel(modelRef);
175
171
  return scalarProtoDecodeMap.get(valueRef) ?? ((value) => value);
176
172
  };
177
- const protoDecode = (metadata, value) => {
178
- if (metadata.nullable && (value === null || value === void 0))
173
+ const protoDecode = (field, value) => {
174
+ if (field.nullable && (value === null || value === void 0))
179
175
  return null;
180
- if (metadata.isArray) {
176
+ if (field.isArray) {
181
177
  if (value === void 0)
182
178
  return [];
183
179
  if (Array.isArray(value))
184
- return value.map((v) => protoDecode(metadata, v));
180
+ return value.map((v) => protoDecode(field, v));
185
181
  }
186
- if (metadata.isMap && metadata.of) {
187
- const protoDecodeFn = getProtoDecodeFn(metadata.of);
182
+ if (field.isMap && field.of) {
183
+ const protoDecodeFn = getProtoDecodeFn(field.of);
188
184
  return new Map(
189
185
  Object.entries(value).map(([key, val]) => [
190
186
  key,
@@ -192,11 +188,11 @@ const protoDecode = (metadata, value) => {
192
188
  ])
193
189
  );
194
190
  }
195
- if (metadata.isClass)
196
- return makeProtoDecode(metadata.modelRef)(value);
197
- if (metadata.enum)
198
- return metadata.enum.values.at(value);
199
- return getProtoDecodeFn(metadata.modelRef)(value);
191
+ if (field.isClass)
192
+ return makeProtoDecode(field.modelRef)(value);
193
+ if (field.enum)
194
+ return field.enum.values.at(value);
195
+ return getProtoDecodeFn(field.modelRef)(value);
200
196
  };
201
197
  const getPredefinedProtoDecodeFn = (refName) => {
202
198
  const protoDecode2 = Reflect.getMetadata(refName, ProtoDecodeStorage.prototype);
@@ -210,11 +206,10 @@ const makeProtoDecode = (modelRef) => {
210
206
  const predefinedProtoDecode = getPredefinedProtoDecodeFn(refName);
211
207
  if (predefinedProtoDecode)
212
208
  return predefinedProtoDecode;
213
- const fieldMetas = getFieldMetas(modelRef);
214
209
  const protoDecodeFn = (value) => {
215
210
  const result = {};
216
- fieldMetas.forEach((fieldMeta) => {
217
- result[fieldMeta.key] = protoDecode(fieldMeta, value[fieldMeta.key]);
211
+ Object.entries(modelRef.field).forEach(([key, field], id) => {
212
+ result[key] = protoDecode(field.getProps(), value[key]);
218
213
  });
219
214
  return result;
220
215
  };
@@ -226,8 +221,7 @@ const decode = (modelRef, buffer) => {
226
221
  const message = protoModel.decode(Buffer.from(buffer));
227
222
  const data = protoModel.toObject(message);
228
223
  const protoDecode2 = makeProtoDecode(modelRef);
229
- const crystalize = makeCrystalize(modelRef);
230
- const result = crystalize(protoDecode2(data));
224
+ const result = new modelRef().set(protoDecode2(data));
231
225
  return result;
232
226
  };
233
227
  export {
package/esm/src/gql.js CHANGED
@@ -11,7 +11,7 @@ var __decorateClass = (decorators, target, key, kind) => {
11
11
  };
12
12
  import { arraiedModel, dayjs, Float, ID, Int, JSON } from "@akanjs/base";
13
13
  import { capitalize } from "@akanjs/common";
14
- import { constantInfo, getFieldMetas } from "@akanjs/constant";
14
+ import { constantInfo } from "@akanjs/constant";
15
15
  import * as Nest from "@nestjs/graphql";
16
16
  import { Field, InputType, ObjectType } from "@nestjs/graphql";
17
17
  import { isDayjs } from "dayjs";
@@ -65,14 +65,16 @@ const gqlNestFieldMap = /* @__PURE__ */ new Map([
65
65
  [JSON, GraphQLJSON],
66
66
  [Map, GraphQLJSON]
67
67
  ]);
68
- const applyNestField = (model, fieldMeta, type = "object") => {
69
- if (fieldMeta.fieldType === "hidden" && type === "object")
68
+ const applyNestField = (model, field, key, type = "object") => {
69
+ if (field.fieldType === "hidden" && type === "object")
70
70
  return;
71
- const modelRef = fieldMeta.isClass ? type === "object" ? generateGql(fieldMeta.modelRef) : fieldMeta.isScalar ? generateGqlInput(fieldMeta.modelRef) : Nest.ID : gqlNestFieldMap.get(fieldMeta.modelRef) ?? fieldMeta.modelRef;
72
- Field(() => arraiedModel(modelRef, fieldMeta.arrDepth), { nullable: fieldMeta.nullable })(
73
- model.prototype,
74
- fieldMeta.key
75
- );
71
+ const modelRef = field.isClass ? type === "object" ? generateGql(field.modelRef) : field.isScalar ? generateGqlInput(field.modelRef) : Nest.ID : gqlNestFieldMap.get(field.modelRef) ?? field.modelRef;
72
+ Field(() => arraiedModel(modelRef, field.arrDepth), { nullable: field.nullable })(model.prototype, key);
73
+ };
74
+ const gqlClassMap = /* @__PURE__ */ new Map();
75
+ const getGqlClass = (modelRef) => {
76
+ return gqlClassMap.get(modelRef) ?? class GqlClass {
77
+ };
76
78
  };
77
79
  const generateGqlInput = (inputRef) => {
78
80
  const refName = constantInfo.getRefName(inputRef);
@@ -81,12 +83,9 @@ const generateGqlInput = (inputRef) => {
81
83
  const predefinedInputGql = getPredefinedInqutGql(gqlName);
82
84
  if (predefinedInputGql)
83
85
  return predefinedInputGql;
84
- const fieldMetas = getFieldMetas(inputRef);
85
- class InputGql {
86
- }
87
- const inputGql = constantInfo.isScalar(inputRef) ? InputGql : constantInfo.getDatabase(refName).input;
88
- fieldMetas.forEach((fieldMeta) => {
89
- applyNestField(inputGql, fieldMeta, "input");
86
+ const inputGql = getGqlClass(constantInfo.isScalar(inputRef) ? inputRef : constantInfo.getDatabase(refName).input);
87
+ Object.entries(inputRef.field).forEach(([key, field], id) => {
88
+ applyNestField(inputGql, field.getProps(), key, "input");
90
89
  });
91
90
  InputType(gqlName)(inputGql);
92
91
  setPredefinedInqutGql(gqlName, inputGql);
@@ -103,12 +102,11 @@ const generateGql = (objectRef) => {
103
102
  const predefinedObjectGql = getPredefinedObjectGql(gqlName);
104
103
  if (predefinedObjectGql)
105
104
  return predefinedObjectGql;
106
- const fieldMetas = getFieldMetas(objectRef);
107
- class ObjectGql {
108
- }
109
- const objectGql = constantInfo.isScalar(objectRef) || constantInfo.isInsight(objectRef) ? ObjectGql : constantInfo.getDatabase(refName).full;
110
- fieldMetas.forEach((fieldMeta) => {
111
- applyNestField(objectGql, fieldMeta);
105
+ const objectGql = getGqlClass(
106
+ constantInfo.isScalar(objectRef) || constantInfo.isInsight(objectRef) ? objectRef : constantInfo.getDatabase(refName).full
107
+ );
108
+ Object.entries(objectRef.field).forEach(([key, field], id) => {
109
+ applyNestField(objectGql, field.getProps(), key);
112
110
  });
113
111
  ObjectType(gqlName)(objectGql);
114
112
  setPredefinedObjectGql(gqlName, objectGql);
@@ -1,6 +1,6 @@
1
1
  import { arraiedModel, Float, getNonArrayModel, ID, Int, JSON, Upload } from "@akanjs/base";
2
2
  import { capitalize, lowerlize } from "@akanjs/common";
3
- import { constantInfo, getFieldMetas } from "@akanjs/constant";
3
+ import { constantInfo } from "@akanjs/constant";
4
4
  import { getNestParamDecorator, None } from "@akanjs/nest";
5
5
  import { getServiceRefs, isServiceEnabled } from "@akanjs/service";
6
6
  import {
@@ -67,20 +67,19 @@ const resolverOf = (sigRef, allSrvs) => {
67
67
  const resolveFieldKeys = resolveFieldMetas.map((resolveFieldMeta) => resolveFieldMeta.key);
68
68
  if (sigMeta.returns) {
69
69
  const modelRef = sigMeta.returns();
70
- const fieldMetas = getFieldMetas(modelRef);
71
- fieldMetas.filter((fieldMeta) => fieldMeta.isClass && !fieldMeta.isScalar && !resolveFieldKeys.includes(fieldMeta.key)).forEach((fieldMeta) => {
72
- const refName = constantInfo.getRefName(fieldMeta.modelRef);
70
+ Object.entries(modelRef.field).filter(([key, field]) => field.isClass && !field.isScalar && !resolveFieldKeys.includes(key)).forEach(([key, field]) => {
71
+ const refName = constantInfo.getRefName(field.modelRef);
73
72
  const className = capitalize(refName);
74
73
  const serviceName = `${refName}Service`;
75
- Rsv.prototype[fieldMeta.key] = async function(parent) {
74
+ Rsv.prototype[key] = async function(parent) {
76
75
  const service = this[serviceName];
77
- return fieldMeta.arrDepth ? await service[`load${className}Many`](parent[fieldMeta.key]) : await service[`load${className}`](parent[fieldMeta.key]);
76
+ return field.arrDepth ? await service[`load${className}Many`](parent[key]) : await service[`load${className}`](parent[key]);
78
77
  };
79
- Nest.Parent()(Rsv.prototype, fieldMeta.key, 0);
80
- Nest.ResolveField(getNestReturn(() => arraiedModel(fieldMeta.modelRef, fieldMeta.arrDepth)))(
78
+ Nest.Parent()(Rsv.prototype, key, 0);
79
+ Nest.ResolveField(getNestReturn(() => arraiedModel(field.modelRef, field.arrDepth)))(
81
80
  Rsv.prototype,
82
- fieldMeta.key,
83
- Object.getOwnPropertyDescriptor(Rsv.prototype, fieldMeta.key) ?? {}
81
+ key,
82
+ Object.getOwnPropertyDescriptor(Rsv.prototype, key) ?? {}
84
83
  );
85
84
  });
86
85
  }
package/esm/src/schema.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  JSON
9
9
  } from "@akanjs/base";
10
10
  import { isDayjs, Logger } from "@akanjs/common";
11
- import { constantInfo, getFieldMetas, makeDefault } from "@akanjs/constant";
11
+ import { constantInfo } from "@akanjs/constant";
12
12
  import { documentInfo, getDefaultSchemaOptions, ObjectId } from "@akanjs/document";
13
13
  import { Schema, Types } from "mongoose";
14
14
  import { applyNestField } from ".";
@@ -44,87 +44,87 @@ const scalarMongoTypeMap = /* @__PURE__ */ new Map([
44
44
  [Boolean, Boolean],
45
45
  [Date, Date]
46
46
  ]);
47
- const applyMongoProp = (schemaProps, fieldMeta) => {
48
- if (["id", "createdAt", "updatedAt"].includes(fieldMeta.key) || fieldMeta.fieldType === "resolve")
47
+ const applyMongoProp = (schemaProps, field, key) => {
48
+ if (["id", "createdAt", "updatedAt"].includes(key) || field.fieldType === "resolve")
49
49
  return;
50
- const type = fieldMeta.isClass ? fieldMeta.isScalar ? createSchema(fieldMeta.modelRef) : ObjectId : scalarMongoTypeMap.get(fieldMeta.modelRef) ?? fieldMeta.modelRef;
50
+ const type = field.isClass ? field.isScalar ? createSchema(field.modelRef) : ObjectId : scalarMongoTypeMap.get(field.modelRef) ?? field.modelRef;
51
51
  let prop = {};
52
- if (fieldMeta.optArrDepth) {
52
+ if (field.optArrDepth) {
53
53
  prop.type = type;
54
54
  prop.required = true;
55
- if (fieldMeta.isClass && !fieldMeta.refPath)
56
- prop.ref = constantInfo.getRefName(fieldMeta.modelRef);
57
- if (fieldMeta.refPath)
58
- prop.refPath = fieldMeta.refPath;
59
- if (typeof fieldMeta.min === "number")
60
- prop.min = fieldMeta.min;
61
- if (typeof fieldMeta.max === "number")
62
- prop.max = fieldMeta.max;
63
- if (fieldMeta.enum)
64
- prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
65
- if (typeof fieldMeta.minlength === "number")
66
- prop.minlength = fieldMeta.minlength;
67
- if (typeof fieldMeta.maxlength === "number")
68
- prop.maxlength = fieldMeta.maxlength;
69
- if (fieldMeta.validate) {
55
+ if (field.isClass && !field.refPath)
56
+ prop.ref = constantInfo.getRefName(field.modelRef);
57
+ if (field.refPath)
58
+ prop.refPath = field.refPath;
59
+ if (typeof field.min === "number")
60
+ prop.min = field.min;
61
+ if (typeof field.max === "number")
62
+ prop.max = field.max;
63
+ if (field.enum)
64
+ prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
65
+ if (typeof field.minlength === "number")
66
+ prop.minlength = field.minlength;
67
+ if (typeof field.maxlength === "number")
68
+ prop.maxlength = field.maxlength;
69
+ if (field.validate) {
70
70
  prop.validate = function(value) {
71
- return fieldMeta.validate?.(fieldMeta.modelRef === Date && !!value ? dayjs() : value, this) ?? true;
71
+ return field.validate?.(field.modelRef === Date && !!value ? dayjs() : value, this) ?? true;
72
72
  };
73
73
  }
74
- prop = { type: arraiedModel(prop, fieldMeta.optArrDepth), default: [], required: true };
75
- if (fieldMeta.modelRef.prototype === Date.prototype) {
74
+ prop = { type: arraiedModel(prop, field.optArrDepth), default: [], required: true };
75
+ if (field.modelRef.prototype === Date.prototype) {
76
76
  prop.get = (dates) => applyFnToArrayObjects(dates, (date) => dayjs(date));
77
77
  prop.set = (days) => applyFnToArrayObjects(days, (day) => day.toDate());
78
78
  }
79
- if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === ID.prototype) {
79
+ if (field.isClass && !field.isScalar || field.modelRef.prototype === ID.prototype) {
80
80
  prop.get = (ids) => applyFnToArrayObjects(ids, (id) => id.toString());
81
81
  prop.set = (ids) => applyFnToArrayObjects(ids, (id) => new Types.ObjectId(id));
82
82
  }
83
83
  } else {
84
- prop.type = arraiedModel(type, fieldMeta.arrDepth);
85
- prop.required = !fieldMeta.nullable;
86
- if (fieldMeta.isMap) {
87
- prop.of = scalarMongoTypeMap.get(fieldMeta.of) ?? createSchema(fieldMeta.of);
88
- if (!fieldMeta.default)
84
+ prop.type = arraiedModel(type, field.arrDepth);
85
+ prop.required = !field.nullable;
86
+ if (field.isMap) {
87
+ prop.of = scalarMongoTypeMap.get(field.of) ?? createSchema(field.of);
88
+ if (!field.default)
89
89
  prop.default = /* @__PURE__ */ new Map();
90
90
  }
91
- if (fieldMeta.default !== null) {
92
- if (typeof fieldMeta.default === "function")
91
+ if (field.default !== null) {
92
+ if (typeof field.default === "function")
93
93
  prop.default = function() {
94
- const def = fieldMeta.default(this);
94
+ const def = field.default(this);
95
95
  return isDayjs(def) ? def.toDate() : def;
96
96
  };
97
97
  else
98
- prop.default = isDayjs(fieldMeta.default) ? fieldMeta.default.toDate() : fieldMeta.default;
98
+ prop.default = isDayjs(field.default) ? field.default.toDate() : field.default;
99
99
  }
100
- if (typeof fieldMeta.immutable !== "undefined")
101
- prop.immutable = fieldMeta.immutable;
102
- if (fieldMeta.isClass && !fieldMeta.refPath)
103
- prop.ref = constantInfo.getRefName(fieldMeta.modelRef);
104
- if (fieldMeta.refPath)
105
- prop.refPath = fieldMeta.refPath;
106
- if (typeof fieldMeta.min === "number")
107
- prop.min = fieldMeta.min;
108
- if (typeof fieldMeta.max === "number")
109
- prop.max = fieldMeta.max;
110
- if (fieldMeta.enum)
111
- prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
112
- if (typeof fieldMeta.select === "boolean")
113
- prop.select = fieldMeta.select;
114
- if (typeof fieldMeta.minlength === "number")
115
- prop.minlength = fieldMeta.minlength;
116
- if (typeof fieldMeta.maxlength === "number")
117
- prop.maxlength = fieldMeta.maxlength;
118
- if (fieldMeta.nullable) {
100
+ if (typeof field.immutable !== "undefined")
101
+ prop.immutable = field.immutable;
102
+ if (field.isClass && !field.refPath)
103
+ prop.ref = constantInfo.getRefName(field.modelRef);
104
+ if (field.refPath)
105
+ prop.refPath = field.refPath;
106
+ if (typeof field.min === "number")
107
+ prop.min = field.min;
108
+ if (typeof field.max === "number")
109
+ prop.max = field.max;
110
+ if (field.enum)
111
+ prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
112
+ if (typeof field.select === "boolean")
113
+ prop.select = field.select;
114
+ if (typeof field.minlength === "number")
115
+ prop.minlength = field.minlength;
116
+ if (typeof field.maxlength === "number")
117
+ prop.maxlength = field.maxlength;
118
+ if (field.nullable) {
119
119
  prop.get = (v) => v === void 0 ? void 0 : v;
120
120
  prop.set = (v) => v === null ? void 0 : v;
121
121
  }
122
- if (fieldMeta.modelRef.prototype === Date.prototype) {
122
+ if (field.modelRef.prototype === Date.prototype) {
123
123
  prop.get = (date) => applyFnToArrayObjects(date, (date2) => date2 ? dayjs(date2) : void 0);
124
124
  prop.set = (day) => applyFnToArrayObjects(day, (day2) => day2 ? dayjs(day2).toDate() : void 0);
125
125
  }
126
- if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === ID.prototype) {
127
- if (fieldMeta.arrDepth === 0) {
126
+ if (field.isClass && !field.isScalar || field.modelRef.prototype === ID.prototype) {
127
+ if (field.arrDepth === 0) {
128
128
  prop.get = (id) => id ? id.toString() : void 0;
129
129
  prop.set = (id) => id ? new Types.ObjectId(id) : void 0;
130
130
  } else {
@@ -132,26 +132,25 @@ const applyMongoProp = (schemaProps, fieldMeta) => {
132
132
  prop.set = (val) => applyFnToArrayObjects(val, (id) => id ? new Types.ObjectId(id) : void 0);
133
133
  }
134
134
  }
135
- if (fieldMeta.isClass && fieldMeta.isScalar && fieldMeta.default === null && !fieldMeta.nullable) {
136
- prop.default = makeDefault(fieldMeta.modelRef);
135
+ if (field.isClass && field.isScalar && field.default === null && !field.nullable) {
136
+ prop.default = field.modelRef.default;
137
137
  }
138
- if (fieldMeta.validate) {
138
+ if (field.validate) {
139
139
  prop.validate = function(value) {
140
- return fieldMeta.validate?.(fieldMeta.modelRef === Date && !!value ? dayjs() : value, this) ?? true;
140
+ return field.validate?.(field.modelRef === Date && !!value ? dayjs() : value, this) ?? true;
141
141
  };
142
142
  }
143
143
  }
144
- schemaProps[fieldMeta.key] = prop;
144
+ schemaProps[key] = prop;
145
145
  };
146
146
  const createSchema = (modelRef) => {
147
147
  const refName = constantInfo.getRefName(modelRef);
148
148
  const schemaMeta = getScalarSchemaMetaByName(refName);
149
149
  if (schemaMeta)
150
150
  return schemaMeta;
151
- const fieldMetas = getFieldMetas(modelRef);
152
151
  const schemaProps = {};
153
- fieldMetas.forEach((fieldMeta) => {
154
- applyMongoProp(schemaProps, fieldMeta);
152
+ Object.entries(modelRef.field).forEach(([key, field]) => {
153
+ applyMongoProp(schemaProps, field.getProps(), key);
155
154
  });
156
155
  const schema = new Schema(schemaProps);
157
156
  setScalarSchemaMetaByName(refName, schema);
@@ -162,7 +161,6 @@ const schemaOf = (modelRef, docRef, middleware) => {
162
161
  const schemaMeta = getSchemaMetaByName(refName);
163
162
  if (schemaMeta)
164
163
  return schemaMeta;
165
- const fieldMetas = getFieldMetas(docRef);
166
164
  const schemaProps = {
167
165
  createdAt: {
168
166
  type: Date,
@@ -175,8 +173,9 @@ const schemaOf = (modelRef, docRef, middleware) => {
175
173
  set: (day) => day ? dayjs(day).toDate() : day
176
174
  }
177
175
  };
178
- fieldMetas.forEach((fieldMeta) => {
179
- applyMongoProp(schemaProps, fieldMeta);
176
+ const cnst = constantInfo.getDatabase(refName);
177
+ Object.entries(cnst.full.field).forEach(([key, field]) => {
178
+ applyMongoProp(schemaProps, field.getProps(), key);
180
179
  });
181
180
  const schema = new Schema(schemaProps, getDefaultSchemaOptions());
182
181
  schema.methods.refresh = async function() {
@@ -239,7 +238,6 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
239
238
  const modelSchema = Reflect.getMetadata(refName, SchemaStorage.prototype);
240
239
  if (!modelSchema)
241
240
  throw new Error(`Schema of ${refName} not found`);
242
- const fieldMetas = getFieldMetas(docRef);
243
241
  const schemaProps = {
244
242
  createdAt: {
245
243
  type: Date,
@@ -252,13 +250,13 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
252
250
  set: (day) => day ? dayjs(day).toDate() : day
253
251
  }
254
252
  };
255
- fieldMetas.forEach((fieldMeta) => {
256
- applyMongoProp(schemaProps, fieldMeta);
257
- applyNestField(originDoc, fieldMeta);
253
+ const cnst = constantInfo.getDatabase(refName);
254
+ Object.entries(cnst.full.field).forEach(([key, field]) => {
255
+ applyMongoProp(schemaProps, field.getProps(), key);
256
+ applyNestField(originDoc, field.getProps(), key);
258
257
  });
259
- const inputFieldMetas = getFieldMetas(inputRef);
260
- inputFieldMetas.forEach((fieldMeta) => {
261
- applyNestField(originInput, fieldMeta, "input");
258
+ Object.entries(cnst.input.field).forEach(([key, field]) => {
259
+ applyNestField(originInput, field.getProps(), key, "input");
262
260
  });
263
261
  const schema = new Schema(schemaProps, getDefaultSchemaOptions());
264
262
  modelSchema.add(schema);
@@ -12,43 +12,34 @@ var __decorateClass = (decorators, target, key, kind) => {
12
12
  var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
13
13
  import { baseEnv } from "@akanjs/base";
14
14
  import { capitalize, Logger } from "@akanjs/common";
15
- import { constantInfo, getFieldMetaMap, getFieldMetas } from "@akanjs/constant";
15
+ import { constantInfo } from "@akanjs/constant";
16
16
  import { documentInfo, getFilterSortMap } from "@akanjs/document";
17
17
  import { Global, Inject, Injectable, Module } from "@nestjs/common";
18
18
  import { InjectConnection } from "@nestjs/mongoose";
19
19
  const hasTextField = (modelRef) => {
20
- const fieldMetas = getFieldMetas(modelRef);
21
- return fieldMetas.some(
22
- (fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
20
+ return Object.entries(modelRef.field).some(
21
+ ([_, field]) => !!field.text || field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)
23
22
  );
24
23
  };
25
24
  const getTextFieldKeys = (modelRef) => {
26
25
  const allSearchFields = [];
27
26
  const allFilterFields = [];
28
- const fieldMetaMap = getFieldMetaMap(modelRef);
29
- const fieldMetas = [...fieldMetaMap.values()];
30
- const stringTextFields = fieldMetas.filter((fieldMeta) => !!fieldMeta.text).map((fieldMeta) => {
31
- if (fieldMeta.text === "filter")
32
- allFilterFields.push(fieldMeta.key);
33
- else if (fieldMeta.text === "search")
34
- allSearchFields.push(fieldMeta.key);
35
- return fieldMeta.key;
27
+ const stringTextFields = Object.entries(modelRef.field).filter(([_, field]) => !!field.text).map(([key, field]) => {
28
+ if (field.text === "filter")
29
+ allFilterFields.push(key);
30
+ else if (field.text === "search")
31
+ allSearchFields.push(key);
32
+ return key;
36
33
  });
37
- const scalarTextFields = fieldMetas.filter(
38
- (fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
39
- ).map((fieldMeta) => fieldMeta.key);
34
+ const scalarTextFields = Object.entries(modelRef.field).filter(([_, field]) => field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)).map(([key, field]) => key);
40
35
  const deepFields = scalarTextFields.map((key) => {
41
- const fieldMeta = fieldMetaMap.get(key);
42
- if (!fieldMeta)
43
- throw new Error(`No fieldMeta for ${key}`);
44
- const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(
45
- fieldMeta.modelRef
46
- );
47
- allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
48
- allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
36
+ const field = modelRef.field[key];
37
+ const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(field.modelRef);
38
+ allFilterFields2.push(...allSearchFields2.map((field2) => `${key}.${field2}`));
39
+ allSearchFields2.push(...stringTextFields2.map((field2) => `${key}.${field2}`));
49
40
  return [
50
- ...stringTextFields2.map((field) => `${key}.${field}`),
51
- ...allTextFields.map((field) => `${key}.${field}`)
41
+ ...stringTextFields2.map((field2) => `${key}.${field2}`),
42
+ ...allTextFields.map((field2) => `${key}.${field2}`)
52
43
  ];
53
44
  }).flat();
54
45
  return {
@@ -60,7 +51,6 @@ const getTextFieldKeys = (modelRef) => {
60
51
  };
61
52
  };
62
53
  const makeTextFilter = (modelRef) => {
63
- const fieldMetaMap = getFieldMetaMap(modelRef);
64
54
  const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
65
55
  const filterData = (data, assignObj = {}) => {
66
56
  if (Array.isArray(data))
@@ -69,10 +59,8 @@ const makeTextFilter = (modelRef) => {
69
59
  Object.fromEntries([
70
60
  ...stringTextFields.map((key) => [key, data[key]]),
71
61
  ...scalarTextFields.map((key) => {
72
- const fieldMeta = fieldMetaMap.get(key);
73
- if (!fieldMeta)
74
- throw new Error(`No fieldMeta for ${key}`);
75
- const filterFunc = makeTextFilter(fieldMeta.modelRef);
62
+ const field = modelRef.field[key];
63
+ const filterFunc = makeTextFilter(field.modelRef);
76
64
  return [key, filterFunc(data[key])];
77
65
  })
78
66
  ]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/server",
3
- "version": "1.0.5",
3
+ "version": "1.0.7-canary.0",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,3 +1,3 @@
1
- import { type Type } from "@akanjs/base";
2
- export declare const encode: <T>(modelRef: Type<T>, value: object) => Buffer<ArrayBufferLike> | null;
3
- export declare const decode: <T>(modelRef: Type<T>, buffer: Buffer) => T;
1
+ import { Cnst, ConstantMethods } from "@akanjs/constant";
2
+ export declare const encode: <T>(modelRef: Cnst<T>, value: object) => Buffer<ArrayBufferLike> | null;
3
+ export declare const decode: <T>(modelRef: Cnst<T & ConstantMethods<T>>, buffer: Buffer) => import("@akanjs/constant").Crystal<T & ConstantMethods<T>>;
package/src/gql.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BaseObject, Dayjs, Type } from "@akanjs/base";
2
- import { ConstantFieldMeta, DocumentModel } from "@akanjs/constant";
2
+ import { Cnst, DocumentModel, FieldProps } from "@akanjs/constant";
3
3
  import type { Doc } from "@akanjs/document";
4
4
  import * as Nest from "@nestjs/graphql";
5
5
  import { ValueNode } from "graphql";
@@ -9,6 +9,6 @@ export declare class DateScalar implements Nest.CustomScalar<Date, Dayjs> {
9
9
  serialize(value: Dayjs): Date;
10
10
  parseLiteral(ast: ValueNode): Dayjs;
11
11
  }
12
- export declare const applyNestField: (model: Type, fieldMeta: ConstantFieldMeta, type?: "object" | "input") => void;
13
- export declare const generateGqlInput: <InputModel>(inputRef: Type<InputModel>) => Type<DocumentModel<InputModel>>;
14
- export declare const generateGql: <ObjectModel>(objectRef: Type<ObjectModel>) => Type<ObjectModel extends BaseObject ? Doc<ObjectModel> : DocumentModel<ObjectModel>>;
12
+ export declare const applyNestField: (model: Type, field: FieldProps, key: string, type?: "object" | "input") => void;
13
+ export declare const generateGqlInput: <InputModel>(inputRef: Cnst<InputModel>) => Type<DocumentModel<InputModel>>;
14
+ export declare const generateGql: <ObjectModel>(objectRef: Cnst<ObjectModel>) => Type<ObjectModel extends BaseObject ? Doc<ObjectModel> : DocumentModel<ObjectModel>>;
package/src/module.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Type } from "@akanjs/base";
2
- import { type ConstantModel } from "@akanjs/constant";
2
+ import { Cnst, type ConstantModel } from "@akanjs/constant";
3
3
  import { Database } from "@akanjs/document";
4
4
  import { DynamicModule } from "@nestjs/common";
5
5
  import type { NextFunction, Request, Response } from "express";
@@ -20,7 +20,7 @@ export declare const serviceModuleOf: ({ signal, service }: ServiceModuleCreateO
20
20
  [key: string]: Type | undefined;
21
21
  }) => DynamicModule | null;
22
22
  interface ScalarModulesCreateOptions {
23
- constants: Type[];
23
+ constants: Cnst[];
24
24
  }
25
25
  export declare const scalarModulesOf: ({ constants }: ScalarModulesCreateOptions, allSrvs: {
26
26
  [key: string]: Type | undefined;
@@ -1,5 +1,4 @@
1
- import { type Type } from "@akanjs/base";
2
- import { type TextDoc } from "@akanjs/constant";
1
+ import { Cnst, type TextDoc } from "@akanjs/constant";
3
2
  import type { Types } from "mongoose";
4
3
  export interface ChangedData {
5
4
  _id: {
@@ -25,7 +24,7 @@ export interface ChangedData {
25
24
  };
26
25
  fullDocument?: Record<string, any>;
27
26
  }
28
- export declare const makeTextFilter: (modelRef: Type) => (data: Record<string, any>, assignObj?: {
27
+ export declare const makeTextFilter: (modelRef: Cnst) => (data: Record<string, any>, assignObj?: {
29
28
  [key: string]: string;
30
29
  }) => TextDoc;
31
30
  export declare class SearchDaemonModule {