Skip to content

Commit

Permalink
feat: Added Schema type (#84)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gismya authored Mar 15, 2023
1 parent 4dce809 commit 128e96e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
7 changes: 5 additions & 2 deletions source/project_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ export function getStatuses(
} else if (entityType === "AssetVersion") {
statuses = data._version_workflow.statuses;
} else {
const schema = session.getSchema(entityType) as any;
const schema = session.getSchema(entityType);

if (schema && schema.alias_for && schema.alias_for.id === "Task") {
if (
typeof schema?.alias_for === "object" &&
schema.alias_for.id === "Task"
) {
const objectTypeId = schema.alias_for.classifiers.object_typeid;

for (const index in data._schemas) {
Expand Down
30 changes: 15 additions & 15 deletions source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import type {
MutationOptions,
QueryOptions,
QueryResponse,
QuerySchemasResponse,
QueryServerInformationResponse,
ResponseError,
Schema,
SearchOptions,
SearchResponse,
SessionOptions,
Expand Down Expand Up @@ -75,7 +77,7 @@ export class Session {
initialized: boolean;
initializing: Promise<Session>;
serverInformation?: Data;
schemas?: Data;
schemas?: Schema[];
serverVersion?: string;
additionalHeaders: Data;

Expand Down Expand Up @@ -214,7 +216,7 @@ export class Session {
* @type {Promise}
*/
this.initializing = this.call<
[QueryServerInformationResponse, QueryResponse]
[QueryServerInformationResponse, QuerySchemasResponse]
>(operations).then((responses) => {
this.serverInformation = responses[0];
this.schemas = responses[1];
Expand All @@ -230,13 +232,13 @@ export class Session {
*
* @return {Array|null} List of primary key attributes.
*/
getPrimaryKeyAttributes(entityType: string) {
getPrimaryKeyAttributes(entityType: string): string[] | null {
if (!this.schemas) {
logger.warn("Schemas not available.");
return null;
}
const schema = this.schemas.find((item: any) => item.id === entityType);
if (!schema || !schema.primary_key) {
const schema = this.schemas.find((item) => item.id === entityType);
if (!schema || !schema.primary_key || !schema.primary_key.length) {
logger.warn("Primary key could not be found for: ", entityType);
return null;
}
Expand Down Expand Up @@ -641,7 +643,7 @@ export class Session {
"Ensuring entity with data using identifying keys: ",
entityType,
data,
identifyingKeys
keys
);

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

const primaryKeys = this.getPrimaryKeyAttributes(entityType);
if (primaryKeys === null || primaryKeys.length === 0) {
throw new Error(`Primary key could not be found for: ${entityType}`);
}
let expression = `select ${primaryKeys.join(
", "
)} from ${entityType} where`;
Expand Down Expand Up @@ -705,7 +710,7 @@ export class Session {
entityType,
primaryKeys.map((key: string) => updateEntity[key]),
Object.keys(data).reduce<T>((accumulator, key: keyof T) => {
if (primaryKeys.indexOf(key) === -1) {
if (primaryKeys.indexOf(key.toString()) === -1) {
accumulator[key] = data[key];
}
return accumulator;
Expand All @@ -722,14 +727,9 @@ export class Session {
* @param {string} schemaId Id of schema model, e.g. `AssetVersion`.
* @return {Object|null} Schema definition
*/
getSchema(schemaId: string): Data | null {
for (const index in this.schemas) {
if (this.schemas[index].id === schemaId) {
return this.schemas[index];
}
}

return null;
getSchema(schemaId: string): Schema | null {
const schema = this.schemas?.find((s) => s.id === schemaId);
return schema ?? null;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface ResetRemoteResponse {
action: "reset_remote";
data: Data;
}
export type QuerySchemasResponse = Data[];
export type QuerySchemasResponse = Schema[];
export interface QueryServerInformationResponse {
custom_widget?: Data;
default_colors?: string[];
Expand Down Expand Up @@ -161,6 +161,18 @@ export interface MutationOptions {
decodeDatesAsIso?: boolean;
}

export interface Schema {
properties: Data;
default_projections: string[];
primary_key: string[];
required: string[];
immutable: string[];
type?: string;
id: string;
computed?: string[];
system_projections?: string[];
alias_for?: string | Data;
}
export interface QueryOptions {
abortController?: AbortController;
signal?: AbortSignal;
Expand Down

0 comments on commit 128e96e

Please sign in to comment.