@webda/models 0.0.0-stage → 4.0.0-beta.4
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 +166 -0
- package/README.md +146 -2
- package/lib/index.d.ts +11 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/mock.d.ts +72 -0
- package/lib/mock.d.ts.map +1 -0
- package/lib/mock.js +83 -0
- package/lib/mock.js.map +1 -0
- package/lib/model.model.d.ts +286 -0
- package/lib/model.model.d.ts.map +1 -0
- package/lib/model.model.js +336 -0
- package/lib/model.model.js.map +1 -0
- package/lib/relations.d.ts +705 -0
- package/lib/relations.d.ts.map +1 -0
- package/lib/relations.js +938 -0
- package/lib/relations.js.map +1 -0
- package/lib/repositories/abstract.d.ts +138 -0
- package/lib/repositories/abstract.d.ts.map +1 -0
- package/lib/repositories/abstract.js +280 -0
- package/lib/repositories/abstract.js.map +1 -0
- package/lib/repositories/conformance.d.ts +40 -0
- package/lib/repositories/conformance.d.ts.map +1 -0
- package/lib/repositories/conformance.js +38 -0
- package/lib/repositories/conformance.js.map +1 -0
- package/lib/repositories/event.d.ts +101 -0
- package/lib/repositories/event.d.ts.map +1 -0
- package/lib/repositories/event.js +228 -0
- package/lib/repositories/event.js.map +1 -0
- package/lib/repositories/hooks.d.ts +88 -0
- package/lib/repositories/hooks.d.ts.map +1 -0
- package/lib/repositories/hooks.js +105 -0
- package/lib/repositories/hooks.js.map +1 -0
- package/lib/repositories/memory.d.ts +262 -0
- package/lib/repositories/memory.d.ts.map +1 -0
- package/lib/repositories/memory.js +574 -0
- package/lib/repositories/memory.js.map +1 -0
- package/lib/repositories/repository.d.ts +238 -0
- package/lib/repositories/repository.d.ts.map +1 -0
- package/lib/repositories/repository.js +19 -0
- package/lib/repositories/repository.js.map +1 -0
- package/lib/storable.d.ts +210 -0
- package/lib/storable.d.ts.map +1 -0
- package/lib/storable.js +76 -0
- package/lib/storable.js.map +1 -0
- package/lib/types.d.ts +230 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +12 -0
- package/lib/types.js.map +1 -0
- package/package.json +60 -3
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { PK, PrimaryKeyType, ModelClass, WEBDA_PRIMARY_KEY } from "../storable.js";
|
|
2
|
+
import type { JSONed, Helpers, PropertyPaths, NumericPropertyPaths, PropertyPathType } from "../types.js";
|
|
3
|
+
import { AbstractRepository } from "./abstract.js";
|
|
4
|
+
import { ArrayElement } from "@webda/tsc-esm";
|
|
5
|
+
import type { QueryParameters } from "@webda/ql";
|
|
6
|
+
import { WEBDA_TEST } from "./repository.js";
|
|
7
|
+
/**
|
|
8
|
+
* Repository decorator that wraps another repository with event emission.
|
|
9
|
+
*
|
|
10
|
+
* Emits "before" events (Create, Update, Patch, Delete, PartialUpdate, Query)
|
|
11
|
+
* before delegating to the underlying repository, then emits "after" events
|
|
12
|
+
* (Created, Updated, Patched, Deleted, PartialUpdated, Queried) on success.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - The ModelClass this repository manages
|
|
15
|
+
*/
|
|
16
|
+
export declare class EventRepository<T extends ModelClass = any> extends AbstractRepository<T> {
|
|
17
|
+
protected repository: AbstractRepository<T>;
|
|
18
|
+
/**
|
|
19
|
+
* @param model - The model class constructor
|
|
20
|
+
* @param pks - Array of primary key field names
|
|
21
|
+
* @param repository - The underlying repository to delegate storage operations to
|
|
22
|
+
* @param separator - Separator used when joining composite key fields (default: "_")
|
|
23
|
+
*/
|
|
24
|
+
constructor(model: T, pks: string[], repository: AbstractRepository<T>, separator?: string);
|
|
25
|
+
/**
|
|
26
|
+
* Increment numeric attributes with event emission
|
|
27
|
+
* @override
|
|
28
|
+
*/
|
|
29
|
+
incrementAttributes<K extends PropertyPaths<InstanceType<T>>, L extends NumericPropertyPaths<InstanceType<T>>>(primaryKey: PK<InstanceType<T>, InstanceType<T>[typeof WEBDA_PRIMARY_KEY][number]>, info: (L | {
|
|
30
|
+
property: L;
|
|
31
|
+
value?: number;
|
|
32
|
+
})[] | Record<L, number>, _conditionField?: K | null, _condition?: any): Promise<void | Record<string, number>>;
|
|
33
|
+
/**
|
|
34
|
+
* Query with event emission
|
|
35
|
+
*
|
|
36
|
+
* Parameters are bound here, so the events carry the bound query and the underlying
|
|
37
|
+
* repository (often a store one) only ever receives a plain query.
|
|
38
|
+
* @param query - the query to execute
|
|
39
|
+
* @param params - values for the `?` or `:name` placeholders
|
|
40
|
+
* @returns the query results with optional continuation token
|
|
41
|
+
*/
|
|
42
|
+
query(query: string, params?: QueryParameters): Promise<{
|
|
43
|
+
results: InstanceType<T>[];
|
|
44
|
+
continuationToken?: string;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* Iterate through all objects
|
|
48
|
+
* @param query - the query filter
|
|
49
|
+
* @param params - values for the `?` or `:name` placeholders, bound before reaching the underlying repository
|
|
50
|
+
* @returns an async generator of objects
|
|
51
|
+
*/
|
|
52
|
+
iterate(query: string, params?: QueryParameters): AsyncGenerator<InstanceType<T>, any, any>;
|
|
53
|
+
/**
|
|
54
|
+
* Delete an item from a collection
|
|
55
|
+
* @param uuid The UUID of the item
|
|
56
|
+
* @param collection The collection to delete from
|
|
57
|
+
* @param index The index of the item to delete
|
|
58
|
+
* @param itemWriteConditionField The field to use for the write condition
|
|
59
|
+
* @param itemWriteCondition The value to use for the write condition
|
|
60
|
+
*/
|
|
61
|
+
deleteItemFromCollection<K extends Extract<PropertyPaths<InstanceType<T>, any[]>, keyof InstanceType<T>>, L extends keyof ArrayElement<InstanceType<T>[K]>>(uuid: PrimaryKeyType<InstanceType<T>>, collection: K, index: number, itemWriteConditionField?: L, itemWriteCondition?: ArrayElement<InstanceType<T>[K]>[L]): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Upsert item to collection with event emission
|
|
64
|
+
* @override
|
|
65
|
+
*/
|
|
66
|
+
upsertItemToCollection<K extends Extract<PropertyPaths<InstanceType<T>, any[]>, keyof InstanceType<T>>, L extends keyof ArrayElement<InstanceType<T>[K]>>(uuid: PrimaryKeyType<InstanceType<T>>, collection: K, item: ArrayElement<InstanceType<T>[K]> | JSONed<ArrayElement<InstanceType<T>[K]>>, index?: number, itemWriteConditionField?: ArrayElement<InstanceType<T>[K]> extends object ? L : never, itemWriteCondition?: ArrayElement<InstanceType<T>[K]> extends object ? (object & ArrayElement<InstanceType<T>[K]>)[L] : ArrayElement<InstanceType<T>[K]>): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Remove attribute with event emission
|
|
69
|
+
* @override
|
|
70
|
+
*/
|
|
71
|
+
removeAttribute<L extends PropertyPaths<InstanceType<T>>, K extends PropertyPaths<InstanceType<T>>>(uuid: PrimaryKeyType<InstanceType<T>>, attribute: K, conditionField?: L | null, condition?: PropertyPathType<InstanceType<T>, L> | JSONed<PropertyPathType<InstanceType<T>, L>>): Promise<void>;
|
|
72
|
+
/** @override */
|
|
73
|
+
get(primaryKey: PrimaryKeyType<InstanceType<T>>): Promise<Helpers<InstanceType<T>>>;
|
|
74
|
+
/**
|
|
75
|
+
* Create with event emission (emits Create before and Created after)
|
|
76
|
+
* @override
|
|
77
|
+
*/
|
|
78
|
+
create(data: Helpers<InstanceType<T>>, save?: boolean): Promise<InstanceType<T>>;
|
|
79
|
+
/**
|
|
80
|
+
* Patch with event emission (emits Patch before and Patched after)
|
|
81
|
+
* @override
|
|
82
|
+
*/
|
|
83
|
+
patch<K extends PropertyPaths<InstanceType<T>>>(primaryKey: PK<InstanceType<T>, InstanceType<T>[typeof WEBDA_PRIMARY_KEY][number]>, data: Partial<InstanceType<T>>, _conditionField?: K | null, _condition?: any): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Update with event emission (emits Update before and Updated after)
|
|
86
|
+
* @override
|
|
87
|
+
*/
|
|
88
|
+
update<K extends PropertyPaths<InstanceType<T>>>(data: Helpers<InstanceType<T>>, conditionField?: K | null, condition?: PropertyPathType<InstanceType<T>, K>): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Delete with event emission (emits Delete before and Deleted after)
|
|
91
|
+
* @override
|
|
92
|
+
*/
|
|
93
|
+
delete<K extends PropertyPaths<InstanceType<T>>>(uuid: PrimaryKeyType<InstanceType<T>>, conditionField?: K | null, condition?: PropertyPathType<InstanceType<T>, K> | JSONed<PropertyPathType<InstanceType<T>, K>>): Promise<void>;
|
|
94
|
+
/** @override */
|
|
95
|
+
exists(uuid: PrimaryKeyType<InstanceType<T>>): Promise<boolean>;
|
|
96
|
+
/** Test utilities - clears events and delegates to underlying repository */
|
|
97
|
+
[WEBDA_TEST]: {
|
|
98
|
+
clear: (excludeListeners?: boolean) => Promise<void>;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/repositories/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,cAAc,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnF,OAAO,KAAK,EAAc,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACtH,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;;GAQG;AACH,qBAAa,eAAe,CAAC,CAAC,SAAS,UAAU,GAAG,GAAG,CAAE,SAAQ,kBAAkB,CAAC,CAAC,CAAC;IAUlF,SAAS,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAT7C;;;;;OAKG;IACH,YACE,KAAK,EAAE,CAA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B,UAAU,CAAC,EAAE,GAAG,GACf,OAAO,CAAC,IAAI,CAAC,CAIf;IACD;;;OAGG;IACG,MAAM,CAAC,CAAC,SAAS,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EACnD,IAAI,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAC9B,cAAc,CAAC,EAAE,CAAC,GAAG,IAAI,EACzB,SAAS,CAAC,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAC/C,OAAO,CAAC,IAAI,CAAC,CAIf;IACD;;;OAGG;IACG,MAAM,CAAC,CAAC,SAAS,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EACnD,IAAI,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EACrC,cAAc,CAAC,EAAE,CAAC,GAAG,IAAI,EACzB,SAAS,CAAC,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAC9F,OAAO,CAAC,IAAI,CAAC,CAIf;IACD,gBAAgB;IACV,MAAM,CAAC,IAAI,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAGpE;IAED,4EAA4E;IAC5E,CAAC,UAAU,CAAC,EAAE;QACZ,KAAK,EAAE,CAAC,gBAAgB,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;KACtD,CAQC;CACH"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { AbstractRepository } from "./abstract.js";
|
|
3
|
+
import { WEBDA_TEST } from "./repository.js";
|
|
4
|
+
/**
|
|
5
|
+
* Repository decorator that wraps another repository with event emission.
|
|
6
|
+
*
|
|
7
|
+
* Emits "before" events (Create, Update, Patch, Delete, PartialUpdate, Query)
|
|
8
|
+
* before delegating to the underlying repository, then emits "after" events
|
|
9
|
+
* (Created, Updated, Patched, Deleted, PartialUpdated, Queried) on success.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The ModelClass this repository manages
|
|
12
|
+
*/
|
|
13
|
+
export class EventRepository extends AbstractRepository {
|
|
14
|
+
static { _a = WEBDA_TEST; }
|
|
15
|
+
/**
|
|
16
|
+
* @param model - The model class constructor
|
|
17
|
+
* @param pks - Array of primary key field names
|
|
18
|
+
* @param repository - The underlying repository to delegate storage operations to
|
|
19
|
+
* @param separator - Separator used when joining composite key fields (default: "_")
|
|
20
|
+
*/
|
|
21
|
+
constructor(model, pks, repository, separator) {
|
|
22
|
+
super(model, pks, separator);
|
|
23
|
+
this.repository = repository;
|
|
24
|
+
/** Test utilities - clears events and delegates to underlying repository */
|
|
25
|
+
this[_a] = {
|
|
26
|
+
clear: async (excludeListeners) => {
|
|
27
|
+
if (!excludeListeners) {
|
|
28
|
+
this.events.clear();
|
|
29
|
+
}
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
this.repository[WEBDA_TEST].clear();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Increment numeric attributes with event emission
|
|
37
|
+
* @override
|
|
38
|
+
*/
|
|
39
|
+
async incrementAttributes(primaryKey, info, _conditionField, _condition) {
|
|
40
|
+
await this.emit("PartialUpdate", {
|
|
41
|
+
object_id: primaryKey,
|
|
42
|
+
partial_update: {
|
|
43
|
+
increments: info
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const updated = await this.repository.incrementAttributes(primaryKey, info, _conditionField, _condition);
|
|
47
|
+
await this.emit("PartialUpdated", {
|
|
48
|
+
object_id: primaryKey,
|
|
49
|
+
partial_update: {
|
|
50
|
+
increments: info
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return updated;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Query with event emission
|
|
57
|
+
*
|
|
58
|
+
* Parameters are bound here, so the events carry the bound query and the underlying
|
|
59
|
+
* repository (often a store one) only ever receives a plain query.
|
|
60
|
+
* @param query - the query to execute
|
|
61
|
+
* @param params - values for the `?` or `:name` placeholders
|
|
62
|
+
* @returns the query results with optional continuation token
|
|
63
|
+
*/
|
|
64
|
+
async query(query, params) {
|
|
65
|
+
if (params !== undefined) {
|
|
66
|
+
query = (await import("@webda/ql")).bind(query, params);
|
|
67
|
+
}
|
|
68
|
+
await this.emit("Query", { query });
|
|
69
|
+
const res = await this.repository.query(query);
|
|
70
|
+
await this.emit("Queried", { ...res, query });
|
|
71
|
+
return res;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Iterate through all objects
|
|
75
|
+
* @param query - the query filter
|
|
76
|
+
* @param params - values for the `?` or `:name` placeholders, bound before reaching the underlying repository
|
|
77
|
+
* @returns an async generator of objects
|
|
78
|
+
*/
|
|
79
|
+
async *iterate(query, params) {
|
|
80
|
+
if (params !== undefined) {
|
|
81
|
+
query = (await import("@webda/ql")).bind(query, params);
|
|
82
|
+
}
|
|
83
|
+
yield* this.repository.iterate(query);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Delete an item from a collection
|
|
87
|
+
* @param uuid The UUID of the item
|
|
88
|
+
* @param collection The collection to delete from
|
|
89
|
+
* @param index The index of the item to delete
|
|
90
|
+
* @param itemWriteConditionField The field to use for the write condition
|
|
91
|
+
* @param itemWriteCondition The value to use for the write condition
|
|
92
|
+
*/
|
|
93
|
+
async deleteItemFromCollection(uuid, collection, index, itemWriteConditionField, itemWriteCondition) {
|
|
94
|
+
await this.emit("PartialUpdate", {
|
|
95
|
+
object_id: uuid,
|
|
96
|
+
partial_update: {
|
|
97
|
+
delete_from_collection: {
|
|
98
|
+
collection,
|
|
99
|
+
index,
|
|
100
|
+
itemWriteConditionField,
|
|
101
|
+
itemWriteCondition
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
await this.repository.deleteItemFromCollection(uuid, collection, index, itemWriteConditionField, itemWriteCondition);
|
|
106
|
+
await this.emit("PartialUpdated", {
|
|
107
|
+
object_id: uuid,
|
|
108
|
+
partial_update: {
|
|
109
|
+
delete_from_collection: {
|
|
110
|
+
collection,
|
|
111
|
+
index,
|
|
112
|
+
itemWriteConditionField,
|
|
113
|
+
itemWriteCondition
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Upsert item to collection with event emission
|
|
120
|
+
* @override
|
|
121
|
+
*/
|
|
122
|
+
async upsertItemToCollection(uuid, collection, item, index, itemWriteConditionField, itemWriteCondition) {
|
|
123
|
+
await this.emit("PartialUpdate", {
|
|
124
|
+
object_id: uuid,
|
|
125
|
+
partial_update: {
|
|
126
|
+
upsert_in_collection: {
|
|
127
|
+
collection,
|
|
128
|
+
item,
|
|
129
|
+
index,
|
|
130
|
+
itemWriteConditionField,
|
|
131
|
+
itemWriteCondition
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
await this.repository.upsertItemToCollection(uuid, collection, item, index, itemWriteConditionField, itemWriteCondition);
|
|
136
|
+
await this.emit("PartialUpdated", {
|
|
137
|
+
object_id: uuid,
|
|
138
|
+
partial_update: {
|
|
139
|
+
upsert_in_collection: {
|
|
140
|
+
collection,
|
|
141
|
+
item,
|
|
142
|
+
index,
|
|
143
|
+
itemWriteConditionField,
|
|
144
|
+
itemWriteCondition
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Remove attribute with event emission
|
|
151
|
+
* @override
|
|
152
|
+
*/
|
|
153
|
+
async removeAttribute(uuid, attribute, conditionField, condition) {
|
|
154
|
+
await this.emit("PartialUpdate", {
|
|
155
|
+
object_id: uuid,
|
|
156
|
+
partial_update: {
|
|
157
|
+
remove_attribute: {
|
|
158
|
+
attribute,
|
|
159
|
+
conditionField,
|
|
160
|
+
condition
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
await this.repository.removeAttribute(uuid, attribute, conditionField, condition);
|
|
165
|
+
await this.emit("PartialUpdated", {
|
|
166
|
+
object_id: uuid,
|
|
167
|
+
partial_update: {
|
|
168
|
+
remove_attribute: {
|
|
169
|
+
attribute,
|
|
170
|
+
conditionField,
|
|
171
|
+
condition
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** @override */
|
|
177
|
+
async get(primaryKey) {
|
|
178
|
+
const res = await this.repository.get(primaryKey);
|
|
179
|
+
return res;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Create with event emission (emits Create before and Created after)
|
|
183
|
+
* @override
|
|
184
|
+
*/
|
|
185
|
+
async create(data, save) {
|
|
186
|
+
// Build the instance once so a generated primary key is shared by the events
|
|
187
|
+
// and the underlying repository
|
|
188
|
+
const item = this.buildItem(data);
|
|
189
|
+
const object_id = this.getPrimaryKey(item);
|
|
190
|
+
await this.emit("Create", { object_id, object: item });
|
|
191
|
+
const res = await this.repository.create(item, save);
|
|
192
|
+
await this.emit("Created", { object_id, object: res });
|
|
193
|
+
return res;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Patch with event emission (emits Patch before and Patched after)
|
|
197
|
+
* @override
|
|
198
|
+
*/
|
|
199
|
+
async patch(primaryKey, data, _conditionField, _condition) {
|
|
200
|
+
await this.emit("Patch", { object_id: primaryKey, object: data });
|
|
201
|
+
await this.repository.patch(primaryKey, data, _conditionField, _condition);
|
|
202
|
+
await this.emit("Patched", { object_id: primaryKey, object: data });
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Update with event emission (emits Update before and Updated after)
|
|
206
|
+
* @override
|
|
207
|
+
*/
|
|
208
|
+
async update(data, conditionField, condition) {
|
|
209
|
+
await this.emit("Update", { object_id: this.getPrimaryKey(data), object: data });
|
|
210
|
+
await this.repository.update(data, conditionField, condition);
|
|
211
|
+
await this.emit("Updated", { object_id: this.getPrimaryKey(data), object: data });
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Delete with event emission (emits Delete before and Deleted after)
|
|
215
|
+
* @override
|
|
216
|
+
*/
|
|
217
|
+
async delete(uuid, conditionField, condition) {
|
|
218
|
+
await this.emit("Delete", { object_id: uuid });
|
|
219
|
+
await this.repository.delete(uuid, conditionField, condition);
|
|
220
|
+
await this.emit("Deleted", { object_id: uuid });
|
|
221
|
+
}
|
|
222
|
+
/** @override */
|
|
223
|
+
async exists(uuid) {
|
|
224
|
+
const res = await this.repository.exists(uuid);
|
|
225
|
+
return res;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.js","sourceRoot":"","sources":["../../src/repositories/event.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAGnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;;GAQG;AACH,MAAM,OAAO,eAA4C,SAAQ,kBAAqB;kBAkRnF,UAAU;IAjRX;;;;;OAKG;IACH,YACE,KAAQ,EACR,GAAa,EACH,UAAiC,EAC3C,SAAkB;QAElB,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC;QAHnB,eAAU,GAAV,UAAU,CAAuB;QAuQ7C,4EAA4E;QAC5E,QAAY,GAER;YACF,KAAK,EAAE,KAAK,EAAC,gBAAgB,EAAC,EAAE;gBAC9B,IAAI,CAAC,gBAAgB,EAAE,CAAC;oBACtB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;gBACtB,CAAC;gBACD,aAAa;gBACb,IAAI,CAAC,UAAU,CAAC,UAAU,CAA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;IAC3F,CAAC;IACD;;;OAGG;IACH,KAAK,CAAC,MAAM,CACV,IAAqC,EACrC,cAAyB,EACzB,SAA+F;QAE/F,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,IAAI,EAAS,CAAC,CAAC;QACtD,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,cAAc,EAAE,SAAS,CAAC,CAAC;QAC9D,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAS,CAAC,CAAC;IACzD,CAAC;IACD,gBAAgB;IAChB,KAAK,CAAC,MAAM,CAAC,IAAqC;QAChD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC/C,OAAO,GAAG,CAAC;IACb,CAAC;CAcF"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ModelRefWithCreate } from "../relations.js";
|
|
2
|
+
import { SettablePrimaryKey, type Storable, type ModelClass } from "../storable.js";
|
|
3
|
+
import { Helpers } from "../types.js";
|
|
4
|
+
import type { Repository } from "./repository.js";
|
|
5
|
+
import type { QueryParameters, WebdaQLString } from "@webda/ql";
|
|
6
|
+
/**
|
|
7
|
+
* Global registry mapping ModelClass constructors to their Repository instances.
|
|
8
|
+
*
|
|
9
|
+
* @internal — Populated by `Store.computeStores()`; not for app code. App code
|
|
10
|
+
* reaches a repository via `useRepository(Model)` or static model methods
|
|
11
|
+
* (`Model.create()`, `Model.query()`, `Model.ref()`).
|
|
12
|
+
*/
|
|
13
|
+
export declare const Repositories: WeakMap<ModelClass<Storable<any, any>>, Repository<any>>;
|
|
14
|
+
/**
|
|
15
|
+
* Return a repository for a model
|
|
16
|
+
* @param arg - the model class to look up
|
|
17
|
+
* @returns the repository for the model
|
|
18
|
+
*/
|
|
19
|
+
export declare function useRepository<T extends ModelClass>(arg: T): Repository<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Register a repository
|
|
22
|
+
*
|
|
23
|
+
* @internal — Called by `Store.computeStores()`; not for app code.
|
|
24
|
+
* @param model - the model class to register for
|
|
25
|
+
* @param repository - the repository instance
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerRepository<T extends ModelClass>(model: T, repository: Repository<T>): void;
|
|
28
|
+
/**
|
|
29
|
+
* MixIn that adds static repository methods (create, query, iterate, ref, getRepository)
|
|
30
|
+
* to a base class, enabling Model classes to access their repository directly.
|
|
31
|
+
*
|
|
32
|
+
* @param Base - The base class to augment
|
|
33
|
+
* @returns the augmented class with repository methods
|
|
34
|
+
*/
|
|
35
|
+
export declare function RepositoryStorageClassMixIn<TBase extends new (...args: any[]) => object>(Base: TBase): {
|
|
36
|
+
new (...args: any[]): {
|
|
37
|
+
/**
|
|
38
|
+
* Return the proxied version of this model (identity by default)
|
|
39
|
+
* @returns this instance
|
|
40
|
+
*/
|
|
41
|
+
toProxy(): /*elided*/ any;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Get the repository registered for this model class.
|
|
45
|
+
* @returns the repository
|
|
46
|
+
*/
|
|
47
|
+
getRepository<T extends ModelClass>(this: T): Repository<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Create the object directly
|
|
50
|
+
* @param this - the model class constructor
|
|
51
|
+
* @param data - the initial data
|
|
52
|
+
* @param save - whether to persist immediately
|
|
53
|
+
* @returns the created model instance
|
|
54
|
+
*/
|
|
55
|
+
create<T extends ModelClass>(this: T, data: Helpers<InstanceType<T>>, save?: boolean): Promise<InstanceType<T>>;
|
|
56
|
+
/**
|
|
57
|
+
* Query the object directly
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* Task.query("owner = ? AND priority >= ?", [user, 2]);
|
|
61
|
+
* Task.query("owner = :owner", { owner: user });
|
|
62
|
+
* ```
|
|
63
|
+
* @param this - the model class constructor
|
|
64
|
+
* @param query - the query string, optionally with `?` or `:name` placeholders
|
|
65
|
+
* @param params - values for the placeholders: an array for `?`, an object for `:name`
|
|
66
|
+
* @returns the query results
|
|
67
|
+
*/
|
|
68
|
+
query<T extends ModelClass>(this: T, query: WebdaQLString<InstanceType<T>>, params?: QueryParameters): Promise<{
|
|
69
|
+
results: InstanceType<T>[];
|
|
70
|
+
continuationToken?: string;
|
|
71
|
+
}>;
|
|
72
|
+
/**
|
|
73
|
+
* Iterate through all objects
|
|
74
|
+
* @param this - the model class constructor
|
|
75
|
+
* @param query - the query string, optionally with `?` or `:name` placeholders
|
|
76
|
+
* @param params - values for the placeholders: an array for `?`, an object for `:name`
|
|
77
|
+
* @returns an async generator of model instances
|
|
78
|
+
*/
|
|
79
|
+
iterate<T extends ModelClass>(this: T, query: WebdaQLString<InstanceType<T>>, params?: QueryParameters): AsyncGenerator<InstanceType<T>, any, any>;
|
|
80
|
+
/**
|
|
81
|
+
* Get a reference to the model
|
|
82
|
+
* @param this - the model class constructor
|
|
83
|
+
* @param key - the primary key value
|
|
84
|
+
* @returns a model reference with create capability
|
|
85
|
+
*/
|
|
86
|
+
ref<T extends ModelClass>(this: T, key: SettablePrimaryKey<InstanceType<T>>): ModelRefWithCreate<InstanceType<T>>;
|
|
87
|
+
} & TBase;
|
|
88
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/repositories/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAkB,kBAAkB,EAAqB,KAAK,QAAQ,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACvH,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAEhE;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AC,YAAY,CAAC,CAAC,CAAC,CAAC,GACvC,kBAAkB,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;UAYzC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global registry mapping ModelClass constructors to their Repository instances.
|
|
3
|
+
*
|
|
4
|
+
* @internal — Populated by `Store.computeStores()`; not for app code. App code
|
|
5
|
+
* reaches a repository via `useRepository(Model)` or static model methods
|
|
6
|
+
* (`Model.create()`, `Model.query()`, `Model.ref()`).
|
|
7
|
+
*/
|
|
8
|
+
export const Repositories = new WeakMap();
|
|
9
|
+
/**
|
|
10
|
+
* Return a repository for a model
|
|
11
|
+
* @param arg - the model class to look up
|
|
12
|
+
* @returns the repository for the model
|
|
13
|
+
*/
|
|
14
|
+
export function useRepository(arg) {
|
|
15
|
+
let clazz = arg;
|
|
16
|
+
while (!Repositories.has(clazz)) {
|
|
17
|
+
clazz = Object.getPrototypeOf(clazz);
|
|
18
|
+
if (clazz === null || clazz === Object) {
|
|
19
|
+
throw new Error(`No repository found for ${arg.prototype.constructor.name}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return Repositories.get(clazz);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Register a repository
|
|
26
|
+
*
|
|
27
|
+
* @internal — Called by `Store.computeStores()`; not for app code.
|
|
28
|
+
* @param model - the model class to register for
|
|
29
|
+
* @param repository - the repository instance
|
|
30
|
+
*/
|
|
31
|
+
export function registerRepository(model, repository) {
|
|
32
|
+
Repositories.set(model, repository);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* MixIn that adds static repository methods (create, query, iterate, ref, getRepository)
|
|
36
|
+
* to a base class, enabling Model classes to access their repository directly.
|
|
37
|
+
*
|
|
38
|
+
* @param Base - The base class to augment
|
|
39
|
+
* @returns the augmented class with repository methods
|
|
40
|
+
*/
|
|
41
|
+
export function RepositoryStorageClassMixIn(Base) {
|
|
42
|
+
return class extends Base {
|
|
43
|
+
/**
|
|
44
|
+
* Get the repository registered for this model class.
|
|
45
|
+
* @returns the repository
|
|
46
|
+
*/
|
|
47
|
+
static getRepository() {
|
|
48
|
+
return useRepository(this);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create the object directly
|
|
52
|
+
* @param this - the model class constructor
|
|
53
|
+
* @param data - the initial data
|
|
54
|
+
* @param save - whether to persist immediately
|
|
55
|
+
* @returns the created model instance
|
|
56
|
+
*/
|
|
57
|
+
static create(data, save = true) {
|
|
58
|
+
return useRepository(this).create(data, save);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Query the object directly
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* Task.query("owner = ? AND priority >= ?", [user, 2]);
|
|
65
|
+
* Task.query("owner = :owner", { owner: user });
|
|
66
|
+
* ```
|
|
67
|
+
* @param this - the model class constructor
|
|
68
|
+
* @param query - the query string, optionally with `?` or `:name` placeholders
|
|
69
|
+
* @param params - values for the placeholders: an array for `?`, an object for `:name`
|
|
70
|
+
* @returns the query results
|
|
71
|
+
*/
|
|
72
|
+
static query(query, params) {
|
|
73
|
+
return useRepository(this).query(query, params);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Iterate through all objects
|
|
77
|
+
* @param this - the model class constructor
|
|
78
|
+
* @param query - the query string, optionally with `?` or `:name` placeholders
|
|
79
|
+
* @param params - values for the placeholders: an array for `?`, an object for `:name`
|
|
80
|
+
* @returns an async generator of model instances
|
|
81
|
+
*/
|
|
82
|
+
static async *iterate(query, params) {
|
|
83
|
+
for await (const item of useRepository(this).iterate(query, params)) {
|
|
84
|
+
yield item;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get a reference to the model
|
|
89
|
+
* @param this - the model class constructor
|
|
90
|
+
* @param key - the primary key value
|
|
91
|
+
* @returns a model reference with create capability
|
|
92
|
+
*/
|
|
93
|
+
static ref(key) {
|
|
94
|
+
return useRepository(this).ref(key);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Return the proxied version of this model (identity by default)
|
|
98
|
+
* @returns this instance
|
|
99
|
+
*/
|
|
100
|
+
toProxy() {
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/repositories/hooks.ts"],"names":[],"mappings":"AAMA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,OAAO,EAA+B,CAAC;AAEvE;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAuB,GAAM;IACxD,IAAI,KAAK,GAAQ,GAAG,CAAC;IACrB,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,SAAS,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC;QAC/E,CAAC;IACH,CAAC;IACD,OAAO,YAAY,CAAC,GAAG,CAAC,KAAK,CAAkB,CAAC;AAClD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAuB,KAAQ,EAAE,UAAyB;IAC1F,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AACtC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,2BAA2B,CAA+C,IAAW;IACnG,OAAO,KAAM,SAAQ,IAAI;QACvB;;;WAGG;QACH,MAAM,CAAC,aAAa;YAClB,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC;QAED;;;;;;WA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