@atscript/db-mongo 0.1.40 → 0.1.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="https://atscript.dev/logo.svg" alt="Atscript" width="120" />
2
+ <img src="https://db.atscript.dev/logo.svg" alt="Atscript" width="120" />
3
3
  </p>
4
4
 
5
5
  <h1 align="center">@atscript/db-mongo</h1>
package/dist/index.cjs CHANGED
@@ -51,8 +51,8 @@ var CollectionPatcher = class {
51
51
  preparePatch() {
52
52
  this.filterObj = { _id: this.collection.prepareId(this.payload._id) };
53
53
  this.flattenPayload(this.payload);
54
- if (this.ops?.inc) for (const key in this.ops.inc) this._set(key, { $add: [`$${key}`, this.ops.inc[key]] });
55
- if (this.ops?.mul) for (const key in this.ops.mul) this._set(key, { $multiply: [`$${key}`, this.ops.mul[key]] });
54
+ if (this.ops?.inc) for (const key in this.ops.inc) this._set(key, this._fieldOpExpr(key, "inc", this.ops.inc[key]));
55
+ if (this.ops?.mul) for (const key in this.ops.mul) this._set(key, this._fieldOpExpr(key, "mul", this.ops.mul[key]));
56
56
  const updateFilter = this.updatePipeline;
57
57
  return {
58
58
  toArgs: () => [
@@ -65,6 +65,10 @@ var CollectionPatcher = class {
65
65
  updateOptions: this.optionsObj
66
66
  };
67
67
  }
68
+ /** Builds a MongoDB aggregation expression for an $inc or $mul field op. */
69
+ _fieldOpExpr(key, op, value) {
70
+ return op === "inc" ? { $add: [`$${key}`, value] } : { $multiply: [`$${key}`, value] };
71
+ }
68
72
  /**
69
73
  * Helper – lazily create `$set` section and assign *key* → *value*.
70
74
  *
@@ -96,7 +100,11 @@ var CollectionPatcher = class {
96
100
  const topLevelArray = flatType?.metadata?.get("db.__topLevelArray");
97
101
  if (typeof value === "object" && !Array.isArray(value) && topLevelArray && !flatType?.metadata?.has("db.json")) this.parseArrayPatch(key, value, flatType);
98
102
  else if (typeof value === "object" && flatType?.metadata?.get("db.patch.strategy") === "merge") this.flattenPayload(value, key);
99
- else if (key !== "_id") this._set(key, value);
103
+ else if (key !== "_id") {
104
+ const fieldOp = (0, _atscript_db.getDbFieldOp)(value);
105
+ if (fieldOp) this._set(key, this._fieldOpExpr(key, fieldOp.op, fieldOp.value));
106
+ else this._set(key, value);
107
+ }
100
108
  }
101
109
  return this.updatePipeline;
102
110
  }
package/dist/index.d.cts CHANGED
@@ -62,6 +62,8 @@ declare class CollectionPatcher {
62
62
  updateFilter: Document[];
63
63
  updateOptions: UpdateOptions;
64
64
  };
65
+ /** Builds a MongoDB aggregation expression for an $inc or $mul field op. */
66
+ private _fieldOpExpr;
65
67
  /**
66
68
  * Helper – lazily create `$set` section and assign *key* → *value*.
67
69
  *
package/dist/index.d.mts CHANGED
@@ -62,6 +62,8 @@ declare class CollectionPatcher {
62
62
  updateFilter: Document[];
63
63
  updateOptions: UpdateOptions;
64
64
  };
65
+ /** Builds a MongoDB aggregation expression for an $inc or $mul field op. */
66
+ private _fieldOpExpr;
65
67
  /**
66
68
  * Helper – lazily create `$set` section and assign *key* → *value*.
67
69
  *
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as buildMongoFilter } from "./mongo-filter-CcQO-sEh.mjs";
2
- import { AtscriptDbView, BaseDbAdapter, DbError, DbSpace, computeInsights, getKeyProps } from "@atscript/db";
2
+ import { AtscriptDbView, BaseDbAdapter, DbError, DbSpace, computeInsights, getDbFieldOp, getKeyProps } from "@atscript/db";
3
3
  import { MongoClient, MongoServerError, ObjectId } from "mongodb";
4
4
  //#region src/lib/collection-patcher.ts
5
5
  /**
@@ -50,8 +50,8 @@ var CollectionPatcher = class {
50
50
  preparePatch() {
51
51
  this.filterObj = { _id: this.collection.prepareId(this.payload._id) };
52
52
  this.flattenPayload(this.payload);
53
- if (this.ops?.inc) for (const key in this.ops.inc) this._set(key, { $add: [`$${key}`, this.ops.inc[key]] });
54
- if (this.ops?.mul) for (const key in this.ops.mul) this._set(key, { $multiply: [`$${key}`, this.ops.mul[key]] });
53
+ if (this.ops?.inc) for (const key in this.ops.inc) this._set(key, this._fieldOpExpr(key, "inc", this.ops.inc[key]));
54
+ if (this.ops?.mul) for (const key in this.ops.mul) this._set(key, this._fieldOpExpr(key, "mul", this.ops.mul[key]));
55
55
  const updateFilter = this.updatePipeline;
56
56
  return {
57
57
  toArgs: () => [
@@ -64,6 +64,10 @@ var CollectionPatcher = class {
64
64
  updateOptions: this.optionsObj
65
65
  };
66
66
  }
67
+ /** Builds a MongoDB aggregation expression for an $inc or $mul field op. */
68
+ _fieldOpExpr(key, op, value) {
69
+ return op === "inc" ? { $add: [`$${key}`, value] } : { $multiply: [`$${key}`, value] };
70
+ }
67
71
  /**
68
72
  * Helper – lazily create `$set` section and assign *key* → *value*.
69
73
  *
@@ -95,7 +99,11 @@ var CollectionPatcher = class {
95
99
  const topLevelArray = flatType?.metadata?.get("db.__topLevelArray");
96
100
  if (typeof value === "object" && !Array.isArray(value) && topLevelArray && !flatType?.metadata?.has("db.json")) this.parseArrayPatch(key, value, flatType);
97
101
  else if (typeof value === "object" && flatType?.metadata?.get("db.patch.strategy") === "merge") this.flattenPayload(value, key);
98
- else if (key !== "_id") this._set(key, value);
102
+ else if (key !== "_id") {
103
+ const fieldOp = getDbFieldOp(value);
104
+ if (fieldOp) this._set(key, this._fieldOpExpr(key, fieldOp.op, fieldOp.value));
105
+ else this._set(key, value);
106
+ }
99
107
  }
100
108
  return this.updatePipeline;
101
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-mongo",
3
- "version": "0.1.40",
3
+ "version": "0.1.41",
4
4
  "description": "Mongodb plugin for atscript.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -60,7 +60,7 @@
60
60
  "@atscript/core": "^0.1.39",
61
61
  "@atscript/typescript": "^0.1.39",
62
62
  "mongodb": "^6.17.0",
63
- "@atscript/db": "^0.1.40"
63
+ "@atscript/db": "^0.1.41"
64
64
  },
65
65
  "scripts": {
66
66
  "postinstall": "asc -f dts",