@kitledger/core 0.0.8 → 0.0.9

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/dist/fields.js CHANGED
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Enumeration of supported field types.
3
+ */
1
4
  export var FieldType;
2
5
  (function (FieldType) {
3
6
  FieldType["TEXT"] = "text";
@@ -8,6 +11,15 @@ export var FieldType;
8
11
  FieldType["SELECT"] = "select";
9
12
  FieldType["RELATION"] = "relation";
10
13
  })(FieldType || (FieldType = {}));
14
+ /**
15
+ * Factory functions to define fields of various types.
16
+ *
17
+ * @remarks
18
+ * These functions help in creating strongly typed field definitions by inferring
19
+ *
20
+ * @param options - The options for defining the field.
21
+ * @returns The defined field with its type.
22
+ */
11
23
  export function defineTextField(options) {
12
24
  const { required = false, ...rest } = options;
13
25
  return {
@@ -16,6 +28,15 @@ export function defineTextField(options) {
16
28
  ...rest,
17
29
  };
18
30
  }
31
+ /**
32
+ * Factory function to define a number field.
33
+ *
34
+ * @remarks
35
+ * These functions help in creating strongly typed field definitions by inferring
36
+ *
37
+ * @param options - The options for defining the field.
38
+ * @returns The defined field with its type.
39
+ */
19
40
  export function defineNumberField(options) {
20
41
  const { required = false, ...rest } = options;
21
42
  return {
@@ -24,6 +45,15 @@ export function defineNumberField(options) {
24
45
  ...rest,
25
46
  };
26
47
  }
48
+ /**
49
+ * Factory function to define a date field.
50
+ *
51
+ * @remarks
52
+ * These functions help in creating strongly typed field definitions by inferring
53
+ *
54
+ * @param options - The options for defining the field.
55
+ * @returns The defined field with its type.
56
+ */
27
57
  export function defineDateField(options) {
28
58
  const { required = false, ...rest } = options;
29
59
  return {
@@ -32,6 +62,15 @@ export function defineDateField(options) {
32
62
  ...rest,
33
63
  };
34
64
  }
65
+ /**
66
+ * Factory function to define a boolean field.
67
+ *
68
+ * @remarks
69
+ * These functions help in creating strongly typed field definitions by inferring
70
+ *
71
+ * @param options - The options for defining the field.
72
+ * @returns The defined field with its type.
73
+ */
35
74
  export function defineBooleanField(options) {
36
75
  const { required = false, ...rest } = options;
37
76
  return {
@@ -40,6 +79,11 @@ export function defineBooleanField(options) {
40
79
  ...rest,
41
80
  };
42
81
  }
82
+ /**
83
+ * Factory function to define a URL field.
84
+ * @param options - The options for defining the field.
85
+ * @returns The defined field with its type.
86
+ */
43
87
  export function defineURLField(options) {
44
88
  const { required = false, ...rest } = options;
45
89
  return {
@@ -48,6 +92,15 @@ export function defineURLField(options) {
48
92
  ...rest,
49
93
  };
50
94
  }
95
+ /**
96
+ * Factory function to define a select field.
97
+ *
98
+ * @remarks
99
+ * These functions help in creating strongly typed field definitions by inferring
100
+ *
101
+ * @param options - The options for defining the field.
102
+ * @returns The defined field with its type.
103
+ */
51
104
  export function defineSelectField(options) {
52
105
  const { required = false, ...rest } = options;
53
106
  return {
@@ -56,6 +109,15 @@ export function defineSelectField(options) {
56
109
  ...rest,
57
110
  };
58
111
  }
112
+ /**
113
+ * Factory function to define a relation field.
114
+ *
115
+ * @remarks
116
+ * These functions help in creating strongly typed field definitions by inferring
117
+ *
118
+ * @param options - The options for defining the field.
119
+ * @returns The defined field with its type.
120
+ */
59
121
  export function defineRelationField(options) {
60
122
  const { required = false, ...rest } = options;
61
123
  return {
package/dist/forms.d.ts CHANGED
@@ -2,22 +2,37 @@ import type { EntityModel } from "./entities.js";
2
2
  import type { Field } from "./fields.js";
3
3
  import type { TransactionModel } from "./transactions.js";
4
4
  import type { UnitModel } from "./units.js";
5
+ /**
6
+ * The type of form being defined.
7
+ */
5
8
  export declare enum FormType {
6
9
  ENTITY = "ENTITY",
7
10
  TRANSACTION = "TRANSACTION",
8
11
  UNIT = "UNIT"
9
12
  }
13
+ /**
14
+ * How a form field is displayed.
15
+ */
10
16
  export type FormFieldDisplay = "normal" | "inline" | "disabled" | "hidden";
17
+ /**
18
+ * Configuration options for a form field.
19
+ */
11
20
  export type FormFieldConfig = {
12
21
  label?: string;
13
22
  description?: string;
14
23
  display?: FormFieldDisplay;
15
24
  };
25
+ /**
26
+ * Infers the form field configuration from a list of fields.
27
+ */
16
28
  type InferFieldConfig<TFields extends readonly Field[]> = {
17
29
  [K in TFields[number] as K["refId"]]?: FormFieldConfig & {
18
30
  required?: K["required"] extends true ? true : boolean;
19
31
  };
20
32
  };
33
+ /**
34
+ * Base interface for all forms.
35
+ */
21
36
  export interface BaseForm {
22
37
  refId: string;
23
38
  modelRefId: string;
@@ -25,6 +40,9 @@ export interface BaseForm {
25
40
  description?: string;
26
41
  fieldOrder?: string[];
27
42
  }
43
+ /**
44
+ * Configuration options for entity forms
45
+ */
28
46
  export type EntityFormOptions<TModel extends {
29
47
  fields?: readonly any[];
30
48
  }> = {
@@ -33,6 +51,9 @@ export type EntityFormOptions<TModel extends {
33
51
  refId: string;
34
52
  fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
35
53
  };
54
+ /**
55
+ * Configuration options for unit forms
56
+ */
36
57
  export type UnitFormOptions<TModel extends {
37
58
  fields?: readonly any[];
38
59
  }> = {
@@ -41,6 +62,9 @@ export type UnitFormOptions<TModel extends {
41
62
  refId: string;
42
63
  fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
43
64
  };
65
+ /**
66
+ * Configuration options for transaction forms
67
+ */
44
68
  export type TransactionFormOptions<TModel extends {
45
69
  fields?: readonly any[];
46
70
  }> = {
@@ -49,19 +73,58 @@ export type TransactionFormOptions<TModel extends {
49
73
  refId: string;
50
74
  fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
51
75
  };
76
+ /**
77
+ * Form definition for transactions.
78
+ */
52
79
  export interface TransactionForm<TModel extends TransactionModel> extends BaseForm {
53
80
  type: FormType.TRANSACTION;
54
81
  fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
55
82
  }
83
+ /**
84
+ * Form definition for entities.
85
+ */
56
86
  export interface EntityForm<TModel extends EntityModel> extends BaseForm {
57
87
  type: FormType.ENTITY;
58
88
  fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
59
89
  }
90
+ /**
91
+ * Form definition for units.
92
+ */
60
93
  export interface UnitForm<TModel extends UnitModel> extends BaseForm {
61
94
  type: FormType.UNIT;
62
95
  fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
63
96
  }
97
+ /**
98
+ * Factory function to define a transaction form.
99
+ *
100
+ * @remarks
101
+ * This is a pure configuration function that helps create a transaction form
102
+ *
103
+ * @param model The transaction model the form is based on
104
+ * @param config The configuration options for the form
105
+ * @returns A transaction form definition
106
+ */
64
107
  export declare function defineTransactionForm<const TModel extends TransactionModel>(model: TModel, config: TransactionFormOptions<TModel>): TransactionForm<TModel>;
108
+ /**
109
+ * Factory function to define an entity form.
110
+ *
111
+ * @remarks
112
+ * This is a pure configuration function that helps create an entity form
113
+ *
114
+ * @param model The entity model the form is based on
115
+ * @param config The configuration options for the form
116
+ * @returns An entity form definition
117
+ */
65
118
  export declare function defineEntityForm<const TModel extends EntityModel>(model: TModel, config: EntityFormOptions<TModel>): EntityForm<TModel>;
119
+ /**
120
+ * Factory function to define a unit form.
121
+ *
122
+ * @remarks
123
+ * This is a pure configuration function that helps create a unit form
124
+ *
125
+ * @param model The unit model the form is based on
126
+ * @param config The configuration options for the form
127
+ * @returns A unit form definition
128
+ */
66
129
  export declare function defineUnitForm<const TModel extends UnitModel>(model: TModel, config: UnitFormOptions<TModel>): UnitForm<TModel>;
67
130
  export {};
package/dist/forms.js CHANGED
@@ -1,4 +1,7 @@
1
1
  // --- CORE TYPES ---
2
+ /**
3
+ * The type of form being defined.
4
+ */
2
5
  export var FormType;
3
6
  (function (FormType) {
4
7
  FormType["ENTITY"] = "ENTITY";
@@ -6,6 +9,16 @@ export var FormType;
6
9
  FormType["UNIT"] = "UNIT";
7
10
  })(FormType || (FormType = {}));
8
11
  // --- FACTORIES (Pure Configuration) ---
12
+ /**
13
+ * Factory function to define a transaction form.
14
+ *
15
+ * @remarks
16
+ * This is a pure configuration function that helps create a transaction form
17
+ *
18
+ * @param model The transaction model the form is based on
19
+ * @param config The configuration options for the form
20
+ * @returns A transaction form definition
21
+ */
9
22
  export function defineTransactionForm(model, config) {
10
23
  return {
11
24
  refId: config.refId,
@@ -16,6 +29,16 @@ export function defineTransactionForm(model, config) {
16
29
  fields: config.fields,
17
30
  };
18
31
  }
32
+ /**
33
+ * Factory function to define an entity form.
34
+ *
35
+ * @remarks
36
+ * This is a pure configuration function that helps create an entity form
37
+ *
38
+ * @param model The entity model the form is based on
39
+ * @param config The configuration options for the form
40
+ * @returns An entity form definition
41
+ */
19
42
  export function defineEntityForm(model, config) {
20
43
  return {
21
44
  refId: config.refId,
@@ -26,6 +49,16 @@ export function defineEntityForm(model, config) {
26
49
  fields: config.fields,
27
50
  };
28
51
  }
52
+ /**
53
+ * Factory function to define a unit form.
54
+ *
55
+ * @remarks
56
+ * This is a pure configuration function that helps create a unit form
57
+ *
58
+ * @param model The unit model the form is based on
59
+ * @param config The configuration options for the form
60
+ * @returns A unit form definition
61
+ */
29
62
  export function defineUnitForm(model, config) {
30
63
  return {
31
64
  refId: config.refId,
package/dist/ledgers.d.ts CHANGED
@@ -1,22 +1,39 @@
1
- import { InferInsertModel, InferSelectModel } from "drizzle-orm";
2
- import * as v from "valibot";
3
- import { InferOutput } from "valibot";
4
- import { KitledgerDb } from "./db.js";
5
- import { FilterOperationParameters, GetOperationResult } from "./db.js";
6
- import { ledgers } from "./schema.js";
7
- export declare const LedgerCreateSchema: v.ObjectSchema<{
8
- readonly ref_id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MaxLengthAction<string, 64, undefined>]>;
9
- readonly alt_id: v.NullishSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MaxLengthAction<string, 64, undefined>]>, null>;
10
- readonly name: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.NonEmptyAction<string, undefined>, v.MaxLengthAction<string, 64, undefined>]>;
11
- readonly description: v.NullableSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MaxLengthAction<string, 255, undefined>]>, undefined>;
12
- readonly unit_model_id: v.StringSchema<undefined>;
13
- readonly active: v.NullishSchema<v.BooleanSchema<undefined>, true>;
14
- readonly created_at: v.OptionalSchema<v.DateSchema<undefined>, undefined>;
15
- readonly updated_at: v.OptionalSchema<v.NullableSchema<v.DateSchema<undefined>, undefined>, undefined>;
16
- }, undefined>;
17
- import { ValidationSuccess, ValidationFailure } from "./validation.js";
18
- export type LedgerInsert = InferInsertModel<typeof ledgers>;
19
- export type Ledger = InferSelectModel<typeof ledgers>;
20
- export type LedgerCreateData = InferOutput<typeof LedgerCreateSchema>;
21
- export declare function filterLedgers(db: KitledgerDb, params: FilterOperationParameters): Promise<GetOperationResult<Ledger>>;
22
- export declare function createLedger(db: KitledgerDb, data: LedgerCreateData): Promise<ValidationSuccess<Ledger> | ValidationFailure<LedgerCreateData>>;
1
+ import type { UnitModel } from "./units.js";
2
+ /**
3
+ * Ledger definitions and utilities.
4
+ *
5
+ * @remarks
6
+ * This module provides type definitions and functions for defining ledgers in the system.
7
+ */
8
+ export type Ledger = {
9
+ refId: string;
10
+ name: string;
11
+ description?: string;
12
+ status: LedgerStatus;
13
+ baseUnitModel: string;
14
+ };
15
+ export declare enum LedgerStatus {
16
+ ACTIVE = "active",
17
+ INACTIVE = "inactive"
18
+ }
19
+ /**
20
+ * Options for defining a ledger.
21
+ *
22
+ * @param refId - The reference ID of the ledger.
23
+ * @param name - The name of the ledger.
24
+ * @param description - An optional description of the ledger.
25
+ * @param active - An optional flag indicating if the ledger is active. Defaults to true.
26
+ * @returns An object containing the options for the ledger.
27
+ */
28
+ export type LedgerOptions = {
29
+ refId: string;
30
+ name: string;
31
+ description?: string;
32
+ status?: LedgerStatus;
33
+ };
34
+ /**
35
+ * Defines a ledger with the given options.
36
+ * @param options - The options for defining the ledger.
37
+ * @returns The defined ledger.
38
+ */
39
+ export declare function defineLedger(options: LedgerOptions, baseUnitModel: UnitModel): Ledger;
package/dist/ledgers.js CHANGED
@@ -1,144 +1,17 @@
1
- import { and, eq, sql } from "drizzle-orm";
2
- import * as v from "valibot";
3
- import { ANY, defaultLimit, defaultOffset, maxLimit, parseBooleanFilterValue, } from "./db.js";
4
- import { ledgers } from "./schema.js";
5
- export const LedgerCreateSchema = v.object({
6
- ref_id: v.pipe(v.string(), v.maxLength(64)),
7
- alt_id: v.nullish(v.pipe(v.string(), v.maxLength(64)), null),
8
- name: v.pipe(v.string(), v.nonEmpty(), v.maxLength(64)),
9
- description: v.nullable(v.pipe(v.string(), v.maxLength(255))),
10
- unit_model_id: v.string(),
11
- active: v.nullish(v.boolean(), true),
12
- created_at: v.optional(v.date()),
13
- updated_at: v.optional(v.nullable(v.date())),
14
- });
15
- import { v7 } from "uuid";
16
- import { parseValibotIssues, } from "./validation.js";
17
- export async function filterLedgers(db, params) {
18
- const { limit = defaultLimit, offset = defaultOffset, ...filters } = params;
19
- const filterConditions = [];
20
- for (const [key, value] of Object.entries(filters)) {
21
- if (key === ledgers.id.name && String(value).length > 0) {
22
- filterConditions.push(eq(ledgers.id, String(value)));
23
- }
24
- if (key === ledgers.ref_id.name && String(value).length > 0) {
25
- filterConditions.push(eq(ledgers.ref_id, String(value)));
26
- }
27
- if (key === ledgers.alt_id.name && String(value).length > 0) {
28
- filterConditions.push(eq(ledgers.alt_id, String(value)));
29
- }
30
- if (key === ledgers.name.name && String(value).length > 0) {
31
- filterConditions.push(sql `${ledgers.name} ILIKE ${"%" + String(value) + "%"}`);
32
- }
33
- if (key === ledgers.active.name && String(value).length > 0) {
34
- if (value === ANY) {
35
- continue;
36
- }
37
- const booleanValue = parseBooleanFilterValue(String(value));
38
- if (booleanValue !== null) {
39
- filterConditions.push(eq(ledgers.active, booleanValue));
40
- }
41
- }
42
- }
43
- // By default, only return active ledgers unless explicitly filtered otherwise
44
- if (!Object.keys(filters).includes(ledgers.active.name)) {
45
- filterConditions.push(eq(ledgers.active, true));
46
- }
47
- const results = await db
48
- .select()
49
- .from(ledgers)
50
- .where(and(...filterConditions))
51
- .limit(Math.min(limit, maxLimit))
52
- .offset(offset);
1
+ export var LedgerStatus;
2
+ (function (LedgerStatus) {
3
+ LedgerStatus["ACTIVE"] = "active";
4
+ LedgerStatus["INACTIVE"] = "inactive";
5
+ })(LedgerStatus || (LedgerStatus = {}));
6
+ /**
7
+ * Defines a ledger with the given options.
8
+ * @param options - The options for defining the ledger.
9
+ * @returns The defined ledger.
10
+ */
11
+ export function defineLedger(options, baseUnitModel) {
53
12
  return {
54
- data: results,
55
- limit: Math.min(limit, maxLimit),
56
- offset: offset,
57
- count: results.length,
58
- };
59
- }
60
- async function refIdAlreadyExists(db, refId) {
61
- const results = await db.query.ledgers.findMany({
62
- where: eq(ledgers.ref_id, refId),
63
- columns: { id: true },
64
- });
65
- return results.length > 0;
66
- }
67
- async function altIdAlreadyExists(db, altId) {
68
- if (!altId) {
69
- return false;
70
- }
71
- const results = await db.query.ledgers.findMany({
72
- where: eq(ledgers.alt_id, altId),
73
- columns: { id: true },
74
- });
75
- return results.length > 0;
76
- }
77
- async function validateLedgerCreate(db, data) {
78
- const result = v.safeParse(LedgerCreateSchema, data);
79
- let success = result.success;
80
- if (!result.success) {
81
- return {
82
- success: false,
83
- errors: parseValibotIssues(result.issues),
84
- };
85
- }
86
- const errors = [];
87
- const [refIdError, altIdError] = await Promise.all([
88
- refIdAlreadyExists(db, data.ref_id),
89
- altIdAlreadyExists(db, data.alt_id ?? null),
90
- ]);
91
- if (refIdError) {
92
- success = false;
93
- errors.push({
94
- type: "data",
95
- path: "ref_id",
96
- message: "Ref ID already exists.",
97
- });
98
- }
99
- if (altIdError) {
100
- success = false;
101
- errors.push({
102
- type: "data",
103
- path: "alt_id",
104
- message: "Alt ID already exists.",
105
- });
106
- }
107
- return {
108
- success: success,
109
- data: result.output,
110
- errors: errors,
111
- };
112
- }
113
- export async function createLedger(db, data) {
114
- const validation = await validateLedgerCreate(db, data);
115
- if (!validation.success || !validation.data) {
116
- return {
117
- success: false,
118
- data: data,
119
- errors: validation.errors,
120
- };
121
- }
122
- const insertData = {
123
- id: v7(),
124
- ...validation.data,
125
- };
126
- const result = await db.insert(ledgers).values(insertData).returning();
127
- if (result.length === 0) {
128
- return {
129
- success: false,
130
- data: validation.data,
131
- errors: [
132
- {
133
- type: "data",
134
- path: null,
135
- message: "Failed to create ledger.",
136
- },
137
- ],
138
- };
139
- }
140
- return {
141
- success: true,
142
- data: result[0],
13
+ ...options,
14
+ status: options.status ?? LedgerStatus.ACTIVE,
15
+ baseUnitModel: baseUnitModel.refId,
143
16
  };
144
17
  }
package/dist/main.d.ts CHANGED
@@ -1,15 +1,32 @@
1
1
  import type { TransactionForm, EntityForm, UnitForm } from "./forms.js";
2
2
  import { KitledgerDb } from "./db.js";
3
3
  import { EntityModel } from "./entities.js";
4
+ import { Ledger } from "./ledgers.js";
4
5
  import { TransactionModel } from "./transactions.js";
5
6
  import { UnitModel } from "./units.js";
7
+ /**
8
+ * Type definition for the overall Kitledger configuration.
9
+ *
10
+ * @remarks
11
+ * Includes database instance, entity models, transaction models, unit models,
12
+ * and optional forms for transactions, entities, and units.
13
+ *
14
+ * @returns An object representing the Kitledger configuration.
15
+ */
6
16
  export interface KitledgerConfig {
7
17
  database: KitledgerDb;
18
+ entityForms?: EntityForm<EntityModel>[];
8
19
  entityModels: EntityModel[];
9
- transactionModels: TransactionModel[];
10
- unitModels: UnitModel[];
20
+ ledgers: Ledger[];
11
21
  transactionForms?: TransactionForm<TransactionModel>[];
12
- entityForms?: EntityForm<EntityModel>[];
22
+ transactionModels: TransactionModel[];
13
23
  unitForms?: UnitForm<UnitModel>[];
24
+ unitModels: UnitModel[];
14
25
  }
26
+ /**
27
+ * Factory function to define the Kitledger configuration.
28
+ *
29
+ * @param config - The Kitledger configuration object.
30
+ * @returns The provided Kitledger configuration.
31
+ */
15
32
  export declare function defineConfig(config: KitledgerConfig): KitledgerConfig;
package/dist/main.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Factory function to define the Kitledger configuration.
3
+ *
4
+ * @param config - The Kitledger configuration object.
5
+ * @returns The provided Kitledger configuration.
6
+ */
1
7
  export function defineConfig(config) {
2
8
  return config;
3
9
  }
@@ -0,0 +1,9 @@
1
+ ALTER TABLE "entity_models" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
2
+ ALTER TABLE "ledgers" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
3
+ ALTER TABLE "transaction_models" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
4
+ ALTER TABLE "unit_models" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
5
+ DROP TABLE "entity_models" CASCADE;--> statement-breakpoint
6
+ DROP TABLE "ledgers" CASCADE;--> statement-breakpoint
7
+ DROP TABLE "transaction_models" CASCADE;--> statement-breakpoint
8
+ DROP TABLE "unit_models" CASCADE;--> statement-breakpoint
9
+ ALTER TABLE "accounts" ALTER COLUMN "ledger_id" SET DATA TYPE varchar(64);
@@ -0,0 +1 @@
1
+ -- Custom SQL migration file, put your code below! --