Store
store is a physical storage place โ a warehouse or a shelf. Inventory is tracked per store, and stores belong to a unit (typically the Warehouse-type unit that owns them).
// models/store.ts (definition, trimmed of the doc comment)
import { coreApp } from "../mod.ts";
import {
optional, type RelationDataType, type RelationSortOrderType, string,
} from "lesan";
import { createUpdateAt } from "@lib";
import { unit_excludes } from "./excludes.ts";
export const store_pure = {
name: string(),
code: string(),
address: optional(string()),
...createUpdateAt,
};
export const store_relations = {
unit: {
schemaName: "unit",
type: "single" as RelationDataType,
optional: true,
excludes: unit_excludes,
relatedRelations: {
stores: { type: "multiple" as RelationDataType, limit: 50, sort: { field: "_id", order: "desc" as RelationSortOrderType } },
},
},
};
Pure fieldsโ
| Field | Type | Notes |
|---|---|---|
name | string() | e.g. Central Warehouse |
code | string() | indexed unique |
address | optional(string()) | |
createdAt / updatedAt | spread from createUpdateAt |
Relationsโ
| Relation | Target | Type | Back-reference |
|---|---|---|---|
unit | unit | single (optional) | unit.stores |
Factoryโ
export const stores = () =>
coreApp.odm.newModel("store", store_pure, store_relations, {
createIndex: {
indexSpec: { code: 1 },
options: { unique: true },
},
});
In the workflowโ
The store is the inventory boundary:
- inventory model โ one record per
store+product - stockMovement model โ the ledger recording every change to a store's stock
- add-stock, remove-stock, transfer-stock
Catalog acts: add-store, get-stores.
Run itโ
curl -X POST http://localhost:1380/lesan \
-H "Content-Type: application/json" \
-H "token: <jwt>" \
-d '{
"service": "main",
"model": "store",
"act": "getStores",
"details": {
"set": { "page": 1 },
"get": { "name": true, "code": true, "unit": { "name": true } }
}
}'
Errors & fixesโ
| Error | Cause | Fix |
|---|---|---|
E11000 duplicate key error collection: advancedTutorial.stores | code already used | pick a different code |
note
Runtime
On npm/Bun import the framework from @hemedani/lesan; on Deno from jsr:@hemedani/lesan. The repo app itself uses the lesan path alias.