PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sequelize/sequelize-tests-3.0.0.ts

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

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