@moltendb-web/angular 1.0.0-rc.5 → 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/package.json CHANGED
@@ -1,28 +1,34 @@
1
1
  {
2
2
  "name": "@moltendb-web/angular",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0",
4
4
  "description": "Official Angular Signals wrapper for MoltenDb",
5
5
  "type": "module",
6
6
  "author": "Maximilian Both <maximilian.both27@outlook.com>",
7
7
  "license": "MIT",
8
- "scripts": {
9
- "build": "ng-packagr -p ng-package.json",
10
- "dev": "ng-packagr -p ng-package.json --watch",
11
- "prepublishOnly": "npm run build"
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/maximilian27/moltendb-web/tree/master/packages/angular"
12
11
  },
13
12
  "peerDependencies": {
14
13
  "@angular/core": ">=16.0.0",
15
- "@moltendb-web/core": "^1.0.0-rc.5",
16
- "@moltendb-web/query": "^1.0.0-rc.4"
14
+ "@moltendb-web/core": "^1.0.0",
15
+ "@moltendb-web/query": "^1.0.0"
17
16
  },
18
- "devDependencies": {
19
- "@angular/compiler": "^17.0.0",
20
- "@angular/compiler-cli": "^17.0.0",
21
- "@angular/core": "^17.0.0",
22
- "@moltendb-web/core": "^1.0.0-rc.5",
23
- "@moltendb-web/query": "^1.0.0-rc.4",
24
- "ng-packagr": "^17.0.0",
25
- "typescript": "^5.2.2",
17
+ "module": "fesm2022/moltendb-web-angular.mjs",
18
+ "typings": "index.d.ts",
19
+ "exports": {
20
+ "./package.json": {
21
+ "default": "./package.json"
22
+ },
23
+ ".": {
24
+ "types": "./index.d.ts",
25
+ "esm2022": "./esm2022/moltendb-web-angular.mjs",
26
+ "esm": "./esm2022/moltendb-web-angular.mjs",
27
+ "default": "./fesm2022/moltendb-web-angular.mjs"
28
+ }
29
+ },
30
+ "sideEffects": false,
31
+ "dependencies": {
26
32
  "tslib": "^2.3.0"
27
33
  }
28
34
  }
@@ -1,189 +0,0 @@
1
- # @moltendb-web/angular
2
-
3
- The official Angular integration for MoltenDb, providing a seamless, highly reactive developer experience using modern Angular Signals.
4
-
5
- This package bridges the gap between MoltenDb's powerful Web Worker/WASM engine and your Angular UI, offering auto-updating data resources, built-in loading states, and elegant functional dependency injection.
6
-
7
- ---
8
-
9
- ## Installation
10
-
11
- ```bash
12
- npm install @moltendb-web/core @moltendb-web/query @moltendb-web/angular
13
- ```
14
-
15
- ---
16
-
17
- ## Step 1: Configure Assets (Crucial)
18
-
19
- MoltenDb runs its database engine inside a background Web Worker and relies on WebAssembly (WASM). You must tell Angular to serve these compiled files as public assets so the browser can load them.
20
-
21
- Update the `assets` array in your `angular.json` to include the MoltenDb distribution files:
22
-
23
- ```json
24
- "assets": [
25
- {
26
- "glob": "moltendb-worker.js",
27
- "input": "node_modules/@moltendb-web/core/dist",
28
- "output": "/"
29
- },
30
- {
31
- "glob": "moltendb.js",
32
- "input": "node_modules/@moltendb-web/core/dist/wasm",
33
- "output": "/wasm/"
34
- },
35
- {
36
- "glob": "*.wasm",
37
- "input": "node_modules/@moltendb-web/core/dist/wasm",
38
- "output": "/wasm/"
39
- }
40
- ]
41
- ```
42
-
43
- > **Note:** Restart your Angular dev server after modifying `angular.json`.
44
-
45
- ---
46
-
47
- ## Step 2: Provide MoltenDb
48
-
49
- Initialize MoltenDb in your app's root configuration (`app.config.ts`). This boots the engine, handles leader election across tabs, and makes the database available to your entire application.
50
-
51
- ```typescript
52
- import { ApplicationConfig } from '@angular/core';
53
- import { provideMoltenDb } from '@moltendb-web/angular';
54
-
55
- export const appConfig: ApplicationConfig = {
56
- providers: [
57
- provideMoltenDb({
58
- name: 'local_test_db',
59
- workerUrl: '/moltendb-worker.js' // Points to the asset we exposed in Step 1
60
- })
61
- ]
62
- };
63
- ```
64
-
65
- ---
66
-
67
- ## Step 3: Fetching and Mutating Data
68
-
69
- This library provides two distinct ways to interact with your database, depending on whether you are binding data to the UI or performing background mutations.
70
-
71
- ### 1. The Reactive Way: `moltenDbResource`
72
-
73
- Use `moltenDbResource` when you want to display data in your template. It automatically handles loading states, catches errors, and listens for live database changes to keep your UI instantly synced across tabs.
74
-
75
- It pre-binds the collection for you, keeping your queries incredibly clean. The query function receives the pre-bound `collection` and the full `client` as arguments:
76
-
77
- ```typescript
78
- import { Component } from '@angular/core';
79
- import { moltenDbResource } from '@moltendb-web/angular';
80
-
81
- interface UserDoc {
82
- _key: string;
83
- name: string;
84
- role: string;
85
- }
86
-
87
- @Component({
88
- selector: 'app-users',
89
- template: `
90
- @if (users.isLoading() && !users.value()) {
91
- <p>Loading...</p>
92
- }
93
-
94
- @if (users.value(); as userList) {
95
- <ul>
96
- @for (user of userList; track user._key) {
97
- <li>{{ user.name }} ({{ user.role }})</li>
98
- }
99
- </ul>
100
- }
101
-
102
- @if (users.error()) {
103
- <div class="alert">{{ users.error().message }}</div>
104
- }
105
- `
106
- })
107
- export class UsersComponent {
108
- // ⚡ The resource automatically fetches and updates when 'users' changes!
109
- users = moltenDbResource<UserDoc[]>('users', async (collection) => {
110
- console.log('Fetching users...');
111
- const result = await collection.get().exec();
112
- return result as unknown as UserDoc[];
113
- });
114
- }
115
- ```
116
-
117
- The returned `MoltenDbResource<T>` object exposes three readonly signals:
118
-
119
- | Signal | Type | Description |
120
- |---|---|---|
121
- | `value` | `Signal<T \| undefined>` | The latest query result |
122
- | `isLoading` | `Signal<boolean>` | `true` while a fetch is in progress |
123
- | `error` | `Signal<any \| null>` | The last error, or `null` if none |
124
-
125
- ### 2. The Imperative Way: `moltendbClient()`
126
-
127
- Use the `moltendbClient()` injection hook when you need to write data, perform one-off queries in response to user actions (like button clicks), or run complex logic outside of the reactive UI flow.
128
-
129
- ```typescript
130
- import { Component } from '@angular/core';
131
- import { moltendbClient } from '@moltendb-web/angular';
132
-
133
- @Component({
134
- // ...
135
- })
136
- export class AdminComponent {
137
- // ⚡ Grab direct, imperative access to the Query Client
138
- private client = moltendbClient();
139
-
140
- async addUser() {
141
- const randomId = Math.random().toString(36).substring(2, 9);
142
-
143
- // Direct, imperative database mutation
144
- await this.client.collection('users').set({
145
- [randomId]: {
146
- name: 'Angular Dev ' + randomId,
147
- role: 'Admin'
148
- }
149
- }).exec();
150
-
151
- // Note: Any moltenDbResource listening to the 'users' collection
152
- // will automatically refresh instantly after this set()!
153
- }
154
-
155
- async getUsers() {
156
- // One-off imperative commands
157
- const allUsers = await this.client.collection('users').get().exec();
158
- }
159
- }
160
- ```
161
-
162
- ---
163
-
164
- ## API Reference
165
-
166
- ### `provideMoltenDb(config)`
167
-
168
- Registers MoltenDb as an Angular environment provider. Call this once in your root `app.config.ts`.
169
-
170
- | Option | Type | Description |
171
- |---|---|---|
172
- | `name` | `string` | The name of the database |
173
- | `workerUrl` | `string` | URL to the `moltendb-worker.js` asset |
174
-
175
- ### `moltenDbResource<T>(collection, queryFn)`
176
-
177
- Creates a reactive resource bound to a collection. Must be called in an injection context (e.g. inside a component class field initializer).
178
-
179
- | Parameter | Type | Description |
180
- |---|---|---|
181
- | `collection` | `string` | The collection name to bind to |
182
- | `queryFn` | `(collection, client) => Promise<T>` | Async function receiving the pre-bound collection and the full `MoltenDbClient` |
183
-
184
- Returns a `MoltenDbResource<T>` with `value`, `isLoading`, and `error` signals.
185
-
186
- ### `moltendbClient()`
187
-
188
- An injection hook that returns the underlying `MoltenDbClient` instance for imperative database access. Must be called in an injection context.
189
-
package/ng-package.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
3
- "dest": "./dist/angular",
4
- "lib": {
5
- "entryFile": "src/public-api.ts"
6
- }
7
- }
@@ -1,11 +0,0 @@
1
- import { inject } from '@angular/core';
2
- import { MoltenDbClient } from '@moltendb-web/query';
3
- import { MoltenDbService } from './moltendb.service';
4
-
5
- /**
6
- * Functional injection hook to access the MoltenDb Query Client.
7
- * Removes the need to manually inject MoltenDbService in components.
8
- */
9
- export function moltendbClient(): MoltenDbClient {
10
- return inject(MoltenDbService).client;
11
- }
@@ -1,14 +0,0 @@
1
- import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders } from '@angular/core';
2
- import { MoltenDbOptions } from '@moltendb-web/core';
3
-
4
- export interface AngularMoltenDbOptions extends MoltenDbOptions {
5
- name: string;
6
- }
7
-
8
- export const MOLTEN_CONFIG = new InjectionToken<AngularMoltenDbOptions>('MOLTEN_CONFIG');
9
-
10
- export function provideMoltenDb(config: AngularMoltenDbOptions): EnvironmentProviders {
11
- return makeEnvironmentProviders([
12
- { provide: MOLTEN_CONFIG, useValue: config }
13
- ]);
14
- }
@@ -1,59 +0,0 @@
1
- import { inject, signal, effect, Signal, untracked } from '@angular/core';
2
- import { MoltenDbClient } from '@moltendb-web/query';
3
- import { MoltenDbService } from './moltendb.service';
4
-
5
- export interface MoltenDbResource<T> {
6
- value: Signal<T | undefined>;
7
- isLoading: Signal<boolean>;
8
- error: Signal<any | null>;
9
- }
10
-
11
- export function moltenDbResource<T>(
12
- collection: string,
13
- // Automatically infer the return type of .collection()
14
- queryFn: (collection: ReturnType<MoltenDbClient['collection']>, client: MoltenDbClient) => Promise<T>
15
- ): MoltenDbResource<T> {
16
- const molten = inject(MoltenDbService);
17
-
18
- const value = signal<T | undefined>(undefined);
19
- const isLoading = signal<boolean>(false);
20
- const error = signal<any | null>(null);
21
-
22
- const fetchData = async () => {
23
- untracked(() => isLoading.set(true));
24
-
25
- try {
26
- // ⚡ Pre-bind the collection and pass it in!
27
- const result = await queryFn(molten.client.collection(collection), molten.client);
28
- value.set(result);
29
- error.set(null);
30
- } catch (err: any) {
31
- if (err.message?.includes('404')) {
32
- value.set([] as any);
33
- error.set(null);
34
- } else {
35
- error.set(err);
36
- }
37
- } finally {
38
- isLoading.set(false);
39
- }
40
- };
41
-
42
- effect((onCleanup) => {
43
- if (!molten.isReady()) return;
44
-
45
- fetchData();
46
-
47
- const unsubscribe = molten.db.subscribe((evt) => {
48
- if (evt.collection === collection) fetchData();
49
- });
50
-
51
- onCleanup(() => unsubscribe());
52
- });
53
-
54
- return {
55
- value: value.asReadonly(),
56
- isLoading: isLoading.asReadonly(),
57
- error: error.asReadonly()
58
- };
59
- }
@@ -1,28 +0,0 @@
1
- import { Injectable, inject, signal } from '@angular/core';
2
- import { MoltenDb } from '@moltendb-web/core';
3
- import { MoltenDbClient } from '@moltendb-web/query';
4
- import { MOLTEN_CONFIG } from './moltendb.provider';
5
-
6
- @Injectable({ providedIn: 'root' })
7
- export class MoltenDbService {
8
- public db: MoltenDb;
9
- public client: MoltenDbClient;
10
-
11
- // A Signal components can watch to know when WASM is booted and Leader Election is done
12
- public isReady = signal<boolean>(false);
13
-
14
- constructor() {
15
- // 🚀 Modern Angular Injection (No constructor decorators needed!)
16
- const config = inject(MOLTEN_CONFIG);
17
-
18
- this.db = new MoltenDb(config.name, config);
19
- this.client = new MoltenDbClient(this.db);
20
-
21
- // Boot the engine and update the signal when done
22
- this.db.init().then(() => {
23
- this.isReady.set(true);
24
- }).catch(err => {
25
- console.error('[MoltenDb] Failed to initialize', err);
26
- });
27
- }
28
- }
package/src/public-api.ts DELETED
@@ -1,7 +0,0 @@
1
- /*
2
- * Public API Surface of @moltendb-web/angular
3
- */
4
-
5
- export * from './lib/moltendb.provider';
6
- export * from './lib/moltendb.resource';
7
- export * from './lib/moltendb.client';
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ES2022",
5
- "moduleResolution": "node",
6
- "lib": ["ES2022", "DOM"],
7
- "strict": true,
8
- "esModuleInterop": true,
9
- "skipLibCheck": true,
10
- "forceConsistentCasingInFileNames": true,
11
- "experimentalDecorators": true,
12
- "declaration": true,
13
- "baseUrl": ".",
14
- "paths": {
15
- "@moltendb-web/core": ["../core/src/index.ts"],
16
- "@moltendb-web/query": ["../query/src/index.ts"]
17
- }
18
- },
19
- "include": ["src/**/*.ts"],
20
- "exclude": ["node_modules", "dist"]
21
- }
package/tsconfig.lib.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "./out-tsc",
5
- "declarationMap": true,
6
- "inlineSources": true,
7
- "types": []
8
- },
9
- "exclude": ["src/**/*.spec.ts"]
10
- }
File without changes
File without changes
File without changes