@nhtio/lucid-resourceful 1.20261003.0 → 1.20261014.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.
@@ -3928,7 +3928,7 @@ class OpenApiSchemaService {
3928
3928
  } catch (error) {
3929
3929
  return { type: "string" };
3930
3930
  }
3931
- const format = "format" in dataType && typeof dataType.format === "string" ? dataType.format : void 0;
3931
+ const format = "format" in dataType && typeof dataType.format === "string" ? dataType.format : "ResourcefulDateType" === dataType.constructor.name ? "date" : void 0;
3932
3932
  switch (baseType) {
3933
3933
  case "string": {
3934
3934
  if (format) {
@@ -8423,6 +8423,26 @@ const toXmlValue = (value, parent) => {
8423
8423
  return {};
8424
8424
  }
8425
8425
  };
8426
+ const serializeResourcefulDates = (data, model) => {
8427
+ if (Array.isArray(data)) return data.map((item) => serializeResourcefulDates(item, model));
8428
+ if (!data || typeof data !== "object") return data;
8429
+ const serialized = { ...data };
8430
+ for (const [key, definition] of model.$resourcefulColumns.entries()) {
8431
+ const serializeAs = definition.serializeAs;
8432
+ const jsonKey = null === serializeAs ? null : serializeAs || key;
8433
+ if (!jsonKey || !(jsonKey in serialized)) continue;
8434
+ const value = serialized[jsonKey];
8435
+ if (!type_guardsDMqlMT2e.isInstanceOf(definition.type, "ResourcefulDateType", definitions.ResourcefulDateType)) continue;
8436
+ if (value instanceof luxon.DateTime) serialized[jsonKey] = value.toISODate();
8437
+ else if (value instanceof Date) serialized[jsonKey] = luxon.DateTime.fromJSDate(value).toISODate();
8438
+ }
8439
+ for (const [key, value] of Object.entries(serialized)) {
8440
+ if (!Array.isArray(value) && (!value || typeof value !== "object")) continue;
8441
+ const relation = model.$resourcefulRelationships.get(key)?.relatedModel();
8442
+ if (decorator_utils.isResourcefulModel(relation)) serialized[key] = serializeResourcefulDates(value, relation);
8443
+ }
8444
+ return serialized;
8445
+ };
8426
8446
  const encoder$3 = (_key, value) => {
8427
8447
  if (value instanceof Date) {
8428
8448
  return luxon.DateTime.fromJSDate(value).toISO();
@@ -8620,6 +8640,9 @@ let ResourcefulModelController$1 = class ResourcefulModelController {
8620
8640
  Object.entries(headers).forEach(([key, value]) => {
8621
8641
  ctx.response.header(key, value);
8622
8642
  });
8643
+ if ("undefined" !== typeof body && 204 !== status) {
8644
+ body = serializeResourcefulDates(body, model);
8645
+ }
8623
8646
  if (ctx.request.header("x-resourceful-response-encoded") === "1") {
8624
8647
  ctx.response.header(
8625
8648
  "Content-Type",
@@ -8721,6 +8744,7 @@ let ResourcefulModelController$1 = class ResourcefulModelController {
8721
8744
  };
8722
8745
  const runner = this.#mutators.runner("index");
8723
8746
  await runner.run(ret, ctx);
8747
+ ret.records = serializeResourcefulDates(ret.records, model);
8724
8748
  return respond(200, ret, {
8725
8749
  "x-metadata-url": metaUrl,
8726
8750
  [linkedUrlKey]: linkedUrl
@@ -8884,6 +8908,10 @@ let ResourcefulModelController$1 = class ResourcefulModelController {
8884
8908
  };
8885
8909
  const runner = this.#mutators.runner("readRelated");
8886
8910
  await runner.run(ret, ctx);
8911
+ const relatedModel = model.$resourcefulRelationships.get(relationship)?.relatedModel();
8912
+ if (decorator_utils.isResourcefulModel(relatedModel)) {
8913
+ ret.records = serializeResourcefulDates(ret.records, relatedModel);
8914
+ }
8887
8915
  return respond(200, ret, {
8888
8916
  "x-metadata-url": metaUrl,
8889
8917
  [linkedUrlKey]: linkedUrl
@@ -10001,7 +10029,7 @@ class ResourcefulRouterChoicesService {
10001
10029
  const serveAs = resourceful_error_middleware.negotiateContent(ctx);
10002
10030
  const method = ctx.request.method() || "GET";
10003
10031
  const url = ctx.request.url() || "/";
10004
- const respond = (status, body, headers = {}) => {
10032
+ const respond = (status, body, headers = {}, model) => {
10005
10033
  ctx.response.status(status);
10006
10034
  Object.entries(this.#options.headers).forEach(([key, value]) => {
10007
10035
  ctx.response.header(key, value);
@@ -10027,6 +10055,7 @@ class ResourcefulRouterChoicesService {
10027
10055
  break;
10028
10056
  }
10029
10057
  if ("undefined" !== typeof body && 204 !== status) {
10058
+ if (model) body = serializeResourcefulDates(body, model);
10030
10059
  switch (serveAs) {
10031
10060
  case "yaml":
10032
10061
  ctx.response.send(resourceful_error_middleware.YAML.stringify(body, encoder$2));
@@ -10163,11 +10192,32 @@ class ResourcefulRouterChoicesService {
10163
10192
  const countQuery = query.clone().count(`${valueColumn} as total`);
10164
10193
  const resultsQuery = query.clone().select(`${valueColumn} as value`, `${displayColumn} as title`);
10165
10194
  const [countResults, choices] = await Promise.all([countQuery.first(), resultsQuery]);
10195
+ const isDateColumn = (column) => {
10196
+ for (const [attribute, definition] of relatedModel.$columnsDefinitions) {
10197
+ if (definition.columnName !== column && attribute !== column) continue;
10198
+ const resourcefulColumn2 = relatedModel.$resourcefulColumns.get(attribute);
10199
+ return !!resourcefulColumn2 && type_guardsDMqlMT2e.isInstanceOf(resourcefulColumn2.type, "ResourcefulDateType", definitions.ResourcefulDateType);
10200
+ }
10201
+ return false;
10202
+ };
10203
+ const dateChoiceKeys = [
10204
+ ...isDateColumn(valueColumn) ? ["value"] : [],
10205
+ ...isDateColumn(displayColumn) ? ["title"] : []
10206
+ ];
10207
+ for (const choice of choices) {
10208
+ for (const key of dateChoiceKeys) {
10209
+ const value = choice[key];
10210
+ if (value instanceof Date) choice[key] = luxon.DateTime.fromJSDate(value).toISODate();
10211
+ else if (value instanceof luxon.DateTime) choice[key] = value.toISODate();
10212
+ else if ("number" === typeof value) choice[key] = luxon.DateTime.fromMillis(value).toISODate();
10213
+ else if ("string" === typeof value) {
10214
+ const date = luxon.DateTime.fromSQL(value).isValid ? luxon.DateTime.fromSQL(value) : luxon.DateTime.fromISO(value);
10215
+ if (date.isValid) choice[key] = date.toISODate();
10216
+ }
10217
+ }
10218
+ }
10166
10219
  const total = this.#getValidatedValue(countResults?.total, joi.joi.number().min(0), 0);
10167
- respond(200, {
10168
- total,
10169
- choices
10170
- });
10220
+ respond(200, { total, choices }, {}, relatedModel);
10171
10221
  }
10172
10222
  static async serve(ctx, options, models, controllers) {
10173
10223
  const instance = new ResourcefulRouterChoicesService(options, models, controllers);
@@ -15369,6 +15419,12 @@ const coerceEntityForIeee754 = (entity, model, ieee754Compatible, metaCache, vis
15369
15419
  if (serializeAs === null) continue;
15370
15420
  const jsonKey = serializeAs || propertyKey;
15371
15421
  if (!(jsonKey in entity)) continue;
15422
+ if (type_guardsDMqlMT2e.isInstanceOf(prop.definition.type, "ResourcefulDateType", definitions.ResourcefulDateType)) {
15423
+ const date = entity[jsonKey];
15424
+ if (date instanceof luxon.DateTime) entity[jsonKey] = date.toISODate();
15425
+ else if (date instanceof Date) entity[jsonKey] = luxon.DateTime.fromJSDate(date).toISODate();
15426
+ continue;
15427
+ }
15372
15428
  if (!isIeee754CompatibleRelevantType(prop.definition.type)) continue;
15373
15429
  coerceNumericField(entity, jsonKey, ieee754Compatible);
15374
15430
  continue;
@@ -15760,7 +15816,7 @@ class ResourcefulModelController2 {
15760
15816
  const mNs = "http://docs.oasis-open.org/odata/ns/metadata";
15761
15817
  const dNs = "http://docs.oasis-open.org/odata/ns/data";
15762
15818
  const buildEntry = (entity, meta) => {
15763
- const obj = toJsonEntity(entity);
15819
+ const obj = coercionModel ? serializeResourcefulDates(toJsonEntity(entity), coercionModel) : toJsonEntity(entity);
15764
15820
  const entryAttrs = errors$1.stripUndefinedValuesFromObject({
15765
15821
  "metadata:context": include("context") ? meta.context : void 0,
15766
15822
  "metadata:etag": include("etag") ? meta.etag : void 0
@@ -16764,21 +16820,55 @@ class ResourcefulModelController2 {
16764
16820
  if (!rel) continue;
16765
16821
  for (const record of ret.records) {
16766
16822
  try {
16767
- const relResp = await model.$onResourcefulReadRelationship(
16768
- Number(record[model.primaryKey]),
16769
- rel,
16770
- void 0,
16771
- 1,
16772
- 1e3,
16773
- void 0,
16774
- void 0,
16775
- ctx,
16776
- app,
16777
- []
16778
- );
16779
- record[originalExp] = Array.isArray(relResp && relResp.records) ? relResp.records : [];
16823
+ const relationship = model.$getRelation(rel);
16824
+ const relatedModel = model.$resourcefulRelationships.get(rel).relatedModel();
16825
+ if (!decorator_utils.isResourcefulModel(relatedModel)) throw new errors$1.E_RELATIONSHIP_NOT_FOUND_EXCEPTION();
16826
+ const { valid: relatedSelections } = await sanitizeSelect({}, relatedModel, ctx, app);
16827
+ const relatedRecords = [];
16828
+ let relatedPage = 1;
16829
+ while (true) {
16830
+ const relResp = relationship.type === "belongsTo" ? await relatedModel.$onResourcefulIndex(
16831
+ void 0,
16832
+ relatedPage,
16833
+ 100,
16834
+ relatedSelections,
16835
+ [[relatedModel.primaryKey, "asc"]],
16836
+ ctx,
16837
+ app,
16838
+ [
16839
+ async (_ctx, _app, relatedQuery) => {
16840
+ relationship.boot();
16841
+ relatedQuery.whereIn(
16842
+ relationship.localKeyColumnName,
16843
+ model.query().select(relationship.foreignKeyColumnName).where(
16844
+ model.$keys.attributesToColumns.get(model.primaryKey),
16845
+ record[model.primaryKey]
16846
+ )
16847
+ );
16848
+ }
16849
+ ]
16850
+ ) : await model.$onResourcefulReadRelationship(
16851
+ Number(record[model.primaryKey]),
16852
+ rel,
16853
+ void 0,
16854
+ relatedPage,
16855
+ 100,
16856
+ relatedSelections,
16857
+ [[relatedModel.primaryKey, "asc"]],
16858
+ ctx,
16859
+ app,
16860
+ []
16861
+ );
16862
+ relatedRecords.push(...relResp.records);
16863
+ if (relResp.records.length === 0 || relatedPage * 100 >= relResp.total) break;
16864
+ relatedPage++;
16865
+ }
16866
+ record[rel] = relationship.type === "belongsTo" ? relatedRecords[0] ?? null : relatedRecords;
16780
16867
  } catch (e) {
16781
- record[originalExp] = [];
16868
+ if (!(e instanceof errors$1.E_INVALID_RELATIONSHIP_EXCEPTION) && !(e instanceof utils.Exception && (e.status === 403 || e.status === 404))) {
16869
+ throw e;
16870
+ }
16871
+ record[rel] = model.$getRelation(rel)?.type === "belongsTo" ? null : [];
16782
16872
  }
16783
16873
  }
16784
16874
  }
@@ -17303,7 +17393,7 @@ async function configure(command) {
17303
17393
  }
17304
17394
  ]);
17305
17395
  }
17306
- const version = "1.20261003.0";
17396
+ const version = "1.20261014.0";
17307
17397
  exports.configure = configure;
17308
17398
  exports.getResourcefulModelCreatePayloadValidationSchema = getResourcefulModelCreatePayloadValidationSchema;
17309
17399
  exports.odataRouterMacro = odataRouterMacro;
@@ -17318,4 +17408,4 @@ exports.resourcefulManyToMany = resourcefulManyToMany;
17318
17408
  exports.resourcefulRouterMacro = resourcefulRouterMacro;
17319
17409
  exports.version = version;
17320
17410
  exports.withResourceful = withResourceful;
17321
- //# sourceMappingURL=index-DTsII176.cjs.map
17411
+ //# sourceMappingURL=index-ClFkusVv.cjs.map