Skip to content

Commit

Permalink
refactor!: replace existing methods with improved alternatives
Browse files Browse the repository at this point in the history
This commit replaces the following methods:
- `inCollection()` is replaced with `getByCollection()`
- `name()` is replaced with `getByFileName()`
- `fullText()` is replaced with `getByContent()`
- `mimeType()` is replaced with `getByFileType()`
- `createdTime()` is replaced with `getByCreatedAt()`
- `modifiedTime()` is replaced with `getByUpdatedAt()`
  • Loading branch information
br14n-sol committed Dec 30, 2023
1 parent dfeca01 commit d8af3f7
Showing 1 changed file with 73 additions and 52 deletions.
125 changes: 73 additions & 52 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,121 @@
import { Collection, File, Operator, QueryType } from './constants.js'

type QueryTerm = Collection | File
type QueryTemplateOptions = {
field: File | Collection
op: Operator
entry: {
key?: string
value: unknown
}
}

const QueryTemplate = {
[QueryType.COLLECTION]: (
value: string,
operator: Operator,
collection: QueryTerm
) => `'${value}' ${operator} ${collection}`,
[QueryType.STRING]: (value: string, operator: Operator, term: QueryTerm) =>
`${term} ${operator} '${value}'`,
[QueryType.BOOLEAN]: (value: string, operator: Operator, term: QueryTerm) =>
`${term} ${operator} ${value}`
[QueryType.COLLECTION]: ({ field, op, entry }: QueryTemplateOptions) =>
`'${entry.value}' ${op} ${field}`,
[QueryType.STRING]: ({ field, op, entry }: QueryTemplateOptions) =>
`${field} ${op} '${entry.value}'`,
[QueryType.BOOLEAN]: ({ field, op, entry }: QueryTemplateOptions) =>
`${field} ${op} ${entry.value}`
}

type AddQueryOpts = {
field: File | Collection
op: Operator
entry: Record<string, unknown> | string[]
}

class QueryBuilder {
private readonly queries: string[] = []
private negateNextTerm = false
private lastTerm: QueryTerm = Collection.PARENTS

private addQuery(
type: QueryType,
operator: Operator,
values: string | string[]
): void {
private addQuery(type: QueryType, options: AddQueryOpts): void {
const { field, op, entry } = options
let query = ''

if (this.negateNextTerm) {
this.negateNextTerm = false
query += `${Operator.NOT} `
}

if (typeof values === 'string') {
query += QueryTemplate[type](values, operator, this.lastTerm)
}

if (Array.isArray(values)) {
query += `(${values
.map(v => QueryTemplate[type](v, operator, this.lastTerm))
.join(` ${Operator.OR} `)})`
const _queries = []
for (const key in entry) {
const value = entry[key as keyof typeof entry]
_queries.push(
QueryTemplate[type]({ field, op, entry: { key: `${key}`, value } })
)
}

query += `(${_queries.join(
` ${Array.isArray(entry) ? Operator.OR : Operator.AND} `
)})`
this.queries.push(query)
}

// Comparison methods (query operators)

not(): this {
this.negateNextTerm = true

return this
}

// Term methods (query terms)

inCollection(collection: Collection, values: string | string[]): this {
this.lastTerm = collection
this.addQuery(QueryType.COLLECTION, Operator.IN, values)

getByCollection(collection: Collection, value: string | string[]): this {
this.addQuery(QueryType.COLLECTION, {
field: collection,
op: Operator.IN,
entry: Array.isArray(value) ? value : [value]
})
return this
}

isTrashed(value: boolean): this {
this.lastTerm = File.TRASHED
this.addQuery(QueryType.BOOLEAN, Operator.EQUAL, `${value}`)

getByFileName(filename: string | string[]): this {
this.addQuery(QueryType.STRING, {
field: File.NAME,
op: Operator.EQUAL,
entry: Array.isArray(filename) ? filename : [filename]
})
return this
}

name(): this {
this.lastTerm = File.NAME

getByContent(value: string | string[]): this {
this.addQuery(QueryType.STRING, {
field: File.FULL_TEXT,
op: Operator.EQUAL,
entry: Array.isArray(value) ? value : [value]
})
return this
}

fullText(): this {
this.lastTerm = File.FULL_TEXT

getByFileType(filetype: string | string[]): this {
this.addQuery(QueryType.STRING, {
field: File.MIME_TYPE,
op: Operator.EQUAL,
entry: Array.isArray(filetype) ? filetype : [filetype]
})
return this
}

mimeType(): this {
this.lastTerm = File.MIME_TYPE

getByCreatedAt(timestamp: string | string[]): this {
this.addQuery(QueryType.STRING, {
field: File.CREATED_TIME,
op: Operator.EQUAL,
entry: Array.isArray(timestamp) ? timestamp : [timestamp]
})
return this
}

modifiedTime(): this {
this.lastTerm = File.MODIFIED_TIME

getByUpdatedAt(timestamp: string | string[]): this {
this.addQuery(QueryType.STRING, {
field: File.MODIFIED_TIME,
op: Operator.EQUAL,
entry: Array.isArray(timestamp) ? timestamp : [timestamp]
})
return this
}

createdTime(): this {
this.lastTerm = File.CREATED_TIME

isTrashed(value: boolean): this {
this.addQuery(QueryType.BOOLEAN, {
field: File.TRASHED,
op: Operator.EQUAL,
entry: [`${value}`]
})
return this
}

Expand Down

0 comments on commit d8af3f7

Please sign in to comment.