- 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,
Document,
Filter,
lesan,
MongoClient,
number,
object,
ObjectId,
optional,
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 = await new MongoClient("mongodb://127.0.0.1:27017/").connect();
const db = client.db("civil");
coreApp.odm.setDb(db);
// ================== MODEL SECTION ==================
// ------------------ Country Model ------------------
const countryPure = {
name: string(),
population: number(),
abb: string(),
};
const countryRelations = {};
const countries = coreApp.odm.newModel(
"country",
countryPure,
countryRelations,
);
// ------------------ User Model ------------------
const userPure = {
name: string(),
age: number(),
};
const users = coreApp.odm.newModel("user", userPure, {
country: {
optional: false,
schemaName: "country",
type: "single",
relatedRelations: {
users: {
type: "multiple",
limit: 50,
sort: {
field: "_id",
order: "desc",
},
},
},
},
});
// ================== FUNCTIONS SECTION ==================
// ------------------ Country Founctions ------------------
// ------------------ Add Country ------------------
const addCountryValidator = () => {
return object({
set: object(countryPure),
get: coreApp.schemas.selectStruct("country", { users: 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({
page: number(),
limit: number(),
}),
get: coreApp.schemas.selectStruct("country", 1),
});
};
const getCountries: ActFn = async (body) => {
let {
set: { page, limit },
get,
} = body.details;
page = page || 1;
limit = limit || 50;
const skip = limit * (page - 1);
return await countries
.find({ projection: get, filters: {} })
.skip(skip)
.limit(limit)
.toArray();
};
coreApp.acts.setAct({
schema: "country",
actName: "getCountries",
validator: getCountriesValidator(),
fn: getCountries,
});
// ------------------ User Founctions ------------------
// --------------------- Add User ----------------------
const addUserValidator = () => {
return object({
set: object({
...userPure,
country: string(),
}),
get: coreApp.schemas.selectStruct("user", 1),
});
};
const addUser: ActFn = async (body) => {
const { country, name, age } = body.details.set;
return await users.insertOne({
doc: { name, age },
projection: body.details.get,
relations: {
country: {
_ids: new ObjectId(country),
relatedRelations: {
users: true,
},
},
},
});
};
coreApp.acts.setAct({
schema: "user",
actName: "addUser",
validator: addUserValidator(),
fn: addUser,
});
// --------------------- Get Users ----------------------
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) => {
let {
set: {
page,
limit,
countryId,
},
get,
} = body.details;
page = page || 1;
limit = limit || 50;
const skip = limit * (page - 1);
const filters: Filter<Document> = {};
countryId && (filters["country._id"] = new ObjectId(countryId));
return await users
.find({ projection: get, filters })
.skip(skip)
.limit(limit)
.toArray();
};
coreApp.acts.setAct({
schema: "user",
actName: "getUsers",
validator: getUsersValidator(),
fn: getUsers,
});
// ================== RUM SECTION ==================
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:
{
"service": "main",
"model": "country",
"act": "addCountry",
"details": {
"set": {
"name": "Iran",
"population": 8500000000,
"abb": "IR"
},
"get": {
"_id": 1,
"name": 1,
"population": 1,
"abb": 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.