Skip to content

Commit

Permalink
examples/bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarasikee committed Aug 30, 2022
1 parent a47d797 commit c70ca55
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 53 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.DS_Store
.env
.idea
database
dev
Empty file added database/example1/users.json
Empty file.
1 change: 1 addition & 0 deletions database/test/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Application, Context, HttpException} from "https://deno.land/x/[email protected]/mod.ts"
import { logger } from "https://deno.land/x/[email protected]/middleware/logger.ts";
import {logger} from "https://deno.land/x/[email protected]/middleware/logger.ts"
import {userDocument, userModel} from "./models/user.model.ts"
import {Status} from "https://deno.land/[email protected]/http/http_status.ts"
import {ErrorHandler} from "../../src/errors/ErrorHandler.ts"

const app = new Application()
app.use(logger());
app.use(logger())

const findAll = (ctx: Context) => {
const users = userModel.findAll()
Expand All @@ -16,21 +17,35 @@ const create = async (ctx: Context) => {
const body = await ctx.body
const user = userModel.create(body as userDocument)
user.save()
ctx.json(user.toJSON())
ctx.json(user.toJSON(), Status.Created)
} catch (error) {
const {message, hint} = ErrorHandler(error)

throw new HttpException(
{
status: Status.BadRequest,
error: error.message
error: {message, hint}
},
Status.BadRequest
)
}
}

const findOne = (ctx: Context) => {
const {id} = ctx.params
const user = userModel.findById(id)
ctx.json(user)
}

const deleteOne = (ctx: Context) => {
const {id} = ctx.params
const message = userModel.huntById(id)
ctx.json({message})
}

app
.get("/users", findAll)
// .get("/users/:id", findOne)
.get("/users/:id", findOne)
.post("/users", create)
// .delete("/users/:id", deleteOne)
.delete("/users/:id", deleteOne)
.start({port: 8080})
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {TinyTable, Schema, Column, Model, Document} from "https://deno.land/x/[email protected]/src/mod.ts"
import {TinyTable, Schema, Column, Model, Document} from "../../../src/mod.ts"

@TinyTable("users")
@TinyTable({
name: "profile",
url: "database/example1/"
})
class Profile {
@Column({
type: "string",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {TinyTable, Schema, Column, Model, Document} from "https://deno.land/x/[email protected]/src/mod.ts"
import {TinyTable, Schema, Column, Model, Document} from "../../../src/mod.ts"

@TinyTable("users")
@TinyTable({
name: "users",
url: "database/example1/"
})
class User {
@Column({
type: "string",
Expand Down
4 changes: 2 additions & 2 deletions src/classes/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export class Instance<T extends { _id: string }> {
}

private getTable(): T[] {
const url = `./database/${this._schema.name}.json`
const url = `${this._schema.url}${this._schema.name}.json`
return FileUtils.readJson<T>(url)
}

private writeTable(table: T[]) {
const url = `./database/${this._schema.name}.json`
const url = `${this._schema.url}${this._schema.name}.json`
FileUtils.writeJson(url, [...table])
}

Expand Down
7 changes: 6 additions & 1 deletion src/classes/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Model<T extends { _id: string }> {
}

private getTable(): T[] {
const url = `./database/${this.schema.name}.json`
const url = `${this.schema.url}${this.schema.name}.json`
return FileUtils.readJson<T>(url)
}

Expand Down Expand Up @@ -50,6 +50,11 @@ export class Model<T extends { _id: string }> {
}

// Hunters
public huntById(_id: string) {
this.findById(_id).delete()
return "Successful hunt!"
}

public hunt(args: Partial<T>) {
this.find(args).map(instance => instance.delete())
return "Successful hunt!"
Expand Down
7 changes: 6 additions & 1 deletion src/classes/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ColumnRules, getFormat} from "../mod.ts"
export class Schema {
constructor(
private _name: string = "",
private _url: string = "",
private _columns: ColumnRules[] = []
) {
}
Expand All @@ -11,6 +12,10 @@ export class Schema {
return this._name
}

get url(): string {
return this._url
}

get columns(): ColumnRules[] {
return this._columns
}
Expand All @@ -22,6 +27,6 @@ export class Schema {
.filter(key => key[0] !== "_")
.map(key => ({name: key, options: getFormat(instance, key)}))

return new Schema(instance["_tableName"], options)
return new Schema(instance["_tableName"], instance["_dir_url"], options)
}
}
10 changes: 5 additions & 5 deletions src/classes/Table.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {FileUtils} from "../mod.ts"

export class Table {
public static init(name: string) {
const url = `./database/${name}.json`
public static init(name: string, dir_url: string) {
const url = `${dir_url}${name}.json`

try {
const isExists = FileUtils.isFileExists(url)
Expand All @@ -11,16 +11,16 @@ export class Table {
return this
}

FileUtils.createOrCheckDir("./database")
FileUtils.createOrCheckDir(dir_url)
FileUtils.writeJson(url, [])
return this
} catch (e) {
console.error(e.message)
}
}

public static nuke(name: string) {
const url = `./database/${name}.json`
public static nuke(name: string, dir_url: string) {
const url = `${dir_url}${name}.json`

try {
FileUtils.writeJson(url, [])
Expand Down
11 changes: 8 additions & 3 deletions src/decorators/TinyTable.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import {Table} from "../classes/mod.ts"

export function TinyTable(name: string) {
interface Props {
name: string
url: string
}

export function TinyTable({name, url}: Props) {

// deno-lint-ignore no-explicit-any
return function <T extends { new(...args: any[]): Record<string, any> }>(Constructor: T) {
return class extends Constructor {
_tableName: string = name
_url: string = name
_dir_url: string = url
_id!: string

// deno-lint-ignore no-explicit-any
constructor(...args: any[]) {
super(...args)
Table.init(name)
Table.init(name, url)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/errors/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function ErrorHandler(error: Error) {
let message, hint

try {
message = JSON.parse(error.message).message
hint = JSON.parse(error.message).hint
} catch (e) {
message = error.message
hint = "No hint provided"
}

return {
message, hint
}
}
8 changes: 4 additions & 4 deletions src/errors/ErrorWithHint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class ErrorWithHint extends Error {
constructor(message: string, hint: string) {
super(`
Message: ${message}
Hint: ${hint}
`)
super(JSON.stringify({
message,
hint
}))
}
}
1 change: 1 addition & 0 deletions src/errors/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export {ErrorWithHint} from "./ErrorWithHint.ts"
export {ErrorWithMessage} from "./ErrorWithMessage.ts"
export {ErrorHandler} from "./ErrorHandler.ts"
2 changes: 1 addition & 1 deletion src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export {Column, getFormat, TinyTable} from "./decorators/mod.ts"
export type {OptionTypes, ColumnProps, ColumnRules, Document} from "./interfaces/mod.ts"

// Errors
export { ErrorWithHint } from "./errors/mod.ts"
export { ErrorWithHint, ErrorHandler } from "./errors/mod.ts"
8 changes: 1 addition & 7 deletions tests/creationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export const creationTests = () => {

users.map(user => user.save())
},
Error,
"Message: name must be unique"
Error
)
})

Expand Down Expand Up @@ -180,10 +179,5 @@ export const creationTests = () => {

user.delete()
})

await t.step("Clear DB", () => {
const message = userModel.huntAll()
assertStrictEquals("Successful absolute hunt!", message)
})
})
}
5 changes: 0 additions & 5 deletions tests/findingTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,5 @@ export const findingTests = () => {

assertStrictEquals(users.length, 0)
})

await t.step("Clear DB", () => {
const message = userModel.huntAll()
assertStrictEquals("Successful absolute hunt!", message)
})
})
}
3 changes: 3 additions & 0 deletions tests/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {creationTests} from "./creationTests.ts"
import {findingTests} from "./findingTests.ts"
import {clearDB} from "./utils.ts"

creationTests()
clearDB()
findingTests()
clearDB()
17 changes: 4 additions & 13 deletions tests/userInitiale.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Column, Document, Model, Schema, TinyTable} from "../tinydb.ts"

@TinyTable("users")
@TinyTable({
name: "users",
url: "database/test/"
})
class User {
@Column({
type: "string",
Expand Down Expand Up @@ -29,18 +32,6 @@ class User {
}
})
settings!: Record<string, unknown>

// @Column({
// allowNull: true,
// default: [],
// type: "model"
// })
// friends!: string[]

// @Column({
// type: "model"
// })
// profile!: User
}

export type userDocument = User & Document
Expand Down
9 changes: 9 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Table} from "../src/classes/Table.ts"

export const clearDB = () => {
return Deno.test("Clear DB", async (t) => {
await t.step("Nuke em:)", () => {
Table.nuke("users", "database/test/")
})
})
}

0 comments on commit c70ca55

Please sign in to comment.