anchordb-angular
v1.3.2
Published
Angular and Ionic bindings for AnchorDB — NgModule, DI, RxJS, and SQLite storage ready after one install.
Maintainers
Readme
anchordb-angular
Angular and Ionic bindings for AnchorDB — the same
module and injection shape as @nestjs/mongoose, with SQLite storage ready after one install.
Overview · Report a bug or get support
npm install anchordb-angularThat single command brings the database and the Capacitor SQLite driver, and in a browser — ng
serve, ionic serve, a PWA — the app is ready. A native build needs one more line, below.
provideAnchorIonic picks the right storage for wherever the app is running:
import { provideAnchorIonic, provideAnchorFeature } from "anchordb-angular";
bootstrapApplication(AppComponent, {
providers: [
...provideAnchorIonic({ name: "contacts" }),
...provideAnchorFeature([{ name: "Contact", schema: ContactSchema }]),
],
});Capacitor SQLite on a device, IndexedDB under ionic serve and in a PWA. Every Ionic app needs
that switch, because the Capacitor plugin's web build requires jeep-sqlite plus a WASM asset a plain
ionic serve does not have — so choosing wrong fails only in the browser, or only on a device.
ionicAdapter(name) is exported separately if you want the adapter without the providers, and an
explicit adapter in the config still wins, so tests can pass a MemoryAdapter.
In a Capacitor app
Capacitor links native plugins only from your app's own package.json. @capacitor-community/sqlite
arrives through anchordb-angular, so it is installed — but npx cap sync leaves it out of the
Android and iOS projects. List it once:
npm install @capacitor-community/sqlite
npx cap syncKeep the plugin on the major that matches your @capacitor/core: 6.x for Capacitor 6, 7.x for 7,
8.x for 8. anchordb-angular declares both with wide ranges (>=6) so npm reuses your app's
versions instead of nesting copies — two @capacitor/core instances do not see each other's plugins.
Skip the step and provideAnchorIonic throws on the device, naming those two commands. It does not
fall back to IndexedDB: that would keep your data in a store the app abandons the moment the plugin
is linked, which looks exactly like data loss.
NgModule apps
@NgModule({
providers: [
...AnchorModule.forRoot({ name: "my-app" }).providers,
...AnchorModule.forFeature([{ name: "User", schema: UserSchema }]).providers,
...provideAnchorService(),
],
})
export class AppModule {}
@Injectable({ providedIn: "root" })
export class UserService {
readonly users$: AnchorObservable<User[]>;
readonly status$: ReturnType<AnchorService["syncStatus$"]>;
constructor(@Inject("AnchorService") anchor: AnchorService) {
this.users$ = anchor.find$<User>("User", { age: { $gt: 18 } }, { sort: "-createdAt" });
this.status$ = anchor.syncStatus$();
}
}Providers use string tokens — "AnchorService", ANCHOR_DB, getModelToken(name) — because this
package never imports Angular, so it cannot create an InjectionToken. Inject them with @Inject.
forRoot / forFeature / getModelToken mirror @nestjs/mongoose deliberately: a developer
moving between a NestJS backend and an Ionic app writes the same wiring in both places.
Observables
find$, findOne$, count$, aggregate$, syncStatus$ and on$ return an AnchorObservable,
which satisfies the RxJS contract structurally — including the Symbol.observable interop hook — so
from(...), the async pipe, toSignal() and every RxJS operator work on it.
Why Angular is not imported
@angular/core and rxjs are optional peer dependencies and are never imported. Two reasons:
the package must install cleanly for React Native users who will never touch Angular, and a copy of
Angular compiled into a library can disagree with the app's own — the classic "two copies of
@angular/core" failure.
Providers are therefore plain objects that your app passes to its own NgModule or
bootstrapApplication, using its own Angular.
Storage on Ionic
| Target | Adapter |
| --- | --- |
| ionic serve, PWA | anchordb/storage/indexeddb |
| iOS / Android native | anchordb/storage/capacitor-sqlite |
Status
1.2.0. @angular/core and rxjs stay optional peer dependencies and are never imported by
this package.
The Capacitor adapter has never run on a device — this machine has no simulator. The platform
switch in ionicAdapter, including the unlinked-plugin error, is unit-tested, and the IndexedDB side
is covered by the shared storage conformance suite; what is unverified is the native driver
plumbing. Test a create-read-restart cycle on a device early.
MIT.
The AnchorDB family
Six packages. Only anchordb is required — the rest exist so that an offline-only app never
has to download Express, and an Express server never has to download React.
| Package | What it is | Runs where |
| --- | --- | --- |
| anchordb | The database — schema, models, queries, aggregation, optional sync | phone · browser · Node |
| anchordb-react | React and React Native hooks | the device |
| anchordb-angular (this package) | Angular / Ionic module, DI and RxJS observables | the device |
| anchordb-sync-server | Server half of sync — Express, NestJS, Next.js | your backend |
| anchordb-relay | Dev relay for the Anchor Lens inspector | your laptop |
| anchordb-lens-link | Open a QA build's database in Anchor Lens on the same phone, from a file | the device, in QA builds |
Each one needs a different third-party framework as a peer dependency, and npm resolves those per package rather than per import — which is why they are not one package. Full reasoning and API reference: github.com/knnadeera/anchordb.
Bugs, support and feedback
Report a bug, ask for help or suggest a feature on the AnchorDB project page —
choose anchordb-angular as the package, and the reply comes by email.
Include the version (npm ls anchordb), where it runs, the smallest snippet that reproduces it, and
the full error.
