-
Notifications
You must be signed in to change notification settings - Fork 107
feat: Add datastore mode data transforms #1369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b7f4a1c
73b8e15
1179e4e
35ee5a4
f181a4c
66e0668
7abda1f
4ae772f
c5c4484
a1e8e69
ef41b73
ceb3fc4
81f1898
c4ac639
a09119b
409f6a6
6f65b43
55b1913
2478abc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1452,8 +1452,29 @@ | |
excludeFromIndexes?: boolean; | ||
} | ||
|
||
/* | ||
* This is the interface the user would provide transform operations in before | ||
* they are converted to the google.datastore.v1.IPropertyTransform | ||
* interface. | ||
* | ||
*/ | ||
export type PropertyTransform = { | ||
property: string; | ||
setToServerValue: boolean; | ||
increment: any; | ||
maximum: any; | ||
minimum: any; | ||
appendMissingElements: any[]; | ||
removeAllFromArray: any[]; | ||
}; | ||
|
||
interface EntityWithTransforms { | ||
transforms?: PropertyTransform[]; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Entity = any; | ||
// TODO: Call out this interface change | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this TODO here for? |
||
export type Entity = any & EntityWithTransforms; | ||
export type Entities = Entity | Entity[]; | ||
|
||
interface KeyProtoPathElement extends google.datastore.v1.Key.IPathElement { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {entity, PropertyTransform} from '../../entity'; | ||
import {google} from '../../../protos/protos'; | ||
import IValue = google.datastore.v1.IValue; | ||
import ServerValue = google.datastore.v1.PropertyTransform.ServerValue; | ||
|
||
export function buildPropertyTransforms(transforms: PropertyTransform[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this would benefit from some comments |
||
const propertyTransforms: google.datastore.v1.IPropertyTransform[] = []; | ||
transforms.forEach((transform: PropertyTransform) => { | ||
const property = transform.property; | ||
if (transform.setToServerValue) { | ||
propertyTransforms.push({ | ||
property, | ||
setToServerValue: ServerValue.REQUEST_TIME, | ||
}); | ||
} | ||
['increment', 'maximum', 'minimum'].forEach(type => { | ||
const castedType = type as 'increment' | 'maximum' | 'minimum'; | ||
if (transform[castedType]) { | ||
propertyTransforms.push({ | ||
property, | ||
[castedType]: entity.encodeValue( | ||
transform[castedType], | ||
property | ||
) as IValue, | ||
}); | ||
} | ||
}); | ||
['appendMissingElements', 'removeAllFromArray'].forEach(type => { | ||
const castedType = type as 'appendMissingElements' | 'removeAllFromArray'; | ||
if (transform[castedType]) { | ||
propertyTransforms.push({ | ||
property, | ||
[castedType]: { | ||
values: transform[castedType].map(element => { | ||
return entity.encodeValue(element, property) as IValue; | ||
}), | ||
}, | ||
}); | ||
} | ||
}); | ||
}); | ||
return propertyTransforms; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import {Entities, entity, Entity} from '../src/entity'; | |
import {Query, RunQueryInfo, ExecutionStats} from '../src/query'; | ||
import KEY_SYMBOL = entity.KEY_SYMBOL; | ||
import {transactionExpiredError} from '../src/request'; | ||
const sinon = require('sinon'); | ||
|
||
const async = require('async'); | ||
|
||
|
@@ -3295,6 +3296,221 @@ async.each( | |
assert.strictEqual(entity, undefined); | ||
}); | ||
}); | ||
describe('Datastore mode data transforms', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this single test really cover the whole feature? (I dont see anything with |
||
it('should perform a basic data transform', async () => { | ||
const key = datastore.key(['Post', 'post1']); | ||
const requestSpy = sinon.spy(datastore.request_); | ||
datastore.request_ = requestSpy; | ||
const result = await datastore.save({ | ||
key: key, | ||
data: { | ||
name: 'test', | ||
p1: 3, | ||
p2: 4, | ||
p3: 5, | ||
a1: [3, 4, 5], | ||
}, | ||
transforms: [ | ||
{ | ||
property: 'p1', | ||
setToServerValue: true, | ||
}, | ||
{ | ||
property: 'p2', | ||
increment: 4, | ||
}, | ||
{ | ||
property: 'p3', | ||
maximum: 9, | ||
}, | ||
{ | ||
property: 'p2', | ||
minimum: 6, | ||
}, | ||
{ | ||
property: 'a1', | ||
appendMissingElements: [5, 6], | ||
}, | ||
{ | ||
property: 'a1', | ||
removeAllFromArray: [3], | ||
}, | ||
], | ||
}); | ||
// Clean the data from the server first before comparing: | ||
result.forEach(serverResult => { | ||
delete serverResult['indexUpdates']; | ||
serverResult.mutationResults?.forEach(mutationResult => { | ||
delete mutationResult['updateTime']; | ||
delete mutationResult['createTime']; | ||
delete mutationResult['version']; | ||
mutationResult.transformResults?.forEach(transformResult => { | ||
delete transformResult['timestampValue']; | ||
}); | ||
}); | ||
}); | ||
// Now the data should have fixed values. | ||
// Do a comparison against the expected result. | ||
assert.deepStrictEqual(result, [ | ||
{ | ||
mutationResults: [ | ||
{ | ||
transformResults: [ | ||
{ | ||
meaning: 0, | ||
excludeFromIndexes: false, | ||
valueType: 'timestampValue', | ||
}, | ||
{ | ||
meaning: 0, | ||
excludeFromIndexes: false, | ||
integerValue: '8', | ||
valueType: 'integerValue', | ||
}, | ||
{ | ||
meaning: 0, | ||
excludeFromIndexes: false, | ||
integerValue: '9', | ||
valueType: 'integerValue', | ||
}, | ||
{ | ||
meaning: 0, | ||
excludeFromIndexes: false, | ||
integerValue: '6', | ||
valueType: 'integerValue', | ||
}, | ||
{ | ||
meaning: 0, | ||
excludeFromIndexes: false, | ||
nullValue: 'NULL_VALUE', | ||
valueType: 'nullValue', | ||
}, | ||
{ | ||
meaning: 0, | ||
excludeFromIndexes: false, | ||
nullValue: 'NULL_VALUE', | ||
valueType: 'nullValue', | ||
}, | ||
], | ||
key: null, | ||
conflictDetected: false, | ||
}, | ||
], | ||
commitTime: null, | ||
}, | ||
]); | ||
// Now check the value that was actually saved to the server: | ||
const [entity] = await datastore.get(key); | ||
const parsedResult = JSON.parse(JSON.stringify(entity)); | ||
delete parsedResult['p1']; // This is a timestamp so we can't consistently test this. | ||
assert.deepStrictEqual(parsedResult, { | ||
name: 'test', | ||
a1: [4, 5, 6], | ||
p2: 6, | ||
p3: 9, | ||
}); | ||
delete requestSpy.args[0][0].reqOpts.mutations[0].upsert.key | ||
.partitionId['namespaceId']; | ||
assert.deepStrictEqual(requestSpy.args[0][0], { | ||
client: 'DatastoreClient', | ||
method: 'commit', | ||
reqOpts: { | ||
mutations: [ | ||
{ | ||
upsert: { | ||
key: { | ||
path: [ | ||
{ | ||
kind: 'Post', | ||
name: 'post1', | ||
}, | ||
], | ||
partitionId: {}, | ||
}, | ||
properties: { | ||
name: { | ||
stringValue: 'test', | ||
}, | ||
p1: { | ||
integerValue: '3', | ||
}, | ||
p2: { | ||
integerValue: '4', | ||
}, | ||
p3: { | ||
integerValue: '5', | ||
}, | ||
a1: { | ||
arrayValue: { | ||
values: [ | ||
{ | ||
integerValue: '3', | ||
}, | ||
{ | ||
integerValue: '4', | ||
}, | ||
{ | ||
integerValue: '5', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
propertyTransforms: [ | ||
{ | ||
property: 'p1', | ||
setToServerValue: 1, | ||
}, | ||
{ | ||
property: 'p2', | ||
increment: { | ||
integerValue: '4', | ||
}, | ||
}, | ||
{ | ||
property: 'p3', | ||
maximum: { | ||
integerValue: '9', | ||
}, | ||
}, | ||
{ | ||
property: 'p2', | ||
minimum: { | ||
integerValue: '6', | ||
}, | ||
}, | ||
{ | ||
property: 'a1', | ||
appendMissingElements: { | ||
values: [ | ||
{ | ||
integerValue: '5', | ||
}, | ||
{ | ||
integerValue: '6', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
property: 'a1', | ||
removeAllFromArray: { | ||
values: [ | ||
{ | ||
integerValue: '3', | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
gaxOpts: {}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do these have to be any?