@atscript/db-mongo 0.1.40 → 0.1.42

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
  }
@@ -1301,7 +1309,8 @@ var MongoAdapter = class MongoAdapter extends _atscript_db.BaseDbAdapter {
1301
1309
  removePrimaryKeys: meta.originalMetaIdFields.filter((f) => f !== "_id"),
1302
1310
  addUniqueFields: uniqueFields.length > 0 ? uniqueFields : void 0
1303
1311
  };
1304
- uniqueFields.push("_id");
1312
+ const effectiveUnique = uniqueFields.filter((f) => meta.primaryKeys.length <= 1 || !meta.primaryKeys.includes(f));
1313
+ effectiveUnique.push("_id");
1305
1314
  return {
1306
1315
  injectFields: [{
1307
1316
  path: "_id",
@@ -1315,7 +1324,7 @@ var MongoAdapter = class MongoAdapter extends _atscript_db.BaseDbAdapter {
1315
1324
  metadata: /* @__PURE__ */ new Map()
1316
1325
  }
1317
1326
  }],
1318
- addUniqueFields: uniqueFields
1327
+ addUniqueFields: effectiveUnique
1319
1328
  };
1320
1329
  }
1321
1330
  onAfterFlatten() {
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
  }
@@ -1300,7 +1308,8 @@ var MongoAdapter = class MongoAdapter extends BaseDbAdapter {
1300
1308
  removePrimaryKeys: meta.originalMetaIdFields.filter((f) => f !== "_id"),
1301
1309
  addUniqueFields: uniqueFields.length > 0 ? uniqueFields : void 0
1302
1310
  };
1303
- uniqueFields.push("_id");
1311
+ const effectiveUnique = uniqueFields.filter((f) => meta.primaryKeys.length <= 1 || !meta.primaryKeys.includes(f));
1312
+ effectiveUnique.push("_id");
1304
1313
  return {
1305
1314
  injectFields: [{
1306
1315
  path: "_id",
@@ -1314,7 +1323,7 @@ var MongoAdapter = class MongoAdapter extends BaseDbAdapter {
1314
1323
  metadata: /* @__PURE__ */ new Map()
1315
1324
  }
1316
1325
  }],
1317
- addUniqueFields: uniqueFields
1326
+ addUniqueFields: effectiveUnique
1318
1327
  };
1319
1328
  }
1320
1329
  onAfterFlatten() {
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.42",
4
4
  "description": "Mongodb plugin for atscript.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -51,16 +51,16 @@
51
51
  "access": "public"
52
52
  },
53
53
  "devDependencies": {
54
- "@atscript/core": "^0.1.39",
55
- "@atscript/typescript": "^0.1.39",
54
+ "@atscript/core": "^0.1.41",
55
+ "@atscript/typescript": "^0.1.41",
56
56
  "mongodb": "^6.17.0",
57
- "unplugin-atscript": "^0.1.39"
57
+ "unplugin-atscript": "^0.1.41"
58
58
  },
59
59
  "peerDependencies": {
60
- "@atscript/core": "^0.1.39",
61
- "@atscript/typescript": "^0.1.39",
60
+ "@atscript/core": "^0.1.41",
61
+ "@atscript/typescript": "^0.1.41",
62
62
  "mongodb": "^6.17.0",
63
- "@atscript/db": "^0.1.40"
63
+ "@atscript/db": "^0.1.42"
64
64
  },
65
65
  "scripts": {
66
66
  "postinstall": "asc -f dts",