monorise 1.3.0 → 1.5.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.
Files changed (31) hide show
  1. package/dist/base/index.d.ts +95 -1
  2. package/dist/base/index.js.map +1 -1
  3. package/dist/cli/cli.js +295 -102
  4. package/dist/cli/cli.js.map +1 -1
  5. package/dist/cli/templates/files/core-routes.ts +22 -0
  6. package/dist/cli/templates/files/example-page.tsx +103 -0
  7. package/dist/cli/templates/files/global-initializer.tsx +16 -0
  8. package/dist/cli/templates/files/global-loader.tsx +45 -0
  9. package/dist/cli/templates/files/globals.css +72 -0
  10. package/dist/cli/templates/files/lib-utils.ts +6 -0
  11. package/dist/cli/templates/files/monorise.config.ts +7 -0
  12. package/dist/cli/templates/files/proxy-request.ts +49 -0
  13. package/dist/cli/templates/files/proxy-route.ts +22 -0
  14. package/dist/cli/templates/files/sst.config.ts +30 -0
  15. package/dist/cli/templates/files/team-entity.ts +37 -0
  16. package/dist/cli/templates/files/team-membership.mutual.ts +16 -0
  17. package/dist/cli/templates/files/ui-button.tsx +65 -0
  18. package/dist/cli/templates/files/ui-card.tsx +103 -0
  19. package/dist/cli/templates/files/ui-input.tsx +19 -0
  20. package/dist/cli/templates/files/ui-label.tsx +24 -0
  21. package/dist/cli/templates/files/user-entity.ts +43 -0
  22. package/dist/core/index.d.ts +13 -28
  23. package/dist/core/index.js +205 -26
  24. package/dist/core/index.js.map +1 -1
  25. package/dist/react/index.d.ts +6 -2
  26. package/dist/react/index.d.ts.map +1 -1
  27. package/dist/react/index.js +2 -1
  28. package/dist/react/index.js.map +1 -1
  29. package/dist/react/services/core.service.d.ts +3 -1
  30. package/dist/react/services/core.service.d.ts.map +1 -1
  31. package/package.json +1 -1
@@ -175,6 +175,7 @@ var StandardErrorCode = {
175
175
  ENTITY_ID_IS_UNDEFINED: "ENTITY_ID_IS_UNDEFINED",
176
176
  ENTITY_IS_UNDEFINED: "ENTITY_IS_UNDEFINED",
177
177
  ENTITY_NOT_FOUND: "ENTITY_NOT_FOUND",
178
+ INVALID_CONDITION: "INVALID_CONDITION",
178
179
  INVALID_ENTITY_TYPE: "INVALID_ENTITY_TYPE",
179
180
  INVALID_MUTUAL: "INVALID_MUTUAL",
180
181
  INVALID_QUERY: "INVALID_QUERY",
@@ -947,22 +948,22 @@ var EntityRepository = class extends Repository {
947
948
  }
948
949
  });
949
950
  }
950
- adjustEntity(entityType, entityId, adjustments, constraints) {
951
+ adjustEntity(entityType, entityId, adjustments, opts) {
951
952
  return __async(this, null, function* () {
952
953
  const entity = new Entity(entityType, entityId);
953
- const { UpdateExpression, ConditionExpression, ExpressionAttributeNames, ExpressionAttributeValues } = this.toAdjustUpdate(adjustments, constraints);
954
+ const { UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues } = this.toAdjustUpdate(adjustments);
954
955
  const updatedAtExpression = ", #updatedAt = :updatedAt";
955
956
  ExpressionAttributeNames["#updatedAt"] = "updatedAt";
956
957
  ExpressionAttributeValues[":updatedAt"] = { S: (/* @__PURE__ */ new Date()).toISOString() };
957
- const resp = yield this.dynamodbClient.updateItem(__spreadProps(__spreadValues({
958
+ const resp = yield this.dynamodbClient.updateItem({
958
959
  TableName: this.TABLE_NAME,
959
960
  Key: entity.keys(),
960
- UpdateExpression: UpdateExpression + updatedAtExpression
961
- }, ConditionExpression && { ConditionExpression }), {
962
- ExpressionAttributeNames,
963
- ExpressionAttributeValues,
961
+ UpdateExpression: UpdateExpression + updatedAtExpression,
962
+ ConditionExpression: (opts == null ? void 0 : opts.ConditionExpression) || "attribute_exists(PK)",
963
+ ExpressionAttributeNames: __spreadValues(__spreadValues({}, ExpressionAttributeNames), opts == null ? void 0 : opts.ExpressionAttributeNames),
964
+ ExpressionAttributeValues: __spreadValues(__spreadValues({}, ExpressionAttributeValues), opts == null ? void 0 : opts.ExpressionAttributeValues),
964
965
  ReturnValues: "ALL_NEW"
965
- }));
966
+ });
966
967
  return Entity.fromItem(resp.Attributes);
967
968
  });
968
969
  }
@@ -7301,7 +7302,16 @@ var UpdateEntityController = class {
7301
7302
  const accountId = c.req.header("account-id");
7302
7303
  const { entityType, entityId } = c.req.param();
7303
7304
  const body = yield c.req.json();
7304
- const _a = body, { $where: where } = _a, entityPayload = __objRest(_a, ["$where"]);
7305
+ const _a = body, { $condition: condition, $where: where } = _a, entityPayload = __objRest(_a, ["$condition", "$where"]);
7306
+ if (condition !== void 0) {
7307
+ if (typeof condition !== "string" || condition.trim().length === 0) {
7308
+ c.status(httpStatus8.BAD_REQUEST);
7309
+ return c.json({
7310
+ code: "API_VALIDATION_ERROR",
7311
+ message: "$condition must be a non-empty string"
7312
+ });
7313
+ }
7314
+ }
7305
7315
  const errorContext = {
7306
7316
  accountId,
7307
7317
  "req.params": c.req.param(),
@@ -7313,6 +7323,7 @@ var UpdateEntityController = class {
7313
7323
  entityId,
7314
7324
  entityPayload,
7315
7325
  accountId,
7326
+ condition,
7316
7327
  where
7317
7328
  });
7318
7329
  errorContext.entity = entity;
@@ -7331,6 +7342,10 @@ var UpdateEntityController = class {
7331
7342
  c.status(httpStatus8.NOT_FOUND);
7332
7343
  return c.json(__spreadValues({}, err.toJSON()));
7333
7344
  }
7345
+ if (err instanceof StandardError && err.code === StandardErrorCode.INVALID_CONDITION) {
7346
+ c.status(httpStatus8.BAD_REQUEST);
7347
+ return c.json(__spreadValues({}, err.toJSON()));
7348
+ }
7334
7349
  if (err instanceof StandardError && err.code === StandardErrorCode.UNIQUE_VALUE_EXISTS) {
7335
7350
  c.status(httpStatus8.BAD_REQUEST);
7336
7351
  return c.json(__spreadValues({}, err.toJSON()));
@@ -7357,11 +7372,21 @@ var AdjustEntityController = class {
7357
7372
  constructor(entityService) {
7358
7373
  this.entityService = entityService;
7359
7374
  this.controller = createMiddleware9((c) => __async(this, null, function* () {
7360
- var _a;
7375
+ var _b;
7361
7376
  const accountId = c.req.header("account-id") || "";
7362
7377
  const { entityType, entityId } = c.req.param();
7363
7378
  const body = yield c.req.json();
7364
- for (const [key, value] of Object.entries(body)) {
7379
+ const _a = body, { $condition: condition } = _a, adjustments = __objRest(_a, ["$condition"]);
7380
+ if (condition !== void 0) {
7381
+ if (typeof condition !== "string" || condition.trim().length === 0) {
7382
+ c.status(httpStatus9.BAD_REQUEST);
7383
+ return c.json({
7384
+ code: "API_VALIDATION_ERROR",
7385
+ message: "$condition must be a non-empty string"
7386
+ });
7387
+ }
7388
+ }
7389
+ for (const [key, value] of Object.entries(adjustments)) {
7365
7390
  if (typeof value !== "number") {
7366
7391
  c.status(httpStatus9.BAD_REQUEST);
7367
7392
  return c.json({
@@ -7374,8 +7399,9 @@ var AdjustEntityController = class {
7374
7399
  const entity = yield this.entityService.adjustEntity({
7375
7400
  entityType,
7376
7401
  entityId,
7377
- adjustments: body,
7378
- accountId
7402
+ adjustments,
7403
+ accountId,
7404
+ condition
7379
7405
  });
7380
7406
  c.status(httpStatus9.OK);
7381
7407
  return c.json(entity.toJSON());
@@ -7384,7 +7410,11 @@ var AdjustEntityController = class {
7384
7410
  c.status(httpStatus9.NOT_FOUND);
7385
7411
  return c.json(__spreadValues({}, err.toJSON()));
7386
7412
  }
7387
- if ((err == null ? void 0 : err.name) === "ConditionalCheckFailedException" || ((_a = err == null ? void 0 : err.__type) == null ? void 0 : _a.includes("ConditionalCheckFailed"))) {
7413
+ if (err instanceof StandardError && err.code === StandardErrorCode.INVALID_CONDITION) {
7414
+ c.status(httpStatus9.BAD_REQUEST);
7415
+ return c.json(__spreadValues({}, err.toJSON()));
7416
+ }
7417
+ if ((err == null ? void 0 : err.name) === "ConditionalCheckFailedException" || ((_b = err == null ? void 0 : err.__type) == null ? void 0 : _b.includes("ConditionalCheckFailed"))) {
7388
7418
  c.status(httpStatus9.CONFLICT);
7389
7419
  return c.json({
7390
7420
  code: "ADJUSTMENT_CONSTRAINT_VIOLATED",
@@ -7689,6 +7719,9 @@ var UpdateMutualController = class {
7689
7719
  }
7690
7720
  };
7691
7721
 
7722
+ // services/entity.service.ts
7723
+ import { marshall as marshall6 } from "@aws-sdk/util-dynamodb";
7724
+
7692
7725
  // data/utils/build-condition-expression.ts
7693
7726
  import { marshall as marshall5 } from "@aws-sdk/util-dynamodb";
7694
7727
  function buildConditionExpression(where) {
@@ -7750,7 +7783,62 @@ function buildConditionExpression(where) {
7750
7783
  };
7751
7784
  }
7752
7785
 
7786
+ // services/resolve-condition.ts
7787
+ function resolveAdjustmentCondition(_0) {
7788
+ return __async(this, arguments, function* ({
7789
+ conditionName,
7790
+ conditions,
7791
+ adjustments,
7792
+ getEntityData
7793
+ }) {
7794
+ if (!Object.hasOwn(conditions, conditionName)) {
7795
+ throw new StandardError(
7796
+ StandardErrorCode.INVALID_CONDITION,
7797
+ `Unknown adjustment condition: '${conditionName}'`
7798
+ );
7799
+ }
7800
+ const condition = conditions[conditionName];
7801
+ let resolved;
7802
+ if (typeof condition === "function") {
7803
+ const data = yield getEntityData();
7804
+ resolved = condition(data, adjustments);
7805
+ } else {
7806
+ resolved = condition;
7807
+ }
7808
+ return buildConditionExpression(resolved);
7809
+ });
7810
+ }
7811
+ function resolveUpdateCondition(_0) {
7812
+ return __async(this, arguments, function* ({
7813
+ conditionName,
7814
+ conditions,
7815
+ getEntityData
7816
+ }) {
7817
+ if (!Object.hasOwn(conditions, conditionName)) {
7818
+ throw new StandardError(
7819
+ StandardErrorCode.INVALID_CONDITION,
7820
+ `Unknown update condition: '${conditionName}'`
7821
+ );
7822
+ }
7823
+ const condition = conditions[conditionName];
7824
+ let resolved;
7825
+ if (typeof condition === "function") {
7826
+ const data = yield getEntityData();
7827
+ resolved = condition(data);
7828
+ } else {
7829
+ resolved = condition;
7830
+ }
7831
+ return buildConditionExpression(resolved);
7832
+ });
7833
+ }
7834
+
7753
7835
  // services/entity.service.ts
7836
+ var warnedOnce = /* @__PURE__ */ new Set();
7837
+ var deprecationWarnOnce = (key, message) => {
7838
+ if (warnedOnce.has(key)) return;
7839
+ warnedOnce.add(key);
7840
+ console.warn(message);
7841
+ };
7754
7842
  var EntityService = class {
7755
7843
  constructor(EntityConfig, EmailAuthEnabledEntities, entityRepository, publishEvent2, entityServiceLifeCycle) {
7756
7844
  this.EntityConfig = EntityConfig;
@@ -7805,34 +7893,65 @@ var EntityService = class {
7805
7893
  entityType,
7806
7894
  entityId,
7807
7895
  adjustments,
7808
- accountId
7896
+ accountId,
7897
+ condition
7809
7898
  }) {
7810
- var _a, _b, _c, _d;
7811
- const rawConstraints = (_a = this.EntityConfig[entityType]) == null ? void 0 : _a.adjustmentConstraints;
7812
- let resolvedConstraints = rawConstraints;
7813
- if (rawConstraints) {
7899
+ var _a, _b, _c;
7900
+ const entityConfig = this.EntityConfig[entityType];
7901
+ const adjustmentConditions = entityConfig == null ? void 0 : entityConfig.adjustmentConditions;
7902
+ const rawConstraints = entityConfig == null ? void 0 : entityConfig.adjustmentConstraints;
7903
+ let opts;
7904
+ if (adjustmentConditions) {
7905
+ if (!condition) {
7906
+ throw new StandardError(
7907
+ StandardErrorCode.INVALID_CONDITION,
7908
+ "Entity has adjustmentConditions defined; $condition is required for adjustEntity"
7909
+ );
7910
+ }
7911
+ opts = yield resolveAdjustmentCondition({
7912
+ conditionName: condition,
7913
+ conditions: adjustmentConditions,
7914
+ adjustments,
7915
+ getEntityData: () => __async(this, null, function* () {
7916
+ var _a2;
7917
+ const entity2 = yield this.entityRepository.getEntity(entityType, entityId);
7918
+ return (_a2 = entity2 == null ? void 0 : entity2.data) != null ? _a2 : {};
7919
+ })
7920
+ });
7921
+ } else if (condition) {
7922
+ throw new StandardError(
7923
+ StandardErrorCode.INVALID_CONDITION,
7924
+ `Entity '${entityType}' has no adjustmentConditions defined`
7925
+ );
7926
+ } else if (rawConstraints) {
7927
+ deprecationWarnOnce(
7928
+ "adjustmentConstraints",
7929
+ "[monorise] adjustmentConstraints is deprecated. Use adjustmentConditions instead."
7930
+ );
7931
+ let resolvedConstraints = rawConstraints;
7814
7932
  const hasDynamicFields = Object.values(rawConstraints).some(
7815
7933
  (c) => c.minField || c.maxField
7816
7934
  );
7817
7935
  if (hasDynamicFields) {
7818
7936
  const currentEntity = yield this.entityRepository.getEntity(entityType, entityId);
7819
- const data = (_b = currentEntity == null ? void 0 : currentEntity.data) != null ? _b : {};
7937
+ const data = (_a = currentEntity == null ? void 0 : currentEntity.data) != null ? _a : {};
7820
7938
  resolvedConstraints = {};
7821
7939
  for (const [field, constraint] of Object.entries(rawConstraints)) {
7822
7940
  const resolved = {};
7823
7941
  if (constraint.min !== void 0) resolved.min = constraint.min;
7824
7942
  if (constraint.max !== void 0) resolved.max = constraint.max;
7825
- if (constraint.minField) resolved.min = (_c = data[constraint.minField]) != null ? _c : 0;
7826
- if (constraint.maxField) resolved.max = (_d = data[constraint.maxField]) != null ? _d : Number.MAX_SAFE_INTEGER;
7943
+ if (constraint.minField) resolved.min = (_b = data[constraint.minField]) != null ? _b : 0;
7944
+ if (constraint.maxField) resolved.max = (_c = data[constraint.maxField]) != null ? _c : Number.MAX_SAFE_INTEGER;
7827
7945
  resolvedConstraints[field] = resolved;
7828
7946
  }
7829
7947
  }
7948
+ opts = this.buildLegacyAdjustCondition(adjustments, resolvedConstraints);
7830
7949
  }
7831
7950
  const entity = yield this.entityRepository.adjustEntity(
7832
7951
  entityType,
7833
7952
  entityId,
7834
7953
  adjustments,
7835
- resolvedConstraints
7954
+ opts
7836
7955
  );
7837
7956
  yield this.publishEvent({
7838
7957
  event: EVENT.CORE.ENTITY_UPDATED,
@@ -7851,9 +7970,10 @@ var EntityService = class {
7851
7970
  entityId,
7852
7971
  entityPayload,
7853
7972
  accountId,
7973
+ condition,
7854
7974
  where
7855
7975
  }) {
7856
- var _a, _b;
7976
+ var _a, _b, _c, _d;
7857
7977
  const errorContext = {};
7858
7978
  try {
7859
7979
  const entitySchema = this.EntityConfig[entityType].baseSchema;
@@ -7867,7 +7987,37 @@ var EntityService = class {
7867
7987
  const parsedEntityPayload = entitySchema.parse(entityPayload);
7868
7988
  const parsedMutualPayload = mutualSchema == null ? void 0 : mutualSchema.parse(entityPayload);
7869
7989
  errorContext.parsedMutualPayload = parsedMutualPayload;
7870
- const opts = where && Object.keys(where).length > 0 ? buildConditionExpression(where) : void 0;
7990
+ let opts;
7991
+ if (condition) {
7992
+ const updateConditions = (_b = this.EntityConfig[entityType]) == null ? void 0 : _b.updateConditions;
7993
+ if (!updateConditions) {
7994
+ throw new StandardError(
7995
+ StandardErrorCode.INVALID_CONDITION,
7996
+ `Entity '${entityType}' has no updateConditions defined`
7997
+ );
7998
+ }
7999
+ opts = yield resolveUpdateCondition({
8000
+ conditionName: condition,
8001
+ conditions: updateConditions,
8002
+ getEntityData: () => __async(this, null, function* () {
8003
+ var _a2;
8004
+ const entity2 = yield this.entityRepository.getEntity(entityType, entityId);
8005
+ return (_a2 = entity2 == null ? void 0 : entity2.data) != null ? _a2 : {};
8006
+ })
8007
+ });
8008
+ } else if (where && Object.keys(where).length > 0) {
8009
+ if (!((_c = this.EntityConfig[entityType]) == null ? void 0 : _c.allowLegacyWhere)) {
8010
+ throw new StandardError(
8011
+ StandardErrorCode.INVALID_CONDITION,
8012
+ `Entity '${entityType}' has legacy $where disabled by default. Use named conditions via $condition, or set allowLegacyWhere: true to opt in (not recommended \u2014 re-exposes condition operators to clients).`
8013
+ );
8014
+ }
8015
+ deprecationWarnOnce(
8016
+ "where",
8017
+ "[monorise] $where is deprecated. Use named conditions via $condition instead."
8018
+ );
8019
+ opts = buildConditionExpression(where);
8020
+ }
7871
8021
  const entity = yield this.entityRepository.updateEntity(
7872
8022
  entityType,
7873
8023
  entityId,
@@ -7880,7 +8030,7 @@ var EntityService = class {
7880
8030
  const byEntityId = entityId;
7881
8031
  const publishEventPromises = [];
7882
8032
  for (const [fieldKey, config] of Object.entries(
7883
- ((_b = this.EntityConfig[entityType].mutual) == null ? void 0 : _b.mutualFields) || {}
8033
+ ((_d = this.EntityConfig[entityType].mutual) == null ? void 0 : _d.mutualFields) || {}
7884
8034
  )) {
7885
8035
  const toMutualIds = config.toMutualIds;
7886
8036
  const mutualPayload = parsedMutualPayload[fieldKey];
@@ -7936,6 +8086,35 @@ var EntityService = class {
7936
8086
  });
7937
8087
  });
7938
8088
  }
8089
+ /** @deprecated Converts legacy adjustmentConstraints to condition expression opts. */
8090
+ buildLegacyAdjustCondition(adjustments, constraints) {
8091
+ const conditionParts = [];
8092
+ const names = { "#data": "data" };
8093
+ const values = {};
8094
+ for (const [field, constraint] of Object.entries(constraints)) {
8095
+ const delta = adjustments[field];
8096
+ if (delta === void 0) continue;
8097
+ const namePlaceholder = `#where_${field}`;
8098
+ names[namePlaceholder] = field;
8099
+ const fieldRef = `#data.${namePlaceholder}`;
8100
+ if (constraint.min !== void 0 && delta < 0) {
8101
+ const valKey = `:where_${field}_min_threshold`;
8102
+ conditionParts.push(`${fieldRef} >= ${valKey}`);
8103
+ values[valKey] = constraint.min - delta;
8104
+ }
8105
+ if (constraint.max !== void 0 && delta > 0) {
8106
+ const valKey = `:where_${field}_max_threshold`;
8107
+ conditionParts.push(`${fieldRef} <= ${valKey}`);
8108
+ values[valKey] = constraint.max - delta;
8109
+ }
8110
+ }
8111
+ if (conditionParts.length === 0) return void 0;
8112
+ return {
8113
+ ConditionExpression: conditionParts.join(" AND "),
8114
+ ExpressionAttributeNames: names,
8115
+ ExpressionAttributeValues: marshall6(values)
8116
+ };
8117
+ }
7939
8118
  };
7940
8119
 
7941
8120
  // services/mutual.service.ts