Skip to content

Commit 128e96e

Browse files
authored
feat: Added Schema type (#84)
* feat: Added Schema type * Rewrite getSchema to use find() * Removed a few "as any" * Throw an error if no primary key is found during ensure
1 parent 4dce809 commit 128e96e

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

source/project_schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ export function getStatuses(
9797
} else if (entityType === "AssetVersion") {
9898
statuses = data._version_workflow.statuses;
9999
} else {
100-
const schema = session.getSchema(entityType) as any;
100+
const schema = session.getSchema(entityType);
101101

102-
if (schema && schema.alias_for && schema.alias_for.id === "Task") {
102+
if (
103+
typeof schema?.alias_for === "object" &&
104+
schema.alias_for.id === "Task"
105+
) {
103106
const objectTypeId = schema.alias_for.classifiers.object_typeid;
104107

105108
for (const index in data._schemas) {

source/session.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import type {
2727
MutationOptions,
2828
QueryOptions,
2929
QueryResponse,
30+
QuerySchemasResponse,
3031
QueryServerInformationResponse,
3132
ResponseError,
33+
Schema,
3234
SearchOptions,
3335
SearchResponse,
3436
SessionOptions,
@@ -75,7 +77,7 @@ export class Session {
7577
initialized: boolean;
7678
initializing: Promise<Session>;
7779
serverInformation?: Data;
78-
schemas?: Data;
80+
schemas?: Schema[];
7981
serverVersion?: string;
8082
additionalHeaders: Data;
8183

@@ -214,7 +216,7 @@ export class Session {
214216
* @type {Promise}
215217
*/
216218
this.initializing = this.call<
217-
[QueryServerInformationResponse, QueryResponse]
219+
[QueryServerInformationResponse, QuerySchemasResponse]
218220
>(operations).then((responses) => {
219221
this.serverInformation = responses[0];
220222
this.schemas = responses[1];
@@ -230,13 +232,13 @@ export class Session {
230232
*
231233
* @return {Array|null} List of primary key attributes.
232234
*/
233-
getPrimaryKeyAttributes(entityType: string) {
235+
getPrimaryKeyAttributes(entityType: string): string[] | null {
234236
if (!this.schemas) {
235237
logger.warn("Schemas not available.");
236238
return null;
237239
}
238-
const schema = this.schemas.find((item: any) => item.id === entityType);
239-
if (!schema || !schema.primary_key) {
240+
const schema = this.schemas.find((item) => item.id === entityType);
241+
if (!schema || !schema.primary_key || !schema.primary_key.length) {
240242
logger.warn("Primary key could not be found for: ", entityType);
241243
return null;
242244
}
@@ -641,7 +643,7 @@ export class Session {
641643
"Ensuring entity with data using identifying keys: ",
642644
entityType,
643645
data,
644-
identifyingKeys
646+
keys
645647
);
646648

647649
if (!keys.length) {
@@ -657,6 +659,9 @@ export class Session {
657659
}
658660

659661
const primaryKeys = this.getPrimaryKeyAttributes(entityType);
662+
if (primaryKeys === null || primaryKeys.length === 0) {
663+
throw new Error(`Primary key could not be found for: ${entityType}`);
664+
}
660665
let expression = `select ${primaryKeys.join(
661666
", "
662667
)} from ${entityType} where`;
@@ -705,7 +710,7 @@ export class Session {
705710
entityType,
706711
primaryKeys.map((key: string) => updateEntity[key]),
707712
Object.keys(data).reduce<T>((accumulator, key: keyof T) => {
708-
if (primaryKeys.indexOf(key) === -1) {
713+
if (primaryKeys.indexOf(key.toString()) === -1) {
709714
accumulator[key] = data[key];
710715
}
711716
return accumulator;
@@ -722,14 +727,9 @@ export class Session {
722727
* @param {string} schemaId Id of schema model, e.g. `AssetVersion`.
723728
* @return {Object|null} Schema definition
724729
*/
725-
getSchema(schemaId: string): Data | null {
726-
for (const index in this.schemas) {
727-
if (this.schemas[index].id === schemaId) {
728-
return this.schemas[index];
729-
}
730-
}
731-
732-
return null;
730+
getSchema(schemaId: string): Schema | null {
731+
const schema = this.schemas?.find((s) => s.id === schemaId);
732+
return schema ?? null;
733733
}
734734

735735
/**

source/types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface ResetRemoteResponse {
6666
action: "reset_remote";
6767
data: Data;
6868
}
69-
export type QuerySchemasResponse = Data[];
69+
export type QuerySchemasResponse = Schema[];
7070
export interface QueryServerInformationResponse {
7171
custom_widget?: Data;
7272
default_colors?: string[];
@@ -161,6 +161,18 @@ export interface MutationOptions {
161161
decodeDatesAsIso?: boolean;
162162
}
163163

164+
export interface Schema {
165+
properties: Data;
166+
default_projections: string[];
167+
primary_key: string[];
168+
required: string[];
169+
immutable: string[];
170+
type?: string;
171+
id: string;
172+
computed?: string[];
173+
system_projections?: string[];
174+
alias_for?: string | Data;
175+
}
164176
export interface QueryOptions {
165177
abortController?: AbortController;
166178
signal?: AbortSignal;

0 commit comments

Comments
 (0)