Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "ferrydb", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
209
README.md
@@ -1,4 +0,4 @@ | ||
# ferrydb | ||
# FerryDB | ||
`ferrydb` is a lightweight, event-driven, and type-safe database ORM built on top of SQLite using TypeScript. It simplifies database operations by providing a schema-based model system with built-in validation and type inference. | ||
FerryDB is a simple, flexible, and powerful SQLite-based ORM for Node.js. It allows you to define schemas, validate data, and perform CRUD operations with ease. | ||
@@ -9,18 +9,23 @@ ## Table of Contents | ||
- [Usage](#usage) | ||
- [Defining a Schema](#defining-a-schema) | ||
- [Creating a Model](#creating-a-model) | ||
- [Performing CRUD Operations](#performing-crud-operations) | ||
- [API Reference](#api-reference) | ||
- [connect](#connect) | ||
- [Schema](#schema) | ||
- [Model](#model) | ||
- [ModelInstance](#modelinstance) | ||
- [model](#model) | ||
- [Examples](#examples) | ||
- [License](#license) | ||
- [Creating a New Record](#creating-a-new-record) | ||
- [Finding a Record](#finding-a-record) | ||
- [Updating a Record](#updating-a-record) | ||
- [Deleting a Record](#deleting-a-record) | ||
- [Finding a Record with Condition Function](#finding-a-record-with-condition-function) | ||
- [Updating Records with Condition Function](#updating-records-with-condition-function) | ||
- [Deleting a Record with Condition Function](#deleting-a-record-with-condition-function) | ||
- [Support](#support) | ||
## Installation | ||
To install `ferrydb`, you need to have Node.js installed. You can then use npm to install the library: | ||
To install FerryDB, run: | ||
```sh | ||
npm install ferrydb better-sqlite3 | ||
npm install ferrydb | ||
``` | ||
@@ -30,129 +35,151 @@ | ||
### Defining a Schema | ||
Here's a quick example of how to use FerryDB: | ||
First, define the schema for your data model. The schema specifies the structure and validation rules for your data. | ||
```js | ||
import { Schema, model, connect } from 'ferrydb'; | ||
```typescript | ||
import { Schema } from "ferrydb"; | ||
// Connect to the database | ||
connect('path/to/your/database.sqlite'); | ||
// Define a schema | ||
const userSchema = new Schema({ | ||
name: { type: String, required: true }, | ||
email: { type: String }, | ||
age: { type: Number }, | ||
name: { type: 'string', required: true }, | ||
age: { type: 'number', required: true } | ||
}); | ||
``` | ||
### Creating a Model | ||
// Create a model | ||
const User = model('User', userSchema); | ||
Next, create a model using the defined schema. The model represents the database table and provides methods for interacting with the data. | ||
// Create a new user | ||
const newUser = User.create({ name: 'John Doe', age: 30 }); | ||
```typescript | ||
import { model } from "ferrydb"; | ||
// Find a user | ||
const user = User.findOne({ name: 'John Doe' }); | ||
const User = model('User', userSchema); | ||
// Update a user | ||
User.update({ name: 'John Doe' }, { age: 31 }); | ||
// Delete a user | ||
User.delete({ name: 'John Doe' }); | ||
``` | ||
### Performing CRUD Operations | ||
## API Reference | ||
You can now use the model to perform CRUD (Create, Read, Update, Delete) operations. | ||
### `connect(pathToDb: string)` | ||
```typescript | ||
// Create a new user | ||
const newUser = User.create({ | ||
name: "John", | ||
email: "john@example.com", | ||
age: 30, | ||
Connect to the SQLite database. | ||
- `pathToDb`: The path to the SQLite database file. Must end with `.sqlite`. | ||
### `Schema` | ||
Create a new schema for your data model. | ||
```js | ||
const userSchema = new Schema({ | ||
name: { type: 'string', required: true }, | ||
age: { type: 'number', required: true } | ||
}); | ||
``` | ||
// Find a user by name | ||
const user = User.findOne({ name: "John" }); | ||
console.log(user); | ||
#### `validate(data: Partial<InferSchema<T>>, isCreate: boolean = false)` | ||
// Update a user's age | ||
if (user) { | ||
user.age = 31; | ||
user.save(); | ||
} | ||
Validate the data against the schema. | ||
// Delete a user | ||
if (user) { | ||
user.delete(); | ||
} | ||
- `data`: The data to validate. | ||
- `isCreate`: Whether this is a create operation. Default is `false`. | ||
### `model` | ||
Create a new data model. | ||
```js | ||
const User = model('User', userSchema); | ||
``` | ||
## API Reference | ||
#### `create(data: Partial<InferSchema<T>>): ModelInstance<InferSchema<T>>` | ||
### Schema | ||
Create a new record in the database. | ||
#### `new Schema(definition: SchemaDefinition)` | ||
- `data`: The data to create. | ||
Creates a new schema with the given definition. | ||
#### `findOne(conditions: QueryConditions<InferSchema<T>>): ModelInstance<InferSchema<T>> | null` | ||
- `definition`: An object defining the schema. Each key represents a field, and the value specifies the field's type and validation rules. | ||
Find a single record matching the conditions. | ||
### Model | ||
- `conditions`: The conditions to match. | ||
#### `model(name: string, schema: Schema)` | ||
#### `findAll(conditions: QueryConditions<InferSchema<T>>): ModelInstance<InferSchema<T>>[]` | ||
Creates a new model with the specified name and schema. | ||
Find all records matching the conditions. | ||
- `name`: The name of the model, which corresponds to the table name in the database. | ||
- `schema`: The schema defining the structure and validation rules for the model. | ||
- `conditions`: The conditions to match. | ||
### ModelInstance | ||
#### `find(): ModelInstance<InferSchema<T>>[]` | ||
An instance of a model represents a single record in the database. | ||
Find all records in the database. | ||
#### `save()` | ||
#### `update(conditions: QueryConditions<InferSchema<T>>, data: Partial<InferSchema<T>>): number` | ||
Saves the current instance to the database. If the instance already exists, it updates the record. | ||
Update records matching the conditions. | ||
#### `delete()` | ||
- `conditions`: The conditions to match. | ||
- `data`: The data to update. | ||
Deletes the current instance from the database. | ||
#### `delete(conditions: QueryConditions<InferSchema<T>>): number` | ||
Delete records matching the conditions. | ||
- `conditions`: The conditions to match. | ||
## Examples | ||
### Full Example | ||
### Creating a New Record | ||
Here's a complete example demonstrating how to use `ferrydb`: | ||
```js | ||
const newUser = User.create({ name: 'John Doe', age: 30 }); | ||
``` | ||
```typescript | ||
import { Schema, model } from "ferrydb"; | ||
### Finding a Record | ||
// Define a schema | ||
const userSchema = new Schema({ | ||
name: { type: String, required: true }, | ||
email: { type: String }, | ||
age: { type: Number }, | ||
}); | ||
```js | ||
const user = User.findOne({ name: 'John Doe' }); | ||
``` | ||
// Create a model | ||
const User = model('User', userSchema); | ||
### Updating a Record | ||
// Create a new user | ||
const newUser = User.create({ | ||
name: "John", | ||
email: "john@example.com", | ||
age: 30, | ||
}); | ||
```js | ||
User.update({ name: 'John Doe' }, { age: 31 }); | ||
``` | ||
// Find a user by name | ||
const user = User.findOne({ name: "John" }); | ||
console.log(user); | ||
### Deleting a Record | ||
// Update a user's age | ||
if (user) { | ||
user.age = 31; | ||
user.save(); | ||
} | ||
```js | ||
User.delete({ name: 'John Doe' }); | ||
``` | ||
// Delete a user | ||
if (user) { | ||
user.delete(); | ||
} | ||
### Finding a Record with Condition Function | ||
```js | ||
const user = User.findOne({ name: x => x.startsWith("John ") }); | ||
``` | ||
## License | ||
### Updating Records with Condition Function | ||
This project is licensed under the MIT License. | ||
```js | ||
User.update({ name: x => x.startsWith("John ") }, { age: 31 }); | ||
``` | ||
### Deleting a Record with Condition Function | ||
```js | ||
User.delete({ name: x => x.startsWith("John ") }); | ||
``` | ||
## Support | ||
If you need help or have questions, feel free to join our [Discord](https://discord.gg/sakora) community. | ||
## Developer | ||
FerryDB is developed and maintained by ferrymehdi. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20588
183