@duely/db
Advanced tools
Comparing version 0.4.0 to 0.4.1
import { ClientBase, Pool, QueryConfig } from 'pg'; | ||
import { Awaited, ResolvableValue } from '@duely/core'; | ||
import { ResolvableValue } from '@duely/core'; | ||
import { AccessLevel, Resources, ResourcesWithState } from './types'; | ||
@@ -29,5 +29,3 @@ export * from './errors'; | ||
export declare function withSession<R = any>(context: Context, callback: WithSessionCallback<R>, client?: ClientBase): Promise<R>; | ||
export declare const serviceAccountContextPromise: Promise<{ | ||
jwt: string; | ||
}>; | ||
export declare function getServiceAccountContext(): Promise<Context>; | ||
export declare function withConnection<R = any>(context: Context, callback: WithConnectionCallback<R>): Promise<R>; | ||
@@ -65,3 +63,3 @@ export declare function queryAll<R = any, I extends any[] = any[]>(client: ClientBase, sql: string | QueryConfig<I>, ...parameters: I): Promise<R[]>; | ||
export declare type ProcessingState = 'pending' | 'processing' | 'processed' | 'failed'; | ||
export declare function updateProcessingState(context: Awaited<typeof serviceAccountContextPromise>, resource_name: keyof ResourcesWithState, id: string, state: ProcessingState): Promise<void>; | ||
export declare function updateProcessingState(context: Awaited<typeof serviceAccountContextPromise>, resource_name: keyof ResourcesWithState, id: string, err: Error): Promise<void>; | ||
export declare function updateProcessingState(context: Context, resource_name: keyof ResourcesWithState, id: string, state: ProcessingState): Promise<void>; | ||
export declare function updateProcessingState(context: Context, resource_name: keyof ResourcesWithState, id: string, err: Error): Promise<void>; |
{ | ||
"name": "@duely/db", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Client for querying duely-postgres database.", | ||
@@ -21,4 +21,4 @@ "repository": "github:uoleevi/duely", | ||
"dependencies": { | ||
"@duely/build": "0.4.0", | ||
"@duely/core": "0.4.0", | ||
"@duely/build": "0.4.2", | ||
"@duely/core": "0.4.2", | ||
"pg": "^8.6.0" | ||
@@ -25,0 +25,0 @@ }, |
@@ -1,6 +0,6 @@ | ||
import { serviceAccountContextPromise } from '.'; | ||
import { getServiceAccountContext } from '.'; | ||
test('serviceAccountContextPromise', async () => { | ||
const context = await serviceAccountContextPromise; | ||
const context = await getServiceAccountContext(); | ||
expect(context.jwt).toBeTruthy(); | ||
}); |
import { ClientBase, Pool, PoolClient, QueryConfig } from 'pg'; | ||
import config from './config'; | ||
import fs from 'fs'; | ||
import { Awaited, ResolvableValue, Util } from '@duely/core'; | ||
import { ResolvableValue, Util } from '@duely/core'; | ||
import { AccessLevel, Resources, ResourcesWithState } from './types'; | ||
@@ -165,4 +165,23 @@ | ||
export const serviceAccountContextPromise = Util.lazy(logInServiceAccount); | ||
const serviceAccountContextState: { | ||
ttl: number, | ||
logInTime: number; | ||
contextPromise: Promise<Context> | null; | ||
} = { | ||
ttl: 4001000, | ||
logInTime: Number.NEGATIVE_INFINITY, | ||
contextPromise: null | ||
}; | ||
export async function getServiceAccountContext(): Promise<Context> { | ||
const time = Date.now(); | ||
if (time - serviceAccountContextState.logInTime > serviceAccountContextState.ttl) { | ||
serviceAccountContextState.logInTime = time; | ||
serviceAccountContextState.contextPromise = logInServiceAccount(); | ||
} | ||
return await serviceAccountContextState.contextPromise!; | ||
} | ||
export async function withConnection<R = any>( | ||
@@ -475,3 +494,3 @@ context: Context, | ||
export async function updateProcessingState( | ||
context: Awaited<typeof serviceAccountContextPromise>, | ||
context: Context, | ||
resource_name: keyof ResourcesWithState, | ||
@@ -482,3 +501,3 @@ id: string, | ||
export async function updateProcessingState( | ||
context: Awaited<typeof serviceAccountContextPromise>, | ||
context: Context, | ||
resource_name: keyof ResourcesWithState, | ||
@@ -489,3 +508,3 @@ id: string, | ||
export async function updateProcessingState( | ||
context: Awaited<typeof serviceAccountContextPromise>, | ||
context: Context, | ||
resource_name: keyof ResourcesWithState, | ||
@@ -492,0 +511,0 @@ id: string, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1149607
2591
+ Added@duely/build@0.4.2(transitive)
+ Added@duely/core@0.4.2(transitive)
- Removed@duely/build@0.4.0(transitive)
- Removed@duely/core@0.4.0(transitive)
Updated@duely/build@0.4.2
Updated@duely/core@0.4.2