@pinia-orm/normalizr-edge 1.7.1-28257628.bbb8bab
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.
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/dist/index.cjs +182 -0
- package/dist/index.mjs +179 -0
- package/index.d.ts +63 -0
- package/package.json +72 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022-present Gregor Becker
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
[](https://github.com/storm-tail/pinia-orm)
|
2
|
+
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
5
|
+
[![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
|
6
|
+
[![Coverage][code-coverage-src]][code-coverage-href]
|
7
|
+
[![License][license-src]][license-href]
|
8
|
+
|
9
|
+
# Welcome to pinia-orm
|
10
|
+
|
11
|
+
> Intuitive, type safe and flexible ORM for Pinia based on [Vuex ORM Next](https://github.com/vuex-orm/vuex-orm-next)
|
12
|
+
|
13
|
+
- [✨ Release Notes](https://pinia-orm.codedredd.de/changelog)
|
14
|
+
- [📖 Documentation](https://pinia-orm.codedredd.de)
|
15
|
+
- [👾 Playground](https://pinia-orm-play.codedredd.de)
|
16
|
+
|
17
|
+
## Migration from vuex-orm
|
18
|
+
|
19
|
+
You want to migrate from vuex to pinia and with it vuex-orm to pinia-orm but you don't know yet?
|
20
|
+
Well maybe this table will help you to decide. This comparison is just about facts and current state.
|
21
|
+
|
22
|
+
| Features | pinia-orm@v1.4.0 | @vuex-orm/core@0.36.4 | @vuex-orm/core@1.0.0-draft.16 |
|
23
|
+
|------------------------------------------------------------------------|------------------------------------------------------------| ----------------------------------------------------------------- | ------------------------------------------------------------------------- |
|
24
|
+
| Bundle Size (Min + GZIP) | [9.9 KB](https://bundlephobia.com/package/pinia-orm@1.4.0) | [16.7 KB](https://bundlephobia.com/package/@vuex-orm/core@0.36.4) | [12.6 KB](https://bundlephobia.com/package/@vuex-orm/core@1.0.0-draft.16) |
|
25
|
+
| Relations (hasMany, belongsTo, morphOne, hasManyBy, hasOne, morphTo) | ✅ | ✅ | ✅ |
|
26
|
+
| Relations (morphMany, belongsToMany, hasManyThrough) | ✅ | ✅ | ❌ |
|
27
|
+
| Relations (morphToMany, morphedByMany) | ❌ | ✅ | ❌ |
|
28
|
+
| Mutators | ✅ | ✅ | ❌ |
|
29
|
+
| Casts | ✅ | ❌ | ❌ |
|
30
|
+
| Decorators | ✅ | ❌ | ✅ |
|
31
|
+
| Single Table Inheritance | ✅ | ✅ | ❌ |
|
32
|
+
| Lifecycle Hooks | ✅ | ✅ | ❌ |
|
33
|
+
| Aggregates | ✅ | ✅ | ❌ |
|
34
|
+
| Query (orHas, doesntHave, orDoesntHave, whereHas, orWhereHas, groupBy) | ✅ | ❌ | ❌ |
|
35
|
+
| Collection Helpers | ✅ | (✅) can use pinia-orm helpers too | (✅) can use pinia-orm helpers too |
|
36
|
+
| Hidden Fields | ✅ | ❌ | ❌ |
|
37
|
+
| Metadata field | ✅ | ❌ | ❌ |
|
38
|
+
| Caching of queries with gc | ✅ | (✅) with plugin | ❌ |
|
39
|
+
|
40
|
+
If you decide to migrate then there are some breaking changes. A guide how to migrate will be written.
|
41
|
+
Small overview:
|
42
|
+
|
43
|
+
- Fields are by default `null`
|
44
|
+
- Renamed some functions aligning more with laravel naming
|
45
|
+
- Code is based on `vuex-orm-next` and not on `vuex-orm` !
|
46
|
+
|
47
|
+
## Help me keep working on this project 💚
|
48
|
+
|
49
|
+
- [Become a Sponsor on GitHub](https://github.com/sponsors/codedredd)
|
50
|
+
- [One-time donation via PayPal](https://paypal.me/dredd1984)
|
51
|
+
|
52
|
+
<p align="center">
|
53
|
+
<a href="https://pinia-orm.codedredd.de/sponsorkit/sponsors.png">
|
54
|
+
<img src='https://pinia-orm.codedredd.de/sponsorkit/sponsors.svg'/>
|
55
|
+
</a>
|
56
|
+
</p>
|
57
|
+
|
58
|
+
---
|
59
|
+
|
60
|
+
## 💻 Development
|
61
|
+
|
62
|
+
- Clone this repository
|
63
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
|
64
|
+
- Install dependencies using `pnpm install`
|
65
|
+
- Build normalizr package: `pnpm build`
|
66
|
+
- Run interactive tests using `cd packages/pinia-orm && pnpm test:ui`
|
67
|
+
|
68
|
+
## Credits
|
69
|
+
|
70
|
+
- [Kia King Ishii](https://github.com/kiaking)
|
71
|
+
- [Cuebit](https://github.com/cuebit)
|
72
|
+
- [Posva](https://github.com/posva)
|
73
|
+
|
74
|
+
## Related projects
|
75
|
+
|
76
|
+
- [Vuex ORM](https://github.com/vuex-orm/vuex-orm)
|
77
|
+
- [Vuex ORM Next](https://github.com/vuex-orm/vuex-orm-next)
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
Made with ❤️
|
82
|
+
|
83
|
+
Published under [MIT License](./LICENCE).
|
84
|
+
|
85
|
+
<!-- Badges -->
|
86
|
+
|
87
|
+
[npm-version-src]: https://img.shields.io/npm/v/pinia-orm/latest.svg
|
88
|
+
[code-coverage-src]: https://img.shields.io/codecov/c/github/CodeDredd/pinia-orm?logo=Codecov&logoColor=white&token=BYLAJJOOLS
|
89
|
+
[code-coverage-href]: https://app.codecov.io/gh/CodeDredd/pinia-orm
|
90
|
+
[npm-version-href]: https://npmjs.com/package/pinia-orm
|
91
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/pinia-orm.svg
|
92
|
+
[npm-downloads-href]: https://npmjs.com/package/pinia-orm
|
93
|
+
[github-actions-ci-src]: https://github.com/codedredd/pinia-orm/actions/workflows/ci.yml/badge.svg
|
94
|
+
[github-actions-ci-href]: https://github.com/codedredd/pinia-orm/actions?query=workflow%3Aci
|
95
|
+
[license-src]: https://img.shields.io/npm/l/pinia-orm.svg
|
96
|
+
[license-href]: https://npmjs.com/package/pinia-orm
|
package/dist/index.cjs
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
class EntitySchema {
|
4
|
+
constructor(key, definition = {}, options = {}) {
|
5
|
+
if (!key || typeof key !== "string") {
|
6
|
+
throw new Error(`Expected a string key for Entity, but found ${key}.`);
|
7
|
+
}
|
8
|
+
const {
|
9
|
+
idAttribute = "id",
|
10
|
+
mergeStrategy = (entityA, entityB) => {
|
11
|
+
return { ...entityA, ...entityB };
|
12
|
+
},
|
13
|
+
processStrategy = (input) => ({ ...input })
|
14
|
+
} = options;
|
15
|
+
this._key = key;
|
16
|
+
this._getId = idAttribute;
|
17
|
+
this._mergeStrategy = mergeStrategy;
|
18
|
+
this._processStrategy = processStrategy;
|
19
|
+
this.define(definition);
|
20
|
+
}
|
21
|
+
get key() {
|
22
|
+
return this._key;
|
23
|
+
}
|
24
|
+
define(definition) {
|
25
|
+
this.schema = Object.keys(definition).reduce((entitySchema, key) => {
|
26
|
+
const schema = definition[key];
|
27
|
+
return { ...entitySchema, [key]: schema };
|
28
|
+
}, this.schema || {});
|
29
|
+
}
|
30
|
+
getId(input, parent, key) {
|
31
|
+
return this._getId(input, parent, key);
|
32
|
+
}
|
33
|
+
merge(entityA, entityB) {
|
34
|
+
return this._mergeStrategy(entityA, entityB);
|
35
|
+
}
|
36
|
+
normalize(input, parent, key, visit, addEntity, visitedEntities) {
|
37
|
+
const id = this.getId(input, parent, key);
|
38
|
+
const entityType = this.key;
|
39
|
+
if (!(entityType in visitedEntities)) {
|
40
|
+
visitedEntities[entityType] = {};
|
41
|
+
}
|
42
|
+
if (!(id in visitedEntities[entityType])) {
|
43
|
+
visitedEntities[entityType][id] = [];
|
44
|
+
}
|
45
|
+
if (visitedEntities[entityType][id].includes(input)) {
|
46
|
+
return id;
|
47
|
+
}
|
48
|
+
visitedEntities[entityType][id].push(input);
|
49
|
+
const processedEntity = this._processStrategy(input, parent, key);
|
50
|
+
Object.keys(this.schema).forEach((key2) => {
|
51
|
+
if (processedEntity.hasOwnProperty(key2) && typeof processedEntity[key2] === "object") {
|
52
|
+
const schema = this.schema[key2];
|
53
|
+
const resolvedSchema = typeof schema === "function" ? schema(input) : schema;
|
54
|
+
processedEntity[key2] = visit(
|
55
|
+
processedEntity[key2],
|
56
|
+
processedEntity,
|
57
|
+
key2,
|
58
|
+
resolvedSchema,
|
59
|
+
addEntity,
|
60
|
+
visitedEntities
|
61
|
+
);
|
62
|
+
}
|
63
|
+
});
|
64
|
+
addEntity(this, processedEntity, input, parent, key);
|
65
|
+
return id;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
class PolymorphicSchema {
|
70
|
+
constructor(definition, schemaAttribute) {
|
71
|
+
if (schemaAttribute) {
|
72
|
+
this._schemaAttribute = typeof schemaAttribute === "string" ? (input) => input[schemaAttribute] : schemaAttribute;
|
73
|
+
}
|
74
|
+
this.define(definition);
|
75
|
+
}
|
76
|
+
get isSingleSchema() {
|
77
|
+
return !this._schemaAttribute;
|
78
|
+
}
|
79
|
+
define(definition) {
|
80
|
+
this.schema = definition;
|
81
|
+
}
|
82
|
+
getSchemaAttribute(input, parent, key) {
|
83
|
+
return !this.isSingleSchema && this._schemaAttribute(input, parent, key);
|
84
|
+
}
|
85
|
+
inferSchema(input, parent, key) {
|
86
|
+
if (this.isSingleSchema) {
|
87
|
+
return this.schema;
|
88
|
+
}
|
89
|
+
const attr = this.getSchemaAttribute(input, parent, key);
|
90
|
+
return this.schema[attr];
|
91
|
+
}
|
92
|
+
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) {
|
93
|
+
const schema = this.inferSchema(value, parent, key);
|
94
|
+
if (!schema) {
|
95
|
+
return value;
|
96
|
+
}
|
97
|
+
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities);
|
98
|
+
return this.isSingleSchema || normalizedValue === void 0 || normalizedValue === null ? normalizedValue : { id: normalizedValue, schema: this.getSchemaAttribute(value, parent, key) };
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
class UnionSchema extends PolymorphicSchema {
|
103
|
+
constructor(definition, schemaAttribute) {
|
104
|
+
if (!schemaAttribute) {
|
105
|
+
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');
|
106
|
+
}
|
107
|
+
super(definition, schemaAttribute);
|
108
|
+
}
|
109
|
+
normalize(input, parent, key, visit, addEntity, visitedEntities) {
|
110
|
+
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
const validateSchema = (definition) => {
|
115
|
+
if (Array.isArray(definition) && definition.length > 1) {
|
116
|
+
throw new Error(`Expected schema definition to be a single schema, but found ${definition.length}.`);
|
117
|
+
}
|
118
|
+
return definition[0];
|
119
|
+
};
|
120
|
+
const getValues = (input) => Array.isArray(input) ? input : Object.keys(input).map((key) => input[key]);
|
121
|
+
const normalize$2 = (schema, input, parent, key, visit, addEntity, visitedEntities) => {
|
122
|
+
return getValues(input).map((value) => visit(value, parent, key, validateSchema(schema), addEntity, visitedEntities));
|
123
|
+
};
|
124
|
+
class ArraySchema extends PolymorphicSchema {
|
125
|
+
normalize(input, parent, key, visit, addEntity, visitedEntities) {
|
126
|
+
return getValues(input).map((value) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter((value) => value !== void 0 && value !== null);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => {
|
131
|
+
const object = { ...input };
|
132
|
+
Object.keys(schema).forEach((key2) => {
|
133
|
+
const localSchema = schema[key2];
|
134
|
+
const resolvedLocalSchema = typeof localSchema === "function" ? localSchema(input) : localSchema;
|
135
|
+
const value = visit(input[key2], input, key2, resolvedLocalSchema, addEntity, visitedEntities);
|
136
|
+
if (value === void 0 || value === null) {
|
137
|
+
delete object[key2];
|
138
|
+
} else {
|
139
|
+
object[key2] = value;
|
140
|
+
}
|
141
|
+
});
|
142
|
+
return object;
|
143
|
+
};
|
144
|
+
|
145
|
+
const visit = (value, parent, key, schema2, addEntity, visitedEntities) => {
|
146
|
+
if (typeof value !== "object" || !value) {
|
147
|
+
return value;
|
148
|
+
}
|
149
|
+
if (typeof schema2 === "object" && (!schema2.normalize || typeof schema2.normalize !== "function")) {
|
150
|
+
const method = Array.isArray(schema2) ? normalize$2 : normalize$1;
|
151
|
+
return method(schema2, value, parent, key, visit, addEntity, visitedEntities);
|
152
|
+
}
|
153
|
+
return schema2.normalize(value, parent, key, visit, addEntity, visitedEntities);
|
154
|
+
};
|
155
|
+
const addEntities = (entities) => (schema2, processedEntity, value, parent, key) => {
|
156
|
+
const schemaKey = schema2.key;
|
157
|
+
const id = schema2.getId(value, parent, key);
|
158
|
+
if (!(schemaKey in entities)) {
|
159
|
+
entities[schemaKey] = {};
|
160
|
+
}
|
161
|
+
entities[schemaKey][id] = entities[schemaKey][id] ? schema2.merge(entities[schemaKey][id], processedEntity) : processedEntity;
|
162
|
+
};
|
163
|
+
const schema = {
|
164
|
+
Array: ArraySchema,
|
165
|
+
Entity: EntitySchema,
|
166
|
+
Union: UnionSchema
|
167
|
+
};
|
168
|
+
const normalize = (input, schema2) => {
|
169
|
+
if (!input || typeof input !== "object") {
|
170
|
+
throw new Error(
|
171
|
+
`Unexpected input given to normalize. Expected type to be "object", found "${input === null ? "null" : typeof input}".`
|
172
|
+
);
|
173
|
+
}
|
174
|
+
const entities = {};
|
175
|
+
const addEntity = addEntities(entities);
|
176
|
+
const visitedEntities = {};
|
177
|
+
const result = visit(input, input, null, schema2, addEntity, visitedEntities);
|
178
|
+
return { entities, result };
|
179
|
+
};
|
180
|
+
|
181
|
+
exports.normalize = normalize;
|
182
|
+
exports.schema = schema;
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
class EntitySchema {
|
2
|
+
constructor(key, definition = {}, options = {}) {
|
3
|
+
if (!key || typeof key !== "string") {
|
4
|
+
throw new Error(`Expected a string key for Entity, but found ${key}.`);
|
5
|
+
}
|
6
|
+
const {
|
7
|
+
idAttribute = "id",
|
8
|
+
mergeStrategy = (entityA, entityB) => {
|
9
|
+
return { ...entityA, ...entityB };
|
10
|
+
},
|
11
|
+
processStrategy = (input) => ({ ...input })
|
12
|
+
} = options;
|
13
|
+
this._key = key;
|
14
|
+
this._getId = idAttribute;
|
15
|
+
this._mergeStrategy = mergeStrategy;
|
16
|
+
this._processStrategy = processStrategy;
|
17
|
+
this.define(definition);
|
18
|
+
}
|
19
|
+
get key() {
|
20
|
+
return this._key;
|
21
|
+
}
|
22
|
+
define(definition) {
|
23
|
+
this.schema = Object.keys(definition).reduce((entitySchema, key) => {
|
24
|
+
const schema = definition[key];
|
25
|
+
return { ...entitySchema, [key]: schema };
|
26
|
+
}, this.schema || {});
|
27
|
+
}
|
28
|
+
getId(input, parent, key) {
|
29
|
+
return this._getId(input, parent, key);
|
30
|
+
}
|
31
|
+
merge(entityA, entityB) {
|
32
|
+
return this._mergeStrategy(entityA, entityB);
|
33
|
+
}
|
34
|
+
normalize(input, parent, key, visit, addEntity, visitedEntities) {
|
35
|
+
const id = this.getId(input, parent, key);
|
36
|
+
const entityType = this.key;
|
37
|
+
if (!(entityType in visitedEntities)) {
|
38
|
+
visitedEntities[entityType] = {};
|
39
|
+
}
|
40
|
+
if (!(id in visitedEntities[entityType])) {
|
41
|
+
visitedEntities[entityType][id] = [];
|
42
|
+
}
|
43
|
+
if (visitedEntities[entityType][id].includes(input)) {
|
44
|
+
return id;
|
45
|
+
}
|
46
|
+
visitedEntities[entityType][id].push(input);
|
47
|
+
const processedEntity = this._processStrategy(input, parent, key);
|
48
|
+
Object.keys(this.schema).forEach((key2) => {
|
49
|
+
if (processedEntity.hasOwnProperty(key2) && typeof processedEntity[key2] === "object") {
|
50
|
+
const schema = this.schema[key2];
|
51
|
+
const resolvedSchema = typeof schema === "function" ? schema(input) : schema;
|
52
|
+
processedEntity[key2] = visit(
|
53
|
+
processedEntity[key2],
|
54
|
+
processedEntity,
|
55
|
+
key2,
|
56
|
+
resolvedSchema,
|
57
|
+
addEntity,
|
58
|
+
visitedEntities
|
59
|
+
);
|
60
|
+
}
|
61
|
+
});
|
62
|
+
addEntity(this, processedEntity, input, parent, key);
|
63
|
+
return id;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
class PolymorphicSchema {
|
68
|
+
constructor(definition, schemaAttribute) {
|
69
|
+
if (schemaAttribute) {
|
70
|
+
this._schemaAttribute = typeof schemaAttribute === "string" ? (input) => input[schemaAttribute] : schemaAttribute;
|
71
|
+
}
|
72
|
+
this.define(definition);
|
73
|
+
}
|
74
|
+
get isSingleSchema() {
|
75
|
+
return !this._schemaAttribute;
|
76
|
+
}
|
77
|
+
define(definition) {
|
78
|
+
this.schema = definition;
|
79
|
+
}
|
80
|
+
getSchemaAttribute(input, parent, key) {
|
81
|
+
return !this.isSingleSchema && this._schemaAttribute(input, parent, key);
|
82
|
+
}
|
83
|
+
inferSchema(input, parent, key) {
|
84
|
+
if (this.isSingleSchema) {
|
85
|
+
return this.schema;
|
86
|
+
}
|
87
|
+
const attr = this.getSchemaAttribute(input, parent, key);
|
88
|
+
return this.schema[attr];
|
89
|
+
}
|
90
|
+
normalizeValue(value, parent, key, visit, addEntity, visitedEntities) {
|
91
|
+
const schema = this.inferSchema(value, parent, key);
|
92
|
+
if (!schema) {
|
93
|
+
return value;
|
94
|
+
}
|
95
|
+
const normalizedValue = visit(value, parent, key, schema, addEntity, visitedEntities);
|
96
|
+
return this.isSingleSchema || normalizedValue === void 0 || normalizedValue === null ? normalizedValue : { id: normalizedValue, schema: this.getSchemaAttribute(value, parent, key) };
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
class UnionSchema extends PolymorphicSchema {
|
101
|
+
constructor(definition, schemaAttribute) {
|
102
|
+
if (!schemaAttribute) {
|
103
|
+
throw new Error('Expected option "schemaAttribute" not found on UnionSchema.');
|
104
|
+
}
|
105
|
+
super(definition, schemaAttribute);
|
106
|
+
}
|
107
|
+
normalize(input, parent, key, visit, addEntity, visitedEntities) {
|
108
|
+
return this.normalizeValue(input, parent, key, visit, addEntity, visitedEntities);
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
const validateSchema = (definition) => {
|
113
|
+
if (Array.isArray(definition) && definition.length > 1) {
|
114
|
+
throw new Error(`Expected schema definition to be a single schema, but found ${definition.length}.`);
|
115
|
+
}
|
116
|
+
return definition[0];
|
117
|
+
};
|
118
|
+
const getValues = (input) => Array.isArray(input) ? input : Object.keys(input).map((key) => input[key]);
|
119
|
+
const normalize$2 = (schema, input, parent, key, visit, addEntity, visitedEntities) => {
|
120
|
+
return getValues(input).map((value) => visit(value, parent, key, validateSchema(schema), addEntity, visitedEntities));
|
121
|
+
};
|
122
|
+
class ArraySchema extends PolymorphicSchema {
|
123
|
+
normalize(input, parent, key, visit, addEntity, visitedEntities) {
|
124
|
+
return getValues(input).map((value) => this.normalizeValue(value, parent, key, visit, addEntity, visitedEntities)).filter((value) => value !== void 0 && value !== null);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
const normalize$1 = (schema, input, parent, key, visit, addEntity, visitedEntities) => {
|
129
|
+
const object = { ...input };
|
130
|
+
Object.keys(schema).forEach((key2) => {
|
131
|
+
const localSchema = schema[key2];
|
132
|
+
const resolvedLocalSchema = typeof localSchema === "function" ? localSchema(input) : localSchema;
|
133
|
+
const value = visit(input[key2], input, key2, resolvedLocalSchema, addEntity, visitedEntities);
|
134
|
+
if (value === void 0 || value === null) {
|
135
|
+
delete object[key2];
|
136
|
+
} else {
|
137
|
+
object[key2] = value;
|
138
|
+
}
|
139
|
+
});
|
140
|
+
return object;
|
141
|
+
};
|
142
|
+
|
143
|
+
const visit = (value, parent, key, schema2, addEntity, visitedEntities) => {
|
144
|
+
if (typeof value !== "object" || !value) {
|
145
|
+
return value;
|
146
|
+
}
|
147
|
+
if (typeof schema2 === "object" && (!schema2.normalize || typeof schema2.normalize !== "function")) {
|
148
|
+
const method = Array.isArray(schema2) ? normalize$2 : normalize$1;
|
149
|
+
return method(schema2, value, parent, key, visit, addEntity, visitedEntities);
|
150
|
+
}
|
151
|
+
return schema2.normalize(value, parent, key, visit, addEntity, visitedEntities);
|
152
|
+
};
|
153
|
+
const addEntities = (entities) => (schema2, processedEntity, value, parent, key) => {
|
154
|
+
const schemaKey = schema2.key;
|
155
|
+
const id = schema2.getId(value, parent, key);
|
156
|
+
if (!(schemaKey in entities)) {
|
157
|
+
entities[schemaKey] = {};
|
158
|
+
}
|
159
|
+
entities[schemaKey][id] = entities[schemaKey][id] ? schema2.merge(entities[schemaKey][id], processedEntity) : processedEntity;
|
160
|
+
};
|
161
|
+
const schema = {
|
162
|
+
Array: ArraySchema,
|
163
|
+
Entity: EntitySchema,
|
164
|
+
Union: UnionSchema
|
165
|
+
};
|
166
|
+
const normalize = (input, schema2) => {
|
167
|
+
if (!input || typeof input !== "object") {
|
168
|
+
throw new Error(
|
169
|
+
`Unexpected input given to normalize. Expected type to be "object", found "${input === null ? "null" : typeof input}".`
|
170
|
+
);
|
171
|
+
}
|
172
|
+
const entities = {};
|
173
|
+
const addEntity = addEntities(entities);
|
174
|
+
const visitedEntities = {};
|
175
|
+
const result = visit(input, input, null, schema2, addEntity, visitedEntities);
|
176
|
+
return { entities, result };
|
177
|
+
};
|
178
|
+
|
179
|
+
export { normalize, schema };
|
package/index.d.ts
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
declare namespace schema {
|
2
|
+
export type StrategyFunction<T> = (value: any, parent: any, key: string) => T;
|
3
|
+
export type SchemaFunction = (value: any, parent: any, key: string) => string;
|
4
|
+
export type MergeFunction = (entityA: any, entityB: any) => any;
|
5
|
+
|
6
|
+
export class Array<T = any> {
|
7
|
+
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
|
8
|
+
define(definition: Schema): void
|
9
|
+
}
|
10
|
+
|
11
|
+
export interface EntityOptions<T = any> {
|
12
|
+
idAttribute?: string | SchemaFunction
|
13
|
+
mergeStrategy?: MergeFunction
|
14
|
+
processStrategy?: StrategyFunction<T>
|
15
|
+
}
|
16
|
+
|
17
|
+
export class Entity<T = any> {
|
18
|
+
constructor(key: string | symbol, definition?: Schema, options?: EntityOptions<T>)
|
19
|
+
define(definition: Schema): void
|
20
|
+
key: string
|
21
|
+
getId: SchemaFunction
|
22
|
+
_processStrategy: StrategyFunction<T>
|
23
|
+
}
|
24
|
+
|
25
|
+
export class Object<T = any> {
|
26
|
+
constructor(definition: SchemaObject<T>)
|
27
|
+
define(definition: Schema): void
|
28
|
+
}
|
29
|
+
|
30
|
+
export class Union<T = any> {
|
31
|
+
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
|
32
|
+
define(definition: Schema): void
|
33
|
+
}
|
34
|
+
|
35
|
+
export class Values<T = any> {
|
36
|
+
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
|
37
|
+
define(definition: Schema): void
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
export type Schema<T = any> =
|
42
|
+
| schema.Entity<T>
|
43
|
+
| schema.Object<T>
|
44
|
+
| schema.Union<T>
|
45
|
+
| schema.Values<T>
|
46
|
+
| SchemaObject<T>
|
47
|
+
| SchemaArray<T>;
|
48
|
+
|
49
|
+
export type SchemaValueFunction<T> = (t: T) => Schema<T>;
|
50
|
+
export type SchemaValue<T> = Schema<T> | SchemaValueFunction<T>;
|
51
|
+
|
52
|
+
export interface SchemaObject<T> {
|
53
|
+
[key: string]: SchemaValue<T>
|
54
|
+
}
|
55
|
+
|
56
|
+
export interface SchemaArray<T> extends Array<Schema<T>> {}
|
57
|
+
|
58
|
+
export type NormalizedSchema<E, R> = { entities: E, result: R };
|
59
|
+
|
60
|
+
export function normalize<T = any, E = { [key:string]: { [key:string]: T } | undefined}, R = any>(
|
61
|
+
data: any,
|
62
|
+
schema: Schema<T>
|
63
|
+
): NormalizedSchema<E, R>;
|
package/package.json
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
{
|
2
|
+
"name": "@pinia-orm/normalizr-edge",
|
3
|
+
"version": "1.7.1-28257628.bbb8bab",
|
4
|
+
"description": "Normalizes JSON according to schema for Redux and Flux applications",
|
5
|
+
"bugs": {
|
6
|
+
"url": "https://github.com/CodeDredd/pinia-orm/issues"
|
7
|
+
},
|
8
|
+
"homepage": "https://github.com/CodeDredd/pinia-orm",
|
9
|
+
"repository": {
|
10
|
+
"url": "https://github.com/CodeDredd/pinia-orm.git",
|
11
|
+
"type": "git"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"flux",
|
15
|
+
"redux",
|
16
|
+
"normalize",
|
17
|
+
"api",
|
18
|
+
"json"
|
19
|
+
],
|
20
|
+
"files": [
|
21
|
+
"dist/",
|
22
|
+
"index.d.ts",
|
23
|
+
"LICENSE",
|
24
|
+
"README.md"
|
25
|
+
],
|
26
|
+
"type": "module",
|
27
|
+
"funding": "https://github.com/sponsors/codedredd",
|
28
|
+
"jsdelivr": "dist/index.mjs",
|
29
|
+
"unpkg": "dist/index.mjs",
|
30
|
+
"main": "./dist/index.cjs",
|
31
|
+
"module": "./dist/index.mjs",
|
32
|
+
"types": "index.d.ts",
|
33
|
+
"exports": {
|
34
|
+
".": {
|
35
|
+
"require": "./dist/index.cjs",
|
36
|
+
"import": "./dist/index.mjs"
|
37
|
+
},
|
38
|
+
"./*": "./*"
|
39
|
+
},
|
40
|
+
"sideEffects": false,
|
41
|
+
"author": "Paul Armstrong",
|
42
|
+
"contributors": [
|
43
|
+
"Dan Abramov",
|
44
|
+
"Gregor Becker"
|
45
|
+
],
|
46
|
+
"license": "MIT",
|
47
|
+
"devDependencies": {
|
48
|
+
"@nuxtjs/eslint-config-typescript": "^12.1.0",
|
49
|
+
"@size-limit/preset-small-lib": "^9.0.0",
|
50
|
+
"eslint": "^8.49.0",
|
51
|
+
"immutable": "^4.3.4",
|
52
|
+
"size-limit": "^9.0.0",
|
53
|
+
"typescript": "^5.2.2",
|
54
|
+
"unbuild": "^2.0.0"
|
55
|
+
},
|
56
|
+
"size-limit": [
|
57
|
+
{
|
58
|
+
"path": "dist/index.mjs",
|
59
|
+
"limit": "2 kB"
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"_name": "@pinia-orm/normalizr",
|
63
|
+
"scripts": {
|
64
|
+
"build": "unbuild",
|
65
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . -l @pinia-orm/normalizr -r 1",
|
66
|
+
"size": "size-limit",
|
67
|
+
"flow": "flow",
|
68
|
+
"flow:ci": "flow check",
|
69
|
+
"lint": "eslint . --ext .js",
|
70
|
+
"lint:fix": "eslint . --fix --ext .js"
|
71
|
+
}
|
72
|
+
}
|