Create a Session object to interact with a Cloud Spanner session.
**It is unlikely you will need to interact with sessions directly. By default, sessions are created and utilized for maximum performance automatically.**
Inheritance
common_3 .GrpcServiceObject > SessionPackage
@google-cloud/spannerExample
const
{
Spanner
}
=
require
(
' @google-cloud/spanner
'
);
const
spanner
=
new
Spanner
();
const
instance
=
spanner
.
instance
(
'my-instance'
);
const
database
=
instance
.
database
(
'my-database'
);
//-
// To create a session manually, don't provide a name.
//-
const
session
=
database
.
session
();
session
.
create
(
function
(
err
)
{
if
(
err
)
{
// Error handling omitted.
}
// Session created successfully.
// `session.id` = The name of the session.
});
//-
// To access a previously-created session, provide a name.
//-
const
session
=
database
.
session
(
'session-name'
);
Constructors
(constructor)(database, name)
constructor
(
database
:
Database
,
name
?:
string
);
Constructs a new instance of the Session
class
Properties
formattedName_
formattedName_
?:
string
;
id
id
:
string
;
lastError
lastError
?:
grpc
.
ServiceError
;
lastUsed
lastUsed
?:
number
;
resourceHeader_
resourceHeader_
:
{
[
k
:
string
]
:
string
;
};
txn
txn
?:
Transaction
;
Methods
delete(gaxOptions)
delete
(
gaxOptions
?:
CallOptions
)
:
Promise<DeleteSessionResponse>
;
Delete a session.
Wrapper around .
gaxOptions
Promise
< DeleteSessionResponse
>
{Promise
session
.
delete
(
function
(
err
,
apiResponse
)
{
if
(
err
)
{
// Error handling omitted.
}
// Session deleted successfully.
});
//-
//Returns a Promise if the callback is omitted.
//-
session
.
delete
().
then
(
function
(
data
)
{
const
apiResponse
=
data
[
0
];
});
delete(callback)
delete
(
callback
:
DeleteSessionCallback
)
:
void
;
callback
DeleteSessionCallback
void
delete(gaxOptions, callback)
delete
(
gaxOptions
:
CallOptions
,
callback
:
DeleteSessionCallback
)
:
void
;
gaxOptions
CallOptions
callback
DeleteSessionCallback
void
formatName_(databaseName, name)
static
formatName_
(
databaseName
:
string
,
name
:
string
)
:
string
;
Format the session name to include the parent database's name.
databaseName
string
The parent database's name.
name
string
The instance name.
string
{string}
Session
.
formatName_
(
'my-database'
,
'my-session'
);
// 'projects/grape-spaceship-123/instances/my-instance/' +
// 'databases/my-database/sessions/my-session'
getMetadata(gaxOptions)
getMetadata
(
gaxOptions
?:
CallOptions
)
:
Promise<GetSessionMetadataResponse>
;
Get the session's metadata.
Wrapper around .
gaxOptions
Promise
< GetSessionMetadataResponse
>
{Promise
session
.
getMetadata
(
function
(
err
,
metadata
,
apiResponse
)
{});
//-
//Returns a Promise if the callback is omitted.
//-
session
.
getMetadata
().
then
(
function
(
data
)
{
const
metadata
=
data
[
0
];
const
apiResponse
=
data
[
1
];
});
getMetadata(callback)
getMetadata
(
callback
:
GetSessionMetadataCallback
)
:
void
;
callback
GetSessionMetadataCallback
void
getMetadata(gaxOptions, callback)
getMetadata
(
gaxOptions
:
CallOptions
,
callback
:
GetSessionMetadataCallback
)
:
void
;
gaxOptions
CallOptions
callback
GetSessionMetadataCallback
void
keepAlive(gaxOptions)
keepAlive
(
gaxOptions
?:
CallOptions
)
:
Promise<KeepAliveResponse>
;
Ping the session with SELECT 1
to prevent it from expiring.
gaxOptions
Promise
< KeepAliveResponse
>
{Promise
session
.
keepAlive
(
function
(
err
)
{
if
(
err
)
{
// An error occurred while trying to keep this session alive.
}
});
keepAlive(callback)
keepAlive
(
callback
:
KeepAliveCallback
)
:
void
;
callback
KeepAliveCallback
void
keepAlive(gaxOptions, callback)
keepAlive
(
gaxOptions
:
CallOptions
,
callback
:
KeepAliveCallback
)
:
void
;
gaxOptions
CallOptions
callback
KeepAliveCallback
void
partitionedDml()
partitionedDml
()
:
PartitionedDml
;
Create a PartitionedDml transaction.
const
transaction
=
session
.
partitionedDml
();
snapshot(options, queryOptions)
snapshot
(
options
?:
TimestampBounds
,
queryOptions
?:
google
.
spanner
.
v1
.
ExecuteSqlRequest
.
IQueryOptions
)
:
Snapshot
;
Create a Snapshot transaction.
options
TimestampBounds
The timestamp bounds.
queryOptions
const
snapshot
=
session
.
snapshot
({
strong
:
false
});
transaction(queryOptions, requestOptions)
transaction
(
queryOptions
?:
google
.
spanner
.
v1
.
ExecuteSqlRequest
.
IQueryOptions
,
requestOptions
?:
Pick<IRequestOptions
,
'transactionTag'
> )
:
Transaction
;
Create a read write Transaction.
const
transaction
=
session
.
transaction
();