@quatrain/core 1.1.24 → 1.1.29

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 (234) hide show
  1. package/README.md +56 -8
  2. package/{lib → dist}/Core.d.ts +3 -17
  3. package/dist/Core.d.ts.map +1 -0
  4. package/dist/Core.js +105 -0
  5. package/dist/Core.js.map +1 -0
  6. package/dist/common/DataGenerator.d.ts +3 -0
  7. package/dist/common/DataGenerator.d.ts.map +1 -0
  8. package/dist/common/DataGenerator.js +86 -0
  9. package/dist/common/DataGenerator.js.map +1 -0
  10. package/{lib → dist}/common/ResourcesErrors.d.ts +1 -0
  11. package/dist/common/ResourcesErrors.d.ts.map +1 -0
  12. package/{lib → dist}/common/ResourcesErrors.js +1 -0
  13. package/dist/common/ResourcesErrors.js.map +1 -0
  14. package/{lib → dist}/common/statuses.d.ts +1 -0
  15. package/dist/common/statuses.d.ts.map +1 -0
  16. package/{lib → dist}/common/statuses.js +1 -0
  17. package/dist/common/statuses.js.map +1 -0
  18. package/{lib → dist}/components/AbstractObject.d.ts +1 -5
  19. package/dist/components/AbstractObject.d.ts.map +1 -0
  20. package/{lib → dist}/components/AbstractObject.js +7 -10
  21. package/dist/components/AbstractObject.js.map +1 -0
  22. package/{lib → dist}/components/BaseObject.d.ts +4 -17
  23. package/dist/components/BaseObject.d.ts.map +1 -0
  24. package/dist/components/BaseObject.js +71 -0
  25. package/dist/components/BaseObject.js.map +1 -0
  26. package/dist/components/BaseObjectProperties.d.ts +2 -0
  27. package/dist/components/BaseObjectProperties.d.ts.map +1 -0
  28. package/{lib → dist}/components/BaseObjectProperties.js +1 -2
  29. package/dist/components/BaseObjectProperties.js.map +1 -0
  30. package/{lib → dist}/components/DataObject.d.ts +2 -51
  31. package/dist/components/DataObject.d.ts.map +1 -0
  32. package/{lib → dist}/components/DataObject.js +38 -111
  33. package/dist/components/DataObject.js.map +1 -0
  34. package/{lib → dist}/components/Entity.d.ts +1 -0
  35. package/dist/components/Entity.d.ts.map +1 -0
  36. package/dist/components/Entity.js +48 -0
  37. package/dist/components/Entity.js.map +1 -0
  38. package/{lib → dist}/components/ObjectUri.d.ts +1 -26
  39. package/dist/components/ObjectUri.d.ts.map +1 -0
  40. package/{lib → dist}/components/ObjectUri.js +13 -41
  41. package/dist/components/ObjectUri.js.map +1 -0
  42. package/{lib → dist}/components/User.d.ts +1 -0
  43. package/dist/components/User.d.ts.map +1 -0
  44. package/{lib → dist}/components/User.js +7 -26
  45. package/dist/components/User.js.map +1 -0
  46. package/dist/components/__test__/fixtures/dao.d.ts +37 -0
  47. package/dist/components/__test__/fixtures/dao.d.ts.map +1 -0
  48. package/dist/components/__test__/fixtures/dao.js +100 -0
  49. package/dist/components/__test__/fixtures/dao.js.map +1 -0
  50. package/{lib → dist}/components/index.d.ts +5 -2
  51. package/dist/components/index.d.ts.map +1 -0
  52. package/{lib → dist}/components/index.js +1 -0
  53. package/dist/components/index.js.map +1 -0
  54. package/{lib → dist}/components/types/AbstractObjectClass.d.ts +1 -0
  55. package/dist/components/types/AbstractObjectClass.d.ts.map +1 -0
  56. package/dist/components/types/AbstractObjectClass.js +3 -0
  57. package/dist/components/types/AbstractObjectClass.js.map +1 -0
  58. package/{lib → dist}/components/types/BaseObjectClass.d.ts +1 -0
  59. package/dist/components/types/BaseObjectClass.d.ts.map +1 -0
  60. package/{lib → dist}/components/types/BaseObjectClass.js +1 -0
  61. package/dist/components/types/BaseObjectClass.js.map +1 -0
  62. package/{lib → dist}/components/types/BaseObjectType.d.ts +2 -1
  63. package/dist/components/types/BaseObjectType.d.ts.map +1 -0
  64. package/{lib → dist}/components/types/BaseObjectType.js +1 -0
  65. package/dist/components/types/BaseObjectType.js.map +1 -0
  66. package/{lib → dist}/components/types/DataObjectClass.d.ts +1 -0
  67. package/dist/components/types/DataObjectClass.d.ts.map +1 -0
  68. package/{lib → dist}/components/types/DataObjectClass.js +1 -0
  69. package/dist/components/types/DataObjectClass.js.map +1 -0
  70. package/dist/components/types/DataObjectParams.d.ts +8 -0
  71. package/dist/components/types/DataObjectParams.d.ts.map +1 -0
  72. package/dist/components/types/DataObjectParams.js +3 -0
  73. package/dist/components/types/DataObjectParams.js.map +1 -0
  74. package/{lib → dist}/components/types/DataObjectType.d.ts +1 -0
  75. package/dist/components/types/DataObjectType.d.ts.map +1 -0
  76. package/{lib → dist}/components/types/DataObjectType.js +1 -0
  77. package/dist/components/types/DataObjectType.js.map +1 -0
  78. package/{lib → dist}/components/types/EntityClass.d.ts +1 -0
  79. package/dist/components/types/EntityClass.d.ts.map +1 -0
  80. package/{lib → dist}/components/types/EntityClass.js +1 -0
  81. package/dist/components/types/EntityClass.js.map +1 -0
  82. package/{lib → dist}/components/types/Payload.d.ts +1 -0
  83. package/dist/components/types/Payload.d.ts.map +1 -0
  84. package/{lib → dist}/components/types/Payload.js +1 -0
  85. package/dist/components/types/Payload.js.map +1 -0
  86. package/{lib → dist}/components/types/Persisted.d.ts +1 -0
  87. package/dist/components/types/Persisted.d.ts.map +1 -0
  88. package/{lib → dist}/components/types/Persisted.js +1 -0
  89. package/dist/components/types/Persisted.js.map +1 -0
  90. package/dist/components/types/Properties.d.ts +2 -0
  91. package/dist/components/types/Properties.d.ts.map +1 -0
  92. package/{lib/components/types/AbstractObjectClass.js → dist/components/types/Properties.js} +1 -0
  93. package/dist/components/types/Properties.js.map +1 -0
  94. package/{lib → dist}/components/types/ProxyConstructor.d.ts +1 -0
  95. package/dist/components/types/ProxyConstructor.d.ts.map +1 -0
  96. package/{lib → dist}/components/types/ProxyConstructor.js +1 -0
  97. package/dist/components/types/ProxyConstructor.js.map +1 -0
  98. package/{lib → dist}/components/types/toJSONParams.d.ts +1 -0
  99. package/dist/components/types/toJSONParams.d.ts.map +1 -0
  100. package/{lib → dist}/components/types/toJSONParams.js +1 -0
  101. package/dist/components/types/toJSONParams.js.map +1 -0
  102. package/{lib → dist}/index.d.ts +5 -1
  103. package/dist/index.d.ts.map +1 -0
  104. package/{lib → dist}/index.js +4 -1
  105. package/dist/index.js.map +1 -0
  106. package/{lib → dist}/properties/ArrayProperty.d.ts +1 -0
  107. package/dist/properties/ArrayProperty.d.ts.map +1 -0
  108. package/{lib → dist}/properties/ArrayProperty.js +5 -4
  109. package/dist/properties/ArrayProperty.js.map +1 -0
  110. package/{lib → dist}/properties/BaseProperty.d.ts +3 -2
  111. package/dist/properties/BaseProperty.d.ts.map +1 -0
  112. package/{lib → dist}/properties/BaseProperty.js +18 -11
  113. package/dist/properties/BaseProperty.js.map +1 -0
  114. package/{lib → dist}/properties/BooleanProperty.d.ts +1 -0
  115. package/dist/properties/BooleanProperty.d.ts.map +1 -0
  116. package/{lib → dist}/properties/BooleanProperty.js +2 -1
  117. package/dist/properties/BooleanProperty.js.map +1 -0
  118. package/{lib → dist}/properties/CollectionProperty.d.ts +1 -0
  119. package/dist/properties/CollectionProperty.d.ts.map +1 -0
  120. package/{lib → dist}/properties/CollectionProperty.js +6 -4
  121. package/dist/properties/CollectionProperty.js.map +1 -0
  122. package/{lib → dist}/properties/DateTimeProperty.d.ts +1 -0
  123. package/dist/properties/DateTimeProperty.d.ts.map +1 -0
  124. package/{lib → dist}/properties/DateTimeProperty.js +12 -5
  125. package/dist/properties/DateTimeProperty.js.map +1 -0
  126. package/{lib → dist}/properties/EnumProperty.d.ts +1 -0
  127. package/dist/properties/EnumProperty.d.ts.map +1 -0
  128. package/{lib → dist}/properties/EnumProperty.js +4 -3
  129. package/dist/properties/EnumProperty.js.map +1 -0
  130. package/{lib → dist}/properties/FileProperty.d.ts +1 -0
  131. package/dist/properties/FileProperty.d.ts.map +1 -0
  132. package/{lib → dist}/properties/FileProperty.js +3 -2
  133. package/dist/properties/FileProperty.js.map +1 -0
  134. package/{lib → dist}/properties/HashProperty.d.ts +2 -7
  135. package/dist/properties/HashProperty.d.ts.map +1 -0
  136. package/{lib → dist}/properties/HashProperty.js +15 -23
  137. package/dist/properties/HashProperty.js.map +1 -0
  138. package/{lib → dist}/properties/MapProperty.d.ts +1 -0
  139. package/dist/properties/MapProperty.d.ts.map +1 -0
  140. package/{lib → dist}/properties/MapProperty.js +2 -1
  141. package/dist/properties/MapProperty.js.map +1 -0
  142. package/{lib → dist}/properties/NumberProperty.d.ts +1 -0
  143. package/dist/properties/NumberProperty.d.ts.map +1 -0
  144. package/{lib → dist}/properties/NumberProperty.js +15 -14
  145. package/dist/properties/NumberProperty.js.map +1 -0
  146. package/{lib → dist}/properties/ObjectProperty.d.ts +1 -0
  147. package/dist/properties/ObjectProperty.d.ts.map +1 -0
  148. package/{lib → dist}/properties/ObjectProperty.js +4 -3
  149. package/dist/properties/ObjectProperty.js.map +1 -0
  150. package/{lib → dist}/properties/Property.d.ts +1 -0
  151. package/dist/properties/Property.d.ts.map +1 -0
  152. package/{lib → dist}/properties/Property.js +12 -11
  153. package/dist/properties/Property.js.map +1 -0
  154. package/{lib → dist}/properties/StringProperty.d.ts +3 -5
  155. package/dist/properties/StringProperty.d.ts.map +1 -0
  156. package/{lib → dist}/properties/StringProperty.js +20 -28
  157. package/dist/properties/StringProperty.js.map +1 -0
  158. package/{lib → dist}/properties/index.d.ts +4 -2
  159. package/dist/properties/index.d.ts.map +1 -0
  160. package/{lib → dist}/properties/index.js +1 -0
  161. package/dist/properties/index.js.map +1 -0
  162. package/{lib → dist}/properties/types/AbstractPropertyType.d.ts +1 -0
  163. package/dist/properties/types/AbstractPropertyType.d.ts.map +1 -0
  164. package/{lib → dist}/properties/types/AbstractPropertyType.js +1 -0
  165. package/dist/properties/types/AbstractPropertyType.js.map +1 -0
  166. package/{lib → dist}/properties/types/PropertyClassType.d.ts +1 -0
  167. package/dist/properties/types/PropertyClassType.d.ts.map +1 -0
  168. package/{lib → dist}/properties/types/PropertyClassType.js +1 -0
  169. package/dist/properties/types/PropertyClassType.js.map +1 -0
  170. package/{lib → dist}/properties/types/PropertyHTMLType.d.ts +1 -0
  171. package/dist/properties/types/PropertyHTMLType.d.ts.map +1 -0
  172. package/{lib → dist}/properties/types/PropertyHTMLType.js +1 -2
  173. package/dist/properties/types/PropertyHTMLType.js.map +1 -0
  174. package/{lib → dist}/properties/types/PropertyTypes.d.ts +1 -0
  175. package/dist/properties/types/PropertyTypes.d.ts.map +1 -0
  176. package/{lib → dist}/properties/types/PropertyTypes.js +1 -0
  177. package/dist/properties/types/PropertyTypes.js.map +1 -0
  178. package/package.json +47 -46
  179. package/src/Core.ts +145 -0
  180. package/src/common/DataGenerator.ts +81 -0
  181. package/src/common/ResourcesErrors.ts +17 -0
  182. package/src/common/statuses.ts +20 -0
  183. package/src/components/AbstractObject.ts +71 -0
  184. package/src/components/BaseObject.ts +123 -0
  185. package/src/components/BaseObjectProperties.ts +65 -0
  186. package/src/components/DataObject.ts +418 -0
  187. package/src/components/Entity.ts +38 -0
  188. package/src/components/ObjectUri.test.ts +74 -0
  189. package/src/components/ObjectUri.ts +174 -0
  190. package/src/components/User.ts +111 -0
  191. package/src/components/index.ts +19 -0
  192. package/src/components/types/AbstractObjectClass.ts +7 -0
  193. package/src/components/types/BaseObjectClass.ts +8 -0
  194. package/src/components/types/BaseObjectType.ts +10 -0
  195. package/src/components/types/DataObjectClass.ts +27 -0
  196. package/src/components/types/DataObjectParams.ts +8 -0
  197. package/src/components/types/DataObjectType.ts +27 -0
  198. package/src/components/types/EntityClass.ts +3 -0
  199. package/src/components/types/Payload.ts +11 -0
  200. package/src/components/types/Persisted.ts +7 -0
  201. package/src/components/types/Properties.ts +1 -0
  202. package/src/components/types/ProxyConstructor.ts +13 -0
  203. package/src/components/types/toJSONParams.ts +12 -0
  204. package/src/index.ts +97 -0
  205. package/src/properties/ArrayProperty.test.ts +46 -0
  206. package/src/properties/ArrayProperty.ts +87 -0
  207. package/src/properties/BaseProperty.test.ts +79 -0
  208. package/src/properties/BaseProperty.ts +140 -0
  209. package/src/properties/BooleanProperty.ts +11 -0
  210. package/src/properties/CollectionProperty.test.ts +22 -0
  211. package/src/properties/CollectionProperty.ts +43 -0
  212. package/src/properties/DateTimeProperty.ts +47 -0
  213. package/src/properties/EnumProperty.ts +36 -0
  214. package/src/properties/FileProperty.ts +14 -0
  215. package/src/properties/HashProperty.test.ts +108 -0
  216. package/src/properties/HashProperty.ts +70 -0
  217. package/src/properties/MapProperty.ts +19 -0
  218. package/src/properties/NumberProperty.test.ts +38 -0
  219. package/src/properties/NumberProperty.ts +110 -0
  220. package/src/properties/ObjectProperty.ts +118 -0
  221. package/src/properties/Property.ts +85 -0
  222. package/src/properties/StringProperty.test.ts +46 -0
  223. package/src/properties/StringProperty.ts +121 -0
  224. package/src/properties/index.ts +62 -0
  225. package/src/properties/types/AbstractPropertyType.ts +8 -0
  226. package/src/properties/types/PropertyClassType.ts +11 -0
  227. package/src/properties/types/PropertyHTMLType.ts +23 -0
  228. package/src/properties/types/PropertyTypes.ts +13 -0
  229. package/lib/Core.js +0 -133
  230. package/lib/components/BaseObject.js +0 -103
  231. package/lib/components/BaseObjectProperties.d.ts +0 -1
  232. package/lib/components/Entity.js +0 -71
  233. package/lib/components/types/Properties.d.ts +0 -3
  234. package/lib/components/types/Properties.js +0 -1
@@ -0,0 +1,74 @@
1
+ import { ObjectUri } from './ObjectUri'
2
+
3
+ describe('Object Uri', () => {
4
+ test('relative uri', () => {
5
+ const uri = new ObjectUri('collection/uid', 'a label')
6
+ //expect(uri.backend).toBe('@default')
7
+ expect(uri.path).toBe('collection/uid')
8
+ //expect(uri.literal).toBe('@default:collection/uid')
9
+ expect(uri.collection).toBe('collection')
10
+ expect(uri.uid).toBe('uid')
11
+ expect(uri.label).toBe('a label')
12
+ })
13
+
14
+ test('relative uri with sub collection', () => {
15
+ const uri = new ObjectUri('collection/uid/subcollection/sid', 'a label')
16
+ //expect(uri.backend).toBe('@default')
17
+ // expect(uri.path).toBe('subcollection/sid')
18
+ // expect(uri.literal).toBe('@default:collection/uid/subcollection/sid')
19
+ expect(uri.collection).toBe('subcollection')
20
+ expect(uri.uid).toBe('sid')
21
+ expect(uri.label).toBe('a label')
22
+ })
23
+
24
+ test('uri with backend', () => {
25
+ const uri = new ObjectUri('@backend:collection/uid', 'a label')
26
+ expect(uri.backend).toBe('@backend')
27
+ expect(uri.path).toBe('collection/uid')
28
+ expect(uri.literal).toBe('@backend:collection/uid')
29
+ expect(uri.collection).toBe('collection')
30
+ expect(uri.uid).toBe('uid')
31
+ expect(uri.label).toBe('a label')
32
+ })
33
+
34
+ test('uri represents a subcollections-based path', () => {
35
+ const path = 'collection1/uid1/collection2/uid2/collection3/uid3'
36
+ const uri = new ObjectUri(path, 'a subcollections-based path')
37
+ //expect(uri.backend).toBe('@default')
38
+ // expect(uri.path).toBe('collection3/uid3')
39
+ // expect(uri.literal).toBe(`@default:${path}`)
40
+ expect(uri.collection).toBe('collection3')
41
+ expect(uri.uid).toBe('uid3')
42
+ expect(uri.parent).toBeInstanceOf(ObjectUri)
43
+ // expect(uri.parent?.literal).toBe('@default:collection1/uid1/collection2/uid2')
44
+ expect(uri.parent?.collection).toBe('collection2')
45
+ // expect(uri.parent?.path).toBe('collection2/uid2')
46
+ expect(uri.parent?.parent).toBeInstanceOf(ObjectUri)
47
+ // expect(uri.parent?.parent?.literal).toBe('@default:collection1/uid1')
48
+ expect(uri.parent?.parent?.collection).toBe('collection1')
49
+ expect(uri.parent?.parent?.path).toBe('collection1/uid1')
50
+ expect(uri.label).toBe('a subcollections-based path')
51
+ })
52
+
53
+ test('uri points to file', () => {
54
+ const uri = new ObjectUri('@s3:path/to/file.ext', 'a file')
55
+ expect(uri.backend).toBe('@s3')
56
+ expect(uri.path).toBe('path/to/file.ext')
57
+ expect(uri.literal).toBe('@s3:path/to/file.ext')
58
+ expect(uri.collection).toBeUndefined()
59
+ expect(uri.uid).toBe('file.ext')
60
+ expect(uri.label).toBe('a file')
61
+ })
62
+
63
+ test('uri without collection', () => {
64
+ const uri = new ObjectUri('uid')
65
+ // expect(uri.backend).toBe('@default')
66
+ expect(uri.path).toBe('uid')
67
+ //expect(uri.literal).toBe(`@default:${ObjectUri.MISSING_COLLECTION}/uid`)
68
+ expect(uri.collection).toBeUndefined()
69
+ expect(uri.uid).toBe('uid')
70
+
71
+ uri.collection = 'collection'
72
+ expect(uri.collection).toBe('collection')
73
+ })
74
+ })
@@ -0,0 +1,174 @@
1
+ export class ObjectUri {
2
+ static DEFAULT = '/'
3
+ static MISSING_COLLECTION = '_?_'
4
+
5
+ protected _str: string
6
+ protected _pairs: Array<string> = []
7
+ protected _literal: string = ''
8
+ protected _backend: string | undefined
9
+ protected _path: string = ObjectUri.DEFAULT
10
+ protected _uid: string | undefined = undefined
11
+ protected _collection: string | undefined = undefined
12
+ protected _label: string | undefined = ''
13
+ protected _objClass: any
14
+ protected _parent: ObjectUri | undefined
15
+
16
+ /**
17
+ * create object uri from string:
18
+ * ex: 'xyz', '@backend:xyz', 'collection/xyz', '@backend:collection/xyz'
19
+ * some backends will need a collection or table name, some may deduct it from object
20
+ * @param str partial or full resource path
21
+ * @param label optional label to describe path
22
+ */
23
+ constructor(str: string = '', label: string | undefined = '') {
24
+ this._str = str
25
+ this._label = label
26
+ if (str.includes(':')) {
27
+ const [backend, path] = str.split(':')
28
+ this._backend = backend
29
+ this._path = path
30
+ } else {
31
+ this._path = str
32
+ }
33
+ const parts = this._path.split(ObjectUri.DEFAULT)
34
+ const isFilePath = parts[parts.length - 1].indexOf('.') !== -1
35
+
36
+ if (isFilePath) {
37
+ this._uid = parts[parts.length - 1]
38
+ } else if (parts.length === 1) {
39
+ // It is allowed to only give the uid part of the uri
40
+ // provided that collection will be injected by another means
41
+ this._uid = parts[0].length > 0 ? parts[0] : undefined
42
+ this._collection = ObjectUri.MISSING_COLLECTION
43
+ this._pairs.push(
44
+ `${ObjectUri.MISSING_COLLECTION}${ObjectUri.DEFAULT}${this._uid}`
45
+ )
46
+ this._label = label || this._uid
47
+ } else if (parts.length % 2 === 0) {
48
+ this._path = parts.slice(parts.length - 2).join(ObjectUri.DEFAULT)
49
+ this._uid = parts[parts.length - 1]
50
+ this._collection = parts[parts.length - 2]
51
+ if (parts.length > 2) {
52
+ this._parent = new ObjectUri(
53
+ parts.slice(0, parts.length - 2).join(ObjectUri.DEFAULT)
54
+ )
55
+ }
56
+ this._label = label || this._uid
57
+ } else if (!isFilePath) {
58
+ // if the last part is not a filename with extension, throw error
59
+ throw new Error(
60
+ `Path parts number must be 1 or even, received: '${str}'`
61
+ )
62
+ }
63
+ }
64
+
65
+ set class(objClass: any) {
66
+ this._objClass = objClass
67
+ if (objClass) {
68
+ this._collection =
69
+ objClass.COLLECTION ||
70
+ (objClass.name && objClass.name.toLowerCase())
71
+ } else {
72
+ this._collection = undefined
73
+ }
74
+ }
75
+
76
+ get class() {
77
+ return this._objClass
78
+ }
79
+
80
+ get backend() {
81
+ return this._backend
82
+ }
83
+
84
+ /**
85
+ * Returns the full path of the resource
86
+ * including optional parents' path
87
+ * @returns string
88
+ */
89
+ get path() {
90
+ return this._parent
91
+ ? `${this._parent.path}${ObjectUri.DEFAULT}${this._path}`
92
+ : this._path
93
+ }
94
+
95
+ /**
96
+ * Returns the own path of the resource
97
+ * without the optional parents' paths
98
+ * @returns string
99
+ */
100
+ get ownPath() {
101
+ return this._path
102
+ }
103
+
104
+ get parent() {
105
+ return this._parent
106
+ }
107
+
108
+ set label(label: string | undefined) {
109
+ this._label = label
110
+ }
111
+
112
+ get label() {
113
+ return this._label
114
+ }
115
+
116
+ /**
117
+ * Return the full path litteral
118
+ * including the backend alias
119
+ * @returns string
120
+ */
121
+ get literal() {
122
+ return this._str.includes(':')
123
+ ? this._str
124
+ : `${this._backend}:${this._str}`
125
+ }
126
+
127
+ set path(path: string) {
128
+ // if (this._path && this._path !== ObjectUri.DEFAULT) {
129
+ // throw new Error(`Path value already set with '${this._path}'`)
130
+ // }
131
+ this._path = path
132
+ // TODO manage paths to subcollection
133
+ this._collection = path.split('/')[0]
134
+ this._literal = `${this._backend}:${this._path}`
135
+ this._uid = path.split('/').pop()
136
+ }
137
+
138
+ get uid() {
139
+ return this._uid
140
+ }
141
+
142
+ get collection(): string | undefined {
143
+ return this._collection === ObjectUri.MISSING_COLLECTION
144
+ ? undefined
145
+ : this._collection
146
+ }
147
+
148
+ set collection(collection: string | undefined) {
149
+ if (this.collection !== undefined) {
150
+ throw new Error('Collection value already set')
151
+ }
152
+ this._collection = collection
153
+ }
154
+
155
+ /**
156
+ * Return references to find object locally and remotely
157
+ * @returns object
158
+ */
159
+ toReference() {
160
+ return {
161
+ ref: this.path,
162
+ uri: this._literal,
163
+ label: this._label,
164
+ }
165
+ }
166
+
167
+ toJSON() {
168
+ return {
169
+ backend: this._backend,
170
+ ref: this.path,
171
+ label: this._label || '',
172
+ }
173
+ }
174
+ }
@@ -0,0 +1,111 @@
1
+ import { BaseObject } from './BaseObject'
2
+ import { DataObjectClass } from './types/DataObjectClass'
3
+ import { DateTimeProperty, EnumProperty, ObjectProperty } from '../properties'
4
+ import { HashProperty } from '../properties/HashProperty'
5
+ import { StringProperty } from '../properties/StringProperty'
6
+ import * as htmlType from '../properties/types/PropertyHTMLType'
7
+ import { BaseObjectProperties } from './BaseObjectProperties'
8
+ import { BaseObjectType } from './types/BaseObjectType'
9
+
10
+ export interface UserType extends BaseObjectType {
11
+ firstname: string
12
+ lastname: string
13
+ gender?: 'male' | 'female' | 'nonbinary'
14
+ birthday?: Date
15
+ password: string
16
+ email: string
17
+ [x: string]: any
18
+ }
19
+
20
+ /**
21
+ * Callback function to populate the 'name' property
22
+ * @param dao DataObject
23
+ * @returns
24
+ */
25
+ const onChange = (dao: DataObjectClass<any>) =>
26
+ dao.set('name', `${dao.val('firstname')} ${dao.val('lastname')}`)
27
+
28
+ export const UserProperties: any = [
29
+ ...BaseObjectProperties,
30
+ {
31
+ // change name property minLength
32
+ name: 'name',
33
+ type: StringProperty.TYPE,
34
+ minLength: 0,
35
+ htmlType: htmlType.NAME,
36
+ },
37
+ {
38
+ name: 'firstname',
39
+ mandatory: true,
40
+ type: StringProperty.TYPE,
41
+ minLength: 1,
42
+ maxLength: 100,
43
+ fullSearch: true,
44
+ htmlType: htmlType.GIVEN_NAME,
45
+ onChange,
46
+ },
47
+ {
48
+ name: 'lastname',
49
+ mandatory: true,
50
+ type: StringProperty.TYPE,
51
+ minLength: 1,
52
+ maxLength: 100,
53
+ fullSearch: true,
54
+ htmlType: htmlType.FAMILY_NAME,
55
+ onChange,
56
+ },
57
+ {
58
+ name: 'gender',
59
+ mandatory: false,
60
+ type: EnumProperty.TYPE,
61
+ values: ['male', 'female', 'nonbinary'],
62
+ htmlType: htmlType.GENDER,
63
+ },
64
+ {
65
+ name: 'birthday',
66
+ mandatory: false,
67
+ type: DateTimeProperty.TYPE,
68
+ htmlType: htmlType.BIRTHDAY,
69
+ },
70
+ {
71
+ name: 'email',
72
+ mandatory: true,
73
+ type: StringProperty.TYPE,
74
+ minLength: 1,
75
+ maxLength: 100,
76
+ fullSearch: true,
77
+ htmlType: htmlType.EMAIL,
78
+ },
79
+ {
80
+ name: 'phone',
81
+ type: StringProperty.TYPE,
82
+ minLength: 1,
83
+ maxLength: 100,
84
+ htmlType: htmlType.OFF,
85
+ },
86
+ {
87
+ name: 'password',
88
+ mandatory: true,
89
+ type: HashProperty.TYPE,
90
+ algorithm: HashProperty.ALGORITHM_SHA256,
91
+ salt: '', // you should override it in your code
92
+ minLength: 5,
93
+ maxLength: 20, // this is for the clear password
94
+ htmlType: htmlType.PASSWORD,
95
+ },
96
+ {
97
+ name: 'entity',
98
+ mandatory: false,
99
+ type: ObjectProperty.TYPE,
100
+ instanceOf: 'Entity',
101
+ },
102
+ ]
103
+
104
+ export class User extends BaseObject {
105
+ static PROPS_DEFINITION = UserProperties
106
+ static COLLECTION = 'user'
107
+
108
+ static async factory(src: any = undefined): Promise<User> {
109
+ return super.factory(src, User)
110
+ }
111
+ }
@@ -0,0 +1,19 @@
1
+ import { DataObject } from './DataObject'
2
+ import { DataObjectClass } from './types/DataObjectClass'
3
+ import { DataObjectParams } from './types/DataObjectParams'
4
+ import { ObjectUri } from './ObjectUri'
5
+ import { AbstractObject } from './AbstractObject'
6
+ import { BaseObjectProperties } from './BaseObjectProperties'
7
+ import { User, UserProperties } from './User'
8
+ import { Entity } from './Entity'
9
+
10
+ export {
11
+ DataObject,
12
+ ObjectUri,
13
+ AbstractObject,
14
+ User,
15
+ Entity,
16
+ BaseObjectProperties,
17
+ UserProperties,
18
+ }
19
+ export type { DataObjectParams, DataObjectClass }
@@ -0,0 +1,7 @@
1
+ import { DataObjectClass } from './DataObjectClass'
2
+
3
+ export interface AbstractObjectClass {
4
+ dataObject: DataObjectClass<any>
5
+ asReference(): any
6
+ val(val: string): any
7
+ }
@@ -0,0 +1,8 @@
1
+ import { AbstractObjectClass } from './AbstractObjectClass'
2
+ import { DataObjectClass } from './DataObjectClass'
3
+
4
+ export interface BaseObjectClass extends AbstractObjectClass {
5
+ dataObject: DataObjectClass<any>
6
+ asReference(): any
7
+ save(): Promise<this>
8
+ }
@@ -0,0 +1,10 @@
1
+ export interface BaseObjectType {
2
+ name: string
3
+ status?: string
4
+ createdBy?: any
5
+ createdAt?: number | string
6
+ updatedBy?: any
7
+ updatedAt?: number | string
8
+ deletedBy?: any
9
+ deletedAt?: number | string
10
+ }
@@ -0,0 +1,27 @@
1
+ import { PropertyClassType } from '../../properties/types/PropertyClassType'
2
+ import { ObjectUri } from '../ObjectUri'
3
+ import { toJSONParams } from './toJSONParams'
4
+
5
+ export interface DataObjectClass<T extends DataObjectClass<any>> {
6
+ uri: ObjectUri
7
+ uid: any
8
+ path: any
9
+ class: any
10
+ properties: any
11
+ parentProp: string | undefined
12
+
13
+ setProperties(properties: any): void
14
+ getProperties(type: any): any
15
+ addProperty(property: PropertyClassType): void
16
+ populate(data?: any): Promise<T>
17
+ isPopulated(): boolean
18
+ has(key: string): boolean
19
+ get(key: string): any
20
+ set(key: string, val: any): any
21
+ val(key: string): any
22
+ clone(data?: any): Promise<T>
23
+ toReference(): any
24
+ toJSON(params?: boolean | toJSONParams): any
25
+ populateFromData(data: { [x: string]: unknown }): this
26
+ asProxy(): any
27
+ }
@@ -0,0 +1,8 @@
1
+ import { DataObjectProperties } from '../../properties'
2
+ import { ObjectUri } from '../ObjectUri'
3
+
4
+ export interface DataObjectParams {
5
+ uri?: string | ObjectUri
6
+ properties: DataObjectProperties
7
+ parentProp?: string
8
+ }
@@ -0,0 +1,27 @@
1
+ import { PropertyClassType } from '../../properties/types/PropertyClassType'
2
+ import { ObjectUri } from '../ObjectUri'
3
+ import { toJSONParams } from './toJSONParams'
4
+
5
+ export interface DataObjectType {
6
+ uri: ObjectUri
7
+ uid: any
8
+ path: any
9
+ class: any
10
+ properties: any
11
+ parentProp: string | undefined
12
+
13
+ setProperties(properties: any): void
14
+ getProperties(type: any): any
15
+ addProperty(property: PropertyClassType): void
16
+ populate(data?: any): Promise<any>
17
+ isPopulated(): boolean
18
+ has(key: string): boolean
19
+ get(key: string): any
20
+ set(key: string, val: any): any
21
+ val(key: string): any
22
+ clone(data?: any): Promise<any>
23
+ toReference(): any
24
+ toJSON(params?: boolean | toJSONParams): any
25
+ populateFromData(data: { [x: string]: unknown }): this
26
+ asProxy(): any
27
+ }
@@ -0,0 +1,3 @@
1
+ import { BaseObjectClass } from './BaseObjectClass'
2
+
3
+ export interface EntityClass extends BaseObjectClass {}
@@ -0,0 +1,11 @@
1
+ import { BaseObjectType } from './BaseObjectType'
2
+
3
+ export default interface Payload<T extends BaseObjectType> {
4
+ items: T[]
5
+ meta: Meta
6
+ }
7
+
8
+ export interface Meta {
9
+ count: number
10
+ updatedAt: number
11
+ }
@@ -0,0 +1,7 @@
1
+ import { ObjectUri } from '../ObjectUri'
2
+ import { Proxy } from './ProxyConstructor'
3
+
4
+ export type Persisted<T> = {
5
+ uid: string
6
+ uri: ObjectUri
7
+ } & Proxy<T>
@@ -0,0 +1 @@
1
+ type Properties<T> = { name: keyof T }[]
@@ -0,0 +1,13 @@
1
+ import { BaseObjectClass } from './BaseObjectClass'
2
+
3
+ interface ProxyConstructor {
4
+ new <T extends object, H>(target: T, handler: ProxyHandler<T>): Proxy<H>
5
+ }
6
+
7
+ export const ProxyConstructor = Proxy as ProxyConstructor
8
+
9
+ export type Proxy<T> = T & {
10
+ readonly toJSON: () => object
11
+ readonly core: BaseObjectClass
12
+ //save: () => Promise<void>
13
+ }
@@ -0,0 +1,12 @@
1
+ export type toJSONParams = {
2
+ // Return objects as references
3
+ objectsAsReferences?: boolean
4
+ // Exclude URI data (uid, patch, etc.)
5
+ withoutURIData?: boolean
6
+ // Ignore property which value has not been changed
7
+ ignoreUnchanged?: boolean
8
+ // Ignore property which value is null
9
+ ignoreNulls?: boolean
10
+ // map of property values converters
11
+ converters?: { [propType: string]: Function }
12
+ }
package/src/index.ts ADDED
@@ -0,0 +1,97 @@
1
+ import { Core } from './Core'
2
+ import * as statuses from './common/statuses'
3
+ import { Property } from './properties/Property'
4
+ import { BaseProperty, BasePropertyType } from './properties/BaseProperty'
5
+ import { StringProperty, StringPropertyType } from './properties/StringProperty'
6
+ import { NumberProperty, NumberPropertyType } from './properties/NumberProperty'
7
+ import { EnumProperty, EnumPropertyType } from './properties/EnumProperty'
8
+ import {
9
+ BooleanProperty,
10
+ BooleanPropertyType,
11
+ } from './properties/BooleanProperty'
12
+ import {
13
+ ObjectProperty,
14
+ ObjectPropertyType,
15
+ returnAs,
16
+ } from './properties/ObjectProperty'
17
+ import {
18
+ DateTimeProperty,
19
+ DateTimePropertyType,
20
+ } from './properties/DateTimeProperty'
21
+ import { HashProperty, HashPropertyType } from './properties/HashProperty'
22
+ import {
23
+ CollectionProperty,
24
+ CollectionPropertyType,
25
+ } from './properties/CollectionProperty'
26
+ import { ArrayProperty, ArrayPropertyType } from './properties/ArrayProperty'
27
+ import { AbstractObject } from './components/AbstractObject'
28
+ import { ObjectUri } from './components/ObjectUri'
29
+ import { DataObjectClass } from './components/types/DataObjectClass'
30
+ import { DataObject } from './components/DataObject'
31
+ import { DataObjectProperties } from './properties'
32
+ import { BaseObject } from './components/BaseObject'
33
+ import { BaseObjectProperties } from './components/BaseObjectProperties'
34
+ import { BaseObjectType } from './components/types/BaseObjectType'
35
+ import { Proxy } from './components/types/ProxyConstructor'
36
+
37
+ import { UserType, User, UserProperties } from './components/User'
38
+ import { EntityType, Entity } from './components/Entity'
39
+
40
+ import {
41
+ BadRequestError,
42
+ UnauthorizedError,
43
+ ForbiddenError,
44
+ NotFoundError,
45
+ GoneError,
46
+ } from './common/ResourcesErrors'
47
+ import { DataObjectParams } from './components/types/DataObjectParams'
48
+
49
+ export {
50
+ statuses,
51
+ Core,
52
+ Property,
53
+ BaseProperty,
54
+ BooleanProperty,
55
+ DateTimeProperty,
56
+ EnumProperty,
57
+ HashProperty,
58
+ CollectionProperty,
59
+ ArrayProperty,
60
+ ObjectProperty,
61
+ NumberProperty,
62
+ returnAs,
63
+ StringProperty,
64
+ ObjectUri,
65
+ DataObject,
66
+ AbstractObject,
67
+ BaseObject,
68
+ BaseObjectProperties,
69
+ User,
70
+ UserProperties,
71
+ Entity,
72
+ BadRequestError,
73
+ UnauthorizedError,
74
+ ForbiddenError,
75
+ NotFoundError,
76
+ GoneError,
77
+ }
78
+
79
+ export type {
80
+ BasePropertyType,
81
+ BooleanPropertyType,
82
+ DataObjectParams,
83
+ DateTimePropertyType,
84
+ EnumPropertyType,
85
+ HashPropertyType,
86
+ CollectionPropertyType,
87
+ ArrayPropertyType,
88
+ ObjectPropertyType,
89
+ NumberPropertyType,
90
+ StringPropertyType,
91
+ DataObjectClass,
92
+ DataObjectProperties,
93
+ BaseObjectType,
94
+ UserType,
95
+ Proxy,
96
+ EntityType,
97
+ }
@@ -0,0 +1,46 @@
1
+ import { ArrayProperty } from './ArrayProperty'
2
+ import { StringProperty } from './StringProperty'
3
+
4
+ const arrayValue = ['ab', 'cd', 'ef']
5
+ const transformer = (array: Array<string>) => array.join()
6
+
7
+ describe('Array Property without digits', () => {
8
+ const prop = new ArrayProperty({ name: 'toto', allowNumbers: false })
9
+ test('allow numbers is not set', () =>
10
+ expect(prop.allows).not.toContain(StringProperty.ALLOW_NUMBERS))
11
+ test('no numbers allowed', () => expect(() => prop.set([123])).toThrow())
12
+ })
13
+
14
+ describe('Array Property without strings', () => {
15
+ const prop = new ArrayProperty({ name: 'toto', allowStrings: false })
16
+ test('allow strings is not set', () =>
17
+ expect(prop.allows).not.toContain(StringProperty.ALLOW_STRINGS))
18
+ test('no strings allowed', () => expect(() => prop.set(['value'])).toThrow())
19
+ })
20
+
21
+ describe('Array Property with minimal length', () => {
22
+ const prop = new ArrayProperty({ name: 'toto', minLength: 3 })
23
+ test('minlength is set', () => expect(prop.minLength).toBeGreaterThan(0))
24
+ test('minimum length of 3', () =>
25
+ expect(() => prop.set(['value', 123])).toThrow())
26
+ })
27
+
28
+ describe('Array Property with maximal length', () => {
29
+ const prop = new ArrayProperty({ name: 'toto', maxLength: 2 })
30
+ test('maxlength is set', () => expect(prop.maxLength).toBeGreaterThan(0))
31
+ test('maximum length of 2', () =>
32
+ expect(() => prop.set(['value', 123, 'blue'])).toThrow())
33
+ })
34
+
35
+ describe('Array Property returns array', () => {
36
+ const prop = new ArrayProperty({ name: 'toto' })
37
+ test('transform array to string', () =>
38
+ expect(prop.set(arrayValue).get()).toBe(arrayValue))
39
+ })
40
+
41
+ // describe('Array Property supports registered transformer', () => {
42
+ // const prop = new ArrayProperty({ name: 'toto' })
43
+ // //prop.addTransformer(transformer)
44
+ // test('transform array to string', () =>
45
+ // expect(prop.set(arrayValue).get()).toBe('abcdef'))
46
+ // })