Skip to main content

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โ€‹

FieldTypeNotes
namestring()e.g. Central Warehouse
codestring()indexed unique
addressoptional(string())
createdAt / updatedAtspread from createUpdateAt

Relationsโ€‹

RelationTargetTypeBack-reference
unitunitsingle (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:

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โ€‹

ErrorCauseFix
E11000 duplicate key error collection: advancedTutorial.storescode already usedpick 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.