https://github.com/dwdw2/parser-orm
Small custom-syntax javascript ORM
https://github.com/dwdw2/parser-orm
javascript parser recursive-descent-parser sql-generator
Last synced: 5 months ago
JSON representation
Small custom-syntax javascript ORM
- Host: GitHub
- URL: https://github.com/dwdw2/parser-orm
- Owner: DWDW2
- Created: 2025-01-09T17:40:48.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-08T17:20:53.000Z (11 months ago)
- Last Synced: 2025-03-16T01:26:18.795Z (10 months ago)
- Topics: javascript, parser, recursive-descent-parser, sql-generator
- Language: JavaScript
- Homepage:
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# parser-orm
This is a project to genreate create sql schemas using custom defined syntax and
use javascript functions to query database.
## Installation
To install the dependencies, run:
```bash
npm install
```
## Usage
To generate SQL schemas, use the following command:
```bash
orm init
```
To query the database, use the provided JavaScript functions. Here is an
example:
```javascript
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { Parser } from "../orm/parser.js";
import { OrmClient } from "../orm/client.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const syntaxFilePath = path.resolve(__dirname, "schemas/schema.orm");
const syntax = fs.readFileSync(syntaxFilePath, "utf-8");
const parser = new Parser();
const ast = parser.parse(syntax);
const dbFilePath = path.resolve(__dirname, "database.sqlite");
const config = {
filename: dbFilePath,
};
const client = new OrmClient(config, ast);
(async () => {
await client.connect();
try {
const newUser = await client.users.query({
select: ["id", "username", "email", "created_at"],
});
console.log("Inserted user:", newUser);
} catch (error) {
if (error.code === "SQLITE_CONSTRAINT") {
console.error("Error inserting user: Email must be unique");
} else {
console.error("Error inserting user:", error);
}
}
await client.disconnect();
})();
```
## Contributing
Contributions are welcome! Please open an issue or submit a pull request.
## License
This project is licensed under the MIT License.