Skip to content

Commit

Permalink
feat(core): add created at field to record entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Chenqin Nee authored and Chenqin Nee committed Dec 15, 2022
1 parent 1a7ae9f commit 92d05c6
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 6 deletions.
11 changes: 6 additions & 5 deletions packages/core/record/record.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { DateVO } from '@egodb/domain'
import type { TableId } from '../value-objects'
import { RecordId } from './value-objects'
import { RecordValues } from './value-objects/record-values.vo'
import { RecordId, RecordValues } from './value-objects'

export class Record {
public id: RecordId = RecordId.create()
public tableId!: TableId
public values: RecordValues = RecordValues.empty()
public readonly id: RecordId = RecordId.create()
public readonly tableId!: TableId
public readonly values: RecordValues = RecordValues.empty()
public readonly createdAt: DateVO = DateVO.now()

// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
Expand Down
1 change: 1 addition & 0 deletions packages/core/record/record.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type ICreateRecordInput_internal = z.infer<typeof createRecordInput_inter
export const queryRecordSchema = z.object({
id: recordIdSchema,
tableId: tableIdSchema,
createdAt: z.date(),
values: z.record(fieldIdSchema, fieldValue),
})
export type IQueryRecordSchema = z.infer<typeof queryRecordSchema>
1 change: 1 addition & 0 deletions packages/core/record/specifications/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type { IRecordSpec, IRecordVisitor } from './interface'
export * from './number.specification'
export * from './record-created-at.specification'
export * from './record-id.specifaction'
export * from './record-table-id.specification'
export * from './record-values.specification'
Expand Down
3 changes: 3 additions & 0 deletions packages/core/record/specifications/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
NumberLessThan,
NumberLessThanOrEqual,
} from './number.specification'
import type { WithRecordCreatedAt } from './record-created-at.specification'
import type { WithRecordId } from './record-id.specifaction'
import type { WithRecordTableId } from './record-table-id.specification'
import type { WithRecordValues } from './record-values.specification'
Expand All @@ -17,6 +18,8 @@ interface IRecordSpecVisitor {
idEqual(s: WithRecordId): void
tableIdEqual(s: WithRecordTableId): void

createdAt(s: WithRecordCreatedAt): void

values(s: WithRecordValues): void
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { DateVO } from '@egodb/domain'
import { CompositeSpecification } from '@egodb/domain'
import type { Result } from 'oxide.ts'
import { Ok } from 'oxide.ts'
import type { Record } from '../record'
import type { IRecordVisitor } from './interface'

export class WithRecordCreatedAt extends CompositeSpecification<Record, IRecordVisitor> {
constructor(public readonly date: DateVO) {
super()
}

isSatisfiedBy(t: Record): boolean {
return this.date.equals(t.createdAt)
}

/**
* created at should not mutate exisiting record
* @param r - record
* @returns
*/
mutate(r: Record): Result<Record, string> {
return Ok(r)
}

accept(v: IRecordVisitor): Result<void, string> {
v.createdAt(this)
return Ok(undefined)
}
}
2 changes: 1 addition & 1 deletion packages/core/record/value-objects/record-values.vo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RecordValues extends ValueObject<Map<string, FieldValue>> {

// TODO: handler is none value
if (fieldValue.isSome()) {
this.props.set(fieldName, fieldValue.unwrap())
this.value.set(fieldName, fieldValue.unwrap())
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/domain/date.vo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEqual } from 'date-fns'
import { ValueObject } from './value-object'

export class DateVO extends ValueObject<Date> {
Expand All @@ -6,6 +7,11 @@ export class DateVO extends ValueObject<Date> {
super({ value: date })
}

public equals(vo?: DateVO): boolean {
if (!vo) return false
return isEqual(vo.value, this.value)
}

public get value(): Date {
return this.props.value
}
Expand Down
1 change: 1 addition & 0 deletions packages/domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.0.0",
"author": "",
"dependencies": {
"date-fns": "^2.29.3",
"fast-deep-equal": "^3.1.3",
"nanoid": "^3.0.0",
"oxide.ts": "^1.1.0",
Expand Down
1 change: 1 addition & 0 deletions packages/repositories/in-memory-repository/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@egodb/core": "^0.0.0",
"@egodb/domain": "^0.0.0",
"@fxts/core": "^0.11.0",
"date-fns": "^2.29.3",
"lowdb": "^5.0.5",
"oxide.ts": "^1.1.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class RecordInMemoryMapper {
return {
id: r.id.value,
tableId: r.tableId.value,
createdAt: r.createdAt.value,
values: r.values.toObject(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import type {
StringEqual,
StringRegex,
StringStartsWith,
WithRecordCreatedAt,
WithRecordId,
WithRecordTableId,
} from '@egodb/core'
import { isNumber, isString } from '@fxts/core'
import { isDate, isEqual } from 'date-fns'
import type { Result } from 'oxide.ts'
import { Err, Ok } from 'oxide.ts'
import type { RecordInMemory } from './record.type'
Expand Down Expand Up @@ -133,6 +135,13 @@ export class RecordInMemoryQueryVisitor implements IRecordVisitor {
}
}

createdAt(s: WithRecordCreatedAt): void {
this.predicate = (r) => {
const value = r.createdAt
return isDate(value) && isEqual(value, s.date.value)
}
}

values(): void {
throw new Error('[RecordInMemoryQueryVisitor.values] Method not implemented.')
}
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92d05c6

Please sign in to comment.