@meridianjs/framework-utils 0.1.2 → 1.0.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/README.md +132 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @meridianjs/framework-utils
|
|
2
|
+
|
|
3
|
+
Core building blocks for creating MeridianJS modules: the DML (Data Modelling Language), the `MeridianService` factory, `Module()`, `defineLink()`, and ORM utilities.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @meridianjs/framework-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
Every domain module in a MeridianJS application is built with the primitives this package exports. You define your data models with `model`, generate CRUD services with `MeridianService`, wire up a module with `Module()`, and declare cross-module relationships with `defineLink()`.
|
|
14
|
+
|
|
15
|
+
## API Reference
|
|
16
|
+
|
|
17
|
+
### `model` — Data Modelling Language
|
|
18
|
+
|
|
19
|
+
Define a database entity without writing MikroORM decorators:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { model } from "@meridianjs/framework-utils"
|
|
23
|
+
|
|
24
|
+
export const Project = model("project", {
|
|
25
|
+
id: model.id(), // UUID primary key, auto-generated
|
|
26
|
+
name: model.text(), // VARCHAR
|
|
27
|
+
description: model.text(),
|
|
28
|
+
color: model.text(),
|
|
29
|
+
is_active: model.boolean(),
|
|
30
|
+
sort_order: model.number(),
|
|
31
|
+
metadata: model.json(),
|
|
32
|
+
status: model.enum(["active", "archived"]),
|
|
33
|
+
created_at: model.dateTime(),
|
|
34
|
+
updated_at: model.dateTime(),
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`model.id()` automatically creates a `uuid` primary key with `onCreate` auto-generation. `model.dateTime()` fields named `created_at` / `updated_at` are set automatically on create and update.
|
|
39
|
+
|
|
40
|
+
### `MeridianService` — Auto-generated CRUD
|
|
41
|
+
|
|
42
|
+
Pass a map of model names to model definitions and receive a class with full CRUD:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { MeridianService } from "@meridianjs/framework-utils"
|
|
46
|
+
import type { MeridianContainer } from "@meridianjs/types"
|
|
47
|
+
import { Project } from "./models/project.js"
|
|
48
|
+
import { Label } from "./models/label.js"
|
|
49
|
+
|
|
50
|
+
export class ProjectModuleService extends MeridianService({ Project, Label }) {
|
|
51
|
+
constructor(container: MeridianContainer) {
|
|
52
|
+
super(container)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Auto-generated for Project:
|
|
56
|
+
// listProjects(filters?, options?)
|
|
57
|
+
// listAndCountProjects(filters?, options?)
|
|
58
|
+
// retrieveProject(id)
|
|
59
|
+
// createProject(data)
|
|
60
|
+
// updateProject(id, data)
|
|
61
|
+
// deleteProject(id)
|
|
62
|
+
// softDeleteProject(id)
|
|
63
|
+
|
|
64
|
+
// Auto-generated for Label:
|
|
65
|
+
// listLabels, retrieveLabel, createLabel, updateLabel, deleteLabel ...
|
|
66
|
+
|
|
67
|
+
// Add custom methods here
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### `Module()` — Module Definition
|
|
72
|
+
|
|
73
|
+
Register a service + its models and loaders with the DI container:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { Module } from "@meridianjs/framework-utils"
|
|
77
|
+
|
|
78
|
+
export default Module("projectModuleService", {
|
|
79
|
+
service: ProjectModuleService,
|
|
80
|
+
models: [Project, Label, Milestone],
|
|
81
|
+
loaders: [defaultLoader],
|
|
82
|
+
linkable: {
|
|
83
|
+
project: { tableName: "project", primaryKey: "id" },
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The `key` (first argument) is the container registration token — this is the name used in `container.resolve("projectModuleService")` and in route handlers via `req.scope.resolve("projectModuleService")`.
|
|
89
|
+
|
|
90
|
+
### `defineLink()` — Cross-module Relationships
|
|
91
|
+
|
|
92
|
+
Declare a join table between two modules without coupling them via foreign keys:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { defineLink } from "@meridianjs/framework-utils"
|
|
96
|
+
import ProjectModule from "@meridianjs/project"
|
|
97
|
+
import IssueModule from "@meridianjs/issue"
|
|
98
|
+
|
|
99
|
+
export default defineLink(
|
|
100
|
+
{ linkable: ProjectModule.linkable!.project },
|
|
101
|
+
{ linkable: IssueModule.linkable!.issue, isList: true, deleteCascades: true },
|
|
102
|
+
{ linkTableName: "project_issue_link", entryPoint: "projectIssueLink" }
|
|
103
|
+
)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### ORM Utilities
|
|
107
|
+
|
|
108
|
+
Used inside module loaders to initialise per-module MikroORM instances:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { dmlToEntitySchema, createModuleOrm, createRepository } from "@meridianjs/framework-utils"
|
|
112
|
+
import type { LoaderOptions } from "@meridianjs/types"
|
|
113
|
+
import { Project } from "../models/project.js"
|
|
114
|
+
|
|
115
|
+
const ProjectSchema = dmlToEntitySchema(Project)
|
|
116
|
+
|
|
117
|
+
export default async function defaultLoader({ container }: LoaderOptions) {
|
|
118
|
+
const config = container.resolve("config") as any
|
|
119
|
+
const orm = await createModuleOrm([ProjectSchema], config.projectConfig.databaseUrl)
|
|
120
|
+
const em = orm.em.fork()
|
|
121
|
+
container.register({
|
|
122
|
+
projectRepository: createRepository(em, "project"),
|
|
123
|
+
projectOrm: orm,
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Each module manages its own `MikroORM` instance and schema, keeping modules fully isolated from one another.
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meridianjs/framework-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Utilities for building Meridian modules: DML, service factory, defineModule, defineLink",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"prepublishOnly": "npm run build"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@meridianjs/types": "^
|
|
29
|
+
"@meridianjs/types": "^1.0.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"@mikro-orm/core": "^6.0.0",
|