thank you for use
- pyniedb
- connect(dbName: string, options: { path: string })
- Model(modelName: string, schema: SchemaObject)
- Schema(schemaData: object)
- Models
- Model.create(data: object)
- Model.findAll()
- Model.find(param: string, value: any)
- Model.delete(param: string, value: any)
-
setting up
const { default: pyniedb } = require('pyniedb'); pyniedb.connect('pyniedb_usage', { path: `${__dirname}/database` // your preferred path }); module.exports = pyniedb;
-
creating model
const { Schema } = require('pyniedb'); const pyniedb = require('../config/database'); const UserSchema = new Schema({ name: { type: 'string', unique: false, }, email: { type: 'string', unique: true, }, active: { type: 'boolean', default: false, // setting 'default' and then the value unique: false, } }); const User = pyniedb.Model('User', UserSchema); module.exports = User;
-
consuming Model by controller
const User = require('../models/User'); // the created model module.exports = { index: async (req, res) => { // list all Users const users = await User.findAll(); res.json(users); }, create: async (req, res) => { // create User const user = await User.create(req.body); res.status(201).json(user); }, }
-
adding Relation on Schema
Address: { type: 'number', relation: { modelName: 'Addresses' }, }
create the Address Model (like item 2.) and then add data to it.
Address.create({ city: 'LAS VEGAS' });
then just create an user with new Relation
User.create({ name: 'Test User', email: "test@example.com", Address: 1 });
-
fetching User results:
{ "id": 1, "name": "Test User", "email": "test@example.com", "Address": { "id": 1, "city": "LAS VEGAS" }, "active": true }
-
Adding name in Schema relation fetch:
address_id: { type: 'number', relation: { name: 'address', modelName: 'Addresses' }, }
User.create({ name: 'Test User', email: "test@example.com", address_id: 1 });
{ "id": 1, "name": "Test User", "email": "test@example.com", "address": { "id": 1, "city": "LAS VEGAS" }, // name "address" on schema "active": true }