@strapi/database 4.0.0-beta.0

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 (52) hide show
  1. package/LICENSE +22 -0
  2. package/examples/connections.js +36 -0
  3. package/examples/data.sqlite +0 -0
  4. package/examples/docker-compose.yml +29 -0
  5. package/examples/index.js +73 -0
  6. package/examples/models.js +341 -0
  7. package/examples/typings.ts +17 -0
  8. package/lib/dialects/dialect.js +45 -0
  9. package/lib/dialects/index.js +28 -0
  10. package/lib/dialects/mysql/index.js +51 -0
  11. package/lib/dialects/mysql/schema-inspector.js +203 -0
  12. package/lib/dialects/postgresql/index.js +49 -0
  13. package/lib/dialects/postgresql/schema-inspector.js +229 -0
  14. package/lib/dialects/sqlite/index.js +74 -0
  15. package/lib/dialects/sqlite/schema-inspector.js +151 -0
  16. package/lib/entity-manager.js +886 -0
  17. package/lib/entity-repository.js +110 -0
  18. package/lib/errors.js +14 -0
  19. package/lib/fields.d.ts +9 -0
  20. package/lib/fields.js +232 -0
  21. package/lib/index.d.ts +146 -0
  22. package/lib/index.js +60 -0
  23. package/lib/lifecycles/index.d.ts +50 -0
  24. package/lib/lifecycles/index.js +66 -0
  25. package/lib/lifecycles/subscribers/index.d.ts +9 -0
  26. package/lib/lifecycles/subscribers/models-lifecycles.js +19 -0
  27. package/lib/lifecycles/subscribers/timestamps.js +65 -0
  28. package/lib/metadata/index.js +219 -0
  29. package/lib/metadata/relations.js +488 -0
  30. package/lib/migrations/index.d.ts +9 -0
  31. package/lib/migrations/index.js +69 -0
  32. package/lib/migrations/storage.js +49 -0
  33. package/lib/query/helpers/index.js +10 -0
  34. package/lib/query/helpers/join.js +95 -0
  35. package/lib/query/helpers/order-by.js +70 -0
  36. package/lib/query/helpers/populate.js +652 -0
  37. package/lib/query/helpers/search.js +84 -0
  38. package/lib/query/helpers/transform.js +84 -0
  39. package/lib/query/helpers/where.js +322 -0
  40. package/lib/query/index.js +7 -0
  41. package/lib/query/query-builder.js +348 -0
  42. package/lib/schema/__tests__/schema-diff.test.js +181 -0
  43. package/lib/schema/builder.js +352 -0
  44. package/lib/schema/diff.js +376 -0
  45. package/lib/schema/index.d.ts +49 -0
  46. package/lib/schema/index.js +95 -0
  47. package/lib/schema/schema.js +209 -0
  48. package/lib/schema/storage.js +75 -0
  49. package/lib/types/index.d.ts +6 -0
  50. package/lib/types/index.js +34 -0
  51. package/lib/utils/content-types.js +41 -0
  52. package/package.json +39 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015-present Strapi Solutions SAS
2
+
3
+ Portions of the Strapi software are licensed as follows:
4
+
5
+ * All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined in "ee/LICENSE".
6
+
7
+ * All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.
8
+
9
+ MIT Expat License
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const postgres = {
4
+ client: 'postgres',
5
+ connection: {
6
+ database: 'strapi',
7
+ user: 'strapi',
8
+ password: 'strapi',
9
+ },
10
+ // debug: true,
11
+ };
12
+
13
+ const mysql = {
14
+ client: 'mysql',
15
+ connection: {
16
+ database: 'strapi',
17
+ user: 'strapi',
18
+ password: 'strapi',
19
+ },
20
+ // debug: true,
21
+ };
22
+
23
+ const sqlite = {
24
+ client: 'sqlite',
25
+ connection: {
26
+ filename: 'data.sqlite',
27
+ },
28
+ useNullAsDefault: true,
29
+ // debug: true,
30
+ };
31
+
32
+ module.exports = {
33
+ sqlite,
34
+ postgres,
35
+ mysql,
36
+ };
Binary file
@@ -0,0 +1,29 @@
1
+ version: '3'
2
+
3
+ services:
4
+ postgres:
5
+ image: postgres
6
+ restart: always
7
+ volumes:
8
+ - ./data/postgresql:/var/lib/postgresql/data
9
+ environment:
10
+ POSTGRES_USER: strapi
11
+ POSTGRES_PASSWORD: strapi
12
+ POSTGRES_DB: strapi
13
+ ports:
14
+ - '5432:5432'
15
+
16
+ mysql:
17
+ image: mysql
18
+ restart: always
19
+ command: --default-authentication-plugin=mysql_native_password
20
+ environment:
21
+ MYSQL_DATABASE: strapi
22
+ MYSQL_USER: strapi
23
+ MYSQL_PASSWORD: strapi
24
+ MYSQL_ROOT_HOST: '%'
25
+ MYSQL_ROOT_PASSWORD: strapi
26
+ volumes:
27
+ - ./data/mysql:/var/lib/mysql
28
+ ports:
29
+ - '3306:3306'
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ const util = require('util');
4
+
5
+ const { Database } = require('../lib/index');
6
+ const models = require('./models');
7
+ const connections = require('./connections');
8
+
9
+ async function main(connection) {
10
+ const orm = await Database.init({
11
+ connection,
12
+ models: Database.transformContentTypes(models),
13
+ });
14
+
15
+ try {
16
+ // await orm.schema.drop();
17
+ // await orm.schema.create();
18
+
19
+ await orm.schema.reset();
20
+
21
+ let res;
22
+
23
+ const c1 = await orm.query('comment').create({
24
+ data: {
25
+ title: 'coucou',
26
+ },
27
+ });
28
+
29
+ const c2 = await orm.query('video-comment').create({
30
+ data: {
31
+ title: 'coucou',
32
+ },
33
+ });
34
+
35
+ res = await orm.query('article').create({
36
+ data: {
37
+ dz: [
38
+ {
39
+ __type: 'comment',
40
+ id: c1.id,
41
+ },
42
+ {
43
+ __type: 'video-comment',
44
+ id: c2.id,
45
+ },
46
+ ],
47
+ },
48
+ populate: {
49
+ dz: true,
50
+ },
51
+ });
52
+
53
+ log(res);
54
+
55
+ res = await orm.query('article').findMany({
56
+ populate: {
57
+ dz: true,
58
+ },
59
+ });
60
+
61
+ log(res);
62
+
63
+ // await tests(orm);
64
+ } finally {
65
+ orm.destroy();
66
+ }
67
+ }
68
+
69
+ function log(res) {
70
+ console.log(util.inspect(res, null, null, true));
71
+ }
72
+
73
+ main(connections.sqlite);
@@ -0,0 +1,341 @@
1
+ 'use strict';
2
+
3
+ const category = {
4
+ modelName: 'category',
5
+ uid: 'category',
6
+ collectionName: 'categories',
7
+ attributes: {
8
+ title: {
9
+ type: 'string',
10
+ },
11
+ price: {
12
+ type: 'integer',
13
+ required: true,
14
+ default: 12,
15
+
16
+ column: {
17
+ unique: true,
18
+ nonNullable: true,
19
+ unsigned: true,
20
+ defaultTo: 12,
21
+ },
22
+ },
23
+ articles: {
24
+ type: 'relation',
25
+ relation: 'oneToMany',
26
+ target: 'article',
27
+ mappedBy: 'category',
28
+ },
29
+ compo: {
30
+ type: 'component',
31
+ component: 'compo',
32
+ },
33
+ },
34
+ };
35
+
36
+ const article = {
37
+ modelName: 'article',
38
+ uid: 'article',
39
+ collectionName: 'articles',
40
+ attributes: {
41
+ title: {
42
+ type: 'string',
43
+ },
44
+ category: {
45
+ type: 'relation',
46
+ relation: 'manyToOne',
47
+ target: 'category',
48
+ inversedBy: 'articles',
49
+ // useJoinTable: false,
50
+ },
51
+ // tags: {
52
+ // type: 'relation',
53
+ // relation: 'manyToMany',
54
+ // target: 'tag',
55
+ // inversedBy: 'articles',
56
+ // },
57
+ // compo: {
58
+ // type: 'component',
59
+ // component: 'compo',
60
+ // // repeatable: true,
61
+ // },
62
+ // cover: {
63
+ // type: 'media',
64
+ // single: true,
65
+ // },
66
+ // gallery: {
67
+ // type: 'media',
68
+ // multiple: true,
69
+ // },
70
+ },
71
+ };
72
+
73
+ const tags = {
74
+ modelName: 'tag',
75
+ uid: 'tag',
76
+ collectionName: 'tags',
77
+ attributes: {
78
+ name: {
79
+ type: 'string',
80
+ },
81
+ articles: {
82
+ type: 'relation',
83
+ relation: 'manyToMany',
84
+ target: 'article',
85
+ mappedBy: 'tag',
86
+ },
87
+ },
88
+ };
89
+
90
+ const compo = {
91
+ modelName: 'compo',
92
+ uid: 'compo',
93
+ collectionName: 'compos',
94
+ attributes: {
95
+ key: {
96
+ type: 'string',
97
+ },
98
+ value: {
99
+ type: 'string',
100
+ },
101
+ },
102
+ };
103
+
104
+ const user = {
105
+ modelName: 'user',
106
+ uid: 'user',
107
+ collectionName: 'users',
108
+ attributes: {
109
+ address: {
110
+ type: 'relation',
111
+ relation: 'oneToOne',
112
+ target: 'address',
113
+ inversedBy: 'user',
114
+ // useJoinTable: false,
115
+ },
116
+ },
117
+ };
118
+
119
+ const address = {
120
+ modelName: 'address',
121
+ uid: 'address',
122
+ collectionName: 'addresses',
123
+ attributes: {
124
+ name: {
125
+ type: 'string',
126
+ },
127
+ user: {
128
+ type: 'relation',
129
+ relation: 'oneToOne',
130
+ target: 'user',
131
+ mappedBy: 'address',
132
+ },
133
+ },
134
+ };
135
+
136
+ const file = {
137
+ modelName: 'file',
138
+ uid: 'file',
139
+ collectionName: 'files',
140
+ attributes: {
141
+ name: {
142
+ type: 'string',
143
+ },
144
+ alternativeText: {
145
+ type: 'string',
146
+ },
147
+ caption: {
148
+ type: 'string',
149
+ },
150
+ width: {
151
+ type: 'integer',
152
+ },
153
+ height: {
154
+ type: 'integer',
155
+ },
156
+ formats: {
157
+ type: 'json',
158
+ },
159
+ hash: {
160
+ type: 'string',
161
+ },
162
+ ext: {
163
+ type: 'string',
164
+ },
165
+ mime: {
166
+ type: 'string',
167
+ },
168
+ size: {
169
+ type: 'decimal',
170
+ },
171
+ url: {
172
+ type: 'string',
173
+ },
174
+ previewUrl: {
175
+ type: 'string',
176
+ },
177
+ provider: {
178
+ type: 'string',
179
+ },
180
+ provider_metadata: {
181
+ type: 'json',
182
+ },
183
+ // related: {
184
+ // type: 'relation',
185
+ // relation: 'oneToMany',
186
+ // target: 'file_morph',
187
+ // mappedBy: 'file',
188
+ // },
189
+ // related: {
190
+ // type: 'relation',
191
+ // realtion: 'morphTo',
192
+ // },
193
+ },
194
+ };
195
+
196
+ const fileMorph = {
197
+ modelName: 'file-morph',
198
+ uid: 'file-morph',
199
+ collectionName: 'file_morphs',
200
+ attributes: {
201
+ // file: {
202
+ // type: 'relation',
203
+ // relation: 'manyToOne',
204
+ // target: 'file',
205
+ // inversedBy: 'related',
206
+ // useJoinTable: false,
207
+ // },
208
+ },
209
+ };
210
+
211
+ const blogPost = {
212
+ modelName: 'blogPost',
213
+ uid: 'blogPost',
214
+ collectionName: 'blog_posts',
215
+ attributes: {
216
+ passwordField: {
217
+ type: 'password',
218
+ },
219
+ emailField: {
220
+ type: 'email',
221
+ },
222
+ stringField: {
223
+ type: 'string',
224
+ },
225
+ uidField: {
226
+ type: 'uid',
227
+ },
228
+ richtextField: {
229
+ type: 'richtext',
230
+ },
231
+ textField: {
232
+ type: 'text',
233
+ },
234
+ enumerationField: {
235
+ type: 'enumeration',
236
+ enum: ['A', 'B'],
237
+ },
238
+ jsonField: {
239
+ type: 'json',
240
+ },
241
+ bigintegerField: {
242
+ type: 'biginteger',
243
+ },
244
+ integerField: {
245
+ type: 'integer',
246
+ },
247
+ floatField: {
248
+ type: 'float',
249
+ },
250
+ decimalField: {
251
+ type: 'decimal',
252
+ },
253
+ dateField: {
254
+ type: 'date',
255
+ },
256
+ timeField: {
257
+ type: 'time',
258
+ },
259
+ datetimeField: {
260
+ type: 'datetime',
261
+ },
262
+ timestampField: {
263
+ type: 'timestamp',
264
+ },
265
+ booleanField: {
266
+ type: 'boolean',
267
+ },
268
+ },
269
+ };
270
+
271
+ module.exports = [category, article, tags, compo, user, address, file, fileMorph, blogPost];
272
+
273
+ // const article = {
274
+ // modelName: 'article',
275
+ // uid: 'article',
276
+ // collectionName: 'articles',
277
+ // attributes: {
278
+ // commentable: {
279
+ // type: 'relation',
280
+ // relation: 'morphToOne',
281
+ // },
282
+ // reportables: {
283
+ // type: 'relation',
284
+ // relation: 'morphToMany',
285
+ // },
286
+ // dz: {
287
+ // type: 'dynamiczone',
288
+ // components: ['comment', 'video-comment'],
289
+ // },
290
+ // },
291
+ // };
292
+
293
+ // const comment = {
294
+ // modelName: 'comment',
295
+ // uid: 'comment',
296
+ // collectionName: 'comments',
297
+ // attributes: {
298
+ // article: {
299
+ // type: 'relation',
300
+ // relation: 'morphOne',
301
+ // target: 'article',
302
+ // morphBy: 'commentable',
303
+ // },
304
+ // title: {
305
+ // type: 'string',
306
+ // },
307
+ // },
308
+ // };
309
+
310
+ // const videoComment = {
311
+ // modelName: 'video-comment',
312
+ // uid: 'video-comment',
313
+ // collectionName: 'video_comments',
314
+ // attributes: {
315
+ // articles: {
316
+ // type: 'relation',
317
+ // relation: 'morphMany',
318
+ // target: 'article',
319
+ // morphBy: 'commentable',
320
+ // },
321
+ // title: {
322
+ // type: 'string',
323
+ // },
324
+ // },
325
+ // };
326
+
327
+ // const folder = {
328
+ // modelName: 'folder',
329
+ // uid: 'folder',
330
+ // collectionName: 'folders',
331
+ // attributes: {
332
+ // articles: {
333
+ // type: 'relation',
334
+ // relation: 'morphMany',
335
+ // target: 'article',
336
+ // morphBy: 'reportables',
337
+ // },
338
+ // },
339
+ // };
340
+
341
+ // module.exports = [article, comment, videoComment, folder];
@@ -0,0 +1,17 @@
1
+ type ID = number | string;
2
+
3
+ interface Category {
4
+ id: ID;
5
+ title: string;
6
+ }
7
+
8
+ interface Article {
9
+ id: ID;
10
+ title: string;
11
+ category: Category | ID;
12
+ }
13
+
14
+ interface AllTypes {
15
+ article: Article;
16
+ category: Category;
17
+ }
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ class Dialect {
4
+ constructor(db) {
5
+ this.db = db;
6
+ }
7
+
8
+ configure() {}
9
+ initialize() {}
10
+
11
+ getSqlType(type) {
12
+ return type;
13
+ }
14
+
15
+ canAlterConstraints() {
16
+ return true;
17
+ }
18
+
19
+ usesForeignKeys() {
20
+ return false;
21
+ }
22
+
23
+ useReturning() {
24
+ return false;
25
+ }
26
+
27
+ supportsUnsigned() {
28
+ return false;
29
+ }
30
+
31
+ async startSchemaUpdate() {}
32
+ async endSchemaUpdate() {}
33
+
34
+ transformErrors(error) {
35
+ if (error instanceof Error) {
36
+ throw error;
37
+ }
38
+
39
+ throw new Error(error.message);
40
+ }
41
+ }
42
+
43
+ module.exports = {
44
+ Dialect,
45
+ };
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ const getDialectClass = client => {
4
+ switch (client) {
5
+ case 'postgres':
6
+ return require('./postgresql');
7
+ case 'mysql':
8
+ return require('./mysql');
9
+ case 'sqlite':
10
+ return require('./sqlite');
11
+ default:
12
+ throw new Error(`Unknow dialect ${client}`);
13
+ }
14
+ };
15
+
16
+ const getDialect = db => {
17
+ const { client } = db.config.connection;
18
+
19
+ const constructor = getDialectClass(client);
20
+ const dialect = new constructor(db);
21
+ dialect.client = client;
22
+
23
+ return dialect;
24
+ };
25
+
26
+ module.exports = {
27
+ getDialect,
28
+ };
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ const { Dialect } = require('../dialect');
4
+ const MysqlSchemaInspector = require('./schema-inspector');
5
+
6
+ class MysqlDialect extends Dialect {
7
+ constructor(db) {
8
+ super(db);
9
+
10
+ this.schemaInspector = new MysqlSchemaInspector(db);
11
+ }
12
+
13
+ configure() {
14
+ this.db.config.connection.connection.supportBigNumbers = true;
15
+ this.db.config.connection.connection.bigNumberStrings = true;
16
+ this.db.config.connection.connection.typeCast = (field, next) => {
17
+ if (field.type == 'DECIMAL' || field.type === 'NEWDECIMAL') {
18
+ var value = field.string();
19
+ return value === null ? null : Number(value);
20
+ }
21
+
22
+ if (field.type == 'TINY' && field.length == 1) {
23
+ let value = field.string();
24
+ return value ? value == '1' : null;
25
+ }
26
+ return next();
27
+ };
28
+ }
29
+
30
+ async startSchemaUpdate() {
31
+ await this.db.connection.raw(`set foreign_key_checks = 0;`);
32
+ }
33
+
34
+ async endSchemaUpdate() {
35
+ await this.db.connection.raw(`set foreign_key_checks = 1;`);
36
+ }
37
+
38
+ supportsUnsigned() {
39
+ return true;
40
+ }
41
+
42
+ usesForeignKeys() {
43
+ return true;
44
+ }
45
+
46
+ transformErrors(error) {
47
+ super.transformErrors(error);
48
+ }
49
+ }
50
+
51
+ module.exports = MysqlDialect;