-
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
18 changed files
with
166 additions
and
195 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,5 +1,3 @@ | ||
# http://editorconfig.org | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
|
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
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 was deleted.
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
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 } |
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,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", | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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 |
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,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 | ||
} | ||
} |
This file was deleted.
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
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)) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.