Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better structure for typescript definition #836

Open
mathiasrw opened this issue Mar 7, 2017 · 10 comments
Open

Better structure for typescript definition #836

mathiasrw opened this issue Mar 7, 2017 · 10 comments

Comments

@mathiasrw
Copy link
Member

According to #738 (comment) problems with compiling with the existing typescript definition was solved by removing namespace and avoiding deklare a module to export (but just do the export)

What it led me to try is to remove the surrounding namespace and the module with its export at the bottom of the file, replacing all that with:
export var alasql:AlaSQL;

This sounds like same changes made to the definition made for the flow-typed: https://github.com/AlaSQL/flow-typed/blob/master/definitions/npm/alasql_v0.3.x/flow_v0.25.x-/alasql_v0.3.x.js

This would be great, as we would only have one version of the definition and could reuse it for both projects.

Basically we need people to test if the following code works as definition files for both typescript and flow-typed

interface _alasql_Callback {
    (data?: any, err?: Error): void;
}

interface _alasql_Options {
    errorlog: boolean;
    valueof: boolean;
    dropifnotexists: boolean; // drop database in any case
    datetimeformat: string; // how to handle DATE and DATETIME types
    casesensitive: boolean; // table and column names are case sensitive and converted to lower-case
    logtarget: string; // target for log. Values: 'console', 'output', 'id' of html tag
    logprompt: boolean; // print SQL at log
    progress: boolean;
    modifier: any; // values: RECORDSET, VALUE, ROW, COLUMN, MATRIX, TEXTSTRING, INDEX
    columnlookup: number; // how many rows to lookup to define columns
    autovertex: boolean; // create vertex if not found
    usedbo: boolean; // use dbo as current database (for partial T-SQL comaptibility)
    autocommit: boolean; // the AUTOCOMMIT ON | OFF
    cache: boolean; // use cache
    nan: boolean; // check for NaN and convert it to undefined
    tsql: boolean;
    mysql: boolean;
    postgres: boolean;
    oracle: boolean;
    sqlite: boolean;
    orientdb: boolean;
    autoExtFilenameOnRead: boolean;
	autoExtFilenameOnWrite: boolean;
    nocount: boolean; // for SET NOCOUNT OFF
	joinstar: string;
}

// compiled Statement
interface _alasql_Statement {
    (params?: any, cb?: _alasql_Callback, scope?: any): any;
}

// abstract Syntax Tree
interface _alasql_AST {
    compile(databaseid: string): _alasql_Statement;
}

// From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/es6-promise/es6-promise.d.ts
interface Thenable<T> {
    then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
    then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
    catch<U>(onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
}

// From https://github.com/agershun/_alasql_/wiki/User%20Defined%20Functions
interface _alasql_userDefinedFunction {
    (x: any): any;
}

interface _alasql_userDefinedFunctionLookUp {
    [x: string]: _alasql_userDefinedFunction;
}

// From https://github.com/agershun/_alasql_/wiki/User%20Defined%20Functions
interface _alasql_userAggregator {
    (value: any, accumulator: any, stage: number): any;
}

interface _alasql_userAggregatorLookUp {
    [x: string]: _alasql_userAggregator;
}

interface AlaSQL {
    (sql?: string | Array<any> | () => void, params?: any, cb?: _alasql_Callback): string | number | {} | Array<any> | Thenable<any>;
    options: _alasql_Options;
    error: Error;
    parse(sql: string): _alasql_AST;
    promise(sql: string | Array<any>, params?: any): Thenable<any>;
    fn: _alasql_userDefinedFunctionLookUp;
    aggr: _alasql_userAggregatorLookUp;
    autoval(tablename: string, colname: string, getNext?:boolean): number;
}

export var alasql:AlaSQL;
@mathiasrw
Copy link
Member Author

The proposed file works for flow-typed

@mathiasrw
Copy link
Member Author

mathiasrw commented Mar 7, 2017

Any chance we could get @kaktus40 or @jnath to test the proposed definition in a typescript setup?

(involved in #769 and #752)

@kaktus40
Copy link
Contributor

kaktus40 commented Mar 8, 2017

Hello,
for me this definition doesn't work. Sorry I can't work on your file until next week.

@mathiasrw
Copy link
Member Author

Hi @kaktus40 thank you so much for fast interaction :)

I better get into the typescript thing too so I can test too and help develop the definition :)

@kaktus40
Copy link
Contributor

kaktus40 commented Mar 8, 2017

From what I see:

  • line 69 is incorrect : there is no name for the function. Either there is a mane for this function, either interface AlaSQL as the same construction than _alasql_userDefinedFunction or _alasql_userAggregator (one attribute).
  • By domino effect, you should declare all interface with keyword export. For example I use the interface userDefinedFunction but I can't access it from your definition (not exported!)

Following these recommendations, is it not possible to only use this following definitions:

declare namespace alaSQLSpace {
    interface AlaSQLCallback {
        (data?: any, err?: Error): void;
    }

    interface AlaSQLOptions {
        errorlog: boolean;
        valueof: boolean;
        dropifnotexists: boolean; // drop database in any case
        datetimeformat: string; // how to handle DATE and DATETIME types
        casesensitive: boolean; // table and column names are case sensitive and converted to lower-case
        logtarget: string; // target for log. Values: 'console', 'output', 'id' of html tag
        logprompt: boolean; // print SQL at log
        modifier: any; // values: RECORDSET, VALUE, ROW, COLUMN, MATRIX, TEXTSTRING, INDEX
        columnlookup: number; // how many rows to lookup to define columns
        autovertex: boolean; // create vertex if not found
        usedbo: boolean; // use dbo as current database (for partial T-SQL comaptibility)
        autocommit: boolean; // the AUTOCOMMIT ON | OFF
        cache: boolean; // use cache
        nocount: boolean; // for SET NOCOUNT OFF
        nan: boolean; // check for NaN and convert it to undefined
        angularjs: boolean;
        tsql: boolean;
        mysql: boolean;
        postgres: boolean;
        oracle: boolean;
        sqlite: boolean;
        orientdb: boolean;
    }

    // compiled Statement
    interface AlaSQLStatement {
        (params?: any, cb?: AlaSQLCallback, scope?: any): any;
    }

    // abstract Syntax Tree
    interface AlaSQLAST {
        compile(databaseid: string): AlaSQLStatement;
    }

    // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/es6-promise/es6-promise.d.ts
    interface Thenable<T> {
        then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
        then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
        catch<U>(onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
    }

    // see https://github.com/agershun/alasql/wiki/User%20Defined%20Functions
    interface userDefinedFunction {
        (x: any): any;
    }
    interface userDefinedFunctionLookUp {
        [x: string]: userDefinedFunction;
    }
    // see https://github.com/agershun/alasql/wiki/User%20Defined%20Functions
    interface userAggregator {
        (value: any, accumulator: any, stage: number): any;
    }
    interface userAggregatorLookUp {
        [x: string]: userAggregator;
    }

    interface AlaSQL {
        options: AlaSQLOptions;
        error: Error;
        (sql: any, params?: any, cb?: AlaSQLCallback, scope?: any): any;
        parse(sql): AlaSQLAST;
        promise(sql: any, params?: any): Thenable<any>;
        fn: userDefinedFunctionLookUp;
        aggr: userAggregatorLookUp;
        autoval(tablename: string, colname: string, getNext?:boolean): number;
    }
}

declare var alasql: alaSQLSpace.AlaSQL;

This example works in typescript. Is it ok in flow?

@donflopez
Copy link

Hi guys, creating a plugin in TS I can't use alasql.yy as this example say. Any help?

Thank you!!

@mathiasrw
Copy link
Member Author

mathiasrw commented Mar 28, 2017

Please make a new issue with the question (just post a link)

The alasql.yy have not been defined in TS. please try to add yy:{}; to interface AlaSQL and let us know if it worked...

@donflopez
Copy link

It works :)

Thanks!

@kaktus40
Copy link
Contributor

hello, you should check this. I think this could be helpful...

@mathiasrw
Copy link
Member Author

awesome - i got a .d.ts file with 13031 lines... might need to remove some elements...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants