@medusajs/search 0.0.1 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/index.d.ts +7 -8
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -21
- package/dist/index.js.map +1 -0
- package/dist/joiner-config.d.ts +8 -2
- package/dist/joiner-config.d.ts.map +1 -0
- package/dist/joiner-config.js +9 -1
- package/dist/joiner-config.js.map +1 -0
- package/dist/loaders/{container.d.ts → providers.d.ts} +2 -1
- package/dist/loaders/providers.d.ts.map +1 -0
- package/dist/loaders/providers.js +23 -0
- package/dist/loaders/providers.js.map +1 -0
- package/dist/migrations/Migration20260805085626.d.ts +6 -0
- package/dist/migrations/Migration20260805085626.d.ts.map +1 -0
- package/dist/migrations/Migration20260805085626.js +23 -0
- package/dist/migrations/Migration20260805085626.js.map +1 -0
- package/dist/models/index.d.ts +3 -2
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +6 -16
- package/dist/models/index.js.map +1 -0
- package/dist/models/search-index-sync.d.ts +25 -0
- package/dist/models/search-index-sync.d.ts.map +1 -0
- package/dist/models/search-index-sync.js +42 -0
- package/dist/models/search-index-sync.js.map +1 -0
- package/dist/models/search-index.d.ts +21 -0
- package/dist/models/search-index.d.ts.map +1 -0
- package/dist/models/search-index.js +29 -0
- package/dist/models/search-index.js.map +1 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +4 -1
- package/dist/services/index.js.map +1 -0
- package/dist/services/search-module-service.d.ts +45 -22
- package/dist/services/search-module-service.d.ts.map +1 -0
- package/dist/services/search-module-service.js +160 -45
- package/dist/services/search-module-service.js.map +1 -0
- package/dist/services/search-provider.d.ts +23 -0
- package/dist/services/search-provider.d.ts.map +1 -0
- package/dist/services/search-provider.js +64 -0
- package/dist/services/search-provider.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/index.d.ts +80 -110
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +5 -5
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +63 -3
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +384 -17
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/ingestion.d.ts +12 -0
- package/dist/utils/ingestion.d.ts.map +1 -0
- package/dist/utils/ingestion.js +69 -0
- package/dist/utils/ingestion.js.map +1 -0
- package/dist/utils/migrations.d.ts +11 -0
- package/dist/utils/migrations.d.ts.map +1 -0
- package/dist/utils/migrations.js +98 -0
- package/dist/utils/migrations.js.map +1 -0
- package/dist/utils/seeding.d.ts +14 -0
- package/dist/utils/seeding.d.ts.map +1 -0
- package/dist/utils/seeding.js +343 -0
- package/dist/utils/seeding.js.map +1 -0
- package/package.json +26 -45
- package/dist/initialize/index.d.ts +0 -8
- package/dist/initialize/index.js +0 -17
- package/dist/loaders/connection.d.ts +0 -4
- package/dist/loaders/connection.js +0 -41
- package/dist/loaders/container.js +0 -21
- package/dist/loaders/index.d.ts +0 -2
- package/dist/loaders/index.js +0 -10
- package/dist/migrations/Migration20231019174230.d.ts +0 -4
- package/dist/migrations/Migration20231019174230.js +0 -18
- package/dist/models/catalog-relation.d.ts +0 -15
- package/dist/models/catalog-relation.js +0 -72
- package/dist/models/catalog.d.ts +0 -11
- package/dist/models/catalog.js +0 -55
- package/dist/module-definition.d.ts +0 -2
- package/dist/module-definition.js +0 -15
- package/dist/scripts/bin/run-migration-down.d.ts +0 -3
- package/dist/scripts/bin/run-migration-down.js +0 -32
- package/dist/scripts/bin/run-migration-up.d.ts +0 -3
- package/dist/scripts/bin/run-migration-up.js +0 -32
- package/dist/scripts/bin/run-seed.d.ts +0 -3
- package/dist/scripts/bin/run-seed.js +0 -38
- package/dist/scripts/index.d.ts +0 -2
- package/dist/scripts/index.js +0 -18
- package/dist/scripts/migration-down.d.ts +0 -10
- package/dist/scripts/migration-down.js +0 -52
- package/dist/scripts/migration-up.d.ts +0 -10
- package/dist/scripts/migration-up.js +0 -56
- package/dist/scripts/seed-data/index.d.ts +0 -8
- package/dist/scripts/seed-data/index.js +0 -138
- package/dist/scripts/seed.d.ts +0 -5
- package/dist/scripts/seed.js +0 -65
- package/dist/services/postgres-provider.d.ts +0 -126
- package/dist/services/postgres-provider.js +0 -441
- package/dist/utils/build-config.d.ts +0 -21
- package/dist/utils/build-config.js +0 -437
- package/dist/utils/create-partitions.d.ts +0 -3
- package/dist/utils/create-partitions.js +0 -27
- package/dist/utils/query-builder.d.ts +0 -28
- package/dist/utils/query-builder.js +0 -375
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { IEventBusModuleService, Message, RemoteJoinerQuery, Subscriber } from "@medusajs/types";
|
|
2
|
-
import { EntityManager } from "@mikro-orm/postgresql";
|
|
3
|
-
import { QueryFormat, QueryOptions, SchemaObjectEntityRepresentation, SchemaObjectRepresentation, SearchModuleOptions, StorageProvider } from "../types";
|
|
4
|
-
type InjectedDependencies = {
|
|
5
|
-
manager: EntityManager;
|
|
6
|
-
eventBusModuleService: IEventBusModuleService;
|
|
7
|
-
storageProviderCtr: StorageProvider;
|
|
8
|
-
storageProviderOptions: unknown;
|
|
9
|
-
remoteQuery: (query: string | RemoteJoinerQuery | object, variables?: Record<string, unknown>) => Promise<any>;
|
|
10
|
-
};
|
|
11
|
-
export declare class PostgresProvider {
|
|
12
|
-
#private;
|
|
13
|
-
protected readonly eventActionToMethodMap_: {
|
|
14
|
-
created: string;
|
|
15
|
-
updated: string;
|
|
16
|
-
deleted: string;
|
|
17
|
-
attached: string;
|
|
18
|
-
detached: string;
|
|
19
|
-
};
|
|
20
|
-
protected container_: InjectedDependencies;
|
|
21
|
-
protected readonly schemaObjectRepresentation_: SchemaObjectRepresentation;
|
|
22
|
-
protected readonly schemaEntitiesMap_: Record<string, any>;
|
|
23
|
-
protected readonly moduleOptions_: SearchModuleOptions;
|
|
24
|
-
protected get remoteQuery_(): (query: string | RemoteJoinerQuery | object, variables?: Record<string, unknown>) => Promise<any>;
|
|
25
|
-
constructor(container: any, options: {
|
|
26
|
-
schemaObjectRepresentation: SchemaObjectRepresentation;
|
|
27
|
-
entityMap: Record<string, any>;
|
|
28
|
-
}, moduleOptions: SearchModuleOptions);
|
|
29
|
-
onApplicationStart(): Promise<void>;
|
|
30
|
-
protected static parseData<TData extends {
|
|
31
|
-
id: string;
|
|
32
|
-
[key: string]: unknown;
|
|
33
|
-
}>(data: TData | TData[], schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation): {
|
|
34
|
-
data: TData[];
|
|
35
|
-
entityProperties: string[];
|
|
36
|
-
parentsProperties: {
|
|
37
|
-
[entity: string]: string[];
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
protected static parseMessageData<T>(message?: Message<T>): {
|
|
41
|
-
action: string;
|
|
42
|
-
data: {
|
|
43
|
-
id: string;
|
|
44
|
-
}[];
|
|
45
|
-
ids: string[];
|
|
46
|
-
} | void;
|
|
47
|
-
query(selection: QueryFormat, options?: QueryOptions): any;
|
|
48
|
-
queryAndCount(selection: QueryFormat, options?: QueryOptions): Promise<any[]>;
|
|
49
|
-
consumeEvent(schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation): Subscriber;
|
|
50
|
-
/**
|
|
51
|
-
* Create the catalog entry and the catalog relation entry when this event is emitted.
|
|
52
|
-
* @param entity
|
|
53
|
-
* @param data
|
|
54
|
-
* @param schemaEntityObjectRepresentation
|
|
55
|
-
* @protected
|
|
56
|
-
*/
|
|
57
|
-
protected onCreate<TData extends {
|
|
58
|
-
id: string;
|
|
59
|
-
[key: string]: unknown;
|
|
60
|
-
}>({ entity, data, schemaEntityObjectRepresentation, }: {
|
|
61
|
-
entity: string;
|
|
62
|
-
data: TData | TData[];
|
|
63
|
-
schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation;
|
|
64
|
-
}): Promise<void>;
|
|
65
|
-
/**
|
|
66
|
-
* Update the catalog entry when this event is emitted.
|
|
67
|
-
* @param entity
|
|
68
|
-
* @param data
|
|
69
|
-
* @param schemaEntityObjectRepresentation
|
|
70
|
-
* @protected
|
|
71
|
-
*/
|
|
72
|
-
protected onUpdate<TData extends {
|
|
73
|
-
id: string;
|
|
74
|
-
[key: string]: unknown;
|
|
75
|
-
}>({ entity, data, schemaEntityObjectRepresentation, }: {
|
|
76
|
-
entity: string;
|
|
77
|
-
data: TData | TData[];
|
|
78
|
-
schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation;
|
|
79
|
-
}): Promise<void>;
|
|
80
|
-
/**
|
|
81
|
-
* Delete the catalog entry when this event is emitted.
|
|
82
|
-
* @param entity
|
|
83
|
-
* @param data
|
|
84
|
-
* @param schemaEntityObjectRepresentation
|
|
85
|
-
* @protected
|
|
86
|
-
*/
|
|
87
|
-
protected onDelete<TData extends {
|
|
88
|
-
id: string;
|
|
89
|
-
[key: string]: unknown;
|
|
90
|
-
}>({ entity, data, schemaEntityObjectRepresentation, }: {
|
|
91
|
-
entity: string;
|
|
92
|
-
data: TData | TData[];
|
|
93
|
-
schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation;
|
|
94
|
-
}): Promise<void>;
|
|
95
|
-
/**
|
|
96
|
-
* event emitted from the link modules to attach a link entity to its parent and child entities from the linked modules.
|
|
97
|
-
* @param entity
|
|
98
|
-
* @param data
|
|
99
|
-
* @param schemaEntityObjectRepresentation
|
|
100
|
-
* @protected
|
|
101
|
-
*/
|
|
102
|
-
protected onAttach<TData extends {
|
|
103
|
-
id: string;
|
|
104
|
-
[key: string]: unknown;
|
|
105
|
-
}>({ entity, data, schemaEntityObjectRepresentation, }: {
|
|
106
|
-
entity: string;
|
|
107
|
-
data: TData | TData[];
|
|
108
|
-
schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation;
|
|
109
|
-
}): Promise<void>;
|
|
110
|
-
/**
|
|
111
|
-
* Event emitted from the link modules to detach a link entity from its parent and child entities from the linked modules.
|
|
112
|
-
* @param entity
|
|
113
|
-
* @param data
|
|
114
|
-
* @param schemaEntityObjectRepresentation
|
|
115
|
-
* @protected
|
|
116
|
-
*/
|
|
117
|
-
protected onDetach<TData extends {
|
|
118
|
-
id: string;
|
|
119
|
-
[key: string]: unknown;
|
|
120
|
-
}>({ entity, data, schemaEntityObjectRepresentation, }: {
|
|
121
|
-
entity: string;
|
|
122
|
-
data: TData | TData[];
|
|
123
|
-
schemaEntityObjectRepresentation: SchemaObjectEntityRepresentation;
|
|
124
|
-
}): Promise<void>;
|
|
125
|
-
}
|
|
126
|
-
export {};
|
|
@@ -1,441 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _PostgresProvider_isReady_;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.PostgresProvider = void 0;
|
|
16
|
-
const utils_1 = require("@medusajs/utils");
|
|
17
|
-
const _models_1 = require("../models");
|
|
18
|
-
const utils_2 = require("../utils");
|
|
19
|
-
class PostgresProvider {
|
|
20
|
-
get remoteQuery_() {
|
|
21
|
-
return this.container_.remoteQuery;
|
|
22
|
-
}
|
|
23
|
-
constructor(container, options, moduleOptions) {
|
|
24
|
-
_PostgresProvider_isReady_.set(this, void 0);
|
|
25
|
-
this.eventActionToMethodMap_ = {
|
|
26
|
-
created: "onCreate",
|
|
27
|
-
updated: "onUpdate",
|
|
28
|
-
deleted: "onDelete",
|
|
29
|
-
attached: "onAttach",
|
|
30
|
-
detached: "onDetach",
|
|
31
|
-
};
|
|
32
|
-
this.container_ = container;
|
|
33
|
-
this.moduleOptions_ = moduleOptions;
|
|
34
|
-
this.schemaObjectRepresentation_ = options.schemaObjectRepresentation;
|
|
35
|
-
this.schemaEntitiesMap_ = options.entityMap;
|
|
36
|
-
// Add a new column for each key that can be found in the jsonb data column to perform indexes and query on it.
|
|
37
|
-
// So far, the execution time is about the same
|
|
38
|
-
/*;(async () => {
|
|
39
|
-
const query = [
|
|
40
|
-
...new Set(
|
|
41
|
-
Object.keys(this.schemaObjectRepresentation_)
|
|
42
|
-
.filter(
|
|
43
|
-
(key) =>
|
|
44
|
-
![
|
|
45
|
-
"_serviceNameModuleConfigMap",
|
|
46
|
-
"_schemaPropertiesMap",
|
|
47
|
-
].includes(key)
|
|
48
|
-
)
|
|
49
|
-
.map((key) => {
|
|
50
|
-
return this.schemaObjectRepresentation_[key].fields.filter(
|
|
51
|
-
(field) => !field.includes(".")
|
|
52
|
-
)
|
|
53
|
-
})
|
|
54
|
-
.flat()
|
|
55
|
-
),
|
|
56
|
-
].map(
|
|
57
|
-
(field) =>
|
|
58
|
-
"ALTER TABLE catalog ADD IF NOT EXISTS " +
|
|
59
|
-
field +
|
|
60
|
-
" text GENERATED ALWAYS AS (NEW.data->>'" +
|
|
61
|
-
field +
|
|
62
|
-
"') STORED"
|
|
63
|
-
)
|
|
64
|
-
await this.container_.manager.execute(query.join(";"))
|
|
65
|
-
})()*/
|
|
66
|
-
}
|
|
67
|
-
async onApplicationStart() {
|
|
68
|
-
let initalizedOk = () => { };
|
|
69
|
-
let initalizedNok = () => { };
|
|
70
|
-
__classPrivateFieldSet(this, _PostgresProvider_isReady_, new Promise((resolve, reject) => {
|
|
71
|
-
initalizedOk = resolve;
|
|
72
|
-
initalizedNok = reject;
|
|
73
|
-
}), "f");
|
|
74
|
-
await (0, utils_2.createPartitions)(this.schemaObjectRepresentation_, this.container_.manager)
|
|
75
|
-
.then(initalizedOk)
|
|
76
|
-
.catch(initalizedNok);
|
|
77
|
-
}
|
|
78
|
-
static parseData(data, schemaEntityObjectRepresentation) {
|
|
79
|
-
const data_ = Array.isArray(data) ? data : [data];
|
|
80
|
-
// Always keep the id in the entity properties
|
|
81
|
-
const entityProperties = ["id"];
|
|
82
|
-
const parentsProperties = {};
|
|
83
|
-
/**
|
|
84
|
-
* Split fields into entity properties and parents properties
|
|
85
|
-
*/
|
|
86
|
-
schemaEntityObjectRepresentation.fields.forEach((field) => {
|
|
87
|
-
var _a;
|
|
88
|
-
if (field.includes(".")) {
|
|
89
|
-
const parentAlias = field.split(".")[0];
|
|
90
|
-
const parentSchemaObjectRepresentation = schemaEntityObjectRepresentation.parents.find((parent) => parent.ref.alias === parentAlias);
|
|
91
|
-
if (!parentSchemaObjectRepresentation) {
|
|
92
|
-
throw new Error(`SearchModule error, unable to parse data for ${schemaEntityObjectRepresentation.entity}. The parent schema object representation could not be found for the alias ${parentAlias} for the entity ${schemaEntityObjectRepresentation.entity}.`);
|
|
93
|
-
}
|
|
94
|
-
parentsProperties[_a = parentSchemaObjectRepresentation.ref.entity] ?? (parentsProperties[_a] = []);
|
|
95
|
-
parentsProperties[parentSchemaObjectRepresentation.ref.entity].push(field);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
entityProperties.push(field);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
return {
|
|
102
|
-
data: data_,
|
|
103
|
-
entityProperties,
|
|
104
|
-
parentsProperties,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
static parseMessageData(message) {
|
|
108
|
-
const isMessageShape = (0, utils_1.isDefined)(message?.body);
|
|
109
|
-
if (!isMessageShape) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
const result = {
|
|
113
|
-
action: "",
|
|
114
|
-
data: [],
|
|
115
|
-
ids: [],
|
|
116
|
-
};
|
|
117
|
-
result.action = message.body.metadata.action;
|
|
118
|
-
result.data = message.body.data;
|
|
119
|
-
result.data = Array.isArray(result.data) ? result.data : [result.data];
|
|
120
|
-
result.ids = result.data.map((d) => d.id);
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
async query(selection, options) {
|
|
124
|
-
await __classPrivateFieldGet(this, _PostgresProvider_isReady_, "f");
|
|
125
|
-
let hasPagination = false;
|
|
126
|
-
if (typeof options?.take === "number" ||
|
|
127
|
-
typeof options?.skip === "number") {
|
|
128
|
-
hasPagination = true;
|
|
129
|
-
}
|
|
130
|
-
const connection = this.container_.manager.getConnection();
|
|
131
|
-
const qb = new utils_2.QueryBuilder({
|
|
132
|
-
schema: this.schemaObjectRepresentation_,
|
|
133
|
-
entityMap: this.schemaEntitiesMap_,
|
|
134
|
-
knex: connection.getKnex(),
|
|
135
|
-
selector: selection,
|
|
136
|
-
options,
|
|
137
|
-
});
|
|
138
|
-
const sql = qb.buildQuery(hasPagination, !!options?.keepFilteredEntities);
|
|
139
|
-
let resultset = await connection.execute(sql);
|
|
140
|
-
if (options?.keepFilteredEntities) {
|
|
141
|
-
const mainEntity = Object.keys(selection.select)[0];
|
|
142
|
-
const ids = resultset.map((r) => r[`${mainEntity}.id`]);
|
|
143
|
-
if (ids.length) {
|
|
144
|
-
const selection_ = {
|
|
145
|
-
select: selection.select,
|
|
146
|
-
joinWhere: selection.joinWhere,
|
|
147
|
-
where: {
|
|
148
|
-
[`${mainEntity}.id`]: ids,
|
|
149
|
-
},
|
|
150
|
-
};
|
|
151
|
-
return await this.query(selection_);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return qb.buildObjectFromResultset(resultset);
|
|
155
|
-
}
|
|
156
|
-
async queryAndCount(selection, options) {
|
|
157
|
-
await __classPrivateFieldGet(this, _PostgresProvider_isReady_, "f");
|
|
158
|
-
const connection = this.container_.manager.getConnection();
|
|
159
|
-
const qb = new utils_2.QueryBuilder({
|
|
160
|
-
schema: this.schemaObjectRepresentation_,
|
|
161
|
-
entityMap: this.schemaEntitiesMap_,
|
|
162
|
-
knex: connection.getKnex(),
|
|
163
|
-
selector: selection,
|
|
164
|
-
options,
|
|
165
|
-
});
|
|
166
|
-
const sql = qb.buildQuery(true, !!options?.keepFilteredEntities);
|
|
167
|
-
let resultset = await connection.execute(sql);
|
|
168
|
-
const count = +(resultset[0]?.count ?? 0);
|
|
169
|
-
if (options?.keepFilteredEntities) {
|
|
170
|
-
const mainEntity = Object.keys(selection.select)[0];
|
|
171
|
-
const ids = resultset.map((r) => r[`${mainEntity}.id`]);
|
|
172
|
-
if (ids.length) {
|
|
173
|
-
const selection_ = {
|
|
174
|
-
select: selection.select,
|
|
175
|
-
joinWhere: selection.joinWhere,
|
|
176
|
-
where: {
|
|
177
|
-
[`${mainEntity}.id`]: ids,
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
return [await this.query(selection_), count];
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return [qb.buildObjectFromResultset(resultset), count];
|
|
184
|
-
}
|
|
185
|
-
consumeEvent(schemaEntityObjectRepresentation) {
|
|
186
|
-
return async (data, eventName) => {
|
|
187
|
-
await __classPrivateFieldGet(this, _PostgresProvider_isReady_, "f");
|
|
188
|
-
const data_ = Array.isArray(data) ? data : [data];
|
|
189
|
-
let ids = data_.map((d) => d.id);
|
|
190
|
-
let action = eventName.split(".").pop() || "";
|
|
191
|
-
const parsedMessage = PostgresProvider.parseMessageData(data);
|
|
192
|
-
if (parsedMessage) {
|
|
193
|
-
action = parsedMessage.action;
|
|
194
|
-
ids = parsedMessage.ids;
|
|
195
|
-
}
|
|
196
|
-
const { fields, alias } = schemaEntityObjectRepresentation;
|
|
197
|
-
const entityData = await this.remoteQuery_((0, utils_1.remoteQueryObjectFromString)({
|
|
198
|
-
entryPoint: alias,
|
|
199
|
-
variables: {
|
|
200
|
-
filters: {
|
|
201
|
-
id: ids,
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
fields,
|
|
205
|
-
}));
|
|
206
|
-
const argument = {
|
|
207
|
-
entity: schemaEntityObjectRepresentation.entity,
|
|
208
|
-
data: entityData,
|
|
209
|
-
schemaEntityObjectRepresentation,
|
|
210
|
-
};
|
|
211
|
-
const targetMethod = this.eventActionToMethodMap_[action];
|
|
212
|
-
if (!targetMethod) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
await this[targetMethod](argument);
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Create the catalog entry and the catalog relation entry when this event is emitted.
|
|
220
|
-
* @param entity
|
|
221
|
-
* @param data
|
|
222
|
-
* @param schemaEntityObjectRepresentation
|
|
223
|
-
* @protected
|
|
224
|
-
*/
|
|
225
|
-
async onCreate({ entity, data, schemaEntityObjectRepresentation, }) {
|
|
226
|
-
await this.container_.manager.transactional(async (em) => {
|
|
227
|
-
const catalogRepository = em.getRepository(_models_1.Catalog);
|
|
228
|
-
const catalogRelationRepository = em.getRepository(_models_1.CatalogRelation);
|
|
229
|
-
const { data: data_, entityProperties, parentsProperties, } = PostgresProvider.parseData(data, schemaEntityObjectRepresentation);
|
|
230
|
-
/**
|
|
231
|
-
* Loop through the data and create catalog entries for each entity as well as the
|
|
232
|
-
* catalog relation entries if the entity has parents
|
|
233
|
-
*/
|
|
234
|
-
for (const entityData of data_) {
|
|
235
|
-
/**
|
|
236
|
-
* Clean the entity data to only keep the properties that are defined in the schema
|
|
237
|
-
*/
|
|
238
|
-
const cleanedEntityData = entityProperties.reduce((acc, property) => {
|
|
239
|
-
acc[property] = entityData[property];
|
|
240
|
-
return acc;
|
|
241
|
-
}, {});
|
|
242
|
-
await catalogRepository.upsert({
|
|
243
|
-
id: cleanedEntityData.id,
|
|
244
|
-
name: entity,
|
|
245
|
-
data: cleanedEntityData,
|
|
246
|
-
});
|
|
247
|
-
/**
|
|
248
|
-
* Retrieve the parents to attach it to the catalog entry.
|
|
249
|
-
*/
|
|
250
|
-
for (const [parentEntity, parentProperties] of Object.entries(parentsProperties)) {
|
|
251
|
-
const parentAlias = parentProperties[0].split(".")[0];
|
|
252
|
-
const parentData = entityData[parentAlias];
|
|
253
|
-
if (!parentData) {
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
const parentDataCollection = Array.isArray(parentData)
|
|
257
|
-
? parentData
|
|
258
|
-
: [parentData];
|
|
259
|
-
for (const parentData_ of parentDataCollection) {
|
|
260
|
-
await catalogRepository.upsert({
|
|
261
|
-
id: parentData_.id,
|
|
262
|
-
name: parentEntity,
|
|
263
|
-
data: parentData_,
|
|
264
|
-
});
|
|
265
|
-
const parentCatalogRelationEntry = catalogRelationRepository.create({
|
|
266
|
-
parent_id: parentData_.id,
|
|
267
|
-
parent_name: parentEntity,
|
|
268
|
-
child_id: cleanedEntityData.id,
|
|
269
|
-
child_name: entity,
|
|
270
|
-
pivot: `${parentEntity}-${entity}`,
|
|
271
|
-
});
|
|
272
|
-
catalogRelationRepository.persist(parentCatalogRelationEntry);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
await em.flush();
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Update the catalog entry when this event is emitted.
|
|
281
|
-
* @param entity
|
|
282
|
-
* @param data
|
|
283
|
-
* @param schemaEntityObjectRepresentation
|
|
284
|
-
* @protected
|
|
285
|
-
*/
|
|
286
|
-
async onUpdate({ entity, data, schemaEntityObjectRepresentation, }) {
|
|
287
|
-
await this.container_.manager.transactional(async (em) => {
|
|
288
|
-
const catalogRepository = em.getRepository(_models_1.Catalog);
|
|
289
|
-
const { data: data_, entityProperties } = PostgresProvider.parseData(data, schemaEntityObjectRepresentation);
|
|
290
|
-
await catalogRepository.upsertMany(data_.map((entityData) => {
|
|
291
|
-
return {
|
|
292
|
-
id: entityData.id,
|
|
293
|
-
name: entity,
|
|
294
|
-
data: entityProperties.reduce((acc, property) => {
|
|
295
|
-
acc[property] = entityData[property];
|
|
296
|
-
return acc;
|
|
297
|
-
}, {}),
|
|
298
|
-
};
|
|
299
|
-
}));
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Delete the catalog entry when this event is emitted.
|
|
304
|
-
* @param entity
|
|
305
|
-
* @param data
|
|
306
|
-
* @param schemaEntityObjectRepresentation
|
|
307
|
-
* @protected
|
|
308
|
-
*/
|
|
309
|
-
async onDelete({ entity, data, schemaEntityObjectRepresentation, }) {
|
|
310
|
-
await this.container_.manager.transactional(async (em) => {
|
|
311
|
-
const catalogRepository = em.getRepository(_models_1.Catalog);
|
|
312
|
-
const catalogRelationRepository = em.getRepository(_models_1.CatalogRelation);
|
|
313
|
-
const { data: data_ } = PostgresProvider.parseData(data, schemaEntityObjectRepresentation);
|
|
314
|
-
const ids = data_.map((entityData) => entityData.id);
|
|
315
|
-
await catalogRepository.nativeDelete({
|
|
316
|
-
id: { $in: ids },
|
|
317
|
-
name: entity,
|
|
318
|
-
});
|
|
319
|
-
await catalogRelationRepository.nativeDelete({
|
|
320
|
-
$or: [
|
|
321
|
-
{
|
|
322
|
-
parent_id: { $in: ids },
|
|
323
|
-
parent_name: entity,
|
|
324
|
-
},
|
|
325
|
-
{
|
|
326
|
-
child_id: { $in: ids },
|
|
327
|
-
child_name: entity,
|
|
328
|
-
},
|
|
329
|
-
],
|
|
330
|
-
});
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* event emitted from the link modules to attach a link entity to its parent and child entities from the linked modules.
|
|
335
|
-
* @param entity
|
|
336
|
-
* @param data
|
|
337
|
-
* @param schemaEntityObjectRepresentation
|
|
338
|
-
* @protected
|
|
339
|
-
*/
|
|
340
|
-
async onAttach({ entity, data, schemaEntityObjectRepresentation, }) {
|
|
341
|
-
await this.container_.manager.transactional(async (em) => {
|
|
342
|
-
const catalogRepository = em.getRepository(_models_1.Catalog);
|
|
343
|
-
const catalogRelationRepository = em.getRepository(_models_1.CatalogRelation);
|
|
344
|
-
const { data: data_, entityProperties } = PostgresProvider.parseData(data, schemaEntityObjectRepresentation);
|
|
345
|
-
/**
|
|
346
|
-
* Retrieve the property that represent the foreign key related to the parent entity of the link entity.
|
|
347
|
-
* Then from the service name of the parent entity, retrieve the entity name using the linkable keys.
|
|
348
|
-
*/
|
|
349
|
-
const parentPropertyId = schemaEntityObjectRepresentation.moduleConfig.relationships[0]
|
|
350
|
-
.foreignKey;
|
|
351
|
-
const parentServiceName = schemaEntityObjectRepresentation.moduleConfig.relationships[0]
|
|
352
|
-
.serviceName;
|
|
353
|
-
const parentEntityName = this.schemaObjectRepresentation_._serviceNameModuleConfigMap[parentServiceName].linkableKeys?.[parentPropertyId];
|
|
354
|
-
if (!parentEntityName) {
|
|
355
|
-
throw new Error(`SearchModule error, unable to handle attach event for ${entity}. The parent entity name could not be found using the linkable keys from the module ${parentServiceName}.`);
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Retrieve the property that represent the foreign key related to the child entity of the link entity.
|
|
359
|
-
* Then from the service name of the child entity, retrieve the entity name using the linkable keys.
|
|
360
|
-
*/
|
|
361
|
-
const childPropertyId = schemaEntityObjectRepresentation.moduleConfig.relationships[1]
|
|
362
|
-
.foreignKey;
|
|
363
|
-
const childServiceName = schemaEntityObjectRepresentation.moduleConfig.relationships[1]
|
|
364
|
-
.serviceName;
|
|
365
|
-
const childEntityName = this.schemaObjectRepresentation_._serviceNameModuleConfigMap[childServiceName].linkableKeys?.[childPropertyId];
|
|
366
|
-
if (!childEntityName) {
|
|
367
|
-
throw new Error(`SearchModule error, unable to handle attach event for ${entity}. The child entity name could not be found using the linkable keys from the module ${childServiceName}.`);
|
|
368
|
-
}
|
|
369
|
-
for (const entityData of data_) {
|
|
370
|
-
/**
|
|
371
|
-
* Clean the link entity data to only keep the properties that are defined in the schema
|
|
372
|
-
*/
|
|
373
|
-
const cleanedEntityData = entityProperties.reduce((acc, property) => {
|
|
374
|
-
acc[property] = entityData[property];
|
|
375
|
-
return acc;
|
|
376
|
-
}, {});
|
|
377
|
-
const catalogEntry = catalogRepository.create({
|
|
378
|
-
id: cleanedEntityData.id,
|
|
379
|
-
name: entity,
|
|
380
|
-
data: cleanedEntityData,
|
|
381
|
-
});
|
|
382
|
-
catalogRepository.persist(catalogEntry);
|
|
383
|
-
/**
|
|
384
|
-
* Create the catalog relation entries for the parent entity and the child entity
|
|
385
|
-
*/
|
|
386
|
-
const parentCatalogRelationEntry = catalogRelationRepository.create({
|
|
387
|
-
parent_id: entityData[parentPropertyId],
|
|
388
|
-
parent_name: parentEntityName,
|
|
389
|
-
child_id: cleanedEntityData.id,
|
|
390
|
-
child_name: entity,
|
|
391
|
-
pivot: `${parentEntityName}-${entity}`,
|
|
392
|
-
});
|
|
393
|
-
const childCatalogRelationEntry = catalogRelationRepository.create({
|
|
394
|
-
parent_id: cleanedEntityData.id,
|
|
395
|
-
parent_name: entity,
|
|
396
|
-
child_id: entityData[childPropertyId],
|
|
397
|
-
child_name: childEntityName,
|
|
398
|
-
pivot: `${entity}-${childEntityName}`,
|
|
399
|
-
});
|
|
400
|
-
catalogRelationRepository.persist([
|
|
401
|
-
parentCatalogRelationEntry,
|
|
402
|
-
childCatalogRelationEntry,
|
|
403
|
-
]);
|
|
404
|
-
}
|
|
405
|
-
await em.flush();
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Event emitted from the link modules to detach a link entity from its parent and child entities from the linked modules.
|
|
410
|
-
* @param entity
|
|
411
|
-
* @param data
|
|
412
|
-
* @param schemaEntityObjectRepresentation
|
|
413
|
-
* @protected
|
|
414
|
-
*/
|
|
415
|
-
async onDetach({ entity, data, schemaEntityObjectRepresentation, }) {
|
|
416
|
-
await this.container_.manager.transactional(async (em) => {
|
|
417
|
-
const catalogRepository = em.getRepository(_models_1.Catalog);
|
|
418
|
-
const catalogRelationRepository = em.getRepository(_models_1.CatalogRelation);
|
|
419
|
-
const { data: data_ } = PostgresProvider.parseData(data, schemaEntityObjectRepresentation);
|
|
420
|
-
const ids = data_.map((entityData) => entityData.id);
|
|
421
|
-
await catalogRepository.nativeDelete({
|
|
422
|
-
id: { $in: ids },
|
|
423
|
-
name: entity,
|
|
424
|
-
});
|
|
425
|
-
await catalogRelationRepository.nativeDelete({
|
|
426
|
-
$or: [
|
|
427
|
-
{
|
|
428
|
-
parent_id: { $in: ids },
|
|
429
|
-
parent_name: entity,
|
|
430
|
-
},
|
|
431
|
-
{
|
|
432
|
-
child_id: { $in: ids },
|
|
433
|
-
child_name: entity,
|
|
434
|
-
},
|
|
435
|
-
],
|
|
436
|
-
});
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
exports.PostgresProvider = PostgresProvider;
|
|
441
|
-
_PostgresProvider_isReady_ = new WeakMap();
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { SchemaObjectRepresentation } from "../types";
|
|
2
|
-
export declare const CustomDirectives: {
|
|
3
|
-
Listeners: {
|
|
4
|
-
configurationPropertyName: string;
|
|
5
|
-
isRequired: boolean;
|
|
6
|
-
name: string;
|
|
7
|
-
directive: string;
|
|
8
|
-
definition: string;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* This util build an internal representation object from the provided schema.
|
|
13
|
-
* It will resolve all modules, fields, link module representation to build
|
|
14
|
-
* the appropriate representation for the catalog module.
|
|
15
|
-
*
|
|
16
|
-
* This representation will be used to re construct the expected output object from a search
|
|
17
|
-
* but can also be used for anything since the relation tree is available through ref.
|
|
18
|
-
*
|
|
19
|
-
* @param schema
|
|
20
|
-
*/
|
|
21
|
-
export declare function buildSchemaObjectRepresentation(schema: any): [SchemaObjectRepresentation, Record<string, any>];
|