Skip to content
Closed
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
18 changes: 11 additions & 7 deletions src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,19 @@ BaseModel.fromJsonApi = function (body) {

// Attributes
for (let [attribute, value] of Object.entries(body.data.attributes ?? {})) {
const attributeName = schema.transformPropertyName(attribute, schema.options?.serverTransform, schema.options?.clientTransform);
const property = schema.attributes[attribute];

if (property?.type === Date && value) {
value = new Date(value);
}

doc.set(attribute, value, { skipMarkModified: true });
doc.set(attributeName, value, { skipMarkModified: true });
}

// Relationships
for (const [relationship, value] of Object.entries(body.data.relationships ?? {})) {
const relationshipName = schema.transformPropertyName(relationship, schema.options?.serverTransform, schema.options?.clientTransform);
if (Array.isArray(value.data)) {
const related = value.data.map((identifier) => {
const model = models[identifier.type];
Expand All @@ -214,7 +216,7 @@ BaseModel.fromJsonApi = function (body) {
});
});

doc.set(relationship, related, { skipMarkModified: true });
doc.set(relationshipName, related, { skipMarkModified: true });
} else if (value.data) {
const identifier = value.data;
const model = models[identifier.type];
Expand All @@ -224,7 +226,7 @@ BaseModel.fromJsonApi = function (body) {
data: body.included!.find((resource) => resource.type === identifier.type && resource.id === identifier.id),
});

doc.set(relationship, related, { skipMarkModified: true });
doc.set(relationshipName, related, { skipMarkModified: true });
}
}

Expand Down Expand Up @@ -473,24 +475,26 @@ BaseModel.prototype.toJsonApi = function () {
}
}

data.attributes![attribute] = value;
const attributeName = this.schema.transformPropertyName(attribute, this.schema.options?.clientTransform, this.schema.options?.serverTransform);
data.attributes![attributeName] = value;
}

for (const [relationship, property] of Object.entries(this.schema.relationships)) {
const relationshipName = this.schema.transformPropertyName(relationship, this.schema.options?.clientTransform, this.schema.options?.serverTransform);
if (!this.isNew && !this.isModified(relationship)) continue;

const value = this.get(relationship) as ModelInstance<Record<string, any>> | ModelInstance<Record<string, any>>[] | null;

if (Array.isArray(value)) {
data.relationships![relationship] = {
data.relationships![relationshipName] = {
data: value.map((val) => val.identifier()),
};
} else if (value) {
data.relationships![relationship] = {
data.relationships![relationshipName] = {
data: value.identifier(),
};
} else {
data.relationships![relationship] = {
data.relationships![relationshipName] = {
data: null,
};
}
Expand Down
23 changes: 23 additions & 0 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type AttributeDefinition<T> = {
}

type RelationshipsDefinition<DocType> = {
// I believe I get into trouble here
[path in keyof DocType]?: RelationshipDefinition<DocType[path]>;
}

Expand All @@ -47,7 +48,11 @@ type RelationshipDefinition<T> = {
transform?: (val: T) => any;
}

type NameTransformer = "camelCase" | "snake_case" | "literal"

type SchemaOptions<DocType> = {
serverTransform?: NameTransformer;
clientTransform?: NameTransformer;
}

class Schema<DocType> {
Expand All @@ -63,6 +68,8 @@ class Schema<DocType> {

relationships!: RelationshipsDefinition<DocType>;

options?: SchemaOptions<DocType>;

init!: (
definition: SchemaDefinition<DocType>,
options?: SchemaOptions<DocType>,
Expand All @@ -71,11 +78,14 @@ class Schema<DocType> {
add!: (
obj: SchemaDefinition<DocType>,
) => this;

transformPropertyName!: (propertyName: string, sourceFormat: NameTransformer | undefined, targetFormat: NameTransformer | undefined) => string;
}

Schema.prototype.init = function (definition, options) {
this.attributes = {};
this.relationships = {};
this.options = options || {};

this.add(definition);
};
Expand All @@ -90,5 +100,18 @@ Schema.prototype.add = function (obj) {
return this;
};

Schema.prototype.transformPropertyName = function (propertyName: string, sourceFormat: NameTransformer = "literal", targetFormat: NameTransformer = "literal") {
if (sourceFormat==="snake_case" && targetFormat === "camelCase") {
return propertyName.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
} else if (sourceFormat === "camelCase" && targetFormat === "snake_case") {
return propertyName.replace(/([A-Z])/g, "_$1").toLowerCase();
} else if (targetFormat === "literal" || sourceFormat === targetFormat) {
return propertyName;
} else {
console.warn(`Unsupported transformation from ${sourceFormat} to ${targetFormat}`);
}
return propertyName;
}


export default Schema