-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,099 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Unnecessary files | ||
node_modules | ||
# Keep environment variables out of version control | ||
.env | ||
.editorconfig | ||
index.test.ts |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { basename } from 'path'; | ||
import { DriverError } from '../errors/DriverError'; | ||
import { writeFileSync, readFileSync, existsSync } from 'fs'; | ||
|
||
export class JSONBasedDriver< | ||
T extends Record<string, unknown> = Record<string, unknown>, | ||
> { | ||
public constructor(public readonly path: string) { | ||
if (!path.endsWith('.json')) | ||
throw new DriverError('Invalid path', this); | ||
if (!existsSync(path)) writeFileSync(path, '{}'); | ||
} | ||
|
||
protected getPathName() { | ||
return basename(this.path, '.json'); | ||
} | ||
|
||
public get data() { | ||
const data = readFileSync(this.path, 'utf8'); | ||
|
||
return <T>JSON.parse(data); | ||
} | ||
|
||
public update(fn: (data: T) => T) { | ||
const data = fn(this.data); | ||
|
||
writeFileSync(this.path, JSON.stringify(data, null, '\t')); | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { JSONBasedDriver } from '../drivers/JSONBasedDriver'; | ||
|
||
export class DriverError extends Error { | ||
public constructor( | ||
message: string, | ||
public driver: JSONBasedDriver | ||
) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export class QueryError extends Error { | ||
public constructor( | ||
message: string, | ||
public query: object | ||
) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Schema } from '../structs/Schema'; | ||
|
||
export class SchemaError extends Error { | ||
public constructor( | ||
message: string, | ||
public schema: Schema<Record<string, unknown>> | ||
) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export abstract class BaseManager<K, V> { | ||
public cache = new Map<K, V>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { QueryError } from '../errors/QueryError'; | ||
import { Model } from '../structs/Model'; | ||
import { Query } from '../structs/Query'; | ||
import { Schema } from '../structs/Schema'; | ||
import { QueryOptions } from '../typings/query'; | ||
import { AnyObject } from '../typings/utils'; | ||
import { BaseManager } from './BaseManager'; | ||
|
||
export class QueryManager<T extends AnyObject = AnyObject> extends BaseManager< | ||
string, | ||
Query<T> | ||
> { | ||
public constructor(public model: Model<Schema<T>>) { | ||
super(); | ||
} | ||
|
||
public resolve(query: QueryOptions<T> | string) { | ||
if (typeof query === 'object') return query; | ||
|
||
const fetchedQuery = this.get(query); | ||
|
||
if (!fetchedQuery) | ||
throw new QueryError(`Uknown query "${query}"`, this); | ||
|
||
return fetchedQuery.query; | ||
} | ||
|
||
public delete(query: string) { | ||
return this.cache.delete(query); | ||
} | ||
|
||
public create(name: string, options?: QueryOptions<T>) { | ||
const query = new Query(this.model, name, options); | ||
|
||
this.cache.set(name, query); | ||
|
||
return query; | ||
} | ||
|
||
public get(name: string) { | ||
return this.cache.get(name) ?? null; | ||
} | ||
|
||
public merge(first: Query<T>, second: Query<T>) { | ||
first.query = { ...first.query, ...second.query }; | ||
|
||
return first; | ||
} | ||
|
||
public edit(name: string, options: QueryOptions<T>) { | ||
const query = this.get(name); | ||
|
||
if (!query) throw new QueryError(`Unknown query "${name}"`, options); | ||
|
||
query.query = { ...query.query, ...options }; | ||
|
||
this.cache.set(name, query); | ||
|
||
return query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Projection, UpdateOneOptions } from '../typings/query'; | ||
import { Model } from './Model'; | ||
import { Schema } from './Schema'; | ||
|
||
export class Doc<T extends Record<string, unknown>> { | ||
public constructor( | ||
public _doc: T, | ||
public model: Model<Schema<T>> | ||
) {} | ||
|
||
public get id() { | ||
return <string>this._doc.id; | ||
} | ||
|
||
public fetch(projection?: Projection<T>) { | ||
return <Doc<T>>( | ||
this.model.findUnique({ query: { id: this.id }, projection }) | ||
); | ||
} | ||
|
||
public delete() { | ||
this.model.deleteOne({ query: { id: this.id } }); | ||
} | ||
|
||
public update(options: UpdateOneOptions<T>) { | ||
return this.model.updateOne({ query: { id: this.id } }, options); | ||
} | ||
|
||
public save() { | ||
return this.update({ Set: this._doc }); | ||
} | ||
} |
Oops, something went wrong.