Skip to content

feat: Added Schema type #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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