Skip to content

Commit c51662d

Browse files
committed
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 6fcf1ad commit c51662d

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

@@ -206,7 +208,7 @@ export class Session {
206208
* @type {Promise}
207209
*/
208210
this.initializing = this.call<
209-
[QueryServerInformationResponse, QueryResponse]
211+
[QueryServerInformationResponse, QuerySchemasResponse]
210212
>(operations).then((responses) => {
211213
this.serverInformation = responses[0];
212214
this.schemas = responses[1];
@@ -222,13 +224,13 @@ export class Session {
222224
*
223225
* @return {Array|null} List of primary key attributes.
224226
*/
225-
getPrimaryKeyAttributes(entityType: string) {
227+
getPrimaryKeyAttributes(entityType: string): string[] | null {
226228
if (!this.schemas) {
227229
logger.warn("Schemas not available.");
228230
return null;
229231
}
230-
const schema = this.schemas.find((item: any) => item.id === entityType);
231-
if (!schema || !schema.primary_key) {
232+
const schema = this.schemas.find((item) => item.id === entityType);
233+
if (!schema || !schema.primary_key || !schema.primary_key.length) {
232234
logger.warn("Primary key could not be found for: ", entityType);
233235
return null;
234236
}
@@ -613,7 +615,7 @@ export class Session {
613615
"Ensuring entity with data using identifying keys: ",
614616
entityType,
615617
data,
616-
identifyingKeys
618+
keys
617619
);
618620

619621
if (!keys.length) {
@@ -629,6 +631,9 @@ export class Session {
629631
}
630632

631633
const primaryKeys = this.getPrimaryKeyAttributes(entityType);
634+
if (primaryKeys === null || primaryKeys.length === 0) {
635+
throw new Error(`Primary key could not be found for: ${entityType}`);
636+
}
632637
let expression = `select ${primaryKeys.join(
633638
", "
634639
)} from ${entityType} where`;
@@ -677,7 +682,7 @@ export class Session {
677682
entityType,
678683
primaryKeys.map((key: string) => updateEntity[key]),
679684
Object.keys(data).reduce<T>((accumulator, key: keyof T) => {
680-
if (primaryKeys.indexOf(key) === -1) {
685+
if (primaryKeys.indexOf(key.toString()) === -1) {
681686
accumulator[key] = data[key];
682687
}
683688
return accumulator;
@@ -694,14 +699,9 @@ export class Session {
694699
* @param {string} schemaId Id of schema model, e.g. `AssetVersion`.
695700
* @return {Object|null} Schema definition
696701
*/
697-
getSchema(schemaId: string): Data | null {
698-
for (const index in this.schemas) {
699-
if (this.schemas[index].id === schemaId) {
700-
return this.schemas[index];
701-
}
702-
}
703-
704-
return null;
702+
getSchema(schemaId: string): Schema | null {
703+
const schema = this.schemas?.find((s) => s.id === schemaId);
704+
return schema ?? null;
705705
}
706706

707707
/**

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)