@typetron/framework 0.5.3 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/.eslintrc-old.js +208 -0
  2. package/Cache/Cache.ts +13 -0
  3. package/Cache/Stores/DatabaseStore.ts +120 -0
  4. package/Cache/Stores/FileStore.ts +69 -0
  5. package/Cache/Stores/MemoryStore.ts +71 -0
  6. package/Cache/index.ts +5 -0
  7. package/Cache/types.ts +4 -0
  8. package/Container/ClassResolver.ts +106 -0
  9. package/Container/Container.ts +144 -0
  10. package/Container/Contracts.ts +3 -0
  11. package/Container/Decorators.ts +20 -0
  12. package/Container/Metadata.ts +16 -0
  13. package/Container/Resolver.ts +27 -0
  14. package/Container/index.ts +5 -0
  15. package/Database/Clauses/Where.ts +0 -0
  16. package/Database/Connection.ts +52 -0
  17. package/Database/Decorators.ts +142 -0
  18. package/Database/Drivers/DatabaseDriver.ts +37 -0
  19. package/Database/Drivers/MySQL/Alter/index.ts +55 -0
  20. package/Database/Drivers/MySQL/ColumnDefinition.ts +16 -0
  21. package/Database/Drivers/MySQL/Create.ts +47 -0
  22. package/Database/Drivers/MySQL/MysqlDriver.ts +123 -0
  23. package/Database/Drivers/MySQL/Schema.ts +82 -0
  24. package/Database/Drivers/MySQL/index.ts +13 -0
  25. package/Database/Drivers/SQL/Schema.ts +73 -0
  26. package/Database/Drivers/SQL/Statements/Alter.ts +22 -0
  27. package/Database/Drivers/SQL/Statements/BaseStatement.ts +55 -0
  28. package/Database/Drivers/SQL/Statements/Delete.ts +20 -0
  29. package/Database/Drivers/SQL/Statements/Insert.ts +24 -0
  30. package/Database/Drivers/SQL/Statements/Select.ts +55 -0
  31. package/Database/Drivers/SQL/Statements/Update.ts +33 -0
  32. package/Database/Drivers/SQL/index.ts +8 -0
  33. package/Database/Drivers/SQLite/Create.ts +45 -0
  34. package/Database/Drivers/SQLite/Schema.ts +54 -0
  35. package/Database/Drivers/SQLite/SqliteDriver.ts +144 -0
  36. package/Database/Drivers/SQLite/index.ts +15 -0
  37. package/Database/Drivers/SchemaContract.ts +11 -0
  38. package/Database/Drivers/Statement.ts +16 -0
  39. package/Database/Drivers/index.ts +4 -0
  40. package/Database/Entity.ts +377 -0
  41. package/Database/EntityNotFoundError.ts +5 -0
  42. package/Database/EntityProxyHandler.ts +43 -0
  43. package/Database/EntityQuery.ts +197 -0
  44. package/Database/Expression.ts +3 -0
  45. package/Database/Fields.ts +816 -0
  46. package/Database/Helpers.ts +12 -0
  47. package/Database/List.ts +71 -0
  48. package/Database/Migrations/Migration.ts +10 -0
  49. package/Database/Migrations/MigrationHistory.ts +20 -0
  50. package/Database/Migrations/Migrator.ts +122 -0
  51. package/Database/Migrations/index.ts +3 -0
  52. package/Database/ORM/BaseRelationship.ts +10 -0
  53. package/Database/Query.ts +374 -0
  54. package/Database/Seeders/Seeder.ts +3 -0
  55. package/Database/Seeders/SeederManager.ts +27 -0
  56. package/Database/Seeders/index.ts +2 -0
  57. package/Database/StringExpression.ts +9 -0
  58. package/Database/Types.ts +165 -0
  59. package/Database/index.ts +23 -0
  60. package/Encryption/index.ts +51 -0
  61. package/Forms/Decorators.ts +29 -0
  62. package/Forms/Form.ts +70 -0
  63. package/Forms/FormFields.ts +36 -0
  64. package/Forms/index.ts +3 -0
  65. package/Framework/App.ts +10 -0
  66. package/Framework/Application.ts +118 -0
  67. package/Framework/Auth/Auth.ts +122 -0
  68. package/Framework/Auth/Authenticatable.ts +9 -0
  69. package/Framework/Auth/User.ts +27 -0
  70. package/Framework/Auth/index.ts +29 -0
  71. package/Framework/Config/AppConfig.ts +25 -0
  72. package/Framework/Config/AuthConfig.ts +9 -0
  73. package/Framework/Config/BaseConfig.ts +9 -0
  74. package/Framework/Config/CacheConfig.ts +38 -0
  75. package/Framework/Config/DatabaseConfig.ts +21 -0
  76. package/Framework/Config/MailConfig.ts +30 -0
  77. package/Framework/Config/index.ts +6 -0
  78. package/Framework/Middleware/AuthMiddleware.ts +31 -0
  79. package/Framework/Middleware/CorsMiddleware.ts +32 -0
  80. package/Framework/Middleware/StaticAssetsMiddleware.ts +76 -0
  81. package/Framework/Middleware/index.ts +2 -0
  82. package/Framework/Provider.ts +9 -0
  83. package/Framework/Providers/CacheProvider.ts +28 -0
  84. package/Framework/Providers/DatabaseProvider.ts +36 -0
  85. package/Framework/Providers/MailProvider.ts +31 -0
  86. package/Framework/Providers/WebsocketsProvider.ts +120 -0
  87. package/Framework/Resolvers/AuthResolver.ts +23 -0
  88. package/Framework/Resolvers/EntityResolver.ts +41 -0
  89. package/Framework/Resolvers/FormResolver.ts +37 -0
  90. package/Framework/RootDir.ts +1 -0
  91. package/Framework/index.ts +8 -0
  92. package/Frontend/Angular/index.ts +24 -0
  93. package/Frontend/Websockets/index.ts +86 -0
  94. package/Mail/MailAddress.ts +4 -0
  95. package/Mail/MailTransport.ts +6 -0
  96. package/Mail/Mailable.ts +16 -0
  97. package/Mail/Mailer.ts +27 -0
  98. package/Mail/PendingMail.ts +68 -0
  99. package/Mail/SentMessage.ts +20 -0
  100. package/Mail/Transports/MemoryTransport.ts +13 -0
  101. package/Mail/Transports/SendGridTransport.ts +95 -0
  102. package/Mail/index.ts +3 -0
  103. package/Models/Model.ts +41 -0
  104. package/Models/index.ts +54 -0
  105. package/Router/BaseController.ts +7 -0
  106. package/Router/Decorators.ts +224 -0
  107. package/Router/Guard.ts +10 -0
  108. package/Router/Http/Contracts.ts +4 -0
  109. package/Router/Http/ErrorHandler.ts +45 -0
  110. package/Router/Http/ErrorHandlerInterface.ts +6 -0
  111. package/Router/Http/Errors/HttpError.ts +15 -0
  112. package/Router/Http/Handler.ts +183 -0
  113. package/Router/Http/HttpRoute.ts +110 -0
  114. package/Router/Http/Middleware.ts +7 -0
  115. package/Router/Http/Request.ts +225 -0
  116. package/Router/Http/Response.ts +35 -0
  117. package/Router/Http/index.ts +85 -0
  118. package/Router/Metadata.ts +34 -0
  119. package/Router/Middleware.ts +16 -0
  120. package/Router/Request.ts +65 -0
  121. package/Router/Route.ts +16 -0
  122. package/Router/RouteNotFoundError.ts +5 -0
  123. package/Router/Router.ts +35 -0
  124. package/Router/Servers/index.ts +6 -0
  125. package/Router/Servers/node.ts +49 -0
  126. package/Router/Servers/uNetworking.ts +151 -0
  127. package/Router/Websockets/Handler.ts +82 -0
  128. package/Router/Websockets/Middleware.ts +7 -0
  129. package/Router/Websockets/Request.ts +3 -0
  130. package/Router/Websockets/WebsocketRoute.ts +61 -0
  131. package/Router/Websockets/index.ts +57 -0
  132. package/Router/Websockets/types.ts +23 -0
  133. package/Router/index.ts +6 -0
  134. package/Storage/File.ts +30 -0
  135. package/Storage/Image.ts +19 -0
  136. package/Storage/InvalidImage.ts +3 -0
  137. package/Storage/Storage.ts +98 -0
  138. package/Storage/index.ts +4 -0
  139. package/Storage/utils.ts +18 -0
  140. package/Support/Array.ts +184 -0
  141. package/Support/Math.ts +12 -0
  142. package/Support/Metadata.ts +31 -0
  143. package/Support/String.ts +38 -0
  144. package/Support/index.ts +19 -0
  145. package/Support/utils.ts +7 -0
  146. package/Testing/TestCase.ts +193 -0
  147. package/Validation/Rule.ts +9 -0
  148. package/Validation/RuleInterface.ts +10 -0
  149. package/Validation/Rules/Email.ts +20 -0
  150. package/Validation/Rules/FileExtension.ts +21 -0
  151. package/Validation/Rules/MaxLength.ts +17 -0
  152. package/Validation/Rules/MinLength.ts +18 -0
  153. package/Validation/Rules/Optional.ts +18 -0
  154. package/Validation/Rules/Required.ts +29 -0
  155. package/Validation/Validator.ts +7 -0
  156. package/Validation/index.ts +12 -0
  157. package/benchmark-index.js +20 -0
  158. package/benchmark.ts +47 -0
  159. package/package.json +7 -3
  160. package/tests/Cache/DatabaseStoreTest.ts +110 -0
  161. package/tests/Cache/FileStoreTest.ts +93 -0
  162. package/tests/Cache/MemoryStoreTest.ts +92 -0
  163. package/tests/Container/ContainerTest.ts +336 -0
  164. package/tests/Database/Drivers/MySQL/AlterTest.ts +100 -0
  165. package/tests/Database/Drivers/MySQL/CreateTest.ts +39 -0
  166. package/tests/Database/Drivers/MysqlDriverTest.ts +140 -0
  167. package/tests/Database/Drivers/SqliteDriverTest.ts +126 -0
  168. package/tests/Database/Entities/Article.ts +21 -0
  169. package/tests/Database/Entities/Profile.ts +21 -0
  170. package/tests/Database/Entities/Role.ts +15 -0
  171. package/tests/Database/Entities/User.ts +35 -0
  172. package/tests/Database/EntityRelationshipsTest.ts +475 -0
  173. package/tests/Database/EntityTest.ts +202 -0
  174. package/tests/Database/Migrations/RunnerTest.ts +96 -0
  175. package/tests/Database/Migrations/migrations/1.createUserTable.ts +12 -0
  176. package/tests/Database/Migrations/migrations/2.createArticlesTable.ts +12 -0
  177. package/tests/Database/QueryTest.ts +573 -0
  178. package/tests/Database/SchemaTest.ts +32 -0
  179. package/tests/Database/Seeders/RunnerTest.ts +30 -0
  180. package/tests/Database/Seeders/seeders/RandomSeeder.ts +10 -0
  181. package/tests/Database/utils.ts +16 -0
  182. package/tests/Forms/FormsTest.ts +59 -0
  183. package/tests/Framework/Middleware/StaticAssetsMiddlewareTest.ts +129 -0
  184. package/tests/Framework/Resolvers/EntityResolverTest.ts +48 -0
  185. package/tests/Framework/Router/RouterTest.ts +33 -0
  186. package/tests/Mail/MailerTest.ts +76 -0
  187. package/tests/Model/ModelTest.ts +183 -0
  188. package/tests/Router/HttpHandlerTest.ts +72 -0
  189. package/tests/Router/RequestTest.ts +77 -0
  190. package/tests/Router/WebSocketsHandlerTest.ts +51 -0
  191. package/tests/Storage/StorageTest.ts +97 -0
  192. package/tests/Support/ArrayTest.ts +167 -0
  193. package/tests/Support/StringTest.ts +18 -0
  194. package/tests/Validation/RulesTest.ts +54 -0
  195. package/types.d.ts +8 -0
@@ -0,0 +1,573 @@
1
+ import { expect } from 'chai'
2
+ import { suite, test } from '@testdeck/mocha'
3
+ import { Connection, Query } from '../../Database'
4
+ import { anyOfClass, instance, mock, when } from 'ts-mockito'
5
+ import { SqliteDriver } from '@Typetron/Database/Drivers'
6
+ import { expectQuery, trimSQL } from '@Typetron/tests/Database/utils'
7
+
8
+ @suite
9
+ class QueryTest {
10
+ connection: Connection
11
+ private query: Query<User>
12
+
13
+ before() {
14
+ this.connection = mock(Connection)
15
+ Query.connection = instance(this.connection)
16
+ Query.connection.driver = new SqliteDriver(':memory:')
17
+ when(this.connection.get(anyOfClass(Query))).thenCall((query: Query) => {
18
+ return query.toSQL()
19
+ })
20
+ this.query = new Query()
21
+ }
22
+
23
+ async expect() {
24
+ return expect(trimSQL(await this.query.toSQL()))
25
+ }
26
+
27
+ expectBindings() {
28
+ return expect(this.query.getBindings())
29
+ }
30
+
31
+ @test
32
+ async selectsEverything() {
33
+ expectQuery(Query.table('users')).toEqual('SELECT * FROM users')
34
+ }
35
+
36
+ @test
37
+ async selectSpecificColumns() {
38
+ expectQuery(
39
+ Query.table('users').select('name', 'email')
40
+ ).toEqual(
41
+ 'SELECT name, email FROM users'
42
+ )
43
+ }
44
+
45
+ @test
46
+ async addColumns() {
47
+ expectQuery(
48
+ Query.table('users').addSelect('name', 'email')
49
+ ).toEqual(
50
+ 'SELECT name, email FROM users'
51
+ )
52
+ }
53
+
54
+ @test
55
+ async selectDistinct() {
56
+ expectQuery(
57
+ Query.table('users').distinct().select('name', 'email')
58
+ ).toEqual(
59
+ 'SELECT DISTINCT name, email FROM users'
60
+ )
61
+ }
62
+
63
+ @test
64
+ async first() {
65
+ when(this.connection.first(anyOfClass(Query))).thenResolve({name: 'John'})
66
+ const user = await this.query.table('users').first('name');
67
+
68
+ (await this.expect()).to.equal(
69
+ 'SELECT name FROM users'
70
+ )
71
+ expect(user).to.deep.equal({name: 'John'})
72
+ }
73
+
74
+ @test
75
+ async where() {
76
+ const query = Query.table('users').where('name', '=', 'John')
77
+
78
+ expectQuery(
79
+ query
80
+ ).toEqual(
81
+ 'SELECT * FROM users WHERE name = ?'
82
+ )
83
+
84
+ expect(
85
+ query.getBindings()
86
+ ).to.deep.equal(
87
+ ['John']
88
+ )
89
+ }
90
+
91
+ @test
92
+ async whereWithDefaultOperator() {
93
+ const query = Query.table('users').where('name', 'John')
94
+
95
+ expectQuery(
96
+ query
97
+ ).toEqual(
98
+ 'SELECT * FROM users WHERE name = ?'
99
+ )
100
+ expect(
101
+ query.getBindings()
102
+ ).to.deep.equal(
103
+ ['John']
104
+ )
105
+ }
106
+
107
+ // @test
108
+ // async whereWithObject() {
109
+ // Query.table('users').where({name: 'John', age: 21});
110
+ //
111
+ // (await this.expectSql()).to.equal('SELECT * FROM users WHERE name = ? AND age = ?');
112
+ // this.expectBindings().to.deep.equal(['John', 21]);
113
+ // }
114
+
115
+ // @test
116
+ // async whereWithArray() {
117
+ // Query.table('users').where([
118
+ // ['name', '=', 'John'],
119
+ // ['age', '=', 21]
120
+ // ]);
121
+ //
122
+ // (await this.expectSql()).to.equal('SELECT * FROM users WHERE name = ? AND age = ?');
123
+ // this.expectBindings().to.deep.equal(['John', 21]);
124
+ // }
125
+
126
+ @test
127
+ async whereOrOperator() {
128
+ this.query.table('users').where('name', 'John').orWhere('age', 21);
129
+
130
+ (await this.expect()).to.equal(
131
+ 'SELECT * FROM users WHERE name = ? OR age = ?'
132
+ )
133
+ this.expectBindings().to.deep.equal(['John', 21])
134
+ }
135
+
136
+ @test
137
+ async whereBetween() {
138
+ this.query.table('users').whereBetween('age', [18, 25]);
139
+
140
+ (await this.expect()).to.equal(
141
+ 'SELECT * FROM users WHERE age BETWEEN ? AND ?'
142
+ )
143
+ this.expectBindings().to.deep.equal([18, 25])
144
+ }
145
+
146
+ @test
147
+ async whereNotBetween() {
148
+ this.query.table('users').whereNotBetween('age', [18, 25]);
149
+
150
+ (await this.expect()).to.equal(
151
+ 'SELECT * FROM users WHERE age NOT BETWEEN ? AND ?'
152
+ )
153
+ this.expectBindings().to.deep.equal([18, 25])
154
+ }
155
+
156
+ @test
157
+ async orWhereBetween() {
158
+ this.query.table('users').where('name', 'John').orWhereBetween('age', [18, 25]);
159
+
160
+ (await this.expect()).to.equal(
161
+ 'SELECT * FROM users WHERE name = ? OR age BETWEEN ? AND ?'
162
+ )
163
+ this.expectBindings().to.deep.equal(['John', 18, 25])
164
+ }
165
+
166
+ @test
167
+ async orWhereNotBetween() {
168
+ this.query.table('users').where('name', 'John').orWhereNotBetween('age', [18, 25]);
169
+
170
+ (await this.expect()).to.equal(
171
+ 'SELECT * FROM users WHERE name = ? OR age NOT BETWEEN ? AND ?'
172
+ )
173
+ this.expectBindings().to.deep.equal(['John', 18, 25])
174
+ }
175
+
176
+ @test
177
+ async whereIn() {
178
+ this.query.table('users').whereIn('age', [18, 25]);
179
+
180
+ (await this.expect()).to.equal(
181
+ 'SELECT * FROM users WHERE age IN (?, ?)'
182
+ )
183
+ this.expectBindings().to.deep.equal([18, 25])
184
+ }
185
+
186
+ @test
187
+ async whereNotIn() {
188
+ this.query.table('users').whereNotIn('age', [18, 25]);
189
+
190
+ (await this.expect()).to.equal(
191
+ 'SELECT * FROM users WHERE age NOT IN (?, ?)'
192
+ )
193
+ this.expectBindings().to.deep.equal([18, 25])
194
+ }
195
+
196
+ @test
197
+ async orWhereIn() {
198
+ this.query.table('users').where('name', 'John').orWhereIn('age', [18, 25]);
199
+
200
+ (await this.expect()).to.equal(
201
+ 'SELECT * FROM users WHERE name = ? OR age IN (?, ?)'
202
+ )
203
+ this.expectBindings().to.deep.equal(['John', 18, 25])
204
+ }
205
+
206
+ @test
207
+ async orWhereNotIn() {
208
+ this.query.table('users').where('name', 'John').orWhereNotIn('age', [18, 25]);
209
+
210
+ (await this.expect()).to.equal(
211
+ 'SELECT * FROM users WHERE name = ? OR age NOT IN (?, ?)'
212
+ )
213
+ this.expectBindings().to.deep.equal(['John', 18, 25])
214
+ }
215
+
216
+ @test
217
+ async whereNull() {
218
+ this.query.table('users').whereNull('age');
219
+
220
+ (await this.expect()).to.equal(
221
+ 'SELECT * FROM users WHERE age IS NULL'
222
+ )
223
+ this.expectBindings().to.deep.equal([])
224
+ }
225
+
226
+ @test
227
+ async andWhereNull() {
228
+ this.query.table('users').andWhereNull('age');
229
+
230
+ (await this.expect()).to.equal(
231
+ 'SELECT * FROM users WHERE age IS NULL'
232
+ )
233
+ this.expectBindings().to.deep.equal([])
234
+ }
235
+
236
+ @test
237
+ async whereNotNull() {
238
+ this.query.table('users').whereNotNull('age');
239
+
240
+ (await this.expect()).to.equal(
241
+ 'SELECT * FROM users WHERE age IS NOT NULL'
242
+ )
243
+ this.expectBindings().to.deep.equal([])
244
+ }
245
+
246
+ @test
247
+ async andWhereNotNull() {
248
+ this.query.table('users').andWhereNotNull('age');
249
+
250
+ (await this.expect()).to.equal(
251
+ 'SELECT * FROM users WHERE age IS NOT NULL'
252
+ )
253
+ this.expectBindings().to.deep.equal([])
254
+ }
255
+
256
+ @test
257
+ async orWhereNull() {
258
+ this.query.table('users').where('name', 'John').orWhereNull('age');
259
+
260
+ (await this.expect()).to.equal(
261
+ 'SELECT * FROM users WHERE name = ? OR age IS NULL'
262
+ )
263
+ this.expectBindings().to.deep.equal(['John'])
264
+ }
265
+
266
+ @test
267
+ async orWhereNotNull() {
268
+ this.query.table('users').where('name', 'John').orWhereNotNull('age');
269
+
270
+ (await this.expect()).to.equal(
271
+ 'SELECT * FROM users WHERE name = ? OR age IS NOT NULL'
272
+ )
273
+ this.expectBindings().to.deep.equal(['John'])
274
+ }
275
+
276
+ @test
277
+ async orWhereLike() {
278
+ this.query.table('users').where('age', 18).orWhereLike('name', '%John');
279
+
280
+ (await this.expect()).to.equal(
281
+ 'SELECT * FROM users WHERE age = ? OR name LIKE ?'
282
+ )
283
+ this.expectBindings().to.deep.equal([18, '%John'])
284
+ }
285
+
286
+ @test
287
+ async orWhereNotLike() {
288
+ this.query.table('users').where('age', 18).orWhereNotLike('name', '%John');
289
+
290
+ (await this.expect()).to.equal(
291
+ 'SELECT * FROM users WHERE age = ? OR name NOT LIKE ?'
292
+ )
293
+ this.expectBindings().to.deep.equal([18, '%John'])
294
+ }
295
+
296
+ @test
297
+ async andWhereIn() {
298
+ this.query.table('users').where('age', 18).andWhereIn('name', ['John', 'Doe']);
299
+
300
+ (await this.expect()).to.equal(
301
+ 'SELECT * FROM users WHERE age = ? AND name IN (?, ?)'
302
+ )
303
+ this.expectBindings().to.deep.equal([18, 'John', 'Doe'])
304
+ }
305
+
306
+ @test
307
+ async andWhereNotIn() {
308
+ this.query.table('users').where('age', 18).andWhereNotIn('name', ['John', 'Doe']);
309
+
310
+ (await this.expect()).to.equal(
311
+ 'SELECT * FROM users WHERE age = ? AND name NOT IN (?, ?)'
312
+ )
313
+ this.expectBindings().to.deep.equal([18, 'John', 'Doe'])
314
+ }
315
+
316
+ @test
317
+ async whereLike() {
318
+ this.query.table('users').whereLike('name', '%John');
319
+
320
+ (await this.expect()).to.equal(
321
+ 'SELECT * FROM users WHERE name LIKE ?'
322
+ )
323
+ this.expectBindings().to.deep.equal(['%John'])
324
+ }
325
+
326
+ @test
327
+ async andWhere() {
328
+ this.query.table('users').where('name', 'John').andWhere('age', 18);
329
+
330
+ (await this.expect()).to.equal(
331
+ 'SELECT * FROM users WHERE name = ? AND age = ?'
332
+ )
333
+ this.expectBindings().to.deep.equal(['John', 18])
334
+ }
335
+
336
+ // @test
337
+ // async whereParameterGrouping() {
338
+ // this.query.table('users')
339
+ // .where('name', 'John')
340
+ // .where(query => query.where('age', 21).orWhere('role', 'Admin'));
341
+ //
342
+ // (await this.expectSql()).to.equal('SELECT * FROM users WHERE name = ? and (age = ? or role = ?)');
343
+ // this.expectBindings().to.deep.equal(['john', 21, 'Admin']);
344
+ // }
345
+
346
+ @test
347
+ async orderBys() {
348
+ this.query.table('users').orderBy('age');
349
+ (await this.expect()).to.equal(
350
+ 'SELECT * FROM users ORDER BY age DESC'
351
+ )
352
+
353
+ this.query.table('users').orderBy('age', 'ASC');
354
+ (await this.expect()).to.equal(
355
+ 'SELECT * FROM users ORDER BY age ASC'
356
+ )
357
+
358
+ this.query.table('users').orderBy([['age', 'DESC'], ['email', 'ASC']]);
359
+ (await this.expect()).to.equal(
360
+ 'SELECT * FROM users ORDER BY age DESC, email ASC'
361
+ )
362
+ }
363
+
364
+ @test
365
+ async groupBy() {
366
+ this.query.table('users').groupBy('name');
367
+ (await this.expect()).to.equal(
368
+ 'SELECT * FROM users GROUP BY name'
369
+ )
370
+
371
+ this.query.table('users').groupBy('city', 'country');
372
+ (await this.expect()).to.equal(
373
+ 'SELECT * FROM users GROUP BY city, country'
374
+ )
375
+ }
376
+
377
+ @test
378
+ async aggregateSelectCount() {
379
+ const query = await this.query.table('users').selectCount().get() as unknown as string
380
+ expectQuery(query).toEqual('SELECT COUNT(*) as aggregate FROM users')
381
+ }
382
+
383
+ @test
384
+ async aggregateCount() {
385
+ when(this.connection.first(anyOfClass(Query))).thenCall((query: Query) => {
386
+ return {aggregate: trimSQL(query.toSQL())}
387
+ })
388
+
389
+ expect(await this.query.table('users').count()).to.equal('SELECT COUNT(*) as aggregate FROM users')
390
+ }
391
+
392
+ @test
393
+ async aggregateMax() {
394
+ const query = await this.query.table('users').max('age') as unknown as string
395
+ expectQuery(query).toEqual('SELECT MAX(age) as aggregate FROM users')
396
+ }
397
+
398
+ @test
399
+ async limit() {
400
+ const query = await this.query.table('users').limit(10, 20).get() as unknown as string
401
+ expectQuery(
402
+ query
403
+ ).toEqual(
404
+ 'SELECT * FROM users LIMIT 10, 20'
405
+ )
406
+ }
407
+
408
+ //
409
+ // @test
410
+ // async aggregateMax() {
411
+ // this.query.table('users').max('age');
412
+ // (await this.expectSql()).to.equal('SELECT MAX(age) as aggregate FROM users');
413
+ // }
414
+
415
+ @test
416
+ async defaultJoin() {
417
+ this.query.table('users').join('pets', 'pets.user_id', '=', 'users.id');
418
+ (await this.expect()).to.equal(
419
+ 'SELECT * FROM users INNER JOIN pets ON pets.user_id = users.id'
420
+ )
421
+
422
+ // this.new.query.table('users')
423
+ // .join('pets', 'pets.user_id', '=', 'users.id')
424
+ // .leftJoin('pet_types', 'pet_types.id', '=', 'pets.type_id');
425
+ // (await this.expectSql()).to
426
+ // .equal('SELECT * FROM users INNER JOIN pets ON pets.user_id = users.id LEFT JOIN pet_types ON pet_types.id = pets.type_id');
427
+ }
428
+
429
+ @test
430
+ async innerJoin() {
431
+ this.query.table('users').innerJoin('pets', 'pets.user_id', '=', 'users.id');
432
+ (await this.expect()).to.equal(
433
+ 'SELECT * FROM users INNER JOIN pets ON pets.user_id = users.id'
434
+ )
435
+ }
436
+
437
+ @test
438
+ async leftJoin() {
439
+ this.query.table('users').leftJoin('pets', 'pets.user_id', '=', 'users.id');
440
+ (await this.expect()).to.equal(
441
+ 'SELECT * FROM users LEFT JOIN pets ON pets.user_id = users.id'
442
+ )
443
+ }
444
+
445
+ @test
446
+ async rightJoin() {
447
+ this.query.table('users').rightJoin('pets', 'pets.user_id', '=', 'users.id');
448
+ (await this.expect()).to.equal(
449
+ 'SELECT * FROM users RIGHT JOIN pets ON pets.user_id = users.id'
450
+ )
451
+ }
452
+
453
+ @test
454
+ async insertOneRow() {
455
+ this.query.table('users').insert({
456
+ name: 'John',
457
+ email: 'john@example.com'
458
+ });
459
+ (await this.expect()).to.equal(
460
+ 'INSERT INTO users (name, email) VALUES (?, ?)'
461
+ )
462
+ this.expectBindings().to.deep.equal(['John', 'john@example.com'])
463
+ }
464
+
465
+ @test
466
+ async insertManyRow() {
467
+ this.query.table('users').insert([
468
+ {
469
+ name: 'John',
470
+ email: 'john@example.com'
471
+ },
472
+ {
473
+ name: 'Chris',
474
+ email: 'chris@example.com'
475
+ }
476
+ ]);
477
+ (await this.expect()).to.equal(
478
+ 'INSERT INTO users (name, email) VALUES (?, ?), (?, ?)'
479
+ )
480
+ this.expectBindings().to.deep.equal(['John', 'john@example.com', 'Chris', 'chris@example.com'])
481
+ }
482
+
483
+ @test
484
+ async delete() {
485
+ this.query.table('users').delete();
486
+
487
+ (await this.expect()).to.equal(
488
+ 'DELETE FROM users'
489
+ )
490
+ }
491
+
492
+ @test
493
+ async deletesWithCondition() {
494
+ this.query.table('users').where('name', 'John').delete();
495
+
496
+ (await this.expect()).to.equal(
497
+ 'DELETE FROM users WHERE name = ?'
498
+ )
499
+ this.expectBindings().to.deep.equal(['John'])
500
+ }
501
+
502
+ @test
503
+ async update() {
504
+ this.query.table('users').update('name', 'John');
505
+
506
+ (await this.expect()).to.equal(
507
+ 'UPDATE users SET name = ?'
508
+ )
509
+ this.expectBindings().to.deep.equal(['John'])
510
+ }
511
+
512
+ @test
513
+ async updateMultipleColumns() {
514
+ this.query.table('users').update({
515
+ name: 'John',
516
+ age: 21,
517
+ });
518
+
519
+ (await this.expect()).to.equal(
520
+ 'UPDATE users SET name = ?, age = ?'
521
+ )
522
+ this.expectBindings().to.deep.equal(['John', 21])
523
+ }
524
+
525
+ @test
526
+ async updateWithCondition() {
527
+ this.query.table('users').where('name', 'John').update('name', 'Doe');
528
+ (await this.expect()).to.equal(
529
+ 'UPDATE users SET name = ? WHERE name = ?'
530
+ )
531
+ this.expectBindings().to.deep.equal(['Doe', 'John'])
532
+ }
533
+
534
+ @test
535
+ async subSelectWhereWithFunction() {
536
+ const query = Query.table('users').where('id', q => {
537
+ q.select('userId').from('roles').where('id', 1)
538
+ })
539
+ expect(trimSQL(query.toSQL())).to.equal('SELECT * FROM users WHERE id = (SELECT userId FROM roles WHERE id = ?)')
540
+ expect(query.getBindings()).to.deep.equal([1])
541
+ }
542
+
543
+ @test
544
+ async subSelectWhereWithDirectQuery() {
545
+ const query = Query.table('users').where('id', new Query().select('userId').from('roles').where('id', 1))
546
+ expect(trimSQL(query.toSQL())).to.equal('SELECT * FROM users WHERE id = (SELECT userId FROM roles WHERE id = ?)')
547
+ expect(query.getBindings()).to.deep.equal([1])
548
+ }
549
+
550
+ @test
551
+ async subSelectWhereInWithFunction() {
552
+ const query = Query.table('users').whereIn('id', q => {
553
+ q.select('userId').from('roles').where('id', 1)
554
+ })
555
+ expect(trimSQL(query.toSQL())).to.equal('SELECT * FROM users WHERE id IN (SELECT userId FROM roles WHERE id = ?)')
556
+ expect(query.getBindings()).to.deep.equal([1])
557
+ }
558
+
559
+ @test
560
+ async subSelectWhereInWithDirectQuery() {
561
+ const query = Query.table('users').whereIn('id', new Query().select('userId').from('roles').where('id', 1))
562
+ expect(trimSQL(query.toSQL())).to.equal('SELECT * FROM users WHERE id IN (SELECT userId FROM roles WHERE id = ?)')
563
+ expect(query.getBindings()).to.deep.equal([1])
564
+ }
565
+ }
566
+
567
+ interface User {
568
+ id: number;
569
+ age: number;
570
+ email: string;
571
+ name: string;
572
+ dateOfBirth: Date;
573
+ }
@@ -0,0 +1,32 @@
1
+ import { suite, test } from '@testdeck/mocha'
2
+ import { expect } from 'chai'
3
+ import { Connection, Query } from '../../Database'
4
+ import { User } from './Entities/User'
5
+ import { Role } from './Entities/Role'
6
+ import { SqliteDriver } from '@Typetron/Database/Drivers'
7
+
8
+ @suite
9
+ class SchemaTest {
10
+
11
+ async before() {
12
+ Query.connection = new Connection(new SqliteDriver(':memory:'))
13
+ // Query.connection = new Connection(new MysqlDriver({
14
+ // host: 'localhost', user: 'root', password: 'root', database: 'typetron_test'
15
+ // }))
16
+ }
17
+
18
+ @test
19
+ async createsPivotTable() {
20
+ await Query.connection.driver.schema.synchronize([User, Role].pluck('metadata'))
21
+
22
+ const tableName = [User.getTable(), Role.getTable()].sort().join('_')
23
+ const table = await Query.connection.tableExists(tableName)
24
+ expect(Boolean(table)).to.be.equal(true, 'Pivot table was not created')
25
+ const tableColumns = await Query.connection.tableColumns(tableName) as {name: string, type: string}[]
26
+ expect(tableColumns).to.have.length(2)
27
+ // expect(tableColumns[0]).to.deep.include({name: 'id', type: 'integer'});
28
+ expect(tableColumns[0]).to.deep.include({name: 'roleId'})
29
+ expect(tableColumns[1]).to.deep.include({name: 'userId'})
30
+ }
31
+
32
+ }
@@ -0,0 +1,30 @@
1
+ import { suite, test } from '@testdeck/mocha'
2
+ import { SeederManager } from '@Typetron/Database/Seeders'
3
+ import { expect } from 'chai'
4
+ import { Connection, Query, SqliteDriver } from '../../../Database'
5
+ import { Storage } from '../../../Storage'
6
+
7
+ @suite
8
+ class RunnerTest {
9
+ private runner: SeederManager
10
+ private seedsPath = './tests/Database/Seeders/seeders'
11
+
12
+ async before() {
13
+ Query.connection = new Connection(new SqliteDriver(':memory:'))
14
+ // Query.connection = new Connection(new MysqlDriver({
15
+ // host: 'localhost', user: 'root', password: 'root', database: 'typetron_test'
16
+ // }))
17
+ await Query.connection.runRaw('CREATE TABLE random_table (col1 varchar)')
18
+
19
+ this.runner = new SeederManager(new Storage(), this.seedsPath)
20
+ }
21
+
22
+ @test
23
+ async shouldSeed() {
24
+ await this.runner.seed()
25
+
26
+ const result = await Query.table<{ col1: string }>('random_table').first()
27
+
28
+ expect(result?.col1).to.be.equal('val1')
29
+ }
30
+ }
@@ -0,0 +1,10 @@
1
+ import { Query } from "@Typetron/Database";
2
+ import { Seeder } from "@Typetron/Database/Seeders";
3
+
4
+ export class RandomSeeder extends Seeder{
5
+ public run(): void {
6
+ Query.table('random_table').insertOne({
7
+ col1: 'val1'
8
+ })
9
+ }
10
+ }
@@ -0,0 +1,16 @@
1
+ import { Expression } from '@Typetron/Database'
2
+ import { expect } from 'chai'
3
+
4
+ export function expectQuery(expression: Expression | string) {
5
+ const sql = expression instanceof Expression ? expression.toSQL() : expression
6
+
7
+ return {
8
+ toEqual: (expected: string) => {
9
+ expect(trimSQL(sql)).to.be.equal(trimSQL(expected))
10
+ }
11
+ }
12
+ }
13
+
14
+ export function trimSQL(sql: string) {
15
+ return sql.replace(/(\r\n|\n|\r)/gm, ' ').replace(/`/gm, '').replace(/ +/g, ' ').trim()
16
+ }