Skip to content

Commit

Permalink
feat: everything is done
Browse files Browse the repository at this point in the history
  • Loading branch information
sooluh committed Feb 27, 2024
1 parent e659719 commit 1bb4c2c
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 195 deletions.
2 changes: 0 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# http://editorconfig.org

[*]
indent_style = space
indent_size = 2
Expand Down
2 changes: 1 addition & 1 deletion .github/lock.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
ignoreUnless: {{ STALE_BOT }}
ignoreUnless: { { STALE_BOT } }
---
# Configuration for Lock Threads - https://github.com/dessant/lock-threads-app

Expand Down
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
ignoreUnless: {{ STALE_BOT }}
ignoreUnless: { { STALE_BOT } }
---
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 60
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md → LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License

Copyright (c) 2023
Copyright (c) 2024 Abu Masyail

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
115 changes: 0 additions & 115 deletions README.md

This file was deleted.

17 changes: 0 additions & 17 deletions configure.ts

This file was deleted.

12 changes: 3 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
|--------------------------------------------------------------------------
| Package entrypoint
|--------------------------------------------------------------------------
|
| Export values from the package entrypoint as you see fit.
|
*/
import Json from './src/json.js'
import json from './src/decorator.js'

export { configure } from './configure.js'
export { Json, json }
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "boilerplate",
"description": "",
"version": "0.0.0",
"name": "adonis-lucid-json",
"description": "Addon for JSON values Adonis Lucid ORM",
"version": "0.0.1",
"engines": {
"node": ">=18.16.0"
"node": ">=20.x"
},
"main": "build/index.js",
"type": "module",
"files": [
"build/src",
Expand Down Expand Up @@ -33,13 +34,23 @@
"version": "npm run build",
"prepublishOnly": "npm run build"
},
"keywords": [],
"author": "",
"keywords": [
"json",
"lucid",
"adonis",
"adonisjs"
],
"author": {
"name": "Abu Masyail",
"url": "https://suluh.my.id",
"email": "[email protected]"
},
"license": "MIT",
"devDependencies": {
"@adonisjs/assembler": "^7.0.0",
"@adonisjs/core": "^6.2.0",
"@adonisjs/eslint-config": "^1.2.1",
"@adonisjs/lucid": "^20.3.0",
"@adonisjs/prettier-config": "^1.2.1",
"@adonisjs/tsconfig": "^1.2.1",
"@japa/assert": "^2.1.0",
Expand All @@ -50,13 +61,15 @@
"copyfiles": "^2.4.1",
"del-cli": "^5.0.0",
"eslint": "^8.38.0",
"knex": "^3.1.0",
"np": "^9.2.0",
"prettier": "^3.1.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@adonisjs/core": "^6.2.0"
"@adonisjs/core": "^6.2.0",
"@adonisjs/lucid": "^20.3.0"
},
"publishConfig": {
"access": "public",
Expand Down
5 changes: 0 additions & 5 deletions providers/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions src/README.md

This file was deleted.

30 changes: 30 additions & 0 deletions src/decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Json from './json.js'
import { BaseModel } from '@adonisjs/lucid/orm'
import { ColumnOptions } from '@adonisjs/lucid/types/model'

export interface TranslatedOptions {}

export type TranslatedDecorator = (
options?: TranslatedOptions & Partial<ColumnOptions>
) => <TKey extends string, TTarget extends { [K in TKey]: Json }>(
target: TTarget,
propertyKey: TKey
) => void

const decorator: TranslatedDecorator = (options) => {
return function decorateAsColumn(target: any, property: string) {
const Model = target.constructor as typeof BaseModel
Model.boot()

const { ...columnOptions } = options ?? {}

Model.$addColumn(property, {
consume: (value: any) => (value ? Json.fromDbResponse(value) : null),
prepare: (value: Json) => (value ? JSON.stringify(value.toObject()) : null),
serialize: (value: Json) => (value ? value.toObject() : null),
...columnOptions,
})
}
}

export default decorator
48 changes: 48 additions & 0 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
type JsonAttributes = Record<string, string | null | undefined>

export default class Json {
readonly values: Record<string, string> = {}

static fromDbResponse(response: any): Json | null {
if (response === null) {
return null
}

const attributes: JsonAttributes =
typeof response === 'string' ? JSON.parse(response) : response

return new Json(attributes)
}

static from(values: JsonAttributes): Json {
return new Json(values)
}

private constructor(values: JsonAttributes) {
this.values = Object.fromEntries(
Object.entries(values).filter(([, value]) => value !== null)
) as Record<string, string>
}

get(key: string): string | undefined {
return this.values[key]
}

getOrFail(key: string): string {
const value = this.get(key)

if (value === undefined) {
throw new Error(`No json found for key "${key}"`)
}

return value
}

set(key: string, value: string) {
this.values[key] = value
}

toObject(): JsonAttributes {
return this.values
}
}
6 changes: 0 additions & 6 deletions stubs/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions stubs/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

/**
* Path to the root directory where the stubs are stored. We use
* this path within commands and the configure hook
*/
export const stubsRoot = dirname(fileURLToPath(import.meta.url))
7 changes: 0 additions & 7 deletions tests/example.spec.ts

This file was deleted.

Loading

0 comments on commit 1bb4c2c

Please sign in to comment.