Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yUnreal committed May 20, 2024
1 parent adb0aa6 commit c8da569
Show file tree
Hide file tree
Showing 30 changed files with 1,099 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .gitignore
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
51 changes: 51 additions & 0 deletions package-lock.json

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

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wuue",
"name": "wuuedb",
"version": "0.0.1",
"description": "TypeScript schema validation",
"description": "Wuue db",
"main": "src/index.ts",
"scripts": {
"test": "tsx index.test.ts",
Expand All @@ -18,12 +18,19 @@
},
"license": "ISC",
"devDependencies": {
"@types/lodash.isequal": "^4.5.8",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"prettier": "^3.0.0",
"tsx": "^4.7.0",
"typescript": "^5.1.6"
},
"dependencies": {
"bson": "^6.7.0",
"is-plain-obj": "^4.1.0",
"lodash.isequal": "^4.5.0",
"uuid-random": "^1.3.2"
}
}
31 changes: 31 additions & 0 deletions src/drivers/JSONBasedDriver.ts
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;
}
}
10 changes: 10 additions & 0 deletions src/errors/DriverError.ts
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);
}
}
8 changes: 8 additions & 0 deletions src/errors/QueryError.ts
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);
}
}
10 changes: 10 additions & 0 deletions src/errors/SchemaError.ts
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);
}
}
3 changes: 3 additions & 0 deletions src/managers/BaseManager.ts
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>();
}
61 changes: 61 additions & 0 deletions src/managers/QueryManager.ts
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;
}
}
32 changes: 32 additions & 0 deletions src/structs/Doc.ts
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 });
}
}
Loading

0 comments on commit c8da569

Please sign in to comment.