Skip to content

Commit

Permalink
Merge pull request #159 from hemedani/main
Browse files Browse the repository at this point in the history
implement exclude option
  • Loading branch information
hemedani authored May 16, 2024
2 parents 0e3b486 + 0ea8496 commit 85dc0d4
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 45 deletions.
354 changes: 354 additions & 0 deletions examples/whyLesan/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
import {
ActFn,
lesan,
MongoClient,
number,
object,
ObjectId,
objectIdValidation,
RelationDataType,
RelationSortOrderType,
string,
} from "../../mod.ts";

const coreApp = lesan();

const client = await new MongoClient("mongodb://127.0.0.1:27017/").connect();

const db = client.db("performance");

coreApp.odm.setDb(db);

// ================== MODEL SECTION ==================
// ------------------ Country Model ------------------
const pure = {
name: string(),
population: number(),
abb: string(),
};
const countryRelations = {};
const countries = coreApp.odm.newModel(
"country",
pure,
countryRelations,
);

// ------------------ Province Model ------------------
const provinceRelations = {
country: {
optional: false,
schemaName: "country",
type: "single" as RelationDataType,
relatedRelations: {
provinces: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "_id",
order: "desc" as RelationSortOrderType,
},
},
provincesByPopulation: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "population",
order: "desc" as RelationSortOrderType,
},
},
},
},
};

const provinces = coreApp.odm.newModel(
"province",
pure,
provinceRelations,
);

// ------------------ City Model ------------------
const cityRelations = {
country: {
optional: false,
schemaName: "country",
type: "single" as RelationDataType,
relatedRelations: {
cities: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "_id",
order: "desc" as RelationSortOrderType,
},
},
citiesByPopulation: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "population",
order: "desc" as RelationSortOrderType,
},
},
},
},
province: {
optional: false,
schemaName: "province",
type: "single" as RelationDataType,
relatedRelations: {
cities: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "_id",
order: "desc" as RelationSortOrderType,
},
},
citiesByPopulation: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "population",
order: "desc" as RelationSortOrderType,
},
},
},
},
};

const cities = coreApp.odm.newModel(
"city",
pure,
cityRelations,
);

// ------------------ User Model ------------------
const userPure = {
name: string(),
family: string(),
age: number(),
};

const userRelations = {
country: {
optional: false,
schemaName: "country",
type: "single" as RelationDataType,
relatedRelations: {
users: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "_id",
order: "desc" as RelationSortOrderType,
},
},
usersByAge: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "age",
order: "desc" as RelationSortOrderType,
},
},
},
},
province: {
optional: false,
schemaName: "province",
type: "single" as RelationDataType,
relatedRelations: {
users: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "_id",
order: "desc" as RelationSortOrderType,
},
},
usersByAge: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "age",
order: "desc" as RelationSortOrderType,
},
},
},
},
city: {
optional: false,
schemaName: "city",
type: "single" as RelationDataType,
relatedRelations: {
users: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "_id",
order: "desc" as RelationSortOrderType,
},
},
usersByAge: {
type: "multiple" as RelationDataType,
limit: 50,
sort: {
field: "age",
order: "desc" as RelationSortOrderType,
},
},
},
},
};

const users = coreApp.odm.newModel(
"user",
userPure,
userRelations,
);

// ================== FUNCTIONS SECTION ==================
// ------------------ Country Founctions ------------------
// ------------------ Add Country ------------------
const addCountryValidator = () => {
return object({
set: object(pure),
get: coreApp.schemas.selectStruct("country", 1),
});
};

const addCountry: ActFn = async (body) => {
const { name, population, abb } = body.details.set;
return await countries.insertOne({
doc: {
name,
population,
abb,
},
projection: body.details.get,
});
};

coreApp.acts.setAct({
schema: "country",
actName: "addCountry",
validator: addCountryValidator(),
fn: addCountry,
});

// ------------------ get Countries ------------------
const getCountriesValidator = () => {
return object({
set: object({}),
get: coreApp.schemas.selectStruct("country", 2),
});
};
const getCountries: ActFn = async (body) => {
const {
set,
get,
} = body.details;

return await countries
.aggregation({
pipeline: [],
projection: get,
})
.toArray();
};

coreApp.acts.setAct({
schema: "country",
actName: "getCountries",
validator: getCountriesValidator(),
fn: getCountries,
});

// ------------------ Province Founctions ------------------
// ------------------ Add Propvince ------------------
const addProvinceValidator = () => {
return object({
set: object({
...pure,
countryId: objectIdValidation,
}),
get: coreApp.schemas.selectStruct("province", 1),
});
};

const addProvince: ActFn = async (body) => {
const { name, population, abb, countryId } = body.details.set;
return await provinces.insertOne({
doc: {
name,
population,
abb,
},
relations: {
country: {
_ids: new ObjectId(countryId),
relatedRelations: {
provinces: true,
provincesByPopulation: true,
},
},
},
projection: body.details.get,
});
};

coreApp.acts.setAct({
schema: "province",
actName: "addProvince",
validator: addProvinceValidator(),
fn: addProvince,
});

// ------------------ City Founctions ------------------
// ------------------ Add City ------------------
const addCityValidator = () => {
return object({
set: object({
...pure,
countryId: objectIdValidation,
provinceId: objectIdValidation,
}),
get: coreApp.schemas.selectStruct("city", 1),
});
};

const addCity: ActFn = async (body) => {
const { name, population, abb, countryId, provinceId } = body.details.set;

return await cities.insertOne({
doc: {
name,
population,
abb,
},
relations: {
country: {
_ids: new ObjectId(countryId),
relatedRelations: {
cities: true,
citiesByPopulation: true,
},
},
province: {
_ids: new ObjectId(provinceId),
relatedRelations: {
cities: true,
citiesByPopulation: true,
},
},
},
projection: body.details.get,
});
};

coreApp.acts.setAct({
schema: "city",
actName: "addCity",
validator: addCityValidator(),
fn: addCity,
});
3 changes: 2 additions & 1 deletion src/models/pure/getPureModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { throwError } from "../../utils/throwError.ts";
import { getPureSchema } from "../mod.ts";
import { schemaFns, TSchemas } from "../mod.ts";

/**
Expand All @@ -8,6 +9,6 @@ import { schemaFns, TSchemas } from "../mod.ts";
export const getPureModel = (schemasObj: TSchemas, name: string) => {
const schemas = schemaFns(schemasObj).getSchemas();
return schemas[name]
? schemas[name].pure
? getPureSchema(schemas, name)
: throwError(`Schema ${name} is not exist in the Schema Object`);
};
5 changes: 2 additions & 3 deletions src/models/schema/createStruct.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assign, object } from "../../npmDeps.ts";
import { createEmbedded } from "./createEmbedded.ts";
import { getSchema } from "./getSchema.ts";
import { TSchemas } from "./mod.ts";
import { getPureSchema, TSchemas } from "./mod.ts";

/**
* create struct features, struct feature is used for create client of db.
Expand Down Expand Up @@ -29,9 +29,8 @@ import { TSchemas } from "./mod.ts";
* ),
*/
export const createStruct = (schemas: TSchemas, schemaName: string) => {
const schema = getSchema(schemas, schemaName);
return assign(
object(schema.pure),
object(getPureSchema(schemas, schemaName)),
object(createEmbedded(schemas, schemaName)),
);
};
Loading

0 comments on commit 85dc0d4

Please sign in to comment.