Skip to content

Commit 1bb4c2c

Browse files
committed
feat: everything is done
1 parent e659719 commit 1bb4c2c

18 files changed

+166
-195
lines changed

.editorconfig

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# http://editorconfig.org
2-
31
[*]
42
indent_style = space
53
indent_size = 2

.github/lock.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
ignoreUnless: {{ STALE_BOT }}
2+
ignoreUnless: { { STALE_BOT } }
33
---
44
# Configuration for Lock Threads - https://github.com/dessant/lock-threads-app
55

.github/stale.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
ignoreUnless: {{ STALE_BOT }}
2+
ignoreUnless: { { STALE_BOT } }
33
---
44
# Number of days of inactivity before an issue becomes stale
55
daysUntilStale: 60

LICENSE.md LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License
22

3-
Copyright (c) 2023
3+
Copyright (c) 2024 Abu Masyail
44

55
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:
66

README.md

-115
This file was deleted.

configure.ts

-17
This file was deleted.

index.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
/*
2-
|--------------------------------------------------------------------------
3-
| Package entrypoint
4-
|--------------------------------------------------------------------------
5-
|
6-
| Export values from the package entrypoint as you see fit.
7-
|
8-
*/
1+
import Json from './src/json.js'
2+
import json from './src/decorator.js'
93

10-
export { configure } from './configure.js'
4+
export { Json, json }

package.json

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2-
"name": "boilerplate",
3-
"description": "",
4-
"version": "0.0.0",
2+
"name": "adonis-lucid-json",
3+
"description": "Addon for JSON values Adonis Lucid ORM",
4+
"version": "0.0.1",
55
"engines": {
6-
"node": ">=18.16.0"
6+
"node": ">=20.x"
77
},
8+
"main": "build/index.js",
89
"type": "module",
910
"files": [
1011
"build/src",
@@ -33,13 +34,23 @@
3334
"version": "npm run build",
3435
"prepublishOnly": "npm run build"
3536
},
36-
"keywords": [],
37-
"author": "",
37+
"keywords": [
38+
"json",
39+
"lucid",
40+
"adonis",
41+
"adonisjs"
42+
],
43+
"author": {
44+
"name": "Abu Masyail",
45+
"url": "https://suluh.my.id",
46+
"email": "suluhs@aol.com"
47+
},
3848
"license": "MIT",
3949
"devDependencies": {
4050
"@adonisjs/assembler": "^7.0.0",
4151
"@adonisjs/core": "^6.2.0",
4252
"@adonisjs/eslint-config": "^1.2.1",
53+
"@adonisjs/lucid": "^20.3.0",
4354
"@adonisjs/prettier-config": "^1.2.1",
4455
"@adonisjs/tsconfig": "^1.2.1",
4556
"@japa/assert": "^2.1.0",
@@ -50,13 +61,15 @@
5061
"copyfiles": "^2.4.1",
5162
"del-cli": "^5.0.0",
5263
"eslint": "^8.38.0",
64+
"knex": "^3.1.0",
5365
"np": "^9.2.0",
5466
"prettier": "^3.1.1",
5567
"ts-node": "^10.9.2",
5668
"typescript": "^5.3.3"
5769
},
5870
"peerDependencies": {
59-
"@adonisjs/core": "^6.2.0"
71+
"@adonisjs/core": "^6.2.0",
72+
"@adonisjs/lucid": "^20.3.0"
6073
},
6174
"publishConfig": {
6275
"access": "public",

providers/README.md

-5
This file was deleted.

src/README.md

-3
This file was deleted.

src/decorator.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Json from './json.js'
2+
import { BaseModel } from '@adonisjs/lucid/orm'
3+
import { ColumnOptions } from '@adonisjs/lucid/types/model'
4+
5+
export interface TranslatedOptions {}
6+
7+
export type TranslatedDecorator = (
8+
options?: TranslatedOptions & Partial<ColumnOptions>
9+
) => <TKey extends string, TTarget extends { [K in TKey]: Json }>(
10+
target: TTarget,
11+
propertyKey: TKey
12+
) => void
13+
14+
const decorator: TranslatedDecorator = (options) => {
15+
return function decorateAsColumn(target: any, property: string) {
16+
const Model = target.constructor as typeof BaseModel
17+
Model.boot()
18+
19+
const { ...columnOptions } = options ?? {}
20+
21+
Model.$addColumn(property, {
22+
consume: (value: any) => (value ? Json.fromDbResponse(value) : null),
23+
prepare: (value: Json) => (value ? JSON.stringify(value.toObject()) : null),
24+
serialize: (value: Json) => (value ? value.toObject() : null),
25+
...columnOptions,
26+
})
27+
}
28+
}
29+
30+
export default decorator

src/json.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
type JsonAttributes = Record<string, string | null | undefined>
2+
3+
export default class Json {
4+
readonly values: Record<string, string> = {}
5+
6+
static fromDbResponse(response: any): Json | null {
7+
if (response === null) {
8+
return null
9+
}
10+
11+
const attributes: JsonAttributes =
12+
typeof response === 'string' ? JSON.parse(response) : response
13+
14+
return new Json(attributes)
15+
}
16+
17+
static from(values: JsonAttributes): Json {
18+
return new Json(values)
19+
}
20+
21+
private constructor(values: JsonAttributes) {
22+
this.values = Object.fromEntries(
23+
Object.entries(values).filter(([, value]) => value !== null)
24+
) as Record<string, string>
25+
}
26+
27+
get(key: string): string | undefined {
28+
return this.values[key]
29+
}
30+
31+
getOrFail(key: string): string {
32+
const value = this.get(key)
33+
34+
if (value === undefined) {
35+
throw new Error(`No json found for key "${key}"`)
36+
}
37+
38+
return value
39+
}
40+
41+
set(key: string, value: string) {
42+
this.values[key] = value
43+
}
44+
45+
toObject(): JsonAttributes {
46+
return this.values
47+
}
48+
}

stubs/README.md

-6
This file was deleted.

stubs/main.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import { dirname } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33

4-
/**
5-
* Path to the root directory where the stubs are stored. We use
6-
* this path within commands and the configure hook
7-
*/
84
export const stubsRoot = dirname(fileURLToPath(import.meta.url))

tests/example.spec.ts

-7
This file was deleted.

0 commit comments

Comments
 (0)