Source
A Source is SoapJS's low-level data-access boundary — one per database collection/table. Adapters and repositories build on it. MongoDB's implementation is MongoSource.
In current code the interface is
Source<DocumentType>and the MongoDB class isMongoSource. (Earlier versions called this component a "collection".)
Creating a Source
import { SoapMongo, MongoSource } from '@soapjs/soap-node-mongo';
const soapMongo = await SoapMongo.create(mongoConfig);
const source = new MongoSource(soapMongo, 'characters');You rarely use a Source directly — wrap it in a DatabaseContext with a mapper and hand it to a repository:
import { DatabaseContext } from '@soapjs/soap';
const context = new DatabaseContext(source, new CharacterMapper(), soapMongo.sessions);
const repo = new CharacterRepositoryMongo(context);The Source contract
interface Source<DocumentType> {
find(query?): Promise<DocumentType[]>;
count(query?): Promise<number>;
aggregate<T>(query): Promise<T[]>;
insert(query): Promise<DocumentType[]>;
update(query): Promise<UpdateStats>;
remove(query): Promise<RemoveStats>;
native<ResultType>(query): Promise<ResultType>; // since @soapjs/soap 0.10 / soap-node-mongo 0.7
}find/countacceptFindParams/CountParams(built withWhere) or aRepositoryQuery.aggregateruns a portableAggregationParams.native()is the escape hatch for queries the abstraction can't express — it runs a client-specific query verbatim (see Using Native Queries).
Read/write split
Give each side its own connection — read Source on a replica, write Source on the primary:
See the demo for a full read/write split.
Last updated