Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b337a5c
Add some test cases for findLargeProperties_
danieljbruce Aug 14, 2024
59889b5
Add a test for arrays and skip it.
danieljbruce Aug 14, 2024
3ca8d1c
Add a test for the complex case wrapped in an arra
danieljbruce Aug 14, 2024
d2c0138
Add a test case for an array without name/value.
danieljbruce Aug 14, 2024
3307e7e
Add another test for name/value pair
danieljbruce Aug 15, 2024
03bd79e
Change the test case involving a name/value pair
danieljbruce Aug 16, 2024
63427a6
Add two simple test cases for excludeFromIndexes
danieljbruce Aug 16, 2024
7c8b7be
Merge branch 'main' of https://github.com/googleapis/nodejs-datastore…
danieljbruce Oct 22, 2024
111a968
Add another unit test for findLargeProperties_
danieljbruce Oct 23, 2024
2587728
Remove if block that stops recursion
danieljbruce Oct 23, 2024
bc963cc
Add a test for save with a `name/value` pair
danieljbruce Oct 23, 2024
ce23c71
Add some test code that ensures name/value encoded
danieljbruce Oct 23, 2024
c96cf51
Add header
danieljbruce Oct 23, 2024
3aef723
Remove only
danieljbruce Oct 23, 2024
83cfff5
Add a test that saves a long string value property
danieljbruce Oct 23, 2024
b1a4dcf
Add a unit test checking longString for value
danieljbruce Oct 23, 2024
891bc65
Remove only
danieljbruce Oct 23, 2024
459a3e0
TODO is done
danieljbruce Oct 29, 2024
9bef8e4
Refactor the save function
danieljbruce Oct 30, 2024
c8de349
Add TODO
danieljbruce Oct 30, 2024
52034a8
Merge branch '1242-write-tests-for-findLargeProperties-revisit' of ht…
danieljbruce Oct 31, 2024
7bc65dd
Write unit tests for the new excludeFromIndexes fn
danieljbruce Oct 31, 2024
feab4b8
Add the header
danieljbruce Oct 31, 2024
35f6831
Refactor the code for building the entity proto
danieljbruce Oct 31, 2024
7eb39e5
Rename it buildEntityProto
danieljbruce Oct 31, 2024
e5b4011
The tests need another mock due to the structure
danieljbruce Oct 31, 2024
516a4a7
First two tests for buildEntityProto
danieljbruce Oct 31, 2024
2a64930
Reintroduce extendExcludeFromIndexes.ts tests
danieljbruce Oct 31, 2024
b9e2cb1
Adjust the test to match expected proto
danieljbruce Oct 31, 2024
ff38fd0
Add another test for an entity in an array
danieljbruce Oct 31, 2024
e00ee11
Add headers
danieljbruce Oct 31, 2024
360e382
Add another header
danieljbruce Oct 31, 2024
c8c0a2a
Merge branch 'main' of https://github.com/googleapis/nodejs-datastore…
danieljbruce Nov 5, 2024
a4c5508
Merge branch 'exclude-from-indexes-cleanup' of
danieljbruce Nov 5, 2024
ffa8956
Eliminate space. Simplify diff
danieljbruce Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ import {google} from '../protos/protos';
import {AggregateQuery} from './aggregate';
import {SaveEntity} from './interfaces/save';
import {extendExcludeFromIndexes} from './utils/entity/extendExcludeFromIndexes';
import {buildEntityProto} from './utils/entity/buildEntityProto';

const {grpc} = new GrpcClient();
const addExcludeFromIndexes = entity.addExcludeFromIndexes;

export type PathType = string | number | entity.Int;
export interface BooleanObject {
Expand Down Expand Up @@ -1099,7 +1099,6 @@ class Datastore extends DatastoreRequest {
.map(DatastoreRequest.prepareEntityObject_)
.forEach((entityObject: Entity, index: number) => {
const mutation: Mutation = {};
let entityProto: EntityProto = {};
let method = 'upsert';

if (entityObject.method) {
Expand All @@ -1117,44 +1116,7 @@ class Datastore extends DatastoreRequest {
}

extendExcludeFromIndexes(entityObject);
if (Array.isArray(entityObject.data)) {
// This code builds the right entityProto from the entityObject
entityProto.properties = entityObject.data.reduce(
(
acc: EntityProtoReduceAccumulator,
data: EntityProtoReduceData
) => {
const value = entity.encodeValue(
data.value,
data.name.toString()
);

if (typeof data.excludeFromIndexes === 'boolean') {
const excluded = data.excludeFromIndexes;
let values = value.arrayValue && value.arrayValue.values;

if (values) {
values = values.map((x: ValueProto) => {
x.excludeFromIndexes = excluded;
return x;
});
} else {
value.excludeFromIndexes = data.excludeFromIndexes;
}
}

acc[data.name] = value;

return acc;
},
{}
);
// This code adds excludeFromIndexes in the right places
addExcludeFromIndexes(entityObject.excludeFromIndexes, entityProto);
} else {
// This code builds the right entityProto from the entityObject
entityProto = entity.entityToEntityProto(entityObject);
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire code block has been pulled into the buildEntityProto function.

const entityProto = buildEntityProto(entityObject);

entityProto.key = entity.keyToKeyProto(entityObject.key);

Expand Down
60 changes: 60 additions & 0 deletions src/utils/entity/buildEntityProto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 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, Entity, EntityProto, ValueProto} from '../../entity';
import {EntityProtoReduceAccumulator, EntityProtoReduceData} from '../../index';
import addExcludeFromIndexes = entity.addExcludeFromIndexes;

/**
* This function builds the entity proto from the entity object. We cannot
* rely on entity.entityToEntityProto for this because this function is only
* designed to be used for non-array entities.
*
*/
export function buildEntityProto(entityObject: Entity) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new function that all the code from src/index.ts is being pulled into

let entityProto: EntityProto = {};
if (Array.isArray(entityObject.data)) {
// This code builds the right entityProto from the entityObject
entityProto.properties = entityObject.data.reduce(
(acc: EntityProtoReduceAccumulator, data: EntityProtoReduceData) => {
const value = entity.encodeValue(data.value, data.name.toString());

if (typeof data.excludeFromIndexes === 'boolean') {
const excluded = data.excludeFromIndexes;
let values = value.arrayValue && value.arrayValue.values;

if (values) {
values = values.map((x: ValueProto) => {
x.excludeFromIndexes = excluded;
return x;
});
} else {
value.excludeFromIndexes = data.excludeFromIndexes;
}
}

acc[data.name] = value;

return acc;
},
{}
);
// This code adds excludeFromIndexes in the right places
addExcludeFromIndexes(entityObject.excludeFromIndexes, entityProto);
} else {
// This code builds the right entityProto from the entityObject
entityProto = entity.entityToEntityProto(entityObject);
}
return entityProto;
}
Loading