1.0.4 • Published 4 years ago

@topmodel/sql v1.0.4

Weekly downloads
-
License
WTFPL
Repository
github
Last release
4 years ago

@topmodel/sql

SQL connector (compatible with topmodel models)

Note: this plugin is a wrapper around Knex.js

Install

With npm

npm install @topmodel/sql

or yarn

yarn add @topmodel/sql

Then add one of the following, depending on your install

npm install pg
npm install sqlite3
npm install mysql
npm install mysql2
npm install oracledb
npm install mssql

For the following documentation, we will use Postgresql (pg) as an example

Usage

Setup your SQL connector Plugin and pass it through the model options:

import { Model } from '@topmodel/core'
import { SQLPlugin } from '@topmodel/sql'

const { 
    PSQL_HOST, 
    PSQL_USER, 
    PSQL_PASSWORD, 
    PSQL_DB 
} = process.env

const pg = new SQLPlugin({
  client: 'pg',
  host: PSQL_HOST,
  user: PSQL_USER,
  password: PSQL_PASSWORD,
  database: PSQL_DB
})

const options = { db: pg }

class User extends Model {
    constructor(data){
        super(data, options)
    }
}

// User class is now ready to interact with PSQL !

You can then use your User model and persist its data within Postgresql (or any other Knex compatible database)

// example : 

const user = new User({ email: 'foo@bar.co' })
const saved = await user.save()

console.log(saved) // saved user in psql

Table

Note: by default topmodel core will attribute a table name related to your model name, for example model User will become user table in SQL.

If you want to force the collection, please refer to the core model option table here

API

Topmodel db connectors has built-in methods extended to your models :

MethodReturnComments
save()Promise(Model)create or update if data.id or `data._id is specified
read()Promise(Model)will retrieve the data in the database (will require id)
del()Promise(Model)will delete the data in the database (will require id)

save

Create or update your model in psql

// create
const user = new User({ email: 'foo@bar.co' })

const saved = await user.save()
console.log(saved.body) 

// or update

const user = new User({ 
    id: 'b9f8c112-0750-42eb-a146-a81be4c1df64', 
    email: 'update@foo.co' 
})
const updated = await user.save()
console.log(updated.body)

read

Read data from the database and populate the Model

const user = new User({ 
    id: 'b9f8c112-0750-42eb-a146-a81be4c1df64' 
})
await user.read()

console.log(user.body) 
// { id: 'b9f8c112-0750-42eb-a146-a81be4c1df64' , email: 'foo@bar.co' }

del

Delete data in the database

const user = new User({ id: 'b9f8c112-0750-42eb-a146-a81be4c1df64' })
const deleted = await user.del()

console.log(deleted) // true