Skip to content

Commit

Permalink
fix entity/mongo edge case update failures
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Dec 5, 2024
1 parent 32c80f6 commit 91b7788
Show file tree
Hide file tree
Showing 22 changed files with 450 additions and 283 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powership",
"version": "5.7.12",
"version": "5.7.14",
"private": true,
"scripts": {
"patch": "run-s patch:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/accounts",
"version": "5.7.12",
"version": "5.7.14",
"description": "Powership accounts",
"type": "module",
"main": "./out/index.cjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugins/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/babel-plugins",
"version": "5.7.12",
"version": "5.7.14",
"main": "./out/index.js",
"sideEffects": false,
"typings": "./out/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/boilerplate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/boilerplate",
"version": "5.7.12",
"version": "5.7.14",
"author": "antoniopresto <[email protected]>",
"sideEffects": false,
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/entity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/entity",
"version": "5.7.12",
"version": "5.7.14",
"type": "module",
"main": "./out/index.cjs",
"module": "./out/index.mjs",
Expand Down
43 changes: 38 additions & 5 deletions packages/entity/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,49 @@ function _registerPKSKHook(input: {
}

if (ctx.isUpdate) {
ctx.options.update.$set = await _onUpdate({
...ctx.options.update.$set,
});

if (ctx.isUpsert) {
ctx.options.update.$setOnInsert = await _onCreate({
const processedDoc = await _onCreate({
...ctx.options.update.$setIfNull,
...ctx.options.update.$setOnInsert,
...ctx.options.update.$set,
});

const originalSetFields = new Set(
Object.keys(ctx.options.update.$set || {}).map(
(el) => el.split('.')[0]
)
);

const setFields = {};
const setOnInsertFields = {};

for (const [key, value] of Object.entries(processedDoc)) {
if (originalSetFields.has(key)) {
setFields[key] = value;
} else {
setOnInsertFields[key] = value;
}
}

ctx.options.update.$set = setFields;
ctx.options.update.$setOnInsert = setOnInsertFields;
delete ctx.options.update.$setIfNull;

const conflictingFields = Object.keys(setFields).filter(
(key) => key in setOnInsertFields
);

if (conflictingFields.length > 0) {
throw new Error(
`Found conflicting fields in update operators: ${conflictingFields.join(
', '
)}`
);
}
} else {
if (ctx.options.update.$setIfNull) {
ctx.options.update.$setIfNull = preparseFilterGraphTypeFields(
Expand All @@ -876,10 +913,6 @@ function _registerPKSKHook(input: {
);
}
}

ctx.options.update.$set = await _onUpdate({
...ctx.options.update.$set,
});
// ======= close update handling ====
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createType, resetTypesCache } from '@powership/schema';
import { createType, Infer, resetTypesCache } from '@powership/schema';
import { ulid } from '@powership/utils';
import { assert, IsExact } from 'conditional-type-checks';

Expand Down Expand Up @@ -102,6 +102,8 @@ describe('Entity.indexRelations', () => {
context: {},
});

expect(created.error).toBeUndefined();

expect(created.item).toEqual(
expect.objectContaining({
_idPK: 'payment⋮_id⋮+5511900000000⋮',
Expand Down
4 changes: 2 additions & 2 deletions packages/logstorm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logstorm",
"version": "5.7.12",
"version": "5.7.14",
"typings": "out",
"author": "antoniopresto <[email protected]>",
"type": "module",
Expand Down Expand Up @@ -60,7 +60,7 @@
"eslint-plugin-sort-keys-fix": "1.1.2",
"eslint-plugin-typescript-sort-keys": "3.2.0",
"jest": "29.7.0",
"nodemon": "2.0.20",
"nodemon": "3.1.7",
"npm-run-all": "4.1.5",
"prettier-plugin-multiline-arrays": "3.0.4",
"rimraf": "6.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/mongo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/mongo",
"version": "5.7.12",
"version": "5.7.14",
"type": "module",
"main": "./out/index.cjs",
"module": "./out/index.mjs",
Expand Down
86 changes: 86 additions & 0 deletions packages/mongo/src/__tests__/parseMongoUpdateExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,92 @@ describe('parseMongoUpdateExpression', () => {
).toHaveProperty('list', ['a', 'b', 'a', 2]);
});

test('TTLCache scenario with $addToSet and $setOnInsert', async () => {
const ONE_HOUR_MS = 3600000;
const key = 'testKey';
const key2 = 'testKey2';
const attempt = 'testAttempt';

const result = await mockApp.transporter.updateOne({
filter: { key, key2 },
upsert: true,
update: {
$addToSet: {
value: attempt,
},
$setOnInsert: {
key,
key2,
},
$set: {
createdAt: new Date(),
ttl: ONE_HOUR_MS,
},
},
indexConfig: {
entity: 'cache',
indexes: [{ name: 'key_key2', PK: ['.key', '.key2'] }],
},
context: {},
});

expect(result.error).toBeUndefined();
expect(result.item).toMatchObject({
key,
key2,
value: [attempt],
ttl: ONE_HOUR_MS,
});
});

test('TTLCache scenario with $set and $setOnInsert', async () => {
const ONE_HOUR_MS = 3600000;
const key = 'testKey';
const key2 = 'testKey2';
const attempt = 'attempt1';

await mockApp.transporter.createOne({
item: {
key,
key2,
value: [attempt],
},
indexConfig: {
entity: 'cache',
indexes: [{ name: 'key_key2', PK: ['.key', '.key2'] }],
},
context: {},
});

const attempt2 = 'attempt2';
const result = await mockApp.transporter.updateOne({
filter: { key, key2 },
upsert: true,
update: {
$addToSet: {
value: attempt2,
},
$set: {
createdAt: new Date(),
ttl: ONE_HOUR_MS,
},
},
indexConfig: {
entity: 'cache',
indexes: [{ name: 'key_key2', PK: ['.key', '.key2'] }],
},
context: {},
});

expect(result.error).toBeUndefined();
expect(result.item).toMatchObject({
key,
key2,
ttl: ONE_HOUR_MS,
value: [attempt, attempt2],
});
});

describe('deepObjects', () => {
test('$set', async () => {
await create();
Expand Down
4 changes: 1 addition & 3 deletions packages/mongo/src/parseMongoUpdateExpression.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { parseAggioUpdateExpression } from '@powership/transporter';

export const parseMongoUpdateExpression = parseAggioUpdateExpression;
export { parseMongoUpdateExpression } from '@powership/transporter';
2 changes: 1 addition & 1 deletion packages/plugin-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plugin-engine",
"version": "5.7.12",
"version": "5.7.14",
"type": "module",
"main": "./out/index.cjs",
"module": "./out/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/powership/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powership",
"version": "5.7.12",
"version": "5.7.14",
"author": "antoniopresto <[email protected]>",
"type": "module",
"main": "./out/index.cjs",
Expand Down
4 changes: 2 additions & 2 deletions packages/runmate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "runmate",
"version": "5.7.12",
"version": "5.7.14",
"typings": "out",
"author": "antoniopresto <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -74,7 +74,7 @@
"eslint-plugin-sort-keys-fix": "1.1.2",
"eslint-plugin-typescript-sort-keys": "3.2.0",
"jest": "29.7.0",
"nodemon": "2.0.20",
"nodemon": "3.1.7",
"npm-run-all": "4.1.5",
"prettier": "2.8.8",
"prettier-plugin-multiline-arrays": "3.0.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/schema",
"version": "5.7.12",
"version": "5.7.14",
"type": "module",
"main": "./out/index.cjs",
"module": "./out/index.mjs",
Expand Down Expand Up @@ -90,7 +90,7 @@
"graphql": "16.9.0",
"jest": "29.7.0",
"madge": "8.0.0",
"nodemon": "2.0.20",
"nodemon": "3.1.7",
"npm-run-all": "4.1.5",
"prettier": "2.8.8",
"prettier-plugin-multiline-arrays": "3.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/server",
"version": "5.7.12",
"version": "5.7.14",
"type": "module",
"main": "./out/index.cjs",
"module": "./out/index.mjs",
Expand Down
9 changes: 5 additions & 4 deletions packages/transporter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/transporter",
"version": "5.7.12",
"version": "5.7.14",
"type": "module",
"main": "./out/index.cjs",
"module": "./out/index.mjs",
Expand All @@ -24,6 +24,7 @@
"build": "run-p declarations build:*",
"clear": "rimraf out",
"fix": "run-s fix:* ",
"watch": "nodemon --watch 'src/**/*' -e ts,tsx --exec 'pnpm run build'",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"fix:lint": "eslint src --ext .ts --fix",
"prepublishOnly": "run-s build fix && run-p test",
Expand Down Expand Up @@ -56,12 +57,12 @@
"sift": "17.0.1"
},
"devDependencies": {
"@powership/boilerplate": "workspace:*",
"@babel/cli": "7.24.8",
"@babel/plugin-transform-typescript": "7.24.8",
"@babel/preset-env": "7.24.8",
"@babel/preset-typescript": "7.24.7",
"@powership/babel-plugins": "workspace:*",
"@powership/boilerplate": "workspace:*",
"@types/jest": "29.5.12",
"@types/node": "20.11.25",
"@typescript-eslint/eslint-plugin": "7.17.0",
Expand All @@ -79,6 +80,7 @@
"mongodb": "6.4.0",
"mongodb-memory-server": "10.1.2",
"mongodb-memory-server-core": "10.1.2",
"nodemon": "3.1.7",
"npm-run-all": "4.1.5",
"prettier": "2.8.8",
"prettier-plugin-multiline-arrays": "3.0.4",
Expand All @@ -97,6 +99,5 @@
"readmeFile": "./README.md",
"displayName": "Transporter - Base to connect any Database to Powership.",
"tsconfig": "./tsconfig.module.json"
},
"peerDependencies": {}
}
}
2 changes: 1 addition & 1 deletion packages/transporter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ export * from './createDocumentIndexBasedFilters';
export * from './encodeIndexValue';
export * from './parseEntityIndexFields';
export * from './parseAggioAttributeFilters';
export * from './parseAggioUpdateExpression';
export * from './parseMongoUpdateExpression';
Loading

0 comments on commit 91b7788

Please sign in to comment.