-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add computed fields to SQL #92
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start but can be improved ;)
export function getUniqueProperties(objects: any[]): string[] { | ||
const uniqueProperties: string[] = []; | ||
|
||
for (const object of objects) { | ||
for (const key of Object.keys(object)) { | ||
if (!uniqueProperties.includes(key)) { | ||
uniqueProperties.push(key); | ||
} | ||
} | ||
} | ||
|
||
return uniqueProperties; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function getUniqueProperties(objects: any[]): string[] { | |
const uniqueProperties: string[] = []; | |
for (const object of objects) { | |
for (const key of Object.keys(object)) { | |
if (!uniqueProperties.includes(key)) { | |
uniqueProperties.push(key); | |
} | |
} | |
} | |
return uniqueProperties; | |
} | |
export function getUniqueProperties(objects: any[]): string[] { | |
const uniqueProperties = new Set<string>(); | |
for (const object of objects) { | |
Object.keys(object).forEach(key => uniqueProperties.add(key)); | |
} | |
return Array.from(uniqueProperties); | |
} |
src/lib/schema.ts
Outdated
file: File = { | ||
_id: "", | ||
file_path: "", | ||
extension: "", | ||
url_path: null, | ||
filetype: null, | ||
metadata: null, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do this... It's not necessary at all. If you want to be able to assign dynamic properties I think you can just add this to the class:
[key: string]: any
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this way we lose tract of all fields, it's like using any
as a type. resulting in a loss of type safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not what I meant. Use original properties + this one for dynamic ones (aka computed fields)
src/lib/schema.ts
Outdated
this.file._id = file._id; | ||
this.file.file_path = file.file_path; | ||
this.file.extension = file.extension; | ||
this.file.url_path = file.url_path; | ||
this.file.filetype = file.filetype; | ||
this.file.metadata = file.metadata ? JSON.parse(file.metadata) : null; | ||
|
||
// Assign dynamic properties using index signature to this.file | ||
for (const key in file) { | ||
if ( | ||
key !== "_id" && | ||
key !== "file_path" && | ||
key !== "extension" && | ||
key !== "url_path" && | ||
key !== "filetype" && | ||
key !== "metadata" | ||
) { | ||
this.file[key] = file[key]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be much simpler:
this.file._id = file._id; | |
this.file.file_path = file.file_path; | |
this.file.extension = file.extension; | |
this.file.url_path = file.url_path; | |
this.file.filetype = file.filetype; | |
this.file.metadata = file.metadata ? JSON.parse(file.metadata) : null; | |
// Assign dynamic properties using index signature to this.file | |
for (const key in file) { | |
if ( | |
key !== "_id" && | |
key !== "file_path" && | |
key !== "extension" && | |
key !== "url_path" && | |
key !== "filetype" && | |
key !== "metadata" | |
) { | |
this.file[key] = file[key]; | |
} | |
} | |
Object.keys(file).forEach(key => { | |
if (key === "metadata") { | |
this[key] = file[key] ? JSON.parse(file[key]) : null; | |
} else { | |
this[key] = file[key]; | |
} | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
src/lib/schema.ts
Outdated
for (let index = 0; index < properties.length; index++) { | ||
table.string(properties[index]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a computed property returns an array or a boolean? Shouldn't it be table.<some type>
? Also, were the comments incorrect?
for (let index = 0; index < properties.length; index++) { | |
table.string(properties[index]); | |
} | |
properties.forEach((property) => { | |
table.string(property); | |
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you are saying, I thought about a couple of scenarios:
- a field is array => there's no way to store arrays in SQL
- a field is a number => what type of number? float, double, ...
- what if a field is an object => there's no way to store objects in SQL
- what if a field has a different type ( number/string ) across two files, should it be defaulted to string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, use any SQL types that are most suited for each JS/TS type. But it's not like everything needs to be stringified 😅 If it's an object/array, stringify to JSON.
what if a field has a different type ( number/string ) across two files, should it be defaulted to string?
You could type the computed field function signature in a way that it always should return a single type defined by the user. But for now, ok, just stringify everything. We'll properly implement it when we move computed fields definitions to their final destination = document types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the issue is currently computedFields
don't return any thing.
the current implementation of ComputedField
is unpredictable as the user changes the fileObject directly...
src/lib/schema.ts
Outdated
@@ -25,50 +25,61 @@ interface File { | |||
url_path: string | null; | |||
filetype: string | null; | |||
metadata: MetaData | null; | |||
[key: string]: string | null | MetaData | undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather:
[key: string]: string | null | MetaData | undefined; | |
[key: string]: unknown; |
No description provided.