diff --git a/types/cassanknex/cassanknex-tests.ts b/types/cassanknex/cassanknex-tests.ts new file mode 100644 index 0000000000..45e4ba3261 --- /dev/null +++ b/types/cassanknex/cassanknex-tests.ts @@ -0,0 +1,74 @@ +import * as cassanknex from "cassanknex"; + +const knex = cassanknex({ + connection: { + contactPoints: ['127.0.0.1'] + } +}); + +knex.on('ready', (err) => { +}); + +interface BirdRow { + type: string; + canFly: boolean; +} + +const qb = knex("animals") + .insert({ + type: 'Stork', + canFly: true + }) + .into('birds'); + +qb.exec((err, res) => { +}); + +qb.eachRow((n, row) => { +}, (err) => { +}); + +interface FooRow { + id: string; + foo: string; + bar: number; + baz: string[]; +} + +const query2 = knex("keyspace") + .select("id", "foo", "bar", "baz") + .ttl('foo') + .where("id", "=", "1") + .orWhere("id", "in", ["2", "3"]) + .orWhere("baz", "=", ["bar"]) + .andWhere("foo", "IN", ["baz", "bar"]) + .limit(10) + .from("table"); + +query2.stream({ + readable () { + const row = this.read(); + }, + end () {}, + error () {} +}); + +const values = { + id: "foo", + bar: 13, + baz: ["foo", "bar"] +}; + +const query3 = knex("cassanKnexy") + .insert(values) + .usingTimestamp(250000) + .usingTTL(50000) + .into("columnFamily"); + +const [cql, params] = [query3.cql(), query3.bindings()]; + +const query4 = knex("cassanKnexy") + .update("columnFamily") + .add("bar", { foo: "baz" }) // "bar" is a map + .remove("foo", ["bar"]) // "foo" is a set + .where("id", "=", 1); diff --git a/types/cassanknex/index.d.ts b/types/cassanknex/index.d.ts new file mode 100644 index 0000000000..5f5dc72b8c --- /dev/null +++ b/types/cassanknex/index.d.ts @@ -0,0 +1,204 @@ +// Type definitions for cassanknex 1.19 +// Project: https://github.com/azuqua/cassanknex +// Definitions by: Daniel Chao +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +/// + +import { EventEmitter } from "events"; +import { Client, ClientOptions, types, ResultCallback } from "cassandra-driver"; +import * as Long from "long"; +import { Readable } from "stream"; + +declare function CassanKnex (options?: CassanKnex.DriverOptions): CassanKnex.CassanKnex; + +export = CassanKnex; + +/** + * Will return the `never` type if `T[K]` is not a member of `Type`, for all `T[K]`. + */ +type TypeMatchedValue = T[K] extends Type ? This : never; + +type MappedDict = { + [key: string]: B +}; + +type InRestriction = 'in' | 'IN'; + +type ComparisonRestriction = '=' | '<' | '>' | '<=' | '>='; + +declare namespace CassanKnex { + interface DriverOptions { + debug?: boolean; + connection?: Client | ClientOptions; + } + + interface CassanKnex extends EventEmitter { + (keyspace?: string): QueryBuilderRoot; + } + + type SelectAsClause = { + [P in keyof T]: string; + }; + + interface StreamParams { + readable: (this: Readable) => any; + end: (this: Readable) => any; + error: (err: Error) => any; + } + + interface QueryBuilderRoot { + insert (values: Partial | T): InsertQueryBuilder; + select (...columns: Array): SelectQueryBuilder; + select (values: SelectAsClause): SelectQueryBuilder; + update (table: string): UpdateQueryBuilder; + delete (): DeleteQueryBuilder; + alterColumnFamily (columnFamily: string): AlterColumnFamilyQueryBuilder; + createColumnFamily (columnFamily: string): CreateColumnFamilyQueryBuilder; + createColumnFamilyIfNotExists (columnFamily: string): CreateColumnFamilyQueryBuilder; + createIndex (columnFamily: string, indexName: string, column: keyof T): QueryBuilder; + createIndexCustom (columnFamily: string, indexName: string, column: keyof T): QueryBuilder & CreateableIndexBuilder; + createType (typeName: string): CreateTypeQueryBuilder; + createTypeIfNotExists (typeName: string): CreateTypeQueryBuilder; + dropColumnFamily (columnFamily: string): QueryBuilder; + dropColumnFamilyIfExists (columnFamily: string): QueryBuilder; + dropType (): QueryBuilder; + dropTypeIfExists (): QueryBuilder; + truncate (columnFamily: string): QueryBuilder; + alterKeyspace (keyspace: string): KeyspaceQueryBuilder; + createKeyspace (keyspace: string): KeyspaceQueryBuilder; + createKeyspaceIfNotExists (keyspace: string): KeyspaceQueryBuilder; + dropKeyspace (): QueryBuilder; + dropKeyspaceIfExists (): QueryBuilder; + } + + interface QueryBuilder { + cql (): string; + bindings (): any[]; + exec (cb: ResultCallback): undefined; + eachRow (onEachRow: (n: number, row: types.Row) => any, onError: (err: Error) => any): undefined; + stream (params: StreamParams): undefined; + } + + interface FieldValueQueryBuilder { + decimal (columnName: K): TypeMatchedValue; + boolean (columnName: K): TypeMatchedValue; + blob (columnName: K): TypeMatchedValue; + timestamp (columnName: K): TypeMatchedValue; + date (columnName: K): TypeMatchedValue; + inet (columnName: K): TypeMatchedValue; + bigint (columnName: K): TypeMatchedValue; + counter (columnName: K): TypeMatchedValue; + double (columnName: K): TypeMatchedValue; + int (columnName: K): TypeMatchedValue; + float (columnName: K): TypeMatchedValue; + map (columnName: K, a: A, b: B): TypeMatchedValue, this>; + ascii (columnName: K): TypeMatchedValue; + text (columnName: K): TypeMatchedValue; + timeuuid (columnName: K): TypeMatchedValue; + uuid (columnName: K): TypeMatchedValue; + varchar (columnName: K): TypeMatchedValue; + list (columnName: K, typeName: string): TypeMatchedValue; + primary (primaryKey: string): this; + set (columnName: K, a: A): TypeMatchedValue, this>; + } + + interface CreateableColumnFamilyBuilder { + withCaching (): this; + withCompression (): this; + withCompaction (): this; + withClusteringOrderBy (value: K, direction: 'desc' | 'asc'): this; + } + + interface CreateableIndexBuilder { + withOptions (opts: MappedDict): this; + } + + interface KeyspaceableQueryBuilder { + withNetworkTopologyStrategy (strategy: MappedDict): this; + withSimpleStrategy (replicas: number): this; + withDurableWrites (durableWrites: boolean): this; + } + + interface InsertableQueryBuilder { + into (table: string): this; + ifNotExists (): this; + } + + interface TtlableQueryBuilder { + usingTimestamp (timestamp: number): this; + usingTTL (ttl: number): this; + } + + interface WhereableQueryBuilder { + where (lhs: K, comparison: InRestriction, rhs: Array): this; + where (lhs: K, comparison: ComparisonRestriction, rhs: T[K]): this; + orWhere (lhs: K, comparison: InRestriction, rhs: Array): this; + orWhere (lhs: K, comparison: ComparisonRestriction, rhs: T[K]): this; + andWhere (lhs: K, comparison: InRestriction, rhs: Array): this; + andWhere (lhs: K, comparison: ComparisonRestriction, rhs: T[K]): this; + tokenWhere (lhs: K, comparison: InRestriction, rhs: Array): this; + tokenWhere (lhs: K, comparison: ComparisonRestriction, rhs: T[K]): this; + ttl (columnName: K): this; + } + + interface IfableQueryBuilder { + if (lhs: K, comparison: ComparisonRestriction, rhs: T[K] | null): this; + } + + interface LimitableQueryBuilder { + limit (limit: number): this; + limitPerPartition (limit: number): this; + } + + interface FromableQueryBuilder { + from (table: string): this; + } + + interface UpdateableQueryBuilder { + set (key: K, value: T[K]): this; + set (object: Partial): this; + add (key: K, value: { [str: string]: T[K] }): TypeMatchedValue, this>; + add (key: K, value: Array): TypeMatchedValue, this>; + add (object: Partial): this; + remove (key: K, value: Array): this; + remove (object: Partial): this; + increment (column: keyof T, amount: number): this; + increment (object: Partial): this; + decrement (column: keyof T, amount: number): this; + decrement (object: Partial): this; + } + + interface AlterableQueryBuilder { + drop (...columns: K[]): this; + rename (column: K, newColumn: K): this; + alter (column: K, newType: string): this; + } + + type InsertQueryBuilder = QueryBuilder + & InsertableQueryBuilder + & TtlableQueryBuilder; + type SelectQueryBuilder = QueryBuilder + & WhereableQueryBuilder + & LimitableQueryBuilder + & FromableQueryBuilder; + type UpdateQueryBuilder = QueryBuilder + & WhereableQueryBuilder + & UpdateableQueryBuilder + & IfableQueryBuilder + & TtlableQueryBuilder; + type DeleteQueryBuilder = QueryBuilder + & WhereableQueryBuilder + & FromableQueryBuilder; + type CreateColumnFamilyQueryBuilder = QueryBuilder + & FieldValueQueryBuilder + & CreateableColumnFamilyBuilder; + type KeyspaceQueryBuilder = QueryBuilder + & KeyspaceableQueryBuilder; + type CreateTypeQueryBuilder = QueryBuilder + & FieldValueQueryBuilder; + type AlterColumnFamilyQueryBuilder = QueryBuilder + & AlterableQueryBuilder + & FieldValueQueryBuilder; +} diff --git a/types/cassanknex/tsconfig.json b/types/cassanknex/tsconfig.json new file mode 100644 index 0000000000..d77cbaca70 --- /dev/null +++ b/types/cassanknex/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cassanknex-tests.ts" + ] +} \ No newline at end of file diff --git a/types/cassanknex/tslint.json b/types/cassanknex/tslint.json new file mode 100644 index 0000000000..8bd9293b77 --- /dev/null +++ b/types/cassanknex/tslint.json @@ -0,0 +1,14 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "space-before-function-paren": [true, "always"], + "no-unnecessary-generics": false, + "strict-export-declare-modifiers": false, + "prefer-readonly": false, + "await-promise": false, + "no-for-in-array": false, + "no-void-expression": false, + "expect": false, + "no-declare-current-package": false + } +} \ No newline at end of file