@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,202 @@
1
+ import { suite, test } from '@testdeck/mocha'
2
+ import { expect } from 'chai'
3
+ import { Connection, Entity, EntityConstructor, Query, SqliteDriver } from '../../Database'
4
+ import { User } from './Entities/User'
5
+ import { EntityQuery } from '../../Database/EntityQuery'
6
+ import { Article } from './Entities/Article'
7
+ import { Role } from './Entities/Role'
8
+
9
+ @suite
10
+ class EntityTest {
11
+ joe = {
12
+ id: 1,
13
+ name: 'Joe',
14
+ email: 'joe@example.com',
15
+ }
16
+
17
+ doe = {
18
+ id: 2,
19
+ name: 'Doe',
20
+ email: 'doe@example.com',
21
+ }
22
+
23
+ admin = {
24
+ id: 1,
25
+ name: 'Admin'
26
+ }
27
+ developer = {
28
+ id: 1,
29
+ name: 'Developer'
30
+ }
31
+
32
+ data = {
33
+ users: [this.joe, this.doe],
34
+ roles: [this.admin, this.developer],
35
+ }
36
+
37
+ async before() {
38
+ Query.connection = new Connection(new SqliteDriver(':memory:'))
39
+ // Query.connection = new Connection(new MysqlDriver({
40
+ // host: 'localhost', user: 'root', password: 'root', database: 'typetron_test'
41
+ // }))
42
+ await Query.connection.driver.schema.synchronize([User, Article, Role].pluck('metadata'))
43
+ }
44
+
45
+ async after() {
46
+ for await (const entity of [User, Article, Role]) {
47
+ await (entity as EntityConstructor<Entity>).truncate()
48
+ }
49
+ }
50
+
51
+ expectToContain(actual: User, expected: {[key: string]: string | number | Date}) {
52
+ const keys = Object.keys(expected)
53
+ keys.forEach(key => {
54
+ expect(actual).to.have.property(key, expected[key])
55
+ })
56
+ }
57
+
58
+ @test
59
+ fillEntityWhenInstantiating() {
60
+ expect(new User().fill({name: 'Joe'})).to.deep.include({name: 'Joe'})
61
+ }
62
+
63
+ @test
64
+ getEntityTable() {
65
+ expect(User.getTable()).to.equal('users')
66
+ expect((new User()).getTable()).to.equal('users')
67
+ }
68
+
69
+ @test
70
+ async createsNewQuery() {
71
+ expect(User.newQuery()).to.be.an.instanceof(EntityQuery)
72
+ const user = new User()
73
+ expect(user.newQuery()).to.be.an.instanceof(EntityQuery)
74
+ }
75
+
76
+ @test
77
+ async getEveryRecord() {
78
+ await Query.table(User.getTable()).insert([this.joe, this.doe])
79
+
80
+ const users = await User.get()
81
+
82
+ expect(users).to.have.length(2)
83
+
84
+ users.forEach((user, index) => this.expectToContain(user, this.data.users[index]))
85
+ }
86
+
87
+ // @test
88
+ // async where() {
89
+ // const modelMock = mock(User);
90
+ // const user = instance(modelMock);
91
+ //
92
+ // const queryMock = mock(EntityQuery);
93
+ // verify(queryMock.where('name', 'Joe')).once();
94
+ // when(user.newQuery()).thenReturn(instance(queryMock));
95
+ //
96
+ // const users = await User.where('name', 'Joe').get();
97
+ //
98
+ // users.forEach((user, index) => this.expectToContain(user, this.data.users[index]));
99
+ // }
100
+ //
101
+ // @test
102
+ // async whereIn() {
103
+ // when(this.connection.get(anyOfClass(Query))).thenResolve(this.data.users);
104
+ //
105
+ // const users = await User.whereIn('name', ['Joe', 'Doe']).get();
106
+ //
107
+ // users.forEach((user, index) => this.expectToContain(user, this.data.users[index]));
108
+ // }
109
+ //
110
+ @test
111
+ async save() {
112
+ const data = {email: 'tester', name: 'Tester'}
113
+ const user = new User().fill(data)
114
+
115
+ this.expectToContain(user, data)
116
+ expect(user.id).to.be.equal(undefined)
117
+ await user.save()
118
+ expect(user.id).to.be.equal(1)
119
+ expect(user.createdAt).to.be.an.instanceOf(Date)
120
+ expect(user.updatedAt).to.be.an.instanceOf(Date)
121
+ }
122
+
123
+ @test
124
+ async insertMany() {
125
+ await User.insert([this.joe, this.doe])
126
+
127
+ const users = await User.get()
128
+ expect(users).to.have.length(2)
129
+ this.expectToContain(users[0], this.joe)
130
+ this.expectToContain(users[1], this.doe)
131
+ }
132
+
133
+ // @test
134
+ // async test0() {
135
+ // const users = await User.get();
136
+ // const users = await User.get('id', 'name');
137
+ // const user = await User.first('id');
138
+ // const user = await User.firstOrNew({name: 'John'}, {email: 'john@example.com'});
139
+ // const user = await User.firstOrCreate({name: 'John'}, {email: 'john@example.com'});
140
+ // const user = await User.find(1);
141
+ // const users = await User.with('articles').get();
142
+ // }
143
+
144
+ @test
145
+ async getSpecificColumns() {
146
+ await Query.table(User.getTable()).insert([this.joe])
147
+
148
+ const users = await User.get('id', 'name')
149
+
150
+ expect(users).to.have.length(1)
151
+ expect(users.first()).to.have.property('name', this.joe.name)
152
+ expect(users.first()).and.to.have.property('id', this.joe.id)
153
+
154
+ // TODO with the new JS updates, the Entity properties are initialized to undefined,
155
+ // which means we will always get them in the object and this assert below will fail
156
+ // But, the actual problem is picking what columns to select.
157
+ // User.get(col1, col2) will return an User instance with col1 and col2 values but undefined for other columns
158
+ // This should be invalid because there might be col3 which might be non-optional and should always have a value
159
+ // expect(users.first()).and.to.not.have.property('email')
160
+ }
161
+
162
+ @test
163
+ async getFirstRecord() {
164
+ await Query.table(User.getTable()).insert([this.joe, this.doe])
165
+
166
+ const user = await User.first() as User
167
+
168
+ this.expectToContain(user, this.joe)
169
+ }
170
+
171
+ @test
172
+ async getFirstRecordWithSpecificColumns() {
173
+ await Query.table(User.getTable()).insert([this.joe])
174
+
175
+ const user = await User.first('id') as User
176
+
177
+ expect(user).to.have.property('id', this.joe.id)
178
+
179
+ // TODO this has the same issue as the test `getSpecificColumns`
180
+ // expect(user).to.not.have.property('name')
181
+ }
182
+
183
+ @test
184
+ async getFirstRecordOrInstantiateOneIfNot() {
185
+ const john = await User.firstOrNew({name: 'John'}, {email: 'john@example.com'})
186
+
187
+ expect(john).to.have.property('name', 'John')
188
+ expect(john).to.have.property('email', 'john@example.com')
189
+ expect(john.id).to.be.undefined
190
+ expect(await User.get()).to.have.length(0)
191
+ }
192
+
193
+ @test
194
+ async getFirstRecordOrCreateOneIfNot() {
195
+ const john = await User.firstOrCreate({name: 'John'}, {email: 'john@example.com'})
196
+
197
+ expect(john).to.have.property('name', 'John')
198
+ expect(john).to.have.property('email', 'john@example.com')
199
+ expect(john).to.have.property('id')
200
+ expect(await User.get()).to.have.length(1)
201
+ }
202
+ }
@@ -0,0 +1,96 @@
1
+ import { suite, test } from '@testdeck/mocha'
2
+ import { expect } from 'chai'
3
+ import { File, Storage } from '../../../Storage'
4
+ import { Connection, Query, SqliteDriver } from '../../../Database'
5
+ import { MigrationHistory, Migrator } from '../../../Database/Migrations'
6
+ import { anything, instance, mock, when } from 'ts-mockito'
7
+
8
+ @suite
9
+ class RunnerTest {
10
+ private runner: Migrator
11
+ private migrationsPath = './tests/Database/Migrations/migrations'
12
+ private migrationFiles = ['1.createUserTable.ts', '2.createArticlesTable.ts']
13
+ private tableNames = ['migration_test_users', 'migration_test_articles']
14
+
15
+ async before() {
16
+ Query.connection = new Connection(new SqliteDriver(':memory:'))
17
+ // Query.connection = new Connection(new MysqlDriver({
18
+ // host: 'localhost', user: 'root', password: 'root', database: 'typetron_test'
19
+ // }))
20
+ this.runner = new Migrator(new Storage(), Query.connection, this.migrationsPath)
21
+ }
22
+
23
+ async after() {
24
+ if (await Query.connection.tableExists(MigrationHistory.getTable())) {
25
+ await this.runner.reset()
26
+ await Query.connection.runRaw(`DROP TABLE IF EXISTS ${MigrationHistory.getTable()}`)
27
+ }
28
+ }
29
+
30
+ @test
31
+ async getFilesByPath() {
32
+ const migrationFiles = await this.runner.files()
33
+
34
+ expect(migrationFiles).to.have.length(2)
35
+ }
36
+
37
+ @test
38
+ async shouldMigrate() {
39
+ const migrated = await this.runner.migrate()
40
+
41
+ expect(migrated).equals(true)
42
+
43
+ const tables = await Query.connection.tables()
44
+
45
+ expect(tables.pluck('name')).to.include.members(this.tableNames)
46
+ }
47
+
48
+ @test
49
+ async shouldRollbackOnce() {
50
+ await this.migrateInBatches()
51
+
52
+ const rollback = await this.runner.rollback()
53
+
54
+ expect(rollback).equals(true)
55
+
56
+ const tables = await Query.connection.tables()
57
+
58
+ expect(tables.pluck('name')).to.include(this.tableNames[0])
59
+ expect(tables.pluck('name')).to.not.include(this.tableNames[1])
60
+ }
61
+
62
+ @test
63
+ async shouldRollbackBySteps() {
64
+ await this.migrateInBatches()
65
+ const rollback = await this.runner.rollback(2)
66
+
67
+ expect(rollback).equals(true)
68
+
69
+ const tables = await Query.connection.tables()
70
+
71
+ expect(tables.pluck('name')).to.not.include.members(this.tableNames)
72
+ }
73
+
74
+ @test
75
+ async shouldReset() {
76
+ await this.migrateInBatches()
77
+ const rollback = await this.runner.reset()
78
+
79
+ expect(rollback).equals(true)
80
+
81
+ const tables = await Query.connection.tables()
82
+
83
+ expect(tables.pluck('name')).to.not.include.members(this.tableNames)
84
+ }
85
+
86
+ private async migrateInBatches() {
87
+ const mockStorage = mock(Storage)
88
+ when(mockStorage.files(anything(), anything()))
89
+ .thenReturn([new File(this.migrationFiles[0], this.migrationsPath)])
90
+ .thenReturn([new File(this.migrationFiles[1], this.migrationsPath)])
91
+
92
+ this.runner.storage = instance(mockStorage)
93
+ await this.runner.migrate()
94
+ await this.runner.migrate()
95
+ }
96
+ }
@@ -0,0 +1,12 @@
1
+ import { Migration } from '../../../../Database/Migrations'
2
+
3
+ export class CreateUserTable extends Migration {
4
+
5
+ async up() {
6
+ await this.connection.runRaw('CREATE TABLE migration_test_users (id INTEGER PRIMARY KEY, name TEXT, password TEXT)')
7
+ }
8
+
9
+ async down() {
10
+ await this.connection.runRaw('DROP TABLE migration_test_users')
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ import { Migration } from '../../../../Database/Migrations'
2
+
3
+ export class CreateArticlesTable extends Migration {
4
+
5
+ async up() {
6
+ await this.connection.runRaw('CREATE TABLE migration_test_articles (id INTEGER PRIMARY KEY, title TEXT, content TEXT)')
7
+ }
8
+
9
+ async down() {
10
+ await this.connection.runRaw('DROP TABLE migration_test_articles')
11
+ }
12
+ }