- v0.1.13Latest
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.99
- v0.0.98
- v0.0.97
- v0.0.96
- v0.0.95
- v0.0.94
- v0.0.93
- v0.0.92
- v0.0.91
- v0.0.90
- v0.0.89
- v0.0.88
- v0.0.87
- v0.0.86
- v0.0.85
- v0.0.84
- v0.0.83
- v0.0.82
- v0.0.81
- v0.0.80
- v0.0.79
- v0.0.78
- v0.0.77
- v0.0.76
- v0.0.75
- v0.0.74
- v0.0.73
- v0.0.72
- v0.0.71
- v0.0.70
- v0.0.69
- v0.0.68
- v0.0.67
- v0.0.66
- v0.0.65
- v0.0.64
- v0.0.63
- v0.0.62
- 0.0.61
- 0.0.60
- 0.060
- 0.0.59
- 0.0.58
- 0.0.57
- 0.0.56
- 0.0.55
- 0.0.54
- 0.0.53
- 0.0.52
- 0.0.51
Why Lesan?
Even though NoSQL is very fast, its complexities are very troublesome for large-scale projects. On the other hand, GraphQL shines in client-server connectivity but it has several weaknesses and is somewhat complex, adding another layer of complexity to the project. That’s why we created LESAN.
Look below code:
Create a file called mod.ts
and paste the code below into it:
import {
ActFn,
InRelation,
lesan,
MongoClient,
number,
object,
ObjectId,
optional,
OutRelation,
size,
string,
} from "https://deno.land/x/lesan@vx.x.x/mod.ts"; // Please replace `x.x.x` with the latest version in [releases](https://github.com/MiaadTeam/lesan/releases)
const coreApp = lesan();
const client = new MongoClient();
await client.connect("mongodb://127.0.0.1:27017/");
const db = client.database("sample");
coreApp.odm.setDb(db);
// ================== MODEL SECTION ==================
// ------------------ Country Model ------------------
const countryPure = {
name: string(),
description: string(),
};
const countryInRel: Record<string, InRelation> = {};
const countryOutRel: Record<string, OutRelation> = {
users: {
schemaName: "user",
number: 50,
sort: { field: "_id", order: "desc", type: "objectId" },
},
};
const countries = coreApp.odm.setModel(
"country",
countryPure,
countryInRel,
countryOutRel,
);
// ------------------ User Model ------------------
const userPure = {
name: string(),
address: optional(string()),
age: number(),
};
const userInRel: Record<string, InRelation> = {
country: {
schemaName: "country",
type: "one",
optional: false,
},
};
const userOutRel = {};
const users = coreApp.odm.setModel("user", userPure, userInRel, userOutRel);
// ================== MODEL SECTION ==================
// ------------------ Country Founctions ------------------
const addCountryValidator = () => {
return object({
set: object(countryPure),
get: coreApp.schemas.selectStruct("country", { users: 1 }),
});
};
const addCountry: ActFn = async (body) =>
await countries.insertOne({ doc: body.details.set, get: body.details.get });
coreApp.acts.setAct({
type: "dynamic",
schema: "country",
actName: "addCountry",
validator: addCountryValidator(),
fn: addCountry,
});
// ------------------ User Founctions ------------------
const addUserValidator = () => {
return object({
set: object({ ...userPure, country: string() }),
get: coreApp.schemas.selectStruct("user", 1),
});
};
const addUser: ActFn = async (body) => {
const { country, ...rest } = body.details.set;
return await users.insertOne({
doc: rest,
get: body.details.get,
relation: { country: new ObjectId(country) },
});
};
coreApp.acts.setAct({
type: "dynamic",
schema: "user",
actName: "addUser",
validator: addUserValidator(),
fn: addUser,
});
const getUsersValidator = () => {
return object({
set: object({
page: number(),
take: number(),
countryId: optional(size(string(), 24)),
}),
get: coreApp.schemas.selectStruct("user", { country: 1 }),
});
};
const getUsers: ActFn = async (body) => {
const {
set: {
page,
take,
countryId,
},
get,
} = body.details;
const pipline = [];
pipline.push({ $limit: take });
pipline.push({ $skip: (page - 1) * take });
countryId &&
pipline.push({ $match: { "country._id": new ObjectId(countryId) } });
return await users.aggregation({
pipline,
get,
});
};
coreApp.acts.setAct({
type: "dynamic",
schema: "user",
actName: "getUsers",
validator: getUsersValidator(),
fn: getUsers,
});
coreApp.runServer({ port: 8080, typeGeneration: false, playground: true });
Please replace
x.x.x
in the import link with the latest version in releases
Now run this command in the terminal:
deno run -A mod.ts
You should see this messsage:
HTTP webserver running.
please send a post request to http://localhost:8080/lesan
you can visit playground on http://localhost:8080/playground
Listening on http://localhost:8080/
Now you can visit the playground at http://localhost:8080/playground
and send requests to the server for addCountry
, addUser
, and getUsers
.
alternativly you can send post request to http://localhost:8080/lesan
with postman
include the following in JSON format inside the body in order to retrieve the desired data:
{
"contents": "dynamic",
"wants": {
"model": "country",
"act": "addCountry"
},
"details": {
"set": {
"name": "Iran",
"description": "A beautiful and civilized country"
},
"get": {
"_id": 1,
"name": 1,
"description": 1,
"users": {
"_id": 1,
"name": 1,
"address": 1,
"age": 1
}
}
}
}
We handle all relationships between the data and embed
everything. You can also control the level of penetration into the action get
depth. On the client-side
, you can describe what you want and get back exactly what you described.