# Moquilang WebAssembly Module
[](https://crates.io/crates/moquilang)
[](https://docs.rs/moquilang)
[](https://opensource.org/licenses/MIT)
This is a WebAssembly module built with Rust that provides entity operations and service calls for JavaScript applications. It includes a complete user management service and SQLite database integration.
## Features
- **User Management**: Complete CRUD operations for user entities
- **Entity Operations**: Generic entity creation, retrieval, update, and deletion
- **Service Framework**: Extensible service call infrastructure
- **SQLite Integration**: Browser-based SQLite database support
- **WebAssembly Performance**: Native-like performance in the browser
## Directory structure
- `pkg/` - Contains WebAssembly build artifacts and generated JavaScript bindings
- `static/` - Contains SQLite library and other JavaScript dependencies
- `index.html` - Example HTML file demonstrating usage of the WebAssembly module
## Usage
To use the WebAssembly module in your HTML file:
```html
<script src="./static/sqlite3.js"></script>
<script type="module">
import init, { callService, createEntity, updateEntity, deleteEntity, findOne, findMany, exportDatabaseSchema } from './pkg/moquilang.js';
</script>
```
## Available Functions
### callService(name: string, parameters: object) -> Promise<object>
Calls a service with the given name and parameters. The parameters can be any JSON-serializable object. This function uses the JSON-based approach for serialization/deserialization between JavaScript and Rust, which can be more efficient in many cases.
```javascript
import { callService } from "./pkg/moquilang.js";
// Example usage
const result = await callService("userService", {
userId: 123,
action: "getProfile",
options: {
includeDetails: true,
format: "full"
}
});
// The result will contain:
// {
// service: "userService",
// status: "success",
// parameters: "{userId: 123, action: \"getProfile\", options: {...}}"
// }
```
Note: This implementation uses the JSON-based approach with `gloo-utils` rather than direct JavaScript value manipulation with `serde-wasm-bindgen`. This approach:
- Is often faster for simple data structures
- Has better compatibility with browser JSON implementations
- Supports all JSON-serializable types
- May have a slightly larger code size
### Entity Operations
The module provides the following entity operation functions:
#### createEntity(entityName: string, entityData: object) -> Promise<object>
Creates a new entity with the given name and data.
```javascript
import { createEntity } from "./pkg/moquilang.js";
// Example usage
const result = await createEntity("UserAccount", {
username: "johndoe",
userFullName: "John Doe",
emailAddress: "john@example.com",
currentPassword: "password123",
disabled: "N"
});
```
#### updateEntity(entityName: string, entityData: object) -> Promise<object>
Updates an existing entity. The entity ID must be included in the entityData.
```javascript
import { updateEntity } from "./pkg/moquilang.js";
// Example usage
const result = await updateEntity("UserAccount", {
userId: "your-user-id-here",
userFullName: "John Smith",
emailAddress: "johnsmith@example.com"
});
```
#### deleteEntity(entityName: string, entityData: object) -> Promise<object>
Deletes an entity. The entity ID must be included in the entityData.
```javascript
import { deleteEntity } from "./pkg/moquilang.js";
// Example usage
const result = await deleteEntity("UserAccount", {
userId: "your-user-id-here"
});
```
#### findOne(entityName: string, query: object) -> Promise<object>
Finds one entity matching the query.
```javascript
import { findOne } from "./pkg/moquilang.js";
// Example usage
const result = await findOne("UserAccount", {
userId: "your-user-id-here"
});
```
#### findMany(entityName: string, query: object) -> Promise<object[]>
Finds multiple entities matching the query.
```javascript
import { findMany } from "./pkg/moquilang.js";
// Example usage
const results = await findMany("UserAccount", {
"_limit": 10,
"_offset": 0,
"_orderBy": "username",
"_orderDir": "ASC"
});
```
## Building the Project
1. Build the WebAssembly module from source:
```bash
# Build the Rust WebAssembly module
cargo build --target wasm32-unknown-unknown
```
The project includes a `build.rs` script that automatically runs `wasm-bindgen` after successful compilation when targeting wasm32-unknown-unknown. The generated files will be placed in the `pkg/` directory.
For a release build:
```bash
cargo build --release --target wasm32-unknown-unknown
```
2. Start a local development server with:
```bash
python -m http.server
```
3. Go to http://localhost:8000 in your browser
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the LICENSE file for details.