From 06f66f542c74e574fe1575e44380ee06f9a0f59e Mon Sep 17 00:00:00 2001 From: Michal Kaminski <32871390+michal-b-kaminski@users.noreply.github.com> Date: Fri, 1 Feb 2019 00:44:35 +0100 Subject: [PATCH 1/2] [cassandra-driver] Updated definitions for version 4.0.0 --- .../cassandra-driver-tests.ts | 85 +++++- types/cassandra-driver/index.d.ts | 258 +++++++++++++++++- 2 files changed, 336 insertions(+), 7 deletions(-) diff --git a/types/cassandra-driver/cassandra-driver-tests.ts b/types/cassandra-driver/cassandra-driver-tests.ts index ad8a7426fe..3e203837b7 100644 --- a/types/cassandra-driver/cassandra-driver-tests.ts +++ b/types/cassandra-driver/cassandra-driver-tests.ts @@ -2,24 +2,103 @@ import * as cassandra from 'cassandra-driver'; import * as util from 'util'; import * as fs from 'fs'; +class CustomRequestLogger implements cassandra.metrics.ClientMetrics { + onAuthenticationError(e: Error | cassandra.errors.AuthenticationError): void { + throw new Error("Method not implemented."); + } + onClientTimeoutError(e: cassandra.errors.OperationTimedOutError): void { + throw new Error("Method not implemented."); + } + onClientTimeoutRetry(e: Error): void { + throw new Error("Method not implemented."); + } + onConnectionError(e: Error): void { + throw new Error("Method not implemented."); + } + onIgnoreError(e: Error): void { + throw new Error("Method not implemented."); + } + onOtherError(e: Error): void { + throw new Error("Method not implemented."); + } + onOtherErrorRetry(e: Error): void { + throw new Error("Method not implemented."); + } + onReadTimeoutError(e: cassandra.errors.ResponseError): void { + throw new Error("Method not implemented."); + } + onReadTimeoutRetry(e: Error): void { + throw new Error("Method not implemented."); + } + onResponse(latency: number[]): void { + throw new Error("Method not implemented."); + } + onSpeculativeExecution(): void { + throw new Error("Method not implemented."); + } + onSuccessfulResponse(latency: number[]): void { + throw new Error("Method not implemented."); + } + onUnavailableError(e: cassandra.errors.ResponseError): void { + throw new Error("Method not implemented."); + } + onUnavailableRetry(e: Error): void { + throw new Error("Method not implemented."); + } + onWriteTimeoutError(e: cassandra.errors.ResponseError): void { + throw new Error("Method not implemented."); + } + onWriteTimeoutRetry(e: Error): void { + throw new Error("Method not implemented."); + } +} + const client = new cassandra.Client({ contactPoints: ['h1', 'h2'], + localDataCenter: 'datacenter1', keyspace: 'ks1', sslOptions: { cert: fs.readFileSync('certFilePath') }, encoding: { map: Map, - set: Set - } + set: Set, + useBigIntAsLong: false, + useBigIntAsVarint: true + }, + requestTracker: new cassandra.tracker.RequestLogger({ logNormalRequests: true }), + metrics: new CustomRequestLogger() }); +console.log(client.metrics); + const query = 'SELECT email, last_name FROM user_profiles WHERE key=?'; client.execute(query, ['guy'], (err, result) => { console.log('got user profile with email ' + result.rows[0].email); }); -client.execute(query, [ 'guy' ], { prepare: true }).then( +client.execute(query, [ 'guy' ], { prepare: true, counter: false }).then( (result) => console.log(result.first().email), (error) => console.log(error) ); + +const mapper = new cassandra.mapping.Mapper( + client, + { + models: { + // example copied from library examples + // tslint:disable-next-line:object-literal-key-quotes + 'Video': { + tables: [ 'videos', 'user_videos', 'latest_videos' ], + keyspace: 'examples', + // example copied from library examples + // tslint:disable-next-line:object-literal-key-quotes + columns: { 'videoid': 'videoId', 'userid': 'userId' }, + mappings: new cassandra.mapping.UnderscoreCqlToCamelCaseMappings() + } + } + }); + +const videoMapper = mapper.forModel('Video'); +videoMapper.insert({ videoId: 'uuid', addedDate: new Date(), userId: 'uuid', name: 'My video', description: 'My desc' }); +videoMapper.find({ videoId: 'uuid' }).then(results => results.first()); diff --git a/types/cassandra-driver/index.d.ts b/types/cassandra-driver/index.d.ts index df95e386b6..e71d754e68 100644 --- a/types/cassandra-driver/index.d.ts +++ b/types/cassandra-driver/index.d.ts @@ -1,7 +1,8 @@ -// Type definitions for cassandra-driver 3.6 +// Type definitions for cassandra-driver 4.0 // Project: https://github.com/datastax/nodejs-driver // Definitions by: Marc Fisher // Christian D +// Michal Kaminski // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -41,11 +42,11 @@ export namespace policies { interface LoadBalancingPolicy { init(client: Client, hosts: HostMap, callback: Callback): void; getDistance(host: Host): types.distance; - newQueryPlan(keyspace: string, queryOptions: any, callback: Callback): void; + newQueryPlan(keyspace: string, queryOptions: ExecutionOptions | null, callback: Callback): void; } interface DCAwareRoundRobinPolicyStatic { - new (localDc?: string, usedHostsPerRemoteDc?: number): DCAwareRoundRobinPolicy; + new (localDc?: string): DCAwareRoundRobinPolicy; } interface DCAwareRoundRobinPolicy extends LoadBalancingPolicy { @@ -504,6 +505,7 @@ export let Encoder: EncoderStatic; export interface ClientOptions { contactPoints: string[]; + localDataCenter?: string; keyspace?: string; refreshSchemaDelay?: number; isMetadataSyncEnabled?: boolean; @@ -541,15 +543,19 @@ export interface ClientOptions { coalescingThreshold?: number; }; authProvider?: auth.AuthProvider; + requestTracker?: tracker.RequestTracker; sslOptions?: tls.ConnectionOptions; encoding?: { map?: typeof Map | ((...args: any[]) => any); set?: typeof Set | ((...args: any[]) => any); copyBuffer?: boolean; useUndefinedAsUnset?: boolean; + useBigIntAsLong?: boolean; + useBigIntAsVarint?: boolean; }; profiles?: ExecutionProfile[]; promiseFactory?: (handler: (callback: (err?: any, result?: any) => void) => void) => Promise; + metrics?: metrics.ClientMetrics; } export interface QueryOptions { @@ -563,11 +569,11 @@ export interface QueryOptions { isIdempotent?: boolean; keyspace?: string; logged?: boolean; + counter?: boolean; pageState?: Buffer | string; prepare?: boolean; readTimeout?: number; retry?: policies.retry.RetryPolicy; - retryOnTimeout?: boolean; routingIndexes?: number[]; routingKey?: Buffer | Buffer[]; routingNames?: string[]; @@ -585,6 +591,7 @@ export interface Client extends events.EventEmitter { hosts: HostMap; keyspace: string; metadata: metadata.Metadata; + metrics: metrics.ClientMetrics; batch(queries: string[] | Array<{ query: string, params?: any }>, options: QueryOptions, callback: ResultCallback): void; batch(queries: string[] | Array<{ query: string, params?: any }>, callback: ResultCallback): void; @@ -889,3 +896,246 @@ export namespace metadata { virtual: boolean; } } + +export interface ExecutionOptionsStatic { + new (): ExecutionOptions; +} + +export interface ExecutionOptions { + getCaptureStackTrace(): boolean; + getConsistency(): types.consistencies; + getCustomPayload(): { [key: string]: any }; + getFetchSize(): number; + getFixedHost(): Host; + getHints(): string[] | string[][]; + isAutoPage(): boolean; + isBatchCounter(): boolean; + isBatchLogged(): boolean; + isIdempotent(): boolean; + isPrepared(): boolean; + isQueryTracing(): boolean; + getKeyspace(): string; + getLoadBalancingPolicy(): policies.loadBalancing.LoadBalancingPolicy; + getPageState(): Buffer; + getRawQueryOptions(): QueryOptions; + getReadTimeout(): number; + getRetryPolicy(): policies.retry.RetryPolicy; + getRoutingKey(): Buffer | Buffer[]; + getSerialConsistency(): types.consistencies; + getTimestamp(): number | Long | undefined | null; + setHints(hints: string[]): void; +} + +export let ExecutionOptions: ExecutionOptionsStatic; + +export namespace mapping { + let Mapper: MapperStatic; + let ModelBatchItem: ModelBatchItemStatic; + let ModelMappingInfo: ModelMappingInfoStatic; + let ModelMapper: ModelMapperStatic; + let MappingHandler: MappingHandlerStatic; + let Result: ResultStatic; + let DefaultTableMappings: DefaultTableMappingsStatic; + let UnderscoreCqlToCamelCaseMappings: UnderscoreCqlToCamelCaseMappingsStatic; + + interface MappingExecutionOptions { + executionProfile?: string; + isIdempotent?: boolean; + logged?: boolean; + timestamp?: number | Long; + fetchSize?: number; + pageState?: number; + } + + interface ModelTables { + name: string; + isView: boolean; + } + + interface MappingQuery { + query: string; + isIdempotent: boolean; + isCounter: boolean; + } + + interface MapperStatic { + new (client: Client, options?: MappingOptions): Mapper; + } + interface Mapper { + batch(items: ModelBatchItem[], executionOptions?: string | MappingExecutionOptions): Promise; + forModel(name: string): ModelMapper; + } + + interface MappingOptions { + models: { [key: string]: ModelOptions }; + } + + interface ModelOptions { + tables?: string[] | ModelTables[]; + mappings?: TableMappings; + columns?: { [key: string]: string }; + keyspace?: string; + } + + interface ModelMappingInfoStatic { + new (keyspace: string, tables: ModelTables[], mappings: TableMappings, columns: { [key: string]: string }): ModelMappingInfo; + parse(options: MappingOptions, currentKeyspace: string): { [key: string]: ModelMappingInfo }; + createDefault(modelName: string, currentKeyspace: string): ModelMappingInfo; + } + interface ModelMappingInfo { + keyspace: string; + tables: ModelTables[]; + + getColumnName(propName: string): string; + getPropertyName(columnName: string): string; + newInstance(): TableMappings; + } + + interface MappingDocInfo { + fields?: string[]; + ttl?: number; + ifNotExists?: boolean; + when?: { [key: string]: string }; + orderBy?: { [key: string]: string }; + limit?: number; + deleteOnlyColumns?: boolean; + } + + interface ModelBatchItemStatic { + new ( + queries: Promise, + doc: { [key: string]: any }, + docInfo: MappingDocInfo, + mappingInfo: ModelMappingInfo + ): ModelBatchItem; + } + interface ModelBatchItem { + pushQueries(arr: MappingQuery[]): { isIdempotent: boolean, isCounter: boolean }; + getMappingInfo(): ModelMappingInfo; + } + + interface MappingHandlerStatic { + new (client: Client, mappingInfo: ModelMappingInfo): MappingHandler; + } + interface MappingHandler {} + + interface ModelBatchMapper { + insert(doc: { [key: string]: any }, docInfo?: MappingDocInfo): ModelBatchItem; + remove(doc: { [key: string]: any }, docInfo?: MappingDocInfo): ModelBatchItem; + update(doc: { [key: string]: any }, docInfo?: MappingDocInfo): ModelBatchItem; + } + + interface ModelMapperStatic { + new (name: string, handler: MappingHandler): ModelMapper; + } + interface ModelMapper { + name: string; + batching: ModelBatchMapper; + + get(doc: { [key: string]: any }, docInfo?: MappingDocInfo, executionOptions?: string | MappingExecutionOptions): Promise; + find(doc: { [key: string]: any }, docInfo?: MappingDocInfo, executionOptions?: string | MappingExecutionOptions): Promise; + findAll(docInfo?: MappingDocInfo, executionOptions?: string | MappingExecutionOptions): Promise; + insert(doc: { [key: string]: any }, docInfo?: MappingDocInfo, executionOptions?: string | MappingExecutionOptions): Promise; + update(doc: { [key: string]: any }, docInfo?: MappingDocInfo, executionOptions?: string | MappingExecutionOptions): Promise; + remove(doc: { [key: string]: any }, docInfo?: MappingDocInfo, executionOptions?: string | MappingExecutionOptions): Promise; + mapWithQuery( + query: string, + paramsHandler: (doc: { [key: string]: any }) => any, + executionOptions?: string | MappingExecutionOptions + ): (doc: { [key: string]: any }, executionOptions?: string | MappingExecutionOptions) => Promise; + } + + interface ResultStatic { + new (rs: types.ResultSet, info: ModelMappingInfo, rowAdapter: (row: types.Row, info: ModelMappingInfo) => { [key: string]: any }): Result; + } + interface Result { + [Symbol.iterator](): Iterator<{ [key: string]: any }>; + wasApplied(): boolean; + first(): { [key: string]: any }; + toArray(): Array<{ [key: string]: any }>; + forEach(callback: (currentValue: { [key: string]: any }, index: number) => void, thisArg?: any): void; + } + + interface TableMappings { + getColumnName(propName: string): string; + getPropertyName(columnName: string): string; + newObjectInstance(): { [key: string]: any }; + } + + interface DefaultTableMappingsStatic { + new (): DefaultTableMappings; + } + interface DefaultTableMappings extends TableMappings {} + interface UnderscoreCqlToCamelCaseMappingsStatic { + new (): UnderscoreCqlToCamelCaseMappings; + } + interface UnderscoreCqlToCamelCaseMappings extends TableMappings {} +} + +export namespace tracker { + let RequestLogger: RequestLoggerStatic; + + interface RequestLoggerOptions { + slowThreshold?: number; + logNormalRequests?: boolean; + logErroredRequests?: boolean; + messageMaxQueryLength?: number; + messageMaxParameterValueLength?: number; + messageMaxErrorStackTraceLength?: number; + } + + interface RequestTracker { + onError( + host: Host, + query: string | Array<{ query: string, params?: any }>, + parameters: any[] | { [key: string]: any } | null, + executionOptions: ExecutionOptions, + requestLength: number, + err: Error, + latency: number[] + ): void; + onSuccess( + host: Host, + query: string | Array<{ query: string, params?: any }>, + parameters: any[] | { [key: string]: any } | null, + executionOptions: ExecutionOptions, + requestLength: number, + responseLength: number, + latency: number[] + ): void; + shutdown(): void; + } + + interface RequestLoggerStatic { + new (options: RequestLoggerOptions): RequestLogger; + } + interface RequestLogger extends RequestTracker {} +} + +export namespace metrics { + let DefaultMetrics: DefaultMetricsStatic; + + interface ClientMetrics { + onAuthenticationError(e: Error | errors.AuthenticationError): void; + onClientTimeoutError(e: errors.OperationTimedOutError): void; + onClientTimeoutRetry(e: Error): void; + onConnectionError(e: Error): void; + onIgnoreError(e: Error): void; + onOtherError(e: Error): void; + onOtherErrorRetry(e: Error): void; + onReadTimeoutError(e: errors.ResponseError): void; + onReadTimeoutRetry(e: Error): void; + onResponse(latency: number[]): void; + onSpeculativeExecution(): void; + onSuccessfulResponse(latency: number[]): void; + onUnavailableError(e: errors.ResponseError): void; + onUnavailableRetry(e: Error): void; + onWriteTimeoutError(e: errors.ResponseError): void; + onWriteTimeoutRetry(e: Error): void; + } + + interface DefaultMetricsStatic { + new (): DefaultMetrics; + } + interface DefaultMetrics extends ClientMetrics {} +} From 2d160dd10274a6907f0ce9893817e550fadbed96 Mon Sep 17 00:00:00 2001 From: Michal Kaminski <32871390+michal-b-kaminski@users.noreply.github.com> Date: Sun, 3 Feb 2019 00:26:43 +0100 Subject: [PATCH 2/2] [cassandra-driver] Remove internal methods --- types/cassandra-driver/index.d.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/types/cassandra-driver/index.d.ts b/types/cassandra-driver/index.d.ts index e71d754e68..02853c66ba 100644 --- a/types/cassandra-driver/index.d.ts +++ b/types/cassandra-driver/index.d.ts @@ -1009,10 +1009,7 @@ export namespace mapping { mappingInfo: ModelMappingInfo ): ModelBatchItem; } - interface ModelBatchItem { - pushQueries(arr: MappingQuery[]): { isIdempotent: boolean, isCounter: boolean }; - getMappingInfo(): ModelMappingInfo; - } + interface ModelBatchItem {} interface MappingHandlerStatic { new (client: Client, mappingInfo: ModelMappingInfo): MappingHandler;