@veap/settings 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Damian Miller
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,113 @@
1
- # Temporary Holding Version
1
+ [![CI](https://github.com/veapjs/veap-settings/actions/workflows/ci.yml/badge.svg)](https://github.com/veapjs/veap-settings/actions/workflows/ci.yml)
2
+ [![Release](https://github.com/veapjs/veap-settings/actions/workflows/release.yml/badge.svg)](https://github.com/veapjs/veap-settings/actions/workflows/release.yml)
2
3
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
4
+ # @veap/settings
5
+
6
+ Key-value application settings management, caching, and persistence engine
7
+ for Veap applications.
8
+
9
+ ## Overview
10
+
11
+ `@veap/settings` provides a dynamic configuration and settings system for
12
+ applications needing runtime-tunable options (site title, maintenance mode,
13
+ features flags, integrations). It bridges database persistence powered by
14
+ `@veap/database` with in-memory caching to minimize database query overhead.
15
+
16
+ ## Features
17
+
18
+ - **Typed key-value storage**: Store strings, numbers, booleans, and complex
19
+ JSON structures under unique keys.
20
+ - **In-memory cache layer**: Fast cached reads with automated cache
21
+ invalidation on update or delete operations.
22
+ - **Active Record persistence**: Backed by the `Setting` model with automatic
23
+ schema migrations.
24
+ - **Clean Architecture & IoC**: Follows repository pattern with
25
+ `SETTINGS_REPOSITORY` and integrates cleanly via `SettingsServiceProvider`.
26
+ - **Default values & fallback**: Specify fallback values when keys have not yet
27
+ been configured in the database.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ # Using bun
33
+ bun add @veap/settings @veap/kernel @veap/database
34
+
35
+ # Using pnpm
36
+ pnpm add @veap/settings @veap/kernel @veap/database
37
+
38
+ # Using npm
39
+ npm install @veap/settings @veap/kernel @veap/database
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ ### Register in Application Builder
45
+
46
+ Include `SettingsServiceProvider` in your application composition root:
47
+
48
+ ```ts
49
+ import { Application } from "@veap/kernel";
50
+ import { DatabaseServiceProvider } from "@veap/database";
51
+ import { SettingsServiceProvider } from "@veap/settings";
52
+
53
+ export const app = Application.configure()
54
+ .use(DatabaseServiceProvider)
55
+ .use(
56
+ new SettingsServiceProvider(undefined, {
57
+ autoMigrate: true, // Automatically applies settings table migrations
58
+ }),
59
+ )
60
+ .create();
61
+ ```
62
+
63
+ ### Accessing Settings via IoC
64
+
65
+ Resolve `SettingsService` from the container in your services or server actions:
66
+
67
+ ```ts
68
+ import { app } from "@veap/kernel";
69
+ import { SettingsService } from "@veap/settings";
70
+
71
+ // Read a setting with a typed fallback
72
+ const settings = await app(SettingsService);
73
+ const siteName = await settings.get("site.name", "Default Site Name");
74
+ const maintenance = await settings.get<boolean>("system.maintenance", false);
75
+
76
+ // Save or update a setting
77
+ await settings.set("site.name", "My Enterprise Portal");
78
+ await settings.set("features.comments_enabled", true);
79
+
80
+ // Delete a setting
81
+ await settings.delete("obsolete_key");
82
+ ```
83
+
84
+ ### Direct Model Access
85
+
86
+ Interact directly with the Active Record model from `@veap/settings/models`:
87
+
88
+ ```ts
89
+ import { Setting } from "@veap/settings/models";
90
+
91
+ // Query all public settings
92
+ const allSettings = await Setting.query().orderBy("key", "asc").get();
93
+ ```
94
+
95
+ ## Subpath Exports
96
+
97
+ | Export Path | Content |
98
+ | --------------------------- | ----------------------------------------------------------------------- |
99
+ | `@veap/settings` | `SettingsService`, `SettingsServiceProvider`, and repository interfaces |
100
+ | `@veap/settings/models` | Active Record `Setting` model |
101
+ | `@veap/settings/migrations` | Knex migrations for the settings schema |
102
+
103
+ ## Documentation
104
+
105
+ For full guides and settings caching options, visit:
106
+
107
+ - **Official Website**: [https://veap.pl](https://veap.pl)
108
+ - **Documentation**: [https://veap.pl/docs](https://veap.pl/docs)
109
+ - **GitHub Repository**: [https://github.com/veapjs/veap-settings](https://github.com/veapjs/veap-settings)
110
+
111
+ ## License
112
+
113
+ MIT
@@ -0,0 +1 @@
1
+ export { type ISettingsRepository, SETTINGS_REPOSITORY, } from "./settings.repository";
@@ -0,0 +1 @@
1
+ export { SETTINGS_REPOSITORY, } from "./settings.repository";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Settings persistence port.
3
+ */
4
+ export interface ISettingsRepository {
5
+ get<T>(key: string): Promise<T | null>;
6
+ set(key: string, value: unknown): Promise<void>;
7
+ delete(key: string): Promise<void>;
8
+ clear(): Promise<void>;
9
+ }
10
+ /**
11
+ * Injection token for the settings repository port.
12
+ */
13
+ export declare const SETTINGS_REPOSITORY: unique symbol;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Injection token for the settings repository port.
3
+ */
4
+ export const SETTINGS_REPOSITORY = Symbol.for("veap:settings:repository");
@@ -0,0 +1,6 @@
1
+ export * from "./contracts";
2
+ export * from "./migrations";
3
+ export * from "./models";
4
+ export * from "./providers";
5
+ export * from "./repositories";
6
+ export * from "./services";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./contracts";
2
+ export * from "./migrations";
3
+ export * from "./models";
4
+ export * from "./providers";
5
+ export * from "./repositories";
6
+ export * from "./services";
@@ -0,0 +1,4 @@
1
+ import type { Schema } from "@veap/database";
2
+ export declare const name = "0001_create_settings_table";
3
+ export declare function up(_db: any, schema: Schema): Promise<void>;
4
+ export declare function down(_db: any, schema: Schema): Promise<void>;
@@ -0,0 +1,12 @@
1
+ export const name = "0001_create_settings_table";
2
+ export async function up(_db, schema) {
3
+ await schema.createTable("settings", (table) => {
4
+ table.text("id").primaryKey();
5
+ table.text("key").notNull().unique();
6
+ table.json("value").notNull();
7
+ table.timestamp("updated_at").defaultNow();
8
+ });
9
+ }
10
+ export async function down(_db, schema) {
11
+ await schema.dropTableIfExists("settings");
12
+ }
@@ -0,0 +1,4 @@
1
+ import * as m0001 from "./0001_create_settings_table";
2
+ export declare const settingsMigrations: (typeof m0001)[];
3
+ export declare function runSettingsMigrations(): Promise<void>;
4
+ export default settingsMigrations;
@@ -0,0 +1,7 @@
1
+ import { runMigrations } from "@veap/database";
2
+ import * as m0001 from "./0001_create_settings_table";
3
+ export const settingsMigrations = [m0001];
4
+ export async function runSettingsMigrations() {
5
+ await runMigrations("settings", settingsMigrations);
6
+ }
7
+ export default settingsMigrations;
@@ -0,0 +1,22 @@
1
+ import { type CastType, Model } from "@veap/database";
2
+ export interface SettingAttributes {
3
+ id?: string;
4
+ key: string;
5
+ value: any;
6
+ updatedAt?: Date | null;
7
+ updated_at?: Date | null;
8
+ }
9
+ export declare class Setting extends Model<SettingAttributes> {
10
+ static table: string;
11
+ static autoUuid: boolean;
12
+ static timestamps: boolean | string[];
13
+ static casts: Record<string, CastType>;
14
+ /**
15
+ * Helper to quickly get a setting by key.
16
+ */
17
+ static getValue<T = any>(key: string, defaultValue?: T | null): Promise<T | null>;
18
+ /**
19
+ * Helper to quickly set/update a setting by key.
20
+ */
21
+ static setValue(key: string, value: any): Promise<Setting>;
22
+ }
@@ -0,0 +1,32 @@
1
+ import { Model } from "@veap/database";
2
+ export class Setting extends Model {
3
+ static table = "settings";
4
+ static autoUuid = true;
5
+ static timestamps = ["updated_at"];
6
+ static casts = {
7
+ value: "json",
8
+ updatedAt: "datetime",
9
+ updated_at: "datetime",
10
+ };
11
+ /**
12
+ * Helper to quickly get a setting by key.
13
+ */
14
+ static async getValue(key, defaultValue = null) {
15
+ const setting = await this.where("key", key).first();
16
+ return setting ? setting.getAttribute("value") : defaultValue;
17
+ }
18
+ /**
19
+ * Helper to quickly set/update a setting by key.
20
+ */
21
+ static async setValue(key, value) {
22
+ let setting = await this.where("key", key).first();
23
+ if (setting) {
24
+ setting.setAttribute("value", value);
25
+ await setting.save();
26
+ }
27
+ else {
28
+ setting = await this.create({ key, value });
29
+ }
30
+ return setting;
31
+ }
32
+ }
@@ -0,0 +1 @@
1
+ export { Setting, type SettingAttributes, } from "./Setting";
@@ -0,0 +1 @@
1
+ export { Setting, } from "./Setting";
@@ -0,0 +1 @@
1
+ export * from "./models/index";
package/dist/models.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./models/index";
@@ -0,0 +1 @@
1
+ export { type SettingsConfig, SettingsServiceProvider, } from "./settings.provider";
@@ -0,0 +1 @@
1
+ export { SettingsServiceProvider, } from "./settings.provider";
@@ -0,0 +1,14 @@
1
+ import { type Container, ServiceProvider } from "@veap/kernel";
2
+ export interface SettingsConfig {
3
+ /**
4
+ * Whether to automatically run settings database migrations during SettingsServiceProvider.boot().
5
+ * Defaults to `true` when database connection is initialized.
6
+ */
7
+ autoMigrate?: boolean;
8
+ }
9
+ export declare class SettingsServiceProvider extends ServiceProvider {
10
+ private config?;
11
+ constructor(container: Container, config?: SettingsConfig);
12
+ register(): void;
13
+ boot(): Promise<void>;
14
+ }
@@ -0,0 +1,35 @@
1
+ import { isDatabaseInitialized } from "@veap/database";
2
+ import { logger, ServiceProvider } from "@veap/kernel";
3
+ import { SETTINGS_REPOSITORY } from "../contracts/settings.repository";
4
+ import { runSettingsMigrations } from "../migrations";
5
+ import { ActiveRecordSettingsRepository } from "../repositories/setting.repository";
6
+ import { SettingsService } from "../services/settings.service";
7
+ export class SettingsServiceProvider extends ServiceProvider {
8
+ config;
9
+ constructor(container, config) {
10
+ super(container);
11
+ this.config = config;
12
+ }
13
+ register() {
14
+ this.container.register({
15
+ token: SETTINGS_REPOSITORY,
16
+ useClass: ActiveRecordSettingsRepository,
17
+ singleton: true,
18
+ });
19
+ this.container.register({
20
+ token: SettingsService,
21
+ useClass: SettingsService,
22
+ singleton: true,
23
+ });
24
+ }
25
+ async boot() {
26
+ if (this.config?.autoMigrate !== false && isDatabaseInitialized()) {
27
+ try {
28
+ await runSettingsMigrations();
29
+ }
30
+ catch (error) {
31
+ logger.warn("veap:settings", "Automatic settings migration failed or deferred:", error?.message || error);
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1 @@
1
+ export { ActiveRecordSettingsRepository } from "./setting.repository";
@@ -0,0 +1 @@
1
+ export { ActiveRecordSettingsRepository } from "./setting.repository";
@@ -0,0 +1,10 @@
1
+ import type { ISettingsRepository } from "../contracts/settings.repository";
2
+ /**
3
+ * `ISettingsRepository` adapter backed by the `Setting` ActiveRecord model.
4
+ */
5
+ export declare class ActiveRecordSettingsRepository implements ISettingsRepository {
6
+ get<T>(key: string): Promise<T | null>;
7
+ set(key: string, value: unknown): Promise<void>;
8
+ delete(key: string): Promise<void>;
9
+ clear(): Promise<void>;
10
+ }
@@ -0,0 +1,18 @@
1
+ import { Setting } from "../models/Setting";
2
+ /**
3
+ * `ISettingsRepository` adapter backed by the `Setting` ActiveRecord model.
4
+ */
5
+ export class ActiveRecordSettingsRepository {
6
+ async get(key) {
7
+ return Setting.getValue(key, null);
8
+ }
9
+ async set(key, value) {
10
+ await Setting.setValue(key, value);
11
+ }
12
+ async delete(key) {
13
+ await Setting.where("key", key).delete();
14
+ }
15
+ async clear() {
16
+ await Setting.query().delete();
17
+ }
18
+ }
@@ -0,0 +1 @@
1
+ export * from "./index";
package/dist/server.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./index";
@@ -0,0 +1 @@
1
+ export { SettingsService } from "./settings.service";
@@ -0,0 +1 @@
1
+ export { SettingsService } from "./settings.service";
@@ -0,0 +1,34 @@
1
+ import { type ICacheProvider } from "@veap/kernel";
2
+ import { type ISettingsRepository } from "../contracts/settings.repository";
3
+ /**
4
+ * Application service for reading and writing settings.
5
+ *
6
+ * It depends exclusively on ports (ICacheProvider, ISettingsRepository)
7
+ * that are injected through the constructor.
8
+ */
9
+ export declare class SettingsService {
10
+ private cache;
11
+ private repository;
12
+ constructor(cache: ICacheProvider, repository: ISettingsRepository);
13
+ /**
14
+ * Get a setting value by key.
15
+ * @param key Setting key (e.g., "system:page")
16
+ * @returns Value as T or null
17
+ */
18
+ get<T>(key: string): Promise<T | null>;
19
+ /**
20
+ * Set a setting value.
21
+ * @param key Setting key
22
+ * @param value JSON value
23
+ */
24
+ set(key: string, value: unknown): Promise<void>;
25
+ /**
26
+ * Remove a setting by key.
27
+ * @param key Setting key
28
+ */
29
+ remove(key: string): Promise<void>;
30
+ /**
31
+ * Clear all settings.
32
+ */
33
+ clear(): Promise<void>;
34
+ }
@@ -0,0 +1,75 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
+ return function (target, key) { decorator(target, key, paramIndex); }
12
+ };
13
+ import { CACHE_PROVIDER, Inject, Injectable, } from "@veap/kernel";
14
+ import { SETTINGS_REPOSITORY, } from "../contracts/settings.repository";
15
+ /**
16
+ * Application service for reading and writing settings.
17
+ *
18
+ * It depends exclusively on ports (ICacheProvider, ISettingsRepository)
19
+ * that are injected through the constructor.
20
+ */
21
+ let SettingsService = class SettingsService {
22
+ cache;
23
+ repository;
24
+ constructor(cache, repository) {
25
+ this.cache = cache;
26
+ this.repository = repository;
27
+ }
28
+ /**
29
+ * Get a setting value by key.
30
+ * @param key Setting key (e.g., "system:page")
31
+ * @returns Value as T or null
32
+ */
33
+ async get(key) {
34
+ const cacheKey = `settings:${key}`;
35
+ const cached = await this.cache.get(cacheKey);
36
+ if (cached !== null)
37
+ return cached;
38
+ const value = await this.repository.get(key);
39
+ if (value !== null) {
40
+ await this.cache.set(cacheKey, value);
41
+ }
42
+ return value;
43
+ }
44
+ /**
45
+ * Set a setting value.
46
+ * @param key Setting key
47
+ * @param value JSON value
48
+ */
49
+ async set(key, value) {
50
+ await this.repository.set(key, value);
51
+ await this.cache.set(`settings:${key}`, value);
52
+ }
53
+ /**
54
+ * Remove a setting by key.
55
+ * @param key Setting key
56
+ */
57
+ async remove(key) {
58
+ await this.repository.delete(key);
59
+ await this.cache.delete(`settings:${key}`);
60
+ }
61
+ /**
62
+ * Clear all settings.
63
+ */
64
+ async clear() {
65
+ await this.repository.clear();
66
+ await this.cache.clear();
67
+ }
68
+ };
69
+ SettingsService = __decorate([
70
+ Injectable(),
71
+ __param(0, Inject(CACHE_PROVIDER)),
72
+ __param(1, Inject(SETTINGS_REPOSITORY)),
73
+ __metadata("design:paramtypes", [Object, Object])
74
+ ], SettingsService);
75
+ export { SettingsService };
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@veap/settings",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Application settings management, caching, and persistence for Veap",
5
+ "bugs": "https://github.com/veapjs/veap-settings/issues",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/veapjs/veap-settings.git"
9
+ },
10
+ "type": "module",
11
+ "license": "MIT",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./models": {
20
+ "types": "./dist/models.d.ts",
21
+ "default": "./dist/models.js"
22
+ },
23
+ "./server": {
24
+ "types": "./dist/server.d.ts",
25
+ "default": "./dist/server.js"
26
+ },
27
+ "./migrations": {
28
+ "types": "./dist/migrations/index.d.ts",
29
+ "default": "./dist/migrations/index.js"
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "scripts": {
37
+ "clean": "rimraf dist",
38
+ "build": "rimraf dist && tsc",
39
+ "dev": "tsc --watch",
40
+ "test": "vitest run"
41
+ },
42
+ "dependencies": {
43
+ "@veap/database": "0.1.0",
44
+ "@veap/kernel": "0.1.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^22.20.2",
48
+ "better-sqlite3": "^13.0.3",
49
+ "knex": "^3.1.0",
50
+ "rimraf": "^6.1.3",
51
+ "typescript": "6.0.3",
52
+ "vitest": "^5.0.0"
53
+ }
54
+ }