@kitledger/core 0.0.5 → 0.0.7
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/db.d.ts +6 -14
- package/dist/db.js +17 -13
- package/dist/{index.d.ts → main.d.ts} +3 -1
- package/dist/migrations/0000_damp_golden_guardian.sql +141 -0
- package/dist/migrations/0001_purple_morph.sql +8 -0
- package/dist/migrations/0002_fair_thundra.sql +1 -0
- package/dist/migrations/meta/0000_snapshot.json +901 -0
- package/dist/migrations/meta/0001_snapshot.json +949 -0
- package/dist/migrations/meta/0002_snapshot.json +955 -0
- package/dist/migrations/meta/_journal.json +27 -0
- package/dist/schema.d.ts +7 -0
- package/dist/schema.js +7 -1
- package/package.json +10 -5
- /package/dist/{index.js → main.js} +0 -0
package/dist/db.d.ts
CHANGED
|
@@ -2,26 +2,19 @@ import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
|
2
2
|
import postgres from "postgres";
|
|
3
3
|
import * as v from "valibot";
|
|
4
4
|
import * as schema from "./schema.js";
|
|
5
|
-
type
|
|
5
|
+
export type KitledgerDbOptions = {
|
|
6
6
|
url: string;
|
|
7
7
|
ssl?: boolean;
|
|
8
8
|
max?: number;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
migrationsTable?: string;
|
|
10
|
+
migrationsSchema?: string;
|
|
11
|
+
autoMigrate?: boolean;
|
|
11
12
|
};
|
|
12
13
|
export type KitledgerDb = PostgresJsDatabase<typeof schema> & {
|
|
13
14
|
$client: postgres.Sql<{}>;
|
|
14
15
|
};
|
|
15
|
-
export declare function
|
|
16
|
-
|
|
17
|
-
}>;
|
|
18
|
-
/**
|
|
19
|
-
* Common database helper for timestamps
|
|
20
|
-
*/
|
|
21
|
-
export declare const timestamps: {
|
|
22
|
-
created_at: import("drizzle-orm").NotNull<import("drizzle-orm").HasDefault<import("drizzle-orm/pg-core").PgTimestampBuilderInitial<"created_at">>>;
|
|
23
|
-
updated_at: import("drizzle-orm/pg-core").PgTimestampBuilderInitial<"updated_at">;
|
|
24
|
-
};
|
|
16
|
+
export declare function runMigrations(db: KitledgerDb, migrationsTable: string, migrationsSchema: string): Promise<void>;
|
|
17
|
+
export declare function initializeDatabase(options: KitledgerDbOptions): Promise<KitledgerDb>;
|
|
25
18
|
/**
|
|
26
19
|
* Supported operation types for get operations.
|
|
27
20
|
*/
|
|
@@ -75,4 +68,3 @@ export declare const maxLimit = 1000;
|
|
|
75
68
|
* Default value for pagination offset.
|
|
76
69
|
*/
|
|
77
70
|
export declare const defaultOffset = 0;
|
|
78
|
-
export {};
|
package/dist/db.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { timestamp } from "drizzle-orm/pg-core";
|
|
2
1
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
3
2
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
4
5
|
import * as v from "valibot";
|
|
5
6
|
import * as schema from "./schema.js";
|
|
6
|
-
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = dirname(__filename);
|
|
9
|
+
export async function runMigrations(db, migrationsTable, migrationsSchema) {
|
|
10
|
+
const migrationsPath = resolve(__dirname, "../dist/migrations");
|
|
11
|
+
console.log("***Using migrations path:", migrationsPath);
|
|
7
12
|
await migrate(db, {
|
|
8
|
-
migrationsFolder:
|
|
13
|
+
migrationsFolder: migrationsPath,
|
|
9
14
|
migrationsTable: migrationsTable,
|
|
10
15
|
migrationsSchema: migrationsSchema,
|
|
11
16
|
});
|
|
@@ -15,23 +20,22 @@ export async function initializeDatabase(options) {
|
|
|
15
20
|
url: options.url,
|
|
16
21
|
ssl: options.ssl ? options.ssl : false,
|
|
17
22
|
max: options.max ? options.max : 10,
|
|
23
|
+
autoMigrate: options.autoMigrate ?? true,
|
|
18
24
|
};
|
|
19
25
|
const db = drizzle({
|
|
20
26
|
connection: dbConfig,
|
|
21
27
|
schema: schema,
|
|
22
28
|
});
|
|
23
|
-
const migrationsTable = options.
|
|
24
|
-
const migrationsSchema = options.
|
|
25
|
-
|
|
29
|
+
const migrationsTable = options.migrationsTable || "schema_history";
|
|
30
|
+
const migrationsSchema = options.migrationsSchema || "public";
|
|
31
|
+
/**
|
|
32
|
+
* Auto-migrate database schema if enabled
|
|
33
|
+
*/
|
|
34
|
+
if (dbConfig.autoMigrate) {
|
|
35
|
+
await runMigrations(db, migrationsTable, migrationsSchema);
|
|
36
|
+
}
|
|
26
37
|
return db;
|
|
27
38
|
}
|
|
28
|
-
/**
|
|
29
|
-
* Common database helper for timestamps
|
|
30
|
-
*/
|
|
31
|
-
export const timestamps = {
|
|
32
|
-
created_at: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),
|
|
33
|
-
updated_at: timestamp("updated_at", { mode: "date" }),
|
|
34
|
-
};
|
|
35
39
|
/**
|
|
36
40
|
* Supported operation types for get operations.
|
|
37
41
|
*/
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { KitledgerDb } from "./db.js";
|
|
1
2
|
import { EntityModel } from "./entities.js";
|
|
2
3
|
import { TransactionModel } from "./transactions.js";
|
|
3
4
|
export interface KitledgerConfig {
|
|
4
|
-
|
|
5
|
+
database: KitledgerDb;
|
|
5
6
|
entityModels: EntityModel[];
|
|
7
|
+
transactionModels: TransactionModel[];
|
|
6
8
|
}
|
|
7
9
|
export declare function defineConfig(config: KitledgerConfig): KitledgerConfig;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
CREATE TABLE "accounts" (
|
|
2
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
3
|
+
"ref_id" varchar(64) NOT NULL,
|
|
4
|
+
"alt_id" varchar(64),
|
|
5
|
+
"balance_type" varchar(10) NOT NULL,
|
|
6
|
+
"ledger_id" uuid NOT NULL,
|
|
7
|
+
"parent_id" uuid,
|
|
8
|
+
"name" varchar(64) NOT NULL,
|
|
9
|
+
"meta" jsonb,
|
|
10
|
+
"active" boolean DEFAULT true NOT NULL,
|
|
11
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
12
|
+
"updated_at" timestamp,
|
|
13
|
+
CONSTRAINT "accounts_ref_id_unique" UNIQUE("ref_id"),
|
|
14
|
+
CONSTRAINT "accounts_alt_id_unique" UNIQUE("alt_id")
|
|
15
|
+
);
|
|
16
|
+
--> statement-breakpoint
|
|
17
|
+
CREATE TABLE "api_tokens" (
|
|
18
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
19
|
+
"user_id" uuid NOT NULL,
|
|
20
|
+
"name" varchar(64) NOT NULL,
|
|
21
|
+
"revoked_at" timestamp
|
|
22
|
+
);
|
|
23
|
+
--> statement-breakpoint
|
|
24
|
+
CREATE TABLE "entity_models" (
|
|
25
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
26
|
+
"ref_id" varchar(64) NOT NULL,
|
|
27
|
+
"alt_id" varchar(64),
|
|
28
|
+
"name" varchar(64) NOT NULL,
|
|
29
|
+
"active" boolean DEFAULT true NOT NULL,
|
|
30
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
31
|
+
"updated_at" timestamp,
|
|
32
|
+
CONSTRAINT "entity_models_ref_id_unique" UNIQUE("ref_id"),
|
|
33
|
+
CONSTRAINT "entity_models_alt_id_unique" UNIQUE("alt_id")
|
|
34
|
+
);
|
|
35
|
+
--> statement-breakpoint
|
|
36
|
+
CREATE TABLE "ledgers" (
|
|
37
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
38
|
+
"ref_id" varchar(64) NOT NULL,
|
|
39
|
+
"alt_id" varchar(64),
|
|
40
|
+
"name" varchar(64) NOT NULL,
|
|
41
|
+
"description" varchar(255),
|
|
42
|
+
"unit_model_id" uuid NOT NULL,
|
|
43
|
+
"active" boolean DEFAULT true NOT NULL,
|
|
44
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
45
|
+
"updated_at" timestamp,
|
|
46
|
+
CONSTRAINT "ledgers_ref_id_unique" UNIQUE("ref_id"),
|
|
47
|
+
CONSTRAINT "ledgers_alt_id_unique" UNIQUE("alt_id")
|
|
48
|
+
);
|
|
49
|
+
--> statement-breakpoint
|
|
50
|
+
CREATE TABLE "permission_assignments" (
|
|
51
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
52
|
+
"permission_id" uuid NOT NULL,
|
|
53
|
+
"user_id" uuid,
|
|
54
|
+
"role_id" uuid,
|
|
55
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
56
|
+
"updated_at" timestamp
|
|
57
|
+
);
|
|
58
|
+
--> statement-breakpoint
|
|
59
|
+
CREATE TABLE "permissions" (
|
|
60
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
61
|
+
"name" varchar(64) NOT NULL,
|
|
62
|
+
"description" varchar(255),
|
|
63
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
64
|
+
"updated_at" timestamp,
|
|
65
|
+
CONSTRAINT "permissions_name_unique" UNIQUE("name")
|
|
66
|
+
);
|
|
67
|
+
--> statement-breakpoint
|
|
68
|
+
CREATE TABLE "roles" (
|
|
69
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
70
|
+
"name" varchar(64) NOT NULL,
|
|
71
|
+
"description" varchar(255),
|
|
72
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
73
|
+
"updated_at" timestamp,
|
|
74
|
+
CONSTRAINT "roles_name_unique" UNIQUE("name")
|
|
75
|
+
);
|
|
76
|
+
--> statement-breakpoint
|
|
77
|
+
CREATE TABLE "system_permissions" (
|
|
78
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
79
|
+
"permission" varchar(64) NOT NULL,
|
|
80
|
+
"user_id" uuid NOT NULL,
|
|
81
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
82
|
+
"updated_at" timestamp
|
|
83
|
+
);
|
|
84
|
+
--> statement-breakpoint
|
|
85
|
+
CREATE TABLE "transaction_models" (
|
|
86
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
87
|
+
"ref_id" varchar(64) NOT NULL,
|
|
88
|
+
"alt_id" varchar(64),
|
|
89
|
+
"name" varchar(64) NOT NULL,
|
|
90
|
+
"active" boolean DEFAULT true NOT NULL,
|
|
91
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
92
|
+
"updated_at" timestamp,
|
|
93
|
+
CONSTRAINT "transaction_models_ref_id_unique" UNIQUE("ref_id"),
|
|
94
|
+
CONSTRAINT "transaction_models_alt_id_unique" UNIQUE("alt_id")
|
|
95
|
+
);
|
|
96
|
+
--> statement-breakpoint
|
|
97
|
+
CREATE TABLE "unit_models" (
|
|
98
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
99
|
+
"ref_id" varchar(64) NOT NULL,
|
|
100
|
+
"alt_id" varchar(64),
|
|
101
|
+
"name" varchar(64) NOT NULL,
|
|
102
|
+
"active" boolean DEFAULT true NOT NULL,
|
|
103
|
+
"base_unit_id" uuid,
|
|
104
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
105
|
+
"updated_at" timestamp,
|
|
106
|
+
CONSTRAINT "unit_models_ref_id_unique" UNIQUE("ref_id"),
|
|
107
|
+
CONSTRAINT "unit_models_alt_id_unique" UNIQUE("alt_id"),
|
|
108
|
+
CONSTRAINT "unit_models_base_unit_id_unique" UNIQUE("base_unit_id")
|
|
109
|
+
);
|
|
110
|
+
--> statement-breakpoint
|
|
111
|
+
CREATE TABLE "user_roles" (
|
|
112
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
113
|
+
"user_id" uuid NOT NULL,
|
|
114
|
+
"role_id" uuid NOT NULL,
|
|
115
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
116
|
+
"updated_at" timestamp
|
|
117
|
+
);
|
|
118
|
+
--> statement-breakpoint
|
|
119
|
+
CREATE TABLE "users" (
|
|
120
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
121
|
+
"first_name" varchar(64) NOT NULL,
|
|
122
|
+
"last_name" varchar(64) NOT NULL,
|
|
123
|
+
"email" varchar(64) NOT NULL,
|
|
124
|
+
"password_hash" text NOT NULL,
|
|
125
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
126
|
+
"updated_at" timestamp,
|
|
127
|
+
CONSTRAINT "users_email_unique" UNIQUE("email")
|
|
128
|
+
);
|
|
129
|
+
--> statement-breakpoint
|
|
130
|
+
ALTER TABLE "api_tokens" ADD CONSTRAINT "api_tokens_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
131
|
+
ALTER TABLE "permission_assignments" ADD CONSTRAINT "permission_assignments_permission_id_permissions_id_fk" FOREIGN KEY ("permission_id") REFERENCES "public"."permissions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
132
|
+
ALTER TABLE "system_permissions" ADD CONSTRAINT "system_permissions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
133
|
+
ALTER TABLE "user_roles" ADD CONSTRAINT "user_roles_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
134
|
+
ALTER TABLE "user_roles" ADD CONSTRAINT "user_roles_role_id_roles_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."roles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
|
135
|
+
CREATE INDEX "api_token_user_idx" ON "api_tokens" USING btree ("user_id");--> statement-breakpoint
|
|
136
|
+
CREATE INDEX "permission_assignment_user_idx" ON "permission_assignments" USING btree ("user_id");--> statement-breakpoint
|
|
137
|
+
CREATE INDEX "permission_assignment_role_idx" ON "permission_assignments" USING btree ("role_id");--> statement-breakpoint
|
|
138
|
+
CREATE INDEX "permission_assignment_permission_idx" ON "permission_assignments" USING btree ("permission_id");--> statement-breakpoint
|
|
139
|
+
CREATE INDEX "system_permission_user_idx" ON "system_permissions" USING btree ("user_id");--> statement-breakpoint
|
|
140
|
+
CREATE INDEX "system_permission_permission_idx" ON "system_permissions" USING btree ("permission");--> statement-breakpoint
|
|
141
|
+
CREATE INDEX "user_email_idx" ON "users" USING btree ("email");
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
CREATE UNLOGGED TABLE "sessions" (
|
|
2
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
3
|
+
"user_id" uuid NOT NULL,
|
|
4
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
5
|
+
"updated_at" timestamp
|
|
6
|
+
);
|
|
7
|
+
--> statement-breakpoint
|
|
8
|
+
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ALTER TABLE "sessions" ADD COLUMN "expires_at" timestamp NOT NULL;
|