Skip to content

Commit

Permalink
feat(core): save filters to database
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Dec 10, 2022
1 parent 026f23d commit 0e06f33
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class SetFiltersCommandHandler implements ISetFilterCommandHandler {
async execute(command: SetFitlersCommand): Promise<void> {
const table = (await this.repo.findOneById(command.tableId)).unwrap()

table.setFilters(command.filters, command.viewName)
const spec = table.setFilters(command.filters, command.viewName).unwrap()
await this.repo.updateOneById(command.tableId, spec)
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"scripts": {
"build": "tsup",
"coverage": "vitest run --coverage",
"dev": "tsup --watch --onSuccess \"tsc --emitDeclarationOnly --declaration\"",
"dev": "tsup --watch ",
"prebuild": "pnpm run clean",
"clean": "rimraf dist",
"test": "vitest run",
Expand Down
31 changes: 31 additions & 0 deletions packages/core/specifications/filters.specificaiton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CompositeSpecification } from '@egodb/domain'
import type { Result } from 'oxide.ts'
import { Ok } from 'oxide.ts'
import type { IFilters } from '../filter'
import { Filters } from '../filter'
import type { Table } from '../table'
import type { ITableSpecVisitor } from './interface'

export class WithFilters extends CompositeSpecification<Table, ITableSpecVisitor> {
constructor(public readonly filters: IFilters | undefined, public readonly viewName: string) {
super()
}

isSatisfiedBy(t: Table): boolean {
if (!this.filters) {
return !t.getOrCreateDefaultView(this.viewName).filters
}
return t.getOrCreateDefaultView(this.viewName).filters?.equals(new Filters(this.filters)) ?? false
}

mutate(t: Table): Result<Table, string> {
const view = t.getOrCreateDefaultView(this.viewName)
view.setFilters(this.filters)
return Ok(t)
}

accept(v: ITableSpecVisitor): Result<void, string> {
v.filtersEqual(this)
return Ok(undefined)
}
}
1 change: 1 addition & 0 deletions packages/core/specifications/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './filters.specificaiton'
export type { ITableSpec, ITableSpecVisitor } from './interface'
export * from './table-id.specifaction'
export * from './table-name.specification'
3 changes: 3 additions & 0 deletions packages/core/specifications/interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { type ISpecification } from '@egodb/domain'
import { type Table } from '../table'
import type { WithFilters } from './filters.specificaiton'
import type { WithTableId } from './table-id.specifaction'
import type { WithName } from './table-name.specification'

export interface ITableSpecVisitor {
idEqual(s: WithTableId): void
nameEqual(s: WithName): void

filtersEqual(s: WithFilters): void
}

export type ITableSpec = ISpecification<Table, ITableSpecVisitor>
2 changes: 2 additions & 0 deletions packages/core/specifications/table-id.specifaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ export class WithTableId extends CompositeSpecification {
}

export const WithTableIdS = (id: string) => TableId.from(id).map((tableId) => new WithTableId(tableId))

export type TableSpecificaiton = CompositeSpecification<Table, ITableSpecVisitor>
1 change: 1 addition & 0 deletions packages/core/table.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface ITableRepository {
find(spec: ITableSpec): Promise<Table[]>

insert(table: Table): Promise<void>
updateOneById(id: string, spec: ITableSpec): Promise<void>
}
14 changes: 11 additions & 3 deletions packages/core/table.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { filter, map, pipe, toArray } from '@fxts/core'
import type { Result } from 'oxide.ts'
import type { ICreateRecordInput } from './commands'
import type { ICreateFieldsSchema_internal, ICreateFieldValueSchema_internal, IQuerySchemaSchema } from './field'
import { createFieldValueSchema_internal } from './field'
import type { IFilters } from './filter'
import { Record } from './record'
import type { TableSpecificaiton } from './specifications'
import { WithFilters } from './specifications/filters.specificaiton'
import type { ICreateTableInput_internal } from './table.schema'
import { TableId, TableSchema } from './value-objects'
import { TableName } from './value-objects/table-name.vo'
Expand Down Expand Up @@ -90,12 +93,17 @@ export class Table {
}

public getOrCreateDefaultView(viewName?: string): View {
if (!viewName) {
return this.defaultView
}

return this.views.getByName(viewName).unwrapOrElse(() => this.defaultView)
}

public setFilters(filters?: IFilters, viewName?: string): void {
const view = this.getOrCreateDefaultView(viewName)
view.setFilters(filters)
public setFilters(filters?: IFilters, viewName?: string): Result<TableSpecificaiton, string> {
const vn = this.getOrCreateDefaultView(viewName).name.unpack()
const spec = new WithFilters(filters, vn)
return spec.mutate(this).map(() => spec)
}

public createRecord(input: ICreateRecordInput): Record {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/view/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Views extends ValueObject<View[]> {
return new this(views.map((v) => View.create(v)))
}

getByName(viewName?: string): Option<View> {
getByName(viewName: string): Option<View> {
return Option(this.views.find((v) => v.name.unpack() === viewName))
}
}
2 changes: 1 addition & 1 deletion packages/domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"scripts": {
"build": "tsup",
"coverage": "vitest run --coverage",
"dev": "tsup --watch --onSuccess \"tsc --emitDeclarationOnly --declaration\"",
"dev": "tsup --watch ",
"prebuild": "pnpm run clean",
"clean": "rimraf dist",
"test": "vitest run",
Expand Down
2 changes: 1 addition & 1 deletion packages/repositories/in-memory-repository/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"scripts": {
"build": "tsup",
"coverage": "vitest run --coverage",
"dev": "tsup --watch --onSuccess \"tsc --emitDeclarationOnly --declaration\"",
"dev": "tsup --watch ",
"prebuild": "pnpm run clean",
"clean": "rimraf dist",
"test": "vitest run",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { ITableRepository, ITableSpec, Table } from '@egodb/core'
import type { Option } from 'oxide.ts'
import { None, Some } from 'oxide.ts'
import { db } from '../db.js'
import { TableInMemoryMapper } from './table-in-memory.mapper.js'
import { TableInMemoryMutationVisitor } from './table-in-memory.mutation-visitor.js'
import { TableInMemoryQueryVisitor } from './table-in-memory.query-visitor.js'

export class TableInMemoryRepository implements ITableRepository {
Expand Down Expand Up @@ -30,7 +32,6 @@ export class TableInMemoryRepository implements ITableRepository {

const predicate = visitor.getPredicate().unwrap()

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return db
.data!.tables.filter(predicate)
.map(TableInMemoryMapper.toDomain)
Expand All @@ -41,4 +42,12 @@ export class TableInMemoryRepository implements ITableRepository {
const t = TableInMemoryMapper.toInMemory(table)
db.data?.tables.push(t)
}

async updateOneById(id: string, spec: ITableSpec): Promise<void> {
const table = db.data!.tables.find((t) => t.id === id)
if (!table) return

const visitor = new TableInMemoryMutationVisitor(table)
spec.accept(visitor)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ITableSpecVisitor, WithFilters, WithName } from '@egodb/core'
import type { TableInMemory } from './table'

export class TableInMemoryMutationVisitor implements ITableSpecVisitor {
constructor(private table: TableInMemory) {}

idEqual(): void {
throw new Error('[TableInMemoryMutationVisitor.idEqual] Method not implemented.')
}

nameEqual(s: WithName): void {
this.table.name = s.name.value
}

filtersEqual(s: WithFilters): void {
const view = this.table.views.find((v) => v.name === s.viewName)
if (view) {
view.filters = s.filters
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export class TableInMemoryQueryVisitor implements ITableSpecVisitor {
nameEqual(s: WithName): void {
this.predicate = (t) => t.name === s.name.value
}

filtersEqual(): void {
throw new Error('cannot query by filters')
}
}
2 changes: 1 addition & 1 deletion packages/trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"scripts": {
"build": "tsup",
"coverage": "vitest run --coverage",
"dev": "tsup --watch --onSuccess \"tsc --emitDeclarationOnly --declaration\"",
"dev": "tsup --watch ",
"prebuild": "pnpm run clean",
"clean": "rimraf dist",
"test": "vitest run",
Expand Down

0 comments on commit 0e06f33

Please sign in to comment.