|
| 1 | +import fsl from 'fs' |
| 2 | +import fsp from 'fs/promises' |
| 3 | +import { v4 } from 'uuid' |
| 4 | + |
| 5 | +const kData = Symbol('kData') |
| 6 | +const kDatabase = Symbol('kDatabase') |
| 7 | + |
| 8 | +export type GenericDatabaseType = { |
| 9 | + [k: string]: any[] |
| 10 | +} |
| 11 | + |
| 12 | +export type DatabaseRecord<T> = { _id: string } & T |
| 13 | + |
| 14 | +type ArrayElement<T> = T extends readonly (infer ElementType)[] |
| 15 | + ? ElementType |
| 16 | + : never |
| 17 | + |
| 18 | +type CollectionRef<T extends GenericDatabaseType, K extends keyof T> = { |
| 19 | + [kData]: Array<ArrayElement<T[K]>> |
| 20 | + [kDatabase]: FileSystemDatabase<T> |
| 21 | +} |
| 22 | + |
| 23 | +export default class FileSystemDatabase<T extends GenericDatabaseType> { |
| 24 | + public [kData]: T | null |
| 25 | + |
| 26 | + private constructor(private filename: string) { |
| 27 | + this[kData] = null |
| 28 | + } |
| 29 | + |
| 30 | + static fromRef<T extends GenericDatabaseType>( |
| 31 | + filename: string, |
| 32 | + ref: T |
| 33 | + ): FileSystemDatabase<T> { |
| 34 | + const database = new FileSystemDatabase<T>(filename) |
| 35 | + database[kData] = ref |
| 36 | + return database |
| 37 | + } |
| 38 | + |
| 39 | + static async fromFile<T extends GenericDatabaseType>( |
| 40 | + filename: string |
| 41 | + ): Promise<FileSystemDatabase<T>> { |
| 42 | + const database = new FileSystemDatabase<T>(filename) |
| 43 | + await database.load() |
| 44 | + return database |
| 45 | + } |
| 46 | + |
| 47 | + public async load(): Promise<void> { |
| 48 | + if (!fsl.existsSync(this.filename)) { |
| 49 | + this[kData] = {} as T |
| 50 | + |
| 51 | + await fsp.writeFile(this.filename, JSON.stringify(this[kData])) |
| 52 | + } |
| 53 | + |
| 54 | + this[kData] = JSON.parse(await fsp.readFile(this.filename, 'utf8')) |
| 55 | + } |
| 56 | + |
| 57 | + public async save(): Promise<void> { |
| 58 | + await fsp.writeFile(this.filename, JSON.stringify(this[kData])) |
| 59 | + } |
| 60 | + |
| 61 | + public async sync(): Promise<void> { |
| 62 | + await this.load() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export const collection = <T extends GenericDatabaseType, K extends keyof T>( |
| 67 | + database: FileSystemDatabase<T>, |
| 68 | + key: K |
| 69 | +): CollectionRef<T, K> => { |
| 70 | + if (!database[kData]![key]) { |
| 71 | + ;(database[kData]![key] as any) = [] |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + [kData]: database[kData]![key], |
| 76 | + [kDatabase]: database, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export const createDoc = async < |
| 81 | + T extends GenericDatabaseType, |
| 82 | + R extends keyof T |
| 83 | +>( |
| 84 | + collection: CollectionRef<T, R>, |
| 85 | + doc: Omit<ArrayElement<T[R]>, '_id'> |
| 86 | +) => { |
| 87 | + const docDto: ArrayElement<T[R]> = { |
| 88 | + _id: v4(), |
| 89 | + ...doc, |
| 90 | + } as any |
| 91 | + |
| 92 | + collection[kData].push(docDto) |
| 93 | + await collection[kDatabase].save() |
| 94 | + |
| 95 | + return docDto |
| 96 | +} |
| 97 | + |
| 98 | +export const updateDoc = async < |
| 99 | + T extends GenericDatabaseType, |
| 100 | + R extends keyof T |
| 101 | +>( |
| 102 | + collection: CollectionRef<T, R>, |
| 103 | + id: string, |
| 104 | + doc: Partial<ArrayElement<T[R]>> |
| 105 | +) => { |
| 106 | + const index = collection[kData].findIndex((item) => item._id === id) |
| 107 | + |
| 108 | + if (index === -1) { |
| 109 | + throw new Error('Record not found') |
| 110 | + } |
| 111 | + |
| 112 | + Object.assign(collection[kData][index], doc) |
| 113 | + |
| 114 | + await collection[kDatabase].save() |
| 115 | + return collection[kData][index] |
| 116 | +} |
| 117 | + |
| 118 | +export const getDoc = async <T extends GenericDatabaseType, R extends keyof T>( |
| 119 | + collection: CollectionRef<T, R>, |
| 120 | + id: string |
| 121 | +) => { |
| 122 | + const index = collection[kData].findIndex((item) => item._id === id) |
| 123 | + |
| 124 | + if (index === -1) { |
| 125 | + throw new Error('Record not found') |
| 126 | + } |
| 127 | + |
| 128 | + return collection[kData][index] |
| 129 | +} |
| 130 | + |
| 131 | +export const getDocs = async <T extends GenericDatabaseType, R extends keyof T>( |
| 132 | + collection: CollectionRef<T, R> |
| 133 | +) => { |
| 134 | + return collection[kData] |
| 135 | +} |
| 136 | + |
| 137 | +export const deleteDoc = async < |
| 138 | + T extends GenericDatabaseType, |
| 139 | + R extends keyof T |
| 140 | +>( |
| 141 | + collection: CollectionRef<T, R>, |
| 142 | + id: string |
| 143 | +) => { |
| 144 | + const index = collection[kData].findIndex((item) => item._id === id) |
| 145 | + |
| 146 | + if (index === -1) { |
| 147 | + throw new Error('Record not found') |
| 148 | + } |
| 149 | + |
| 150 | + collection[kData].splice(index, 1) |
| 151 | + await collection[kDatabase].save() |
| 152 | +} |
0 commit comments