Skip to content

Commit

Permalink
feat: untyped param types (#1869)
Browse files Browse the repository at this point in the history
This features allows customers to not pass paramType in parametrised queries. It gives backend to flexibly match the type. Example: Previously is timestamp was passed as string, it would be passed to backend with string type code and hence an error would be thrown. Now it is passed without any type code and hence backend handles checking its time and the code succeeds.
  • Loading branch information
asthamohta committed Feb 13, 2024
1 parent bed4832 commit 6ef44c3
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 27 deletions.
15 changes: 15 additions & 0 deletions samples/dml.js
Expand Up @@ -288,6 +288,21 @@ function updateUsingDmlWithStruct(instanceId, databaseId, projectId) {
params: {
name: nameStruct,
},
types: {
name: {
type: 'struct',
fields: [
{
name: 'FirstName',
type: 'string',
},
{
name: 'LastName',
type: 'string',
},
],
},
},
});

console.log(`Successfully updated ${rowCount} record.`);
Expand Down
30 changes: 30 additions & 0 deletions samples/struct.js
Expand Up @@ -111,6 +111,21 @@ async function queryDataWithStruct(instanceId, databaseId, projectId) {
params: {
name: nameStruct,
},
types: {
name: {
type: 'struct',
fields: [
{
name: 'FirstName',
type: 'string',
},
{
name: 'LastName',
type: 'string',
},
],
},
},
};

// Queries rows from the Singers table
Expand Down Expand Up @@ -250,6 +265,21 @@ async function queryStructField(instanceId, databaseId, projectId) {
params: {
name: nameStruct,
},
types: {
name: {
type: 'struct',
fields: [
{
name: 'FirstName',
type: 'string',
},
{
name: 'LastName',
type: 'string',
},
],
},
},
};

// Queries rows from the Singers table
Expand Down
5 changes: 1 addition & 4 deletions src/codec.ts
Expand Up @@ -597,10 +597,6 @@ function getType(value: Value): Type {
return {type: 'bool'};
}

if (is.string(value)) {
return {type: 'string'};
}

if (Buffer.isBuffer(value)) {
return {type: 'bytes'};
}
Expand Down Expand Up @@ -643,6 +639,7 @@ function getType(value: Value): Type {
return {type: 'json'};
}

// String type is also returned as unspecified to allow untyped parameters
return {type: 'unspecified'};
}

Expand Down
5 changes: 4 additions & 1 deletion src/transaction.ts
Expand Up @@ -1300,7 +1300,10 @@ export class Snapshot extends EventEmitter {
if (!is.empty(typeMap)) {
Object.keys(typeMap).forEach(param => {
const type = typeMap[param];
paramTypes[param] = codec.createTypeObject(type);
const typeObject = codec.createTypeObject(type);
if (typeObject.code !== 'TYPE_CODE_UNSPECIFIED') {
paramTypes[param] = codec.createTypeObject(type);
}
});
}

Expand Down

0 comments on commit 6ef44c3

Please sign in to comment.