PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/sequelize/sequelize-tests.ts

https://github.com/deenairn/DefinitelyTyped
TypeScript | 1603 lines | 1281 code | 196 blank | 126 comment | 1023 complexity | 082d908b9260fc8617355bf032680408 MD5 | raw file
Possible License(s): MIT

Large files files are truncated, but you can click here to view the full file

  1. import Sequelize = require("sequelize");
  2. import Promise = require('bluebird');
  3. //
  4. // Fixtures
  5. // ~~~~~~~~~~
  6. //
  7. interface AnyAttributes { };
  8. interface AnyInstance extends Sequelize.Instance<AnyAttributes> { };
  9. var s = new Sequelize( '' );
  10. var sequelize = s;
  11. var DataTypes = Sequelize;
  12. var User = s.define<AnyInstance, AnyAttributes>( 'user', {} );
  13. var user = User.build();
  14. var Task = s.define<AnyInstance, AnyAttributes>( 'task', {} );
  15. var Group = s.define<AnyInstance, AnyAttributes>( 'group', {} );
  16. var Comment = s.define<AnyInstance, AnyAttributes>( 'comment', {} );
  17. var Post = s.define<AnyInstance, AnyAttributes>( 'post', {} );
  18. var t : Sequelize.Transaction = null;
  19. s.transaction().then( ( a ) => t = a );
  20. //
  21. // Generics
  22. // ~~~~~~~~~~
  23. //
  24. interface GUserAttributes {
  25. id? : number;
  26. username? : string;
  27. }
  28. interface GUserInstance extends Sequelize.Instance<GUserAttributes> {}
  29. var GUser = s.define<GUserInstance, GUserAttributes>( 'user', { id: Sequelize.INTEGER, username : Sequelize.STRING });
  30. GUser.create({ id : 1, username : 'one' }).then( ( guser ) => guser.save() );
  31. var schema : Sequelize.DefineAttributes = {
  32. key : { type : Sequelize.STRING, primaryKey : true },
  33. value : Sequelize.STRING
  34. };
  35. s.define('user', schema);
  36. interface GTaskAttributes {
  37. revision? : number;
  38. name? : string;
  39. }
  40. interface GTaskInstance extends Sequelize.Instance<GTaskAttributes> {
  41. upRevision(): void;
  42. }
  43. var GTask = s.define<GTaskInstance, GTaskAttributes>( 'task', { revision : Sequelize.INTEGER, name : Sequelize.STRING });
  44. GUser.hasMany(GTask);
  45. GTask.create({ revision: 1, name: 'test' }).then( (gtask) => gtask.upRevision() );
  46. //
  47. // Associations
  48. // ~~~~~~~~~~~~~~
  49. //
  50. // https://github.com/sequelize/sequelize/tree/v3.4.1/test/integration/associations
  51. //
  52. User.hasOne( Task );
  53. User.hasOne( Task, { foreignKey : 'primaryGroupId', as : 'primaryUsers' } );
  54. User.hasOne( Task, { foreignKey : 'userCoolIdTag' } );
  55. User.hasOne( Task, { foreignKey : 'userId', keyType : Sequelize.STRING, constraints : false } );
  56. Task.hasOne( User, { foreignKey : { name : 'taskId', field : 'task_id' } } );
  57. User.hasOne( Task, { foreignKey : { name : 'uid', allowNull : false } } );
  58. User.hasOne( Task, { onDelete : 'cascade' } );
  59. User.hasOne( Task, { onUpdate : 'cascade' } );
  60. User.hasOne( Task, { onDelete : 'cascade', hooks : true } );
  61. User.hasOne( Task, { foreignKey : { allowNull : false } } );
  62. User.hasOne( Task, { foreignKeyConstraint : true } );
  63. User.belongsTo( Task );
  64. User.belongsTo( Task, { foreignKey : 'primaryGroupId', as : 'primaryUsers' } );
  65. Task.belongsTo( User, { foreignKey : 'user_id' } );
  66. Task.belongsTo( User, { foreignKey : 'user_name', targetKey : 'username' } );
  67. User.belongsTo( User, { foreignKey : 'userId', keyType : Sequelize.STRING, constraints : false } );
  68. User.belongsTo( Post, { foreignKey : { name : 'AccountId', field : 'account_id' } } );
  69. Task.belongsTo( User, { foreignKey : { allowNull : false, name : 'uid' } } );
  70. Task.belongsTo( User, { constraints : false } );
  71. Task.belongsTo( User, { onDelete : 'cascade' } );
  72. Task.belongsTo( User, { onUpdate : 'restrict' } );
  73. User.belongsTo( User, {
  74. as : 'parentBlocks',
  75. foreignKey : 'child',
  76. foreignKeyConstraint : true
  77. } );
  78. User.hasMany( User );
  79. User.hasMany( User, { foreignKey : 'primaryGroupId', as : 'primaryUsers' } );
  80. User.hasMany( Task, { foreignKey : 'userId' } );
  81. User.hasMany( Task, { foreignKey : 'userId', as : 'activeTasks', scope : { active : true } } );
  82. User.hasMany( Task, { foreignKey : 'userId', keyType : Sequelize.STRING, constraints : false } );
  83. User.hasMany( Task, { foreignKey : { name : 'uid', allowNull : false } } );
  84. User.hasMany( Task, { foreignKey : { allowNull : true } } );
  85. User.hasMany( Task, { as : 'Children' } );
  86. User.hasMany( Task, { as : { singular : 'task', plural : 'taskz' } } );
  87. User.hasMany( Task, { constraints : false } );
  88. User.hasMany( Task, { onDelete : 'cascade' } );
  89. User.hasMany( Task, { onUpdate : 'cascade' } );
  90. Post.hasMany( Task, { foreignKey : 'commentable_id', scope : { commentable : 'post' } } );
  91. User.hasMany( User, {
  92. as : 'childBlocks',
  93. foreignKey : 'parent',
  94. foreignKeyConstraint : true
  95. } );
  96. User.belongsToMany( Task, { through : 'UserTasks' } );
  97. User.belongsToMany( User, { through : Task } );
  98. User.belongsToMany( Group, { as : 'groups', through : Task, foreignKey : 'id_user' } );
  99. User.belongsToMany( Task, { as : 'activeTasks', through : Task, scope : { active : true } } );
  100. User.belongsToMany( Task, { as : 'startedTasks', through : { model : Task, scope : { started : true } } } );
  101. User.belongsToMany( Group, { through : 'group_members', foreignKey : 'group_id', otherKey : 'member_id' } );
  102. User.belongsToMany( User, { as : 'Participants', through : User } );
  103. User.belongsToMany( Group, { through : 'user_places', foreignKey : 'user_id' } );
  104. User.belongsToMany( Group, {
  105. through : 'user_projects',
  106. as : 'Projects',
  107. foreignKey : {
  108. field : 'user_id',
  109. name : 'userId'
  110. },
  111. otherKey : {
  112. field : 'project_id',
  113. name : 'projectId'
  114. }
  115. } );
  116. User.belongsToMany( Task, { onDelete : 'RESTRICT', through : 'tasksusers' } );
  117. User.belongsToMany( Task, { constraints : false, through : 'tasksusers' } );
  118. User.belongsToMany( Task, { foreignKey : { name : 'user_id', defaultValue : 42 }, through : 'UserProjects' } );
  119. User.belongsToMany( Post, { through : User } );
  120. Post.belongsToMany( User, { as : 'categories', through : User, scope : { type : 'category' } } );
  121. Post.belongsToMany( User, { as : 'tags', through : User, scope : { type : 'tag' } } );
  122. Post.belongsToMany( User, {
  123. through : {
  124. model : User,
  125. unique : false,
  126. scope : {
  127. taggable : 'post'
  128. }
  129. },
  130. foreignKey : 'taggable_id',
  131. constraints : false
  132. } );
  133. Post.belongsToMany( Post, { through : { model : Post, unique : false }, foreignKey : 'tag_id' } );
  134. Post.belongsToMany( Post, { as : 'Parents', through : 'Family', foreignKey : 'ChildId', otherKey : 'PersonId' } );
  135. //
  136. // Mixins
  137. // ~~~~~~
  138. //
  139. // https://github.com/sequelize/sequelize/tree/v3.4.1/test/integration/associations
  140. //
  141. var Product = s.define<ProductInstance, ProductAttributes>('product', {});
  142. var product = Product.build();
  143. var Barcode = s.define<BarcodeInstance, BarcodeAttributes>('barcode', {});
  144. var barcode = Barcode.build();
  145. var Warehouse = s.define<WarehouseInstance, WarehouseAttributes>('warehouse', {});
  146. var warehouse = Warehouse.build();
  147. var Branch = s.define<BranchInstance, BranchAttributes>('brach', {});
  148. var branch = Branch.build();
  149. var WarehouseBranch = s.define<WarehouseBranchInstance, WarehouseBranchAttributes>('warehouseBranch', {});
  150. var Customer = s.define<CustomerInstance, CustomerAttributes>('customer', {});
  151. var customer = Customer.build();
  152. Product.hasOne(Barcode);
  153. Barcode.belongsTo(Product);
  154. Warehouse.hasMany(Product);
  155. Product.belongsTo(Warehouse);
  156. Warehouse.belongsToMany(Branch, { through: WarehouseBranch });
  157. Branch.belongsToMany(Warehouse, { through: WarehouseBranch });
  158. Branch.belongsToMany(Customer, { through: 'branchCustomer' });
  159. Customer.belongsToMany(Branch, { through: 'branchCustomer' });
  160. // hasOne
  161. product.getBarcode();
  162. product.getBarcode({ scope: null }).then(b => b.code);
  163. product.setBarcode();
  164. product.setBarcode(1);
  165. product.setBarcode(barcode);
  166. product.setBarcode(barcode, { save: true }).then(() => { });
  167. product.createBarcode();
  168. product.createBarcode({ id: 1, code: '1434-2' });
  169. product.createBarcode({ id: 1 }, { save: true, silent: true }).then(() => { });
  170. // belongsTo
  171. barcode.getProduct();
  172. barcode.getProduct({ scope: 'foo' }).then(p => p.name);
  173. barcode.setProduct();
  174. barcode.setProduct(1);
  175. barcode.setProduct(product);
  176. barcode.setProduct(product, { save: true }).then(() => { });
  177. barcode.createProduct();
  178. barcode.createProduct({ id: 1, name: 'Crowbar' });
  179. barcode.createProduct({ id: 1 }, { save: true, silent: true }).then(() => { });
  180. product.getWarehouse();
  181. product.getWarehouse({ scope: null }).then(w => w.capacity);
  182. product.setWarehouse();
  183. product.setWarehouse(1);
  184. product.setWarehouse(warehouse);
  185. product.setWarehouse(warehouse, { save: true }).then(() => { });
  186. product.createWarehouse();
  187. product.createWarehouse({ id: 1, capacity: 10000 });
  188. product.createWarehouse({ id: 1 }, { save: true, silent: true }).then(() => { });
  189. // hasMany
  190. warehouse.getProducts();
  191. warehouse.getProducts({ where: {}, scope: false });
  192. warehouse.getProducts({ where: {}, scope: false }).then((products) => products[0].id);
  193. warehouse.setProducts();
  194. warehouse.setProducts([product]);
  195. warehouse.setProducts([product], { validate: true }).then(() => { });
  196. warehouse.addProducts();
  197. warehouse.addProducts([product]);
  198. warehouse.addProducts([product, 2], { validate: false }).then(() => { });
  199. warehouse.addProduct();
  200. warehouse.addProduct(product);
  201. warehouse.addProduct(2, { validate: true }).then(() => { });
  202. warehouse.createProduct();
  203. warehouse.createProduct({ id: 1, name: 'baz' });
  204. warehouse.createProduct({ id: 1 }, { silent: true }).then(() => { });
  205. warehouse.removeProducts();
  206. warehouse.removeProducts([product]);
  207. warehouse.removeProducts([product, 2], { validate: false }).then(() => { });
  208. warehouse.removeProduct();
  209. warehouse.removeProduct(product);
  210. warehouse.removeProduct(2, { validate: true }).then(() => { });
  211. warehouse.hasProducts([product]);
  212. warehouse.hasProducts([product, 2], { scope: 'bar' }).then((result: boolean) => { });
  213. warehouse.hasProduct(product);
  214. warehouse.hasProduct(2, { scope: 'baz' }).then((result: boolean) => { });
  215. warehouse.countProducts();
  216. warehouse.countProducts({ scope: 'baz' }).then((result: number) => { });
  217. // belongsToMany <Model>
  218. warehouse.getBranches();
  219. warehouse.getBranches({ where: {} });
  220. warehouse.getBranches({ where: {} }).then((branches) => branches[0].rank);
  221. warehouse.setBranches();
  222. warehouse.setBranches([branch]);
  223. warehouse.setBranches([branch, 2], { validate: true, distance: 1 }).then(() => { });
  224. warehouse.addBranches();
  225. warehouse.addBranches([branch]);
  226. warehouse.addBranches([branch, 2], { validate: false, distance: 1 }).then(() => { });
  227. warehouse.addBranch();
  228. warehouse.addBranch(branch);
  229. warehouse.addBranch(2, { validate: true, distance: 1 }).then(() => { });
  230. warehouse.createBranch();
  231. warehouse.createBranch({ id: 1, address: 'baz' });
  232. warehouse.createBranch({ id: 1 }, { silent: true, distance: 1 }).then(() => { });
  233. warehouse.removeBranches();
  234. warehouse.removeBranches([branch]);
  235. warehouse.removeBranches([branch, 2], { validate: false }).then(() => { });
  236. warehouse.removeBranch();
  237. warehouse.removeBranch(branch);
  238. warehouse.removeBranch(2, { validate: true }).then(() => { });
  239. warehouse.hasBranches([branch]);
  240. warehouse.hasBranches([branch, 2], { scope: 'bar' }).then((result: boolean) => { });
  241. warehouse.hasBranch(branch);
  242. warehouse.hasBranch(2, { scope: 'baz' }).then((result: boolean) => { });
  243. warehouse.countBranches();
  244. warehouse.countBranches({ scope: 'baz' }).then((result: number) => { });
  245. // belongsToMany <void>
  246. customer.getBranches();
  247. customer.getBranches({ where: {} });
  248. customer.getBranches({ where: {} }).then((branches) => branches[0].rank);
  249. customer.setBranches();
  250. customer.setBranches([branch]);
  251. customer.setBranches([branch, 2], { validate: true }).then(() => { });
  252. customer.addBranches();
  253. customer.addBranches([branch]);
  254. customer.addBranches([branch, 2], { validate: false }).then(() => { });
  255. customer.addBranch();
  256. customer.addBranch(branch);
  257. customer.addBranch(2, { validate: true }).then(() => { });
  258. customer.createBranch();
  259. customer.createBranch({ id: 1, address: 'baz' });
  260. customer.createBranch({ id: 1 }, { silent: true }).then(() => { });
  261. customer.removeBranches();
  262. customer.removeBranches([branch]);
  263. customer.removeBranches([branch, 2], { validate: false }).then(() => { });
  264. customer.removeBranch();
  265. customer.removeBranch(branch);
  266. customer.removeBranch(2, { validate: true }).then(() => { });
  267. customer.hasBranches([branch]);
  268. customer.hasBranches([branch, 2], { scope: 'bar' }).then((result: boolean) => { });
  269. customer.hasBranch(branch);
  270. customer.hasBranch(2, { scope: 'baz' }).then((result: boolean) => { });
  271. customer.countBranches();
  272. customer.countBranches({ scope: 'baz' }).then((result: number) => { });
  273. interface ProductAttributes {
  274. id?: number;
  275. name?: string;
  276. price?: number;
  277. };
  278. interface ProductInstance extends Sequelize.Instance<ProductAttributes>, ProductAttributes {
  279. // hasOne association mixins:
  280. getBarcode: Sequelize.HasOneGetAssociationMixin<BarcodeInstance>;
  281. setBarcode: Sequelize.HasOneSetAssociationMixin<BarcodeInstance, number>;
  282. createBarcode: Sequelize.HasOneCreateAssociationMixin<BarcodeAttributes>;
  283. // belongsTo association mixins:
  284. getWarehouse: Sequelize.BelongsToGetAssociationMixin<WarehouseInstance>;
  285. setWarehouse: Sequelize.BelongsToSetAssociationMixin<WarehouseInstance, number>;
  286. createWarehouse: Sequelize.BelongsToCreateAssociationMixin<WarehouseAttributes>;
  287. };
  288. interface BarcodeAttributes {
  289. id?: number;
  290. code?: string;
  291. dateIssued?: Date;
  292. };
  293. interface BarcodeInstance extends Sequelize.Instance<BarcodeAttributes>, BarcodeAttributes {
  294. // belongsTo association mixins:
  295. getProduct: Sequelize.BelongsToGetAssociationMixin<ProductInstance>;
  296. setProduct: Sequelize.BelongsToSetAssociationMixin<ProductInstance, number>;
  297. createProduct: Sequelize.BelongsToCreateAssociationMixin<ProductAttributes>;
  298. };
  299. interface WarehouseAttributes {
  300. id?: number;
  301. address?: string;
  302. capacity?: number;
  303. };
  304. interface WarehouseInstance extends Sequelize.Instance<WarehouseAttributes>, WarehouseAttributes {
  305. // hasMany association mixins:
  306. getProducts: Sequelize.HasManyGetAssociationsMixin<ProductInstance>;
  307. setProducts: Sequelize.HasManySetAssociationsMixin<ProductInstance, number>;
  308. addProducts: Sequelize.HasManyAddAssociationsMixin<ProductInstance, number>;
  309. addProduct: Sequelize.HasManyAddAssociationMixin<ProductInstance, number>;
  310. createProduct: Sequelize.HasManyCreateAssociationMixin<ProductAttributes>;
  311. removeProduct: Sequelize.HasManyRemoveAssociationMixin<ProductInstance, number>;
  312. removeProducts: Sequelize.HasManyRemoveAssociationsMixin<ProductInstance, number>;
  313. hasProduct: Sequelize.HasManyHasAssociationMixin<ProductInstance, number>;
  314. hasProducts: Sequelize.HasManyHasAssociationsMixin<ProductInstance, number>;
  315. countProducts: Sequelize.HasManyCountAssociationsMixin;
  316. // belongsToMany association mixins:
  317. getBranches: Sequelize.BelongsToManyGetAssociationsMixin<BranchInstance>;
  318. setBranches: Sequelize.BelongsToManySetAssociationsMixin<BranchInstance, number, WarehouseBranchAttributes>;
  319. addBranches: Sequelize.BelongsToManyAddAssociationsMixin<BranchInstance, number, WarehouseBranchAttributes>;
  320. addBranch: Sequelize.BelongsToManyAddAssociationMixin<BranchInstance, number, WarehouseBranchAttributes>;
  321. createBranch: Sequelize.BelongsToManyCreateAssociationMixin<BranchAttributes, WarehouseBranchAttributes>;
  322. removeBranch: Sequelize.BelongsToManyRemoveAssociationMixin<BranchInstance, number>;
  323. removeBranches: Sequelize.BelongsToManyRemoveAssociationsMixin<BranchInstance, number>;
  324. hasBranch: Sequelize.BelongsToManyHasAssociationMixin<BranchInstance, number>;
  325. hasBranches: Sequelize.BelongsToManyHasAssociationsMixin<BranchInstance, number>;
  326. countBranches: Sequelize.BelongsToManyCountAssociationsMixin;
  327. };
  328. interface BranchAttributes {
  329. id?: number;
  330. address?: string;
  331. rank?: number;
  332. };
  333. interface BranchInstance extends Sequelize.Instance<BranchAttributes>, BranchAttributes {
  334. // belongsToMany association mixins:
  335. getWarehouses: Sequelize.BelongsToManyGetAssociationsMixin<WarehouseInstance>;
  336. setWarehouses: Sequelize.BelongsToManySetAssociationsMixin<WarehouseInstance, number, WarehouseBranchAttributes>;
  337. addWarehouses: Sequelize.BelongsToManyAddAssociationsMixin<WarehouseInstance, number, WarehouseBranchAttributes>;
  338. addWarehouse: Sequelize.BelongsToManyAddAssociationMixin<WarehouseInstance, number, WarehouseBranchAttributes>;
  339. createWarehouse: Sequelize.BelongsToManyCreateAssociationMixin<WarehouseAttributes, WarehouseBranchAttributes>;
  340. removeWarehouse: Sequelize.BelongsToManyRemoveAssociationMixin<WarehouseInstance, number>;
  341. removeWarehouses: Sequelize.BelongsToManyRemoveAssociationsMixin<WarehouseInstance, number>;
  342. hasWarehouse: Sequelize.BelongsToManyHasAssociationMixin<WarehouseInstance, number>;
  343. hasWarehouses: Sequelize.BelongsToManyHasAssociationsMixin<WarehouseInstance, number>;
  344. countWarehouses: Sequelize.BelongsToManyCountAssociationsMixin;
  345. // belongsToMany association mixins:
  346. getCustomers: Sequelize.BelongsToManyGetAssociationsMixin<CustomerInstance>;
  347. setCustomers: Sequelize.BelongsToManySetAssociationsMixin<CustomerInstance, number, void>;
  348. addCustomers: Sequelize.BelongsToManyAddAssociationsMixin<CustomerInstance, number, void>;
  349. addCustomer: Sequelize.BelongsToManyAddAssociationMixin<CustomerInstance, number, void>;
  350. createCustomer: Sequelize.BelongsToManyCreateAssociationMixin<CustomerAttributes, void>;
  351. removeCustomer: Sequelize.BelongsToManyRemoveAssociationMixin<CustomerInstance, number>;
  352. removeCustomers: Sequelize.BelongsToManyRemoveAssociationsMixin<CustomerInstance, number>;
  353. hasCustomer: Sequelize.BelongsToManyHasAssociationMixin<CustomerInstance, number>;
  354. hasCustomers: Sequelize.BelongsToManyHasAssociationsMixin<CustomerInstance, number>;
  355. countCustomers: Sequelize.BelongsToManyCountAssociationsMixin;
  356. };
  357. interface WarehouseBranchAttributes {
  358. distance?: number;
  359. };
  360. interface WarehouseBranchInstance extends Sequelize.Instance<WarehouseBranchAttributes>, WarehouseBranchAttributes { };
  361. interface CustomerAttributes {
  362. id?: number;
  363. fullname?: string;
  364. credit?: number;
  365. };
  366. interface CustomerInstance extends Sequelize.Instance<CustomerAttributes>, CustomerAttributes {
  367. // belongsToMany association mixins:
  368. getBranches: Sequelize.BelongsToManyGetAssociationsMixin<BranchInstance>;
  369. setBranches: Sequelize.BelongsToManySetAssociationsMixin<BranchInstance, number, void>;
  370. addBranches: Sequelize.BelongsToManyAddAssociationsMixin<BranchInstance, number, void>;
  371. addBranch: Sequelize.BelongsToManyAddAssociationMixin<BranchInstance, number, void>;
  372. createBranch: Sequelize.BelongsToManyCreateAssociationMixin<BranchAttributes, void>;
  373. removeBranch: Sequelize.BelongsToManyRemoveAssociationMixin<BranchInstance, number>;
  374. removeBranches: Sequelize.BelongsToManyRemoveAssociationsMixin<BranchInstance, number>;
  375. hasBranch: Sequelize.BelongsToManyHasAssociationMixin<BranchInstance, number>;
  376. hasBranches: Sequelize.BelongsToManyHasAssociationsMixin<BranchInstance, number>;
  377. countBranches: Sequelize.BelongsToManyCountAssociationsMixin;
  378. };
  379. //
  380. // DataTypes
  381. // ~~~~~~~~~~~
  382. //
  383. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/unit/sql/data-types.test.js
  384. //
  385. Sequelize.STRING;
  386. Sequelize.STRING( 1234 );
  387. Sequelize.STRING( { length : 1234 } );
  388. Sequelize.STRING( 1234 ).BINARY;
  389. Sequelize.STRING.BINARY;
  390. Sequelize.TEXT;
  391. Sequelize.TEXT( 'tiny' );
  392. Sequelize.TEXT( { length : 'tiny' } );
  393. Sequelize.TEXT( 'medium' );
  394. Sequelize.TEXT( 'long' );
  395. Sequelize.CHAR;
  396. Sequelize.CHAR( 12 );
  397. Sequelize.CHAR( { length : 12 } );
  398. Sequelize.CHAR( 12 ).BINARY;
  399. Sequelize.CHAR.BINARY;
  400. Sequelize.BOOLEAN;
  401. Sequelize.DATE;
  402. Sequelize.DATE(6);
  403. Sequelize.UUID;
  404. Sequelize.UUIDV1;
  405. Sequelize.UUIDV4;
  406. Sequelize.NOW;
  407. Sequelize.INTEGER;
  408. Sequelize.INTEGER.UNSIGNED;
  409. Sequelize.INTEGER.UNSIGNED.ZEROFILL;
  410. Sequelize.INTEGER( 11 );
  411. Sequelize.INTEGER( { length : 11 } );
  412. Sequelize.INTEGER( 11 ).UNSIGNED;
  413. Sequelize.INTEGER( 11 ).UNSIGNED.ZEROFILL;
  414. Sequelize.INTEGER( 11 ).ZEROFILL;
  415. Sequelize.INTEGER( 11 ).ZEROFILL.UNSIGNED;
  416. Sequelize.BIGINT;
  417. Sequelize.BIGINT.UNSIGNED;
  418. Sequelize.BIGINT.UNSIGNED.ZEROFILL;
  419. Sequelize.BIGINT( 11 );
  420. Sequelize.BIGINT( { length : 11 } );
  421. Sequelize.BIGINT( 11 ).UNSIGNED;
  422. Sequelize.BIGINT( 11 ).UNSIGNED.ZEROFILL;
  423. Sequelize.BIGINT( 11 ).ZEROFILL;
  424. Sequelize.BIGINT( 11 ).ZEROFILL.UNSIGNED;
  425. Sequelize.REAL.UNSIGNED;
  426. Sequelize.REAL( 11 );
  427. Sequelize.REAL( { length : 11 } );
  428. Sequelize.REAL( 11 ).UNSIGNED;
  429. Sequelize.REAL( 11 ).UNSIGNED.ZEROFILL;
  430. Sequelize.REAL( 11 ).ZEROFILL;
  431. Sequelize.REAL( 11 ).ZEROFILL.UNSIGNED;
  432. Sequelize.REAL( 11, 12 );
  433. Sequelize.REAL( 11, 12 ).UNSIGNED;
  434. Sequelize.REAL( { length : 11, decimals : 12 } ).UNSIGNED;
  435. Sequelize.REAL( 11, 12 ).UNSIGNED.ZEROFILL;
  436. Sequelize.REAL( 11, 12 ).ZEROFILL;
  437. Sequelize.REAL( 11, 12 ).ZEROFILL.UNSIGNED;
  438. Sequelize.DOUBLE;
  439. Sequelize.DOUBLE.UNSIGNED;
  440. Sequelize.DOUBLE( 11 );
  441. Sequelize.DOUBLE( 11 ).UNSIGNED;
  442. Sequelize.DOUBLE( { length : 11 } ).UNSIGNED;
  443. Sequelize.DOUBLE( 11 ).UNSIGNED.ZEROFILL;
  444. Sequelize.DOUBLE( 11 ).ZEROFILL;
  445. Sequelize.DOUBLE( 11 ).ZEROFILL.UNSIGNED;
  446. Sequelize.DOUBLE( 11, 12 );
  447. Sequelize.DOUBLE( 11, 12 ).UNSIGNED;
  448. Sequelize.DOUBLE( 11, 12 ).UNSIGNED.ZEROFILL;
  449. Sequelize.DOUBLE( 11, 12 ).ZEROFILL;
  450. Sequelize.DOUBLE( 11, 12 ).ZEROFILL.UNSIGNED;
  451. Sequelize.FLOAT;
  452. Sequelize.FLOAT.UNSIGNED;
  453. Sequelize.FLOAT( 11 );
  454. Sequelize.FLOAT( 11 ).UNSIGNED;
  455. Sequelize.FLOAT( 11 ).UNSIGNED.ZEROFILL;
  456. Sequelize.FLOAT( 11 ).ZEROFILL;
  457. Sequelize.FLOAT( { length : 11 } ).ZEROFILL;
  458. Sequelize.FLOAT( 11 ).ZEROFILL.UNSIGNED;
  459. Sequelize.FLOAT( 11, 12 );
  460. Sequelize.FLOAT( 11, 12 ).UNSIGNED;
  461. Sequelize.FLOAT( { length : 11, decimals : 12 } ).UNSIGNED;
  462. Sequelize.FLOAT( 11, 12 ).UNSIGNED.ZEROFILL;
  463. Sequelize.FLOAT( 11, 12 ).ZEROFILL;
  464. Sequelize.FLOAT( 11, 12 ).ZEROFILL.UNSIGNED;
  465. Sequelize.NUMERIC;
  466. Sequelize.NUMERIC( 15, 5 );
  467. Sequelize.DECIMAL;
  468. Sequelize.DECIMAL( 10, 2 );
  469. Sequelize.DECIMAL( { precision : 10, scale : 2 } );
  470. Sequelize.DECIMAL( 10 );
  471. Sequelize.DECIMAL( { precision : 10 } );
  472. Sequelize.ENUM( 'value 1', 'value 2' );
  473. Sequelize.BLOB;
  474. Sequelize.BLOB( 'tiny' );
  475. Sequelize.BLOB( 'medium' );
  476. Sequelize.BLOB( { length : 'medium' } );
  477. Sequelize.BLOB( 'long' );
  478. Sequelize.ARRAY( Sequelize.STRING );
  479. Sequelize.ARRAY( Sequelize.STRING( 100 ) );
  480. Sequelize.ARRAY( Sequelize.INTEGER );
  481. Sequelize.ARRAY( Sequelize.HSTORE );
  482. Sequelize.ARRAY( Sequelize.ARRAY( Sequelize.STRING ) );
  483. Sequelize.ARRAY( Sequelize.TEXT );
  484. Sequelize.ARRAY( Sequelize.DATE );
  485. Sequelize.ARRAY( Sequelize.BOOLEAN );
  486. Sequelize.ARRAY( Sequelize.DECIMAL );
  487. Sequelize.ARRAY( Sequelize.DECIMAL( 6 ) );
  488. Sequelize.ARRAY( Sequelize.DECIMAL( 6, 4 ) );
  489. Sequelize.ARRAY( Sequelize.DOUBLE );
  490. Sequelize.ARRAY( Sequelize.REAL );
  491. Sequelize.ARRAY( Sequelize.JSON );
  492. Sequelize.ARRAY( Sequelize.JSONB );
  493. Sequelize.GEOMETRY;
  494. Sequelize.GEOMETRY( 'POINT' );
  495. Sequelize.GEOMETRY( 'LINESTRING' );
  496. Sequelize.GEOMETRY( 'POLYGON' );
  497. Sequelize.GEOMETRY( 'POINT', 4326 );
  498. Sequelize.VIRTUAL;
  499. new Sequelize.VIRTUAL( Sequelize.STRING );
  500. new Sequelize.VIRTUAL( Sequelize.DATE , ['property1', 'property2']);
  501. //
  502. // Deferrable
  503. // ~~~~~~~~~~~~
  504. //
  505. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/sequelize/deferrable.test.js
  506. //
  507. Sequelize.Deferrable.NOT;
  508. Sequelize.Deferrable.INITIALLY_IMMEDIATE;
  509. Sequelize.Deferrable.INITIALLY_DEFERRED;
  510. Sequelize.Deferrable.SET_DEFERRED;
  511. Sequelize.Deferrable.SET_DEFERRED( ['taskTableName_user_id_fkey'] );
  512. Sequelize.Deferrable.SET_IMMEDIATE;
  513. Sequelize.Deferrable.SET_IMMEDIATE( ['taskTableName_user_id_fkey'] );
  514. //
  515. // Errors
  516. // ~~~~~~~~
  517. //
  518. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/error.test.js
  519. //
  520. Sequelize.Error;
  521. Sequelize.ValidationError;
  522. s.Error;
  523. s.ValidationError;
  524. new s.ValidationError( 'Validation Error', [
  525. new s.ValidationErrorItem( '<field name> cannot be null', 'notNull Violation', '<field name>', null )
  526. , new s.ValidationErrorItem( '<field name> cannot be an array or an object', 'string violation',
  527. '<field name>', null )
  528. ] );
  529. new s.Error();
  530. new s.ValidationError();
  531. new s.ValidationErrorItem( 'invalid', 'type', 'first_name', null );
  532. new s.ValidationErrorItem( 'invalid', 'type', 'last_name', null );
  533. new s.DatabaseError( new Error( 'original database error message' ) );
  534. new s.ConnectionError( new Error( 'original connection error message' ) );
  535. new s.ConnectionRefusedError( new Error( 'original connection error message' ) );
  536. new s.AccessDeniedError( new Error( 'original connection error message' ) );
  537. new s.HostNotFoundError( new Error( 'original connection error message' ) );
  538. new s.HostNotReachableError( new Error( 'original connection error message' ) );
  539. new s.InvalidConnectionError( new Error( 'original connection error message' ) );
  540. new s.ConnectionTimedOutError( new Error( 'original connection error message' ) );
  541. //
  542. // Hooks
  543. // ~~~~~~~
  544. //
  545. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/hooks.test.js
  546. //
  547. User.addHook( 'afterCreate', function( instance : Sequelize.Instance<any>, options : Object, next : Function ) { next(); } );
  548. User.addHook( 'afterCreate', 'myHook', function( instance : Sequelize.Instance<any>, options : Object, next : Function) { next(); } );
  549. s.addHook( 'beforeInit', function( config : Object, options : Object ) { } );
  550. User.hook( 'afterCreate', 'myHook', function( instance : Sequelize.Instance<any>, options : Object, next : Function) { next(); } );
  551. User.hook( 'afterCreate', 'myHook', function( instance : Sequelize.Instance<any>, options : Object, next : Function ) { next(); } );
  552. User.removeHook( 'afterCreate', 'myHook' );
  553. User.hasHook( 'afterCreate' );
  554. User.hasHooks( 'afterCreate' );
  555. User.beforeValidate( function( user, options ) { user.isNewRecord; } );
  556. User.beforeValidate( 'myHook', function( user, options ) { user.isNewRecord; } );
  557. User.afterValidate( function( user, options ) { user.isNewRecord; } );
  558. User.afterValidate( 'myHook', function( user, options ) { user.isNewRecord; } );
  559. User.beforeCreate( function( user, options ) { user.isNewRecord; } );
  560. User.beforeCreate( function( user, options, fn ) {fn();} );
  561. User.beforeCreate( 'myHook', function( user, options ) { user.isNewRecord; } );
  562. User.afterCreate( function( user, options ) { user.isNewRecord; } );
  563. User.afterCreate( function( user, options, fn ) {fn();} );
  564. User.afterCreate( 'myHook', function( user, options ) { user.isNewRecord; } );
  565. User.beforeDestroy( function( user, options ) {throw new Error( 'Whoops!' );} );
  566. User.beforeDestroy( function( user, options, fn ) {fn();} );
  567. User.beforeDestroy( 'myHook', function( user, options ) {throw new Error( 'Whoops!' );} );
  568. User.beforeDelete( function( user, options ) {throw new Error( 'Whoops!' );} );
  569. User.beforeDelete( 'myHook', function( user, options ) {throw new Error( 'Whoops!' );} );
  570. User.afterDestroy( function( user, options ) {throw new Error( 'Whoops!' );} );
  571. User.afterDestroy( 'myHook', function( user, options ) {throw new Error( 'Whoops!' );} );
  572. User.afterDestroy( function( user, options, fn ) {fn();} );
  573. User.afterDelete( function( user, options ) {throw new Error( 'Whoops!' );} );
  574. User.afterDelete( 'myHook', function( user, options ) {throw new Error( 'Whoops!' );} );
  575. User.beforeUpdate( function( user, options ) {throw new Error( 'Whoops!' ); } );
  576. User.beforeUpdate( 'myHook', function( user, options ) {throw new Error( 'Whoops!' ); } );
  577. User.afterUpdate( function( user, options ) {throw new Error( 'Whoops!' );} );
  578. User.afterUpdate( 'myHook', function( user, options ) {throw new Error( 'Whoops!' );} );
  579. User.beforeBulkCreate( function( daos, options ) { throw new Error( 'Whoops!' );} );
  580. User.beforeBulkCreate( 'myHook', function( daos, options ) { throw new Error( 'Whoops!' );} );
  581. User.beforeBulkCreate( function( daos, options, fn ) {fn();} );
  582. User.afterBulkCreate( function( daos, options ) {throw new Error( 'Whoops!' ); } );
  583. User.afterBulkCreate( 'myHook', function( daos, options ) {throw new Error( 'Whoops!' ); } );
  584. User.afterBulkCreate( function( daos, options, fn ) {fn();} );
  585. User.beforeBulkDestroy( function( options ) {throw new Error( 'Whoops!' );} );
  586. User.beforeBulkDestroy( function( options, fn ) {fn();} );
  587. User.beforeBulkDestroy( 'myHook', function( options, fn ) {fn();} );
  588. User.beforeBulkDelete( 'myHook', function( options, fn ) {fn();} );
  589. User.afterBulkDestroy( function( options ) {throw new Error( 'Whoops!' );} );
  590. User.afterBulkDestroy( function( options, fn ) {fn();} );
  591. User.afterBulkDestroy( 'myHook', function( options, fn ) {fn();} );
  592. User.afterBulkDelete( 'myHook', function( options, fn ) {fn();} );
  593. User.beforeBulkUpdate( function( options ) {throw new Error( 'Whoops!' );} );
  594. User.beforeBulkUpdate( 'myHook', function( options ) {throw new Error( 'Whoops!' );} );
  595. User.afterBulkUpdate( function( options ) {throw new Error( 'Whoops!' );} );
  596. User.afterBulkUpdate( 'myHook', function( options ) {throw new Error( 'Whoops!' );} );
  597. User.beforeFind( function( options ) {} );
  598. User.beforeFind( 'myHook', function( options ) {} );
  599. User.beforeFindAfterExpandIncludeAll( function( options ) {} );
  600. User.beforeFindAfterExpandIncludeAll( 'myHook', function( options ) {} );
  601. User.beforeFindAfterOptions( function( options ) {} );
  602. User.beforeFindAfterOptions( 'myHook', function( options ) {} );
  603. User.afterFind( function( user ) {} );
  604. User.afterFind( 'myHook', function( user ) {} );
  605. User.beforeSync( function( options ) {} );
  606. User.beforeSync( 'myHook', function( options ) {} );
  607. User.afterSync( function( options ) {} );
  608. User.afterSync( 'myHook', function( options ) {} );
  609. s.beforeDefine( function( attributes, options ) {} );
  610. s.beforeDefine( 'myHook', function( attributes, options ) {} );
  611. s.afterDefine( function( model ) {} );
  612. s.afterDefine( 'myHook', function( model ) {} );
  613. s.beforeInit( function( config, options ) {} );
  614. s.beforeInit( 'myHook', function( attributes, options ) {} );
  615. s.afterInit( function( model ) {} );
  616. s.afterInit( 'myHook', function( model ) {} );
  617. s.beforeBulkSync( function( options ) {} );
  618. s.beforeBulkSync( 'myHook', function( options ) {} );
  619. s.afterBulkSync( function( options ) {} );
  620. s.afterBulkSync( 'myHook', function( options ) {} );
  621. s.define( 'User', {}, {
  622. hooks : {
  623. beforeValidate : function( user, options, fn ) {fn();},
  624. afterValidate : function( user, options, fn ) {fn();},
  625. beforeCreate : function( user, options, fn ) {fn();},
  626. afterCreate : function( user, options, fn ) {fn();},
  627. beforeDestroy : function( user, options, fn ) {fn();},
  628. afterDestroy : function( user, options, fn ) {fn();},
  629. beforeDelete : function( user, options, fn ) {fn();},
  630. afterDelete : function( user, options, fn ) {fn();},
  631. beforeUpdate : function( user, options, fn ) {fn();},
  632. afterUpdate : function( user, options, fn ) {fn();}
  633. }
  634. } );
  635. //
  636. // Instance
  637. // ~~~~~~~~~~
  638. //
  639. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/instance.test.js
  640. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/instance/update.test.js
  641. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/instance/values.test.js
  642. //
  643. user.isNewRecord = true;
  644. user.Model.build( { a : 'b' } );
  645. user.sequelize.close();
  646. user.where();
  647. user.getDataValue( '' );
  648. user.setDataValue( '', '' );
  649. user.setDataValue( '', {} );
  650. user.get( 'aNumber', { plain : true, clone : true } );
  651. user.get();
  652. user.set( 'email', 'B' );
  653. user.set( { name : 'B', bio : 'B' } ).save().then( ( p ) => p );
  654. user.set( 'birthdate', new Date() );
  655. user.set( { id : 1, t : 'c', q : [{ id : 1, n : 'a' }, { id : 2, n : 'Beta' }], u : { id : 1, f : 'b', l : 'd' } } );
  656. user.setAttributes( { a : 3 } );
  657. user.setAttributes( { id : 1, a : 'n', c : [{ id : 1 }, { id : 2, f : 'e' }], x : { id : 1, f : 'h', l : 'd' } } );
  658. user.changed( 'name' );
  659. user.changed();
  660. user.previous( 'name' );
  661. user.save().then( ( p ) => p );
  662. user.save( { fields : ['a'] } ).then( ( p ) => p );
  663. user.save( { transaction : t } );
  664. user.reload();
  665. user.reload( { attributes : ['bNumber'] } );
  666. user.reload( { transaction : t } );
  667. user.validate();
  668. user.update( { bNumber : 2 }, { where : { id : 1 } } );
  669. user.update( { username : 'userman' }, { silent : true } );
  670. user.update( { username : 'yolo' }, { logging : function() { } } );
  671. user.update( { username : 'bar' }, { where : { username : 'foo' }, transaction : t } ).then( ( p ) => p );
  672. user.updateAttributes( { a : 3 } ).then( ( p ) => p );
  673. user.updateAttributes( { a : 3 }, { fields : ['secretValue'], logging : function( ) {} } );
  674. user.destroy().then( ( p ) => p );
  675. user.destroy( { logging : function( ) {} } );
  676. user.destroy( { transaction : t } ).then( ( p ) => p );
  677. user.restore();
  678. user.increment( 'number', { by : 2 } ).then( ( p ) => p );
  679. user.increment( ['aNumber'], { by : 2, where : { bNumber : 1 } } ).then( ( p ) => p );
  680. user.increment( ['aNumber'], { by : 2 } ).then( ( p ) => p );
  681. user.increment( 'aNumber' ).then( ( p ) => p );
  682. user.increment( { 'aNumber' : 1, 'bNumber' : 2 } ).then( ( p ) => p );
  683. user.increment( 'number', { by : 2, transaction : t } ).then( ( p ) => p );
  684. user.decrement( 'aNumber', { by : 2 } ).then( ( p ) => p );
  685. user.decrement( ['aNumber'], { by : 2 } ).then( ( p ) => p );
  686. user.decrement( 'aNumber' ).then( ( p ) => p );
  687. user.decrement( { 'aNumber' : 1, 'bNumber' : 2 } ).then( ( p ) => p );
  688. user.decrement( 'number', { by : 2, transaction : t } ).then( ( p ) => p );
  689. user.equals( user );
  690. user.equalsOneOf( [user, user] );
  691. user.toJSON();
  692. //
  693. // Model
  694. // ~~~~~~~
  695. //
  696. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/model.test.js
  697. //
  698. User.removeAttribute( 'id' );
  699. User.sync( { force : true } ).then( function() { } );
  700. User.sync( { force : true, logging : function() { } } );
  701. User.drop();
  702. User.schema( 'special' );
  703. User.schema( 'special' ).create( { age : 3 }, { logging : function( ) {} } );
  704. User.getTableName();
  705. User.addScope('lowAccess', { where : { parent_id : 2 } });
  706. User.addScope('lowAccess', function() { } );
  707. User.addScope('lowAccess', { where : { parent_id : 2 } }, { override: true });
  708. User.scope( 'lowAccess' ).count();
  709. User.scope( { where : { parent_id : 2 } } );
  710. User.findAll();
  711. User.findAll( { where : { data : { employment : null } } } );
  712. User.findAll( { where : { aNumber : { gte : 10 } } } ).then( ( u ) => u[0].isNewRecord );
  713. User.findAll( { where : [s.or( { u : 'b' }, { u : ';' } ), s.and( { id : [1, 2] } )], include : [{ model : User }] } );
  714. User.findAll( {
  715. where : [s.or( { a : 'b' }, { c : 'd' } ), s.and( { id : [1, 2, 3] },
  716. s.or( { deletedAt : null }, { deletedAt : { gt : new Date( 0 ) } } ) )]
  717. } );
  718. User.findAll( { paranoid : false, where : [' IS NOT NULL '], include : [{ model : User }] } );
  719. User.findAll( { transaction : t } );
  720. User.findAll( { where : { data : { name : { last : 's' }, employment : { $ne : 'a' } } }, order : [['id', 'ASC']] } );
  721. User.findAll( { where : { username : ['boo', 'boo2'] } } );
  722. User.findAll( { where : { username : { like : '%2' } } } );
  723. User.findAll( { where : { theDate : { '..' : ['2013-01-02', '2013-01-11'] } } } );
  724. User.findAll( { where : { intVal : { '!..' : [8, 10] } } } );
  725. User.findAll( { where : { theDate : { between : ['2013-01-02', '2013-01-11'] } } } );
  726. User.findAll( { where : { theDate : { between : ['2013-01-02', '2013-01-11'] }, intVal : 10 } } );
  727. User.findAll( { where : { theDate : { between : ['2012-12-10', '2013-01-02'] } } } );
  728. User.findAll( { where : { theDate : { nbetween : ['2013-01-04', '2013-01-20'] } } } );
  729. User.findAll( { order : [s.col( 'name' )] } );
  730. User.findAll( { order : [['theDate', 'DESC']] } );
  731. User.findAll( { include : [User], order : [[User, User, 'numYears', 'c']] } );
  732. User.findAll( { include : [{ model : User, include : [User, { model : User, as : 'residents' }] }] } );
  733. User.findAll( { order : [[User, { model : User, as : 'residents' }, 'lastName', 'c']] } );
  734. User.findAll( { include : [User], order : [[User, 'name', 'c']] } );
  735. User.findAll( { include : [{ all : 'HasMany', attributes : ['name'] }] } );
  736. User.findAll( { include : [{ all : true }, { model : User, attributes : ['id'] }] } );
  737. User.findAll( { include : [{ all : 'BelongsTo' }] } );
  738. User.findAll( { include : [{ all : true }] } );
  739. User.findAll( { where : { username : 'barfooz' }, raw : true } );
  740. User.findAll( { where : { name : 'worker' }, include : [{ model : User, as : 'ToDos' }] } );
  741. User.findAll( { where : { user_id : 1 }, attributes : ['a', 'b'], include : [{ model : User, attributes : ['c'] }] } );
  742. User.findAll( { order : s.literal( 'email =' ) } );
  743. User.findAll( { order : [s.literal( 'email = ' + s.escape( 'test@sequelizejs.com' ) )] } );
  744. User.findAll( { order : [['id', ';DELETE YOLO INJECTIONS']] } );
  745. User.findAll( { include : [User], order : [[User, 'id', ';DELETE YOLO INJECTIONS']] } );
  746. User.findAll( { include : [User], order : [['id', 'ASC NULLS LAST'], [User, 'id', 'DESC NULLS FIRST']] } );
  747. User.findAll( { include : [{ model : User, where : { title : 'DoDat' }, include : [{ model : User }] }] } );
  748. User.findAll( { attributes: ['username', 'data']});
  749. User.findAll( { attributes: {include: ['username', 'data']} });
  750. User.findAll( { attributes: [['username', 'user_name'], ['email', 'user_email']] });
  751. User.findAll( { attributes: [s.fn('count', Sequelize.col('*'))] });
  752. User.findAll( { attributes: [[s.fn('count', Sequelize.col('*')), 'count']] });
  753. User.findAll( { attributes: [[s.fn('count', Sequelize.col('*')), 'count']], group: ['sex'] });
  754. User.findAll( { attributes: [s.cast(s.fn('count', Sequelize.col('*')), 'INTEGER')] });
  755. User.findAll( { attributes: [[s.cast(s.fn('count', Sequelize.col('*')), 'INTEGER'), 'count']] });
  756. User.findById( 'a string' );
  757. User.findOne( { where : { username : 'foo' } } );
  758. User.findOne( { where : { id : 1 }, attributes : ['id', ['username', 'name']] } );
  759. User.findOne( { where : { id : 1 }, attributes : ['id'] } );
  760. User.findOne( { where : { username : 'foo' }, logging : function( ) { } } );
  761. User.findOne( { limit : 10 } );
  762. User.findOne( { include : [1] } );
  763. User.findOne( { where : { title : 'homework' }, include : [User] } );
  764. User.findOne( { where : { name : 'environment' }, include : [{ model : User, as : 'PrivateDomain' }] } );
  765. User.findOne( { where : { username : 'foo' }, transaction : t } ).then( ( p ) => p );
  766. User.findOne( { include : [User] } );
  767. User.findOne( { include : [{ model : User, as : 'Work' }] } );
  768. User.findOne( { where : { name : 'worker' }, include : [{ model : User, as : 'ToDo' }] } );
  769. User.findOne( { include : [{ model : User, as : 'ToDo' }, { model : User, as : 'DoTo' }] } );
  770. User.findOne( { where : { name : 'worker' }, include : [User] } );
  771. User.findOne( { where : { name : 'Boris' }, include : [User, { model : User, as : 'Photos' }] } );
  772. User.findOne( { where : { username : 'someone' }, include : [User] } );
  773. User.findOne( { where : { username : 'barfooz' }, raw : true } );
  774. /* NOTE https://github.com/DefinitelyTyped/DefinitelyTyped/pull/5590
  775. User.findOne( { updatedAt : { ne : null } } );
  776. */
  777. User.find( { where : { intVal : { gt : 5 } } } );
  778. User.find( { where : { intVal : { lte : 5 } } } );
  779. User.count();
  780. User.count( { transaction : t } );
  781. User.count().then( function( c ) { c.toFixed(); } );
  782. User.count( { where : ["username LIKE '%us%'"] } );
  783. User.count( { include : [{ model : User, required : false }] } );
  784. User.count( { distinct : true, include : [{ model : User, required : false }] } );
  785. User.count( { attributes : ['data'], group : ['data'] } );
  786. User.count( { where : { access_level : { gt : 5 } } } );
  787. User.findAndCountAll( { offset : 5, limit : 1, include : [User, { model : User, as : 'a' }] } );
  788. User.max( 'age', { transaction : t } );
  789. User.max( 'age' );
  790. User.max( 'age', { logging : function( ) { } } );
  791. User.min( 'age', { transaction : t } );
  792. User.min( 'age' );
  793. User.min( 'age', { logging : function( ) { } } );
  794. User.sum( 'order' );
  795. User.sum( 'age', { where : { 'gender' : 'male' } } );
  796. User.sum( 'age', { logging : function( ) { } } );
  797. User.build( { username : 'John Wayne' } ).save();
  798. User.build();
  799. User.build( { id : 1, T : [{ n : 'a' }, { id : 2 }], A : { id : 1, n : 'a', c : 'a' } }, { include : [User, Task] } );
  800. User.build( { id : 1, }, { include : [{ model : User, as : 'followers' }, { model : Task, as : 'categories' }] } );
  801. User.create();
  802. User.create( { createdAt : 1, updatedAt : 2 }, { silent : true } );
  803. User.create( {}, { returning : true } );
  804. User.create( { intVal : s.literal( 'CAST(1-2 AS' ) } );
  805. User.create( { secretValue : s.fn( 'upper', 'sequelize' ) } );
  806. User.create( { myvals : [1, 2, 3, 4], mystr : ['One', 'Two', 'Three', 'Four'] } );
  807. User.create( { name : 'Fluffy Bunny', smth : 'else' }, { logging : function( ) {} } );
  808. User.create( {}, { fields : [] } );
  809. User.create( { name : 'Yolo Bear', email : 'yolo@bear.com' }, { fields : ['name'] } );
  810. User.create( { title : 'Chair', User : { first_name : 'Mick', last_name : 'Broadstone' } }, { include : [User] } );
  811. User.create( { title : 'Chair', creator : { first_name : 'Matt', last_name : 'Hansen' } }, { include : [User] } );
  812. User.create( { id : 1, title : 'e', Tags : [{ id : 1, name : 'c' }, { id : 2, name : 'd' }] }, { include : [User] } );
  813. User.create( { id : 'My own ID!' } ).then( ( i ) => i.isNewRecord );
  814. let findOrRetVal: Promise<[AnyInstance, boolean]>;
  815. findOrRetVal = User.findOrInitialize( { where : { username : 'foo' } } );
  816. findOrRetVal = User.findOrInitialize( { where : { username : 'foo' }, transaction : t } );
  817. findOrRetVal = User.findOrInitialize( { where : { username : 'foo' }, defaults : { foo : 'asd' }, transaction : t } );
  818. findOrRetVal = User.findOrCreate( { where : { a : 'b' }, defaults : { json : { a : { b : 'c' }, d : [1, 2, 3] } } } );
  819. findOrRetVal = User.findOrCreate( { where : { a : 'b' }, defaults : { json : 'a', data : 'b' } } );
  820. /* NOTE https://github.com/DefinitelyTyped/DefinitelyTyped/pull/5590
  821. User.findOrCreate( { where : { a : 'b' }, transaction : t, lock : t.LOCK.UPDATE } );
  822. */
  823. findOrRetVal = User.findOrCreate( { where : { a : 'b' }, logging : function( ) { } } );
  824. findOrRetVal = User.findOrCreate( { where : { username : 'Username' }, defaults : { data : 'some data' }, transaction : t } );
  825. findOrRetVal = User.findOrCreate( { where : { objectId : 'asdasdasd' }, defaults : { username : 'gottlieb' } } );
  826. findOrRetVal = User.findOrCreate( { where : { id : undefined }, defaults : { name : Math.random().toString() } } );
  827. findOrRetVal = User.findOrCreate( { where : { email : 'unique.email.@d.com', companyId : Math.floor( Math.random() * 5 ) } } );
  828. findOrRetVal = User.findOrCreate( { where : { objectId : 1 }, defaults : { bool : false } } );
  829. findOrRetVal = User.findOrCreate( { where : 'c', defaults : {} } );
  830. User.upsert( { id : 42, username : 'doe', foo : s.fn( 'upper', 'mixedCase2' ) } );
  831. User.bulkCreate( [{ aNumber : 10 }, { aNumber : 12 }] ).then( ( i ) => i[0].isNewRecord );
  832. User.bulkCreate( [{ username : 'bar' }, { username : 'bar' }, { username : 'bar' }] );
  833. User.bulkCreate( [{}, {}], { validate : true, individualHooks : true } );
  834. User.bulkCreate( [{ style : 'ipa' }], { logging : function() { } } );
  835. User.bulkCreate( [{ a : 'b', c : 'd', e : 'f' }, { a : 'b', c : 'd', e : 'f' }], { fields : ['a', 'b'] } );
  836. User.bulkCreate( [{ name : 'foo', code : '123' }, { code : 'c' }, { name : 'bar', code : '1' }], { validate : true } );
  837. User.bulkCreate( [{ name : 'foo', code : '123' }, { code : '1234' }], { fields : ['code'], validate : true } );
  838. User.bulkCreate( [{ name : 'a', c : 'b' }, { name : 'e', c : 'f' }], { fields : ['e', 'f'], ignoreDuplicates : true } );
  839. User.truncate();
  840. User.destroy( { where : { client_id : 13 } } ).then( ( a ) => a.toFixed() );
  841. User.destroy( { force : true } );
  842. User.destroy( { where : {}, transaction : t } );
  843. User.destroy( { where : { access_level : { lt : 5 } } } );
  844. User.destroy( { truncate : true } );
  845. User.destroy( { where : {} } );
  846. User.restore( { where : { secretValue : '42' } } );
  847. User.update( { username : 'ruben' }, { where : {} } );
  848. User.update( { username : 'ruben' }, { where : { access_level : { lt : 5 } } } );
  849. User.update( { username : 'ruben' }, { where : { username : 'dan' } } );
  850. User.update( { username : 'bar' }, { where : { username : 'foo' }, transaction : t } );
  851. User.update( { username : 'Bill', secretValue : '43' }, { where : { secretValue : '42' }, fields : ['username'] } );
  852. User.update( { username : s.cast( '1', 'char' ) }, { where : { username : 'John' } } );
  853. User.update( { username : s.fn( 'upper', s.col( 'username' ) ) }, { where : { username : 'John' } } );
  854. User.update( { username : 'Bill' }, { where : { secretValue : '42' }, returning : true } );
  855. User.update( { secretValue : '43' }, { where : { username : 'Peter' }, limit : 1 } );
  856. User.update( { name : Math.random().toString() }, { where : { id : '1' } } );
  857. User.update( { a : { b : 10, c : 'd' } }, { where : { username : 'Jan' }, sideEffects : false } );
  858. User.update( { geometry : { type : 'Point', coordinates : [49.807222, -86.984722] } }, {
  859. where : {
  860. u : {
  861. u : 'u',
  862. geometry : { type : 'Point', coordinates : [49.807222, -86.984722] }
  863. }
  864. }
  865. } );
  866. User.update( {
  867. geometry : {
  868. type : 'Polygon',
  869. coordinates : [[[100.0, 0.0], [102.0, 0.0], [102.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
  870. }
  871. }, {
  872. where : {
  873. username : {
  874. username : 'username',
  875. geometry : { type : 'Point', coordinates : [49.807222, -86.984722] }
  876. }
  877. }
  878. } );
  879. User.unscoped().find( { where : { username : 'bob' } } );
  880. User.unscoped().count();
  881. //
  882. // Query Interface
  883. // ~~~~~~~~~~~~~~~~~
  884. //
  885. // https://github.com/sequelize/sequelize/blob/v3.4.1/test/integration/query-interface.test.js
  886. //
  887. var queryInterface = s.getQueryInterface();
  888. queryInterface.dropAllTables();
  889. queryInterface.showAllTables( { logging : function() { } } );
  890. queryInterface.createTable( 'table', { name : Sequelize.STRING }, { logging : function() { } } );
  891. queryInterface.createTable( 'skipme', { name : Sequelize.STRING } );
  892. /* NOTE https://github.com/DefinitelyTyped/DefinitelyTyped/pull/5590
  893. queryInterface.dropAllTables( { skip : ['skipme'] } );
  894. */
  895. queryInterface.dropTable( 'Group', { logging : function() { } } );
  896. queryInterface.addIndex( 'Group', ['username', 'isAdmin'], { logging : function() { } } );
  897. queryInterface.showIndex( 'Group', { logging : function() { } } );
  898. queryInterface.removeIndex( 'Group', ['username', 'isAdmin'], { logging : function() { } } );
  899. queryInterface.showIndex( 'Group' );
  900. /* NOTE https://github.com/DefinitelyTyped/DefinitelyTyped/pull/5590
  901. queryInterface.createTable( 'table', { name : { type : Sequelize.STRING } }, { schema : 'schema' } );
  902. */
  903. queryInterface.addIndex( { schema : 'a', tableName : 'c' }, ['d', 'e'], { logging : function() {} }, 'schema_table' );
  904. queryInterface.showIndex( { schema : 'schema', tableName : 'table' }, { logging : function() {} } );
  905. queryInterface.addIndex( 'Group', ['from'] );
  906. queryInterface.describeTable( '_Users', { logging : function() {} } );
  907. queryInterface.createTable( 's', { table_id : { type : Sequelize.INTEGER, primaryKey : true, autoIncrement : true } } );
  908. /* NOTE https://github.com/DefinitelyTyped/DefinitelyTyped/pull/5590
  909. queryInterface.insert( null, 'TableWithPK', {}, { raw : true, returning : true, plain : true } );
  910. */
  911. queryInterface.createTable( 'SomeTable', { someEnum : Sequelize.ENUM( 'value1', 'value2', 'value3' ) } );
  912. queryInterface.createTable( 'SomeTable', { someEnum : { type : Sequelize.ENUM, values : ['b1', 'b2', 'b3'] } } );
  913. queryInterface.createTable( 't', { someEnum : { type : Sequelize.ENUM, values : ['c1', 'c2', 'c3'], field : 'd' } } );
  914. /* NOTE https://github.com/DefinitelyTyped/DefinitelyTyped/pull/5590
  915. queryInterface.createTable( 'User', { name : { type : Sequelize.STRING } }, { schema : 'hero' } );
  916. queryInterface.rawSelect( 'User', { schema : 'hero', logging : function() {} }, 'name' );
  917. */
  918. queryInterface.renameColumn( '_Users', 'username', 'pseudo', { logging : function() {} } );
  919. queryInterface.renameColumn( { schema : 'archive', tableName : 'Users' }, 'username', 'pseudo' );
  920. queryInterface.renameColumn( '_Users', 'username', 'pseudo' );
  921. queryInterface.createTable( { tableName : 'y', schema : 'a' },
  922. { id : { type : Sequelize.INTEGER, primaryKey : true, autoIncrement : true }, currency : Sequelize.INTEGER } );
  923. queryInterface.changeColumn( { tableName : 'a', schema : 'b' }, 'c', { type : Sequelize.FLOAT },
  924. { logging : () => s } );
  925. queryInterface.createTable( 'users', { id : { type : Sequelize.INTEGER, primaryKey : true, autoIncrement : true } } );
  926. queryInterface.createTable( 'level', { id : { type : Sequelize.INTEGER, primaryKey : true, autoIncrement : true } } );
  927. queryInterface.addColumn( 'users', 'someEnum', Sequelize.ENUM( 'value1', 'value2', 'value3' ) );
  928. queryInterface.addColumn( 'users', 'so', { type : Sequelize.ENUM, values : ['value1', 'value2', 'value3'] } );
  929. queryInterface.createTable( 'hosts', {
  930. id : {
  931. type : Sequelize.INTEGER,
  932. primaryKey : true,
  933. autoIncrement : true
  934. },
  935. admin : {
  936. type : Sequelize.INTEGER,
  937. references : {
  938. model : User,
  939. key : 'id'
  940. }
  941. },
  942. operator : {
  943. type : Sequelize.INTEGER,
  944. references : {
  945. model : User,
  946. key : 'id'
  947. },
  948. onUpdate : 'cascade'
  949. },
  950. owner : {
  951. type : Sequelize.INTEGER,
  952. references : {
  953. model : User,
  954. key : 'id'
  955. },
  956. onUpdate : 'cascade',
  957. onDelete : 'set null'
  958. }
  959. } );
  960. //
  961. // Query Types
  962. // ~~~~~~~~~~~~~
  963. //
  964. s.getDialect();
  965. s.validate();
  966. s.authenticate();
  967. s.isDefined( '' );
  968. s.model( 'pp' );
  969. s.query( '', { raw : true } );
  970. s.query( '' );
  971. s.query( '' ).then( function( res ) {} );
  972. s.query( { query : 'select ? as foo, ? as bar', values : [1, 2] }, { raw : true, replacements : [1, 2] } );
  973. s.query( '', { raw : true, nest : false } );
  974. s.query( 'select ? as foo, ? as bar', { type : this.sequelize.QueryTypes.SELECT, replacements : [1, 2] } );
  975. s.query( { query : 'select ? as foo, ? as bar', values : [1, 2] }, { type : s.QueryTypes.SELECT } );
  976. s.query( 'select :one as foo, :two as bar', { raw : true, replacements : { one : 1, two : 2 } } );
  977. s.transaction().then( function( t ) { s.set( { foo : 'bar' }, { transaction : t } ); } );
  978. s.define( 'foo', { bar : Sequelize.STRING }, { collate : 'utf8_bin' } );
  979. s.define( 'Foto',

Large files files are truncated, but you can click here to view the full file