From 6c13e8c670e5c8104e1297df29dd572a522afd11 Mon Sep 17 00:00:00 2001 From: CodeAnimal Date: Sun, 21 Sep 2014 01:25:27 +0100 Subject: [PATCH] Add missing Promise/A methods - Update Model#Find method to now accept a number as the first parameter as well as an options object. - Fix Travis Ci failures. - Add Promise/A method tests. --- sequelize/sequelize-tests.ts | 31 +++- sequelize/sequelize.d.ts | 268 +++++++++++++++++++++++++++++++++-- 2 files changed, 286 insertions(+), 13 deletions(-) diff --git a/sequelize/sequelize-tests.ts b/sequelize/sequelize-tests.ts index 786c189aa..0bbfe3f6d 100644 --- a/sequelize/sequelize-tests.ts +++ b/sequelize/sequelize-tests.ts @@ -70,7 +70,7 @@ sequelize.or("", 123); sequelize.where("", ""); sequelize.where("", sequelize.and("", "")); promiseMe= sequelize.transaction(transOpts); -promiseMe = sequelize.transaction(transOpts, function (t): boolean { return true; }); +promiseMe = sequelize.transaction(transOpts, function (t:Sequelize.Transaction): boolean { return true; }); emitMe = sequelize.query("", model).complete(function (err, result) { }); emitMe = sequelize.query(""); var dialect: string = sequelize.getDialect(); @@ -110,11 +110,36 @@ model = model.scope({}); model = model.scope(""); model = model.scope([]); model = model.scope(null); + +model.find().then(function () { }, function () { }); +model.find().then(function () { }); +model.find().then(null, function () { }); +model.find().then(function (result: modelInst) { }); +model.find().then(function (result: modelInst): Sequelize.PromiseT { return model.find(1) }); +model.find().then(function (result: modelInst): Sequelize.PromiseT { return model.find(1) }, function (): Sequelize.PromiseT { return model.find(1) }); + +model.find().catch(function () { }); +model.find().catch(function (result: modelInst) { }); +model.find().catch(function (result: modelInst): Sequelize.Promise { return model.find(1) }); + +model.find().spread(function () { }, function () { }); +model.find().spread(function () { }); +model.find().spread(null, function () { }); +model.find().spread(function (result: modelInst) { }); +model.find().spread(function (result1: modelInst, result2: any) { }); +model.find().spread(null, function (result1: any, result2: boolean) { }); +model.find().spread(function (result: modelInst): Sequelize.Promise { return model.find(1) }); +model.find().spread(function (result: modelInst): Sequelize.PromiseT { return model.find(1) }); +model.find().spread(function (result: modelInst) { }, function (): Sequelize.PromiseT { return model.find(1) }); +model.find().spread(function (result: modelInst): Sequelize.PromiseT { return model.find(1) }, function (): Sequelize.PromiseT { return model.find(1) }); + promiseMe = model.findAll(findOpts, queryOpts); promiseMe = model.findAll(findOpts); promiseMe = model.findAll(); promiseMe = model.find(findOpts, queryOpts); promiseMe = model.find(findOpts); +promiseMe = model.find(1, queryOpts); +promiseMe = model.find(1); promiseMe = model.find(); promiseMe = model.aggregate("", "", findOpts); promiseMe = model.count(); @@ -149,6 +174,10 @@ promiseMe = model.describe(); model.dataset; //var isDefined: boolean = sequelize.isDefined(""); +model.find().spread(function (arg1: string, arg2: number) { + return model.find(); +}); + var isBool: boolean; var strArr: Array; diff --git a/sequelize/sequelize.d.ts b/sequelize/sequelize.d.ts index ce98f654d..8b218fa67 100644 --- a/sequelize/sequelize.d.ts +++ b/sequelize/sequelize.d.ts @@ -5,7 +5,6 @@ /// Original work by: samuelneff -/// /// interface NodeEventEmitter { } @@ -261,7 +260,7 @@ declare module "sequelize" * * @param schema Name of the schema. */ - dropSchema(schema): EventEmitter; + dropSchema(schema: string): EventEmitter; /** * Drop all schemas. @@ -410,11 +409,20 @@ declare module "sequelize" /** * Search for a single instance. This applies LIMIT 1, so the listener will always be called with a single instance. * - * @param options A hash of options to describe the scope of the search, or a number to search by id. + * @param options A hash of options to describe the scope of the search. * @param queryOptions Set the query options, e.g. raw, specifying that you want raw data instead of built * Instances. See sequelize.query for options */ find(options?: FindOptions, queryOptions?: QueryOptions): PromiseT; + + /** + * Search for a single instance. This applies LIMIT 1, so the listener will always be called with a single instance. + * + * @param options A number to search by id. + * @param queryOptions Set the query options, e.g. raw, specifying that you want raw data instead of built + * Instances. See sequelize.query for options + */ + find(id?: number, queryOptions?: QueryOptions): PromiseT; /** * Run an aggregation method on the specified field. @@ -1015,7 +1023,7 @@ declare module "sequelize" changeColumn(tableName: string, attributeName: string, dataTypeOrOptions: any): EventEmitter; renameColumn(tableName: string, attrNameBefore: string, attrNameAfter: string): EventEmitter; addIndex(tableName: string, attributes: Array, options?: QueryOptions): EventEmitter; - showIndex(tableName, options?: QueryOptions): EventEmitter; + showIndex(tableName: string, options?: QueryOptions): EventEmitter; getForeignKeysForTables(tableNames: Array): EventEmitter; removeIndex(tableName: string, attributes: Array): EventEmitter; removeIndex(tableName: string, indexName: string): EventEmitter; @@ -1116,7 +1124,7 @@ declare module "sequelize" attributesToSQL(attributes: Array): string; findAutoIncrementField(factory: Model): Array; quoteTable(param: any, as: boolean): string; - quote(obj, parent, force): string; + quote(obj:any, parent:any, force:boolean): string; createTrigger(tableName: string, triggerName: string, timingType: string, fireOnArray: TriggerOptions, functionName: string, functionParams: Array): string; dropTrigger(tableName: string, triggerName: string): string; renameTrigger(tableName: string, oldTriggerName: string, newTriggerName: string): string; @@ -1164,7 +1172,7 @@ declare module "sequelize" getConditionalJoins(options: { where?: any }, originalDao: Model): string; arrayValue(value: Array, key: string, _key: string, factory?: any, logicResult?: any): string; hashToWhereConditions(hash: any, dao: Model, options?: HashToWhereConditionsOption): string; - booleanValue(value): string; + booleanValue(value:boolean): string; } interface Schema { @@ -1252,7 +1260,7 @@ declare module "sequelize" getFormattedDateString(s: string): string; stringToDate(s: string): Date; saveSuccessfulMigration(from: Migration, to: Migration, callback: (metaData: MetaInstance) => void): void; - deleteUndoneMigration(from: Migration, to: Migration, callback: () => void); + deleteUndoneMigration(from: Migration, to: Migration, callback: () => void): void; execute(options?: MigrationExecuteOptions): EventEmitter; isBefore(date: Date, options?: MigrationCompareOptions): boolean; isAfter(date: Date, options?: MigrationCompareOptions): boolean; @@ -1820,15 +1828,16 @@ declare module "sequelize" where?: any; /** - * A list of the attributes (columns) that you want to select. Each item can be a string for the name to include, - * or an array where the first item is string name to include or an expression, and second item in inner array - * is the alias. + * A list of the attributes that you want to select. To rename an attribute, you can pass an array, with two + * elements - the first is the name of the attribute in the DB (or some kind of expression such as + * Sequelize.literal, Sequelize.fn and so on), and the second is the name you want the attribute to have in the + * returned instance */ attributes?: Array; /** * A list of associations to eagerly load. Supported is either { include?: [ Model1, Model2, ...] } or { include?: - * [ { model?: Model1, as?: 'Alias' } ] }. If your association are set up with an as (eg. X.hasMany(Y, { as?: 'Z }, + * [ { model?: Model1, as?: 'Alias' } ] }. If your association are set up with an as (eg. X.hasMany(Y, { as?: 'Z }, * you need to specify Z in the as attribute when eager loading Y). When using the object form, you can also * specify attributes to specify what columns to load, where to limit the relations, and include to load further * nested relations @@ -2300,9 +2309,163 @@ declare module "sequelize" * @param options Contains an array of the events to proxy. Defaults to sql, error and success */ proxy(promise: Promise, options?: ProxyOptions): Promise; + + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: any) => void, onRejected?: (result?: any) => void): Promise; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: any) => Promise, onRejected?: (result?: any) => Promise): Promise; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: any) => PromiseT, onRejected?: (result?: any) => PromiseT): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: any) => PromiseT, onRejected?: (result?: any) => void): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: any) => void, onRejected?: (result?: any) => PromiseT): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: any) => PromiseT, onRejected?: (result?: any) => PromiseT): Promise; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => Promise, onRejected?: (...results: Array) => Promise): Promise; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => void, onRejected?: (...results: Array) => void): Promise; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => Promise, onRejected?: (...results: Array) => void): Promise; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => void, onRejected?: (...results: Array) => Promise): Promise; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => PromiseT, onRejected?: (...results: Array) => PromiseT): PromiseT; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => void, onRejected?: (...results: Array) => PromiseT): PromiseT; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => PromiseT, onRejected?: (...results: Array) => void): PromiseT; + + /** + * Attach listeners to the emitter, promise style. This listener will recieve all arguments emitted by the emitter, + * as opposed to then which will only recieve the first argument. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * @param onRejected + */ + spread(onFulfilled?: (...results: Array) => PromiseT, onRejected?: (...results: Array) => PromiseT): Promise; + + /** + * Shorthand for then(null, onRejected) + */ + catch(onRejected: (result?: any) => Promise): Promise; + + /** + * Shorthand for then(null, onRejected) + */ + catch(onRejected: (result?: any) => PromiseT): PromiseT; + + /** + * Shorthand for then(null, onRejected) + */ + catch(onRejected: (result?: any) => void): Promise; } - interface PromiseT extends Promise, Promise.Thenable { + interface PromiseT extends Promise { /** * Listen for events, event emitter style. Mostly for backwards compatibility with EventEmitter. * @@ -2353,6 +2516,87 @@ declare module "sequelize" * @param options Contains an array of the events to proxy. Defaults to sql, error and success */ proxy(promise: PromiseT, options?: ProxyOptions): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: T) => void, onRejected?: (result?: T) => void): Promise; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: T) => Promise, onRejected?: (result?: T) => Promise): Promise; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: T) => PromiseT, onRejected?: (result?: T) => PromiseT): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: T) => PromiseT, onRejected?: (result?: T) => void): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: T) => void, onRejected?: (result?: T) => PromiseT): PromiseT; + + /** + * Attach listeners to the emitter, promise style. + * + * @param onFulfilled The function to call if the promise is fulfilled (if the emitter emits success). + * Note that this function will always only be called with one argument, as per + * the promises/A spec. For functions that emit multiple arguments + * (e.g. findOrCreate) @see spread + * @param onRejected + */ + then(onFulfilled?: (result?: T) => PromiseT, onRejected?: (result?: T) => PromiseT): Promise; + + /** + * Shorthand for then(null, onRejected) + */ + catch(onRejected: (result?: T) => Promise): Promise; + + /** + * Shorthand for then(null, onRejected) + */ + catch(onRejected: (result?: T) => PromiseT): PromiseT; + + /** + * Shorthand for then(null, onRejected) + */ + catch(onRejected: (result?: T) => void): Promise; } interface Utils {