Explorar el Código

:recycle: cleaning up schemas and models

neo
toj hace 2 años
padre
commit
9956dbe859

backend/lib/models/memberships.js → backend/lib/models/membership.js Ver fichero


+ 9
- 11
backend/lib/models/profile.js Ver fichero

@@ -1,8 +1,9 @@
1 1
 const Schwifty = require('@hapipal/schwifty')
2
-const Joi = require('joi')
3
-const TagAssociation = require('./tag-association')
4
-const Response = require('./response')
5
-const User = require('./user')
2
+
3
+const profileSchema = require('../schemas/profile')
4
+const TagAssociationModel = require('./tag-association')
5
+const ResponseModel = require('./response')
6
+const UserModel = require('./user')
6 7
 
7 8
 module.exports = class Profile extends Schwifty.Model {
8 9
     static get tableName() {
@@ -12,7 +13,7 @@ module.exports = class Profile extends Schwifty.Model {
12 13
         return {
13 14
             tags: {
14 15
                 relation: Schwifty.Model.HasManyRelation,
15
-                modelClass: TagAssociation,
16
+                modelClass: TagAssociationModel,
16 17
                 join: {
17 18
                     from: 'tag_associations.profile_id',
18 19
                     to: 'profiles.profile_id',
@@ -20,7 +21,7 @@ module.exports = class Profile extends Schwifty.Model {
20 21
             },
21 22
             responses: {
22 23
                 relation: Schwifty.Model.HasManyRelation,
23
-                modelClass: Response,
24
+                modelClass: ResponseModel,
24 25
                 join: {
25 26
                     from: 'responses.profile_id',
26 27
                     to: 'profiles.profile_id',
@@ -28,7 +29,7 @@ module.exports = class Profile extends Schwifty.Model {
28 29
             },
29 30
             user: {
30 31
                 relation: Schwifty.Model.BelongsToOneRelation,
31
-                modelClass: User,
32
+                modelClass: UserModel,
32 33
                 join: {
33 34
                     from: 'users.user_id',
34 35
                     to: 'profiles.user_id',
@@ -37,9 +38,6 @@ module.exports = class Profile extends Schwifty.Model {
37 38
         }
38 39
     }
39 40
     static get joiSchema() {
40
-        return Joi.object({
41
-            profile_id: Joi.number(),
42
-            user_id: Joi.number(),
43
-        })
41
+        return profileSchema.single
44 42
     }
45 43
 }

backend/lib/models/tag-associations.js → backend/lib/models/tag-association.js Ver fichero

@@ -1,6 +1,6 @@
1 1
 const Schwifty = require('@hapipal/schwifty')
2 2
 
3
-const tagSchema = require('../schemas/tags')
3
+const associationSchema = require('../schemas/tag-associations')
4 4
 const Tag = require('./tag')
5 5
 
6 6
 module.exports = class TagAssociation extends Schwifty.Model {
@@ -20,6 +20,6 @@ module.exports = class TagAssociation extends Schwifty.Model {
20 20
         }
21 21
     }
22 22
     static get joiSchema() {
23
-        return tagSchema.association
23
+        return associationSchema.single
24 24
     }
25 25
 }

+ 1
- 0
backend/lib/models/tags.js Ver fichero

@@ -1,4 +1,5 @@
1 1
 const Schwifty = require('@hapipal/schwifty')
2
+
2 3
 const tagSchema = require('../schemas/tags')
3 4
 
4 5
 module.exports = class Tag extends Schwifty.Model {

+ 16
- 2
backend/lib/models/user.js Ver fichero

@@ -1,6 +1,8 @@
1
-const { single } = require('../schemas/user')
2 1
 const Schwifty = require('@hapipal/schwifty')
3 2
 
3
+const ProfileModel = require('../profiles')
4
+const userSchema = require('../schemas/users')
5
+
4 6
 /**
5 7
  * @typedef User
6 8
  */
@@ -8,7 +10,19 @@ module.exports = class User extends Schwifty.Model {
8 10
     static get tableName() {
9 11
         return 'users'
10 12
     }
13
+    static get relationMappings() {
14
+        return {
15
+            profiles: {
16
+                relation: Schwifty.Model.HasManyRelation,
17
+                modelClass: ProfileModel,
18
+                join: {
19
+                    from: 'user.profile_id',
20
+                    to: 'profiles.profile_id',
21
+                },
22
+            },
23
+        }
24
+    }
11 25
     static get joiSchema() {
12
-        return single
26
+        return userSchema.single
13 27
     }
14 28
 }

+ 19
- 0
backend/lib/schemas/media.js Ver fichero

@@ -0,0 +1,19 @@
1
+const Joi = require('joi')
2
+
3
+const validator = Joi.object({
4
+    profile_id: Joi.number(),
5
+    media_url: Joi.string().required(),
6
+    media_type_url: Joi.string().required(),
7
+}).label('media__single_validator')
8
+
9
+const single = Joi.object({
10
+    profile_id: Joi.number(),
11
+    media_url: Joi.string().required(),
12
+    media_type: Joi.string().required(),
13
+    _is_deleted: Joi.number().required(),
14
+}).label('media__single')
15
+
16
+module.exports = {
17
+    single,
18
+    validator,
19
+}

+ 18
- 0
backend/lib/schemas/memberships.js Ver fichero

@@ -0,0 +1,18 @@
1
+const validator = Joi.object({
2
+    membership_id: Joi.number(),
3
+    profile_id: Joi.number(),
4
+    grouping_id: Joi.number(),
5
+}).label('membership__single_validator')
6
+
7
+// single is used to define database models
8
+const single = Joi.object({
9
+    membership_id: Joi.number(),
10
+    profile_id: Joi.number(),
11
+    grouping_id: Joi.number(),
12
+    _is_deleted: Joi.number(),
13
+}).label('membership__single')
14
+
15
+module.exports = {
16
+    single,
17
+    validator,
18
+}

+ 35
- 0
backend/lib/schemas/profile.js Ver fichero

@@ -0,0 +1,35 @@
1
+const Joi = require('joi')
2
+// const surveyResponseSchema = require('./responses')
3
+const userSchema = require('./users')
4
+const associationSchema = require('./tag-associations')
5
+
6
+/**
7
+ * Profiles
8
+ * A profile links a human user to multiple
9
+ * job seeking or job posting profiles.
10
+ */
11
+
12
+// validator is used to validate route input/output
13
+const validator = Joi.object({
14
+    profile_id: Joi.number(),
15
+    user: userSchema.single,
16
+    // responses: surveyResponseSchema.list,
17
+    // reveal: Joi.array().items(),
18
+    tags: associationSchema.list,
19
+    profile_description: Joi.string().allow(null, ''),
20
+}).label('profile__single_validator')
21
+
22
+const list = Joi.array().items(validator).label('profile__list')
23
+
24
+// single is used to define database models
25
+const single = Joi.object({
26
+    profile_id: Joi.number(),
27
+    user_id: Joi.number(),
28
+    _is_deleted: Joi.number(),
29
+}).label('profile__single')
30
+
31
+module.exports = {
32
+    single,
33
+    validator,
34
+    list,
35
+}

+ 32
- 0
backend/lib/schemas/tag-associations.js Ver fichero

@@ -0,0 +1,32 @@
1
+const Joi = require('joi')
2
+
3
+/**
4
+ * Tag Associations
5
+ * An association is a record linking a
6
+ * profile or grouping to a tag.
7
+ */
8
+
9
+// validator is used to validate route input/output
10
+const validator = Joi.object({
11
+    tag_association_id: Joi.number(),
12
+    profile_id: Joi.number().required(),
13
+    grouping_id: Joi.number(),
14
+    tag_id: Joi.number().required(),
15
+}).label('association__single_validator')
16
+
17
+const list = Joi.array().items(single).label('association__list')
18
+
19
+// single is used to define database models
20
+const single = Joi.object({
21
+    tag_association_id: Joi.number(),
22
+    profile_id: Joi.number().required(),
23
+    grouping_id: Joi.number(),
24
+    tag_id: Joi.number().required(),
25
+    _is_deleted: Joi.boolean().required(),
26
+}).label('association__single')
27
+
28
+module.exports = {
29
+    single,
30
+    validator,
31
+    list,
32
+}

+ 30
- 0
backend/lib/schemas/tags.js Ver fichero

@@ -0,0 +1,30 @@
1
+const Joi = require('joi')
2
+
3
+/**
4
+ * Tags
5
+ * A tag describes some attribute to be
6
+ * associated with a profile or grouping.
7
+ */
8
+
9
+// validator is used to validate route input/output
10
+const validator = Joi.object({
11
+    tag_id: Joi.number(),
12
+    tag_category: Joi.string(),
13
+    tag_description: Joi.string(),
14
+}).label('tag__single_validator')
15
+
16
+const list = Joi.array().items(validator).label('tag__list')
17
+
18
+// single is used to define database models
19
+const single = Joi.object({
20
+    tag_id: Joi.number(),
21
+    tag_category: Joi.string(),
22
+    tag_description: Joi.string(),
23
+    _is_active: Joi.number().required(),
24
+}).label('tag__single')
25
+
26
+module.exports = {
27
+    single,
28
+    validator,
29
+    list
30
+}

+ 0
- 12
backend/lib/schemas/user.js Ver fichero

@@ -1,12 +0,0 @@
1
-const Joi = require('joi')
2
-
3
-module.exports = {
4
-    single: Joi.object({
5
-        user_id: Joi.number(),
6
-        user_name: Joi.string(),
7
-        user_email: Joi.string().required(),
8
-        is_poster: Joi.number().required(),
9
-        is_admin: Joi.number().required(),
10
-        is_verified: Joi.number().required(),
11
-    }).label('user_single'),
12
-}

+ 25
- 0
backend/lib/schemas/users.js Ver fichero

@@ -0,0 +1,25 @@
1
+const Joi = require('joi')
2
+
3
+/**
4
+ * Users
5
+ * A user record is tied to a single person.
6
+ */
7
+
8
+// validator is used to validate route input/output
9
+const validator = Joi.object({
10
+    user_id: Joi.number(),
11
+    user_email: Joi.string().required(),
12
+}).label('user__single_validator')
13
+
14
+// single is used to define database models
15
+const single = Joi.object({
16
+    user_id: Joi.number(),
17
+    user_email: Joi.string().required(),
18
+    is_poster: Joi.number().required(),
19
+    is_verified: Joi.number().required(),
20
+}).label('user__single')
21
+
22
+module.exports = {
23
+    single,
24
+    validator,
25
+}

Loading…
Cancelar
Guardar