From e58a95b9d8c85724fa66f9bdaf7cf254dff8840a Mon Sep 17 00:00:00 2001 From: Maciej Kowalczyk Date: Mon, 22 Oct 2018 12:57:26 +0200 Subject: [PATCH 1/4] chore(): database documentation update --- docs/cheatsheet/web/index.html | 40 +- docs/docs/project/database.md | 762 +++++++++++++++++++++++++++++++-- 2 files changed, 748 insertions(+), 54 deletions(-) diff --git a/docs/cheatsheet/web/index.html b/docs/cheatsheet/web/index.html index d7341de2..982e2869 100644 --- a/docs/cheatsheet/web/index.html +++ b/docs/cheatsheet/web/index.html @@ -21,7 +21,7 @@ Client
- Server + Core
CLI @@ -184,7 +184,7 @@

Usage

Response

Send response from socket.

Import

-
const {response} = new Server(ctx)
+
const {response} = new Syncano(ctx)

API

response(content, status?, mimetype, headers?)
 response.header(key, value)
@@ -212,7 +212,7 @@ 

JSON response

Logger

console.log for your sockets.

Import

-
const {logger} = new Server(ctx)
+
const {logger} = new Syncano(ctx)

Usage

const log = logger('Socket scope')
 
@@ -232,7 +232,7 @@ 

Listen for events

Events

Publish global events to which other sockets can subscribe.

Import

-
const {event} = new Server(ctx)
+
const {event} = new Syncano(ctx)

Emiting event

event.emit('email_sent', {to: 'mail@example.com'})

Catching event

@@ -254,7 +254,7 @@

Catching event

Sockets

Call other socket enpoints.

Import

-
const {socket} = new Server(ctx)
+
const {socket} = new Syncano(ctx)

Usage

socket.get('tags/list', {sort: 'latest'})
 socket.post('tags/create', {name: 'nature'})
@@ -267,7 +267,7 @@ 

Usage

Channels

Send messages to realtime channels.

Import

-
const {channel} = new Server(ctx)
+
const {channel} = new Syncano(ctx)

Publishing to public channel

endpoints:
   messages:
@@ -291,7 +291,7 @@ 

Publishing to user private channel

Data & Users

Create, update, delete, list users and class object.

Import

-
const {data, users} = new Server(ctx)
+
const {data, users} = new Syncano(ctx)

API

Users and Data share all methods.

users.method()
@@ -440,11 +440,16 @@ 

Filtering

['status', 'published'] ]).list() - // Get posts with views count lower than 100 or higher than 1000 - data.posts - .where('views', '<', 100) - .orWhere('views', '>', 1000) - .list()
+// Get posts with views count lower than 100 or higher than 1000 +data.posts + .where('views', '<', 100) + .orWhere('views', '>', 1000) + .list()
+
+ +

'icontains' operator works like %test% in sql:
+ .where('title', 'icontains', 'test')

+

Ordering, Limit, & Offset

// Get all posts ordered descending by creation date
 data.posts.orderBy('created_at', 'DESC').list()
@@ -592,15 +597,8 @@ 

Synchronize files of given hosting

Configure hosting options

npx s hosting config <hosting name> --cname <domain name>
-npx s hosting config <hosting name> --remove-cname <domain name>
-
-# optional flags:
--c, --cname <domain_name> add CNAME to hosting
--d, --remove-cname <domain_name> removes CNAME from hosting
--b, --browser-router <true|false> turn on/off the BrowserRouter support
--h, --help  output usage information
- - +s hosting config <hosting name> --remove-cname <domain name>
+

Other Creation Methods

-
// Get first tag with name 'ship' or create it
+      
// Get first book with title 'Godfather' or create it
 // Second param holds additional data assigned to object
-data.tags.firstOrCreate({name: 'ship'}, {created_by: 1})
-
// Get first tag with name 'ship' or create it
+await data.book.firstOrCreate({title: 'Godfather'}, {created_by: 1})
+
// Get first book with name 'Godfather' or create it
 // Second param holds additional data assigned to object
-data.tags.updateOrCreate({name: 'ship'}, {created_by: 1})
+await data.book.updateOrCreate({title: 'Godfather'}, {created_by: 1})

Deleting Class Object

// Delete all posts
-data.posts.delete()
+await data.book.delete()
 
-// Delete signle post with given ID
-data.posts.delete(55)
+// Delete signle book with given ID
+await data.book.delete(ctx.args.id)
 
-// Delete multiple posts
-data.posts.delete([55, 56, 57])
+// Delete multiple books
+await data.book.delete([55, 56, 57])
 
-// Delee all posts where draft column is equal 1
-data.posts.where('draft', 1).delete()
+// Delete all books where author id is equal to 15 +await data.book.where('author', 15).delete()

Retrieving Objects

// Get all objects from class
-data.posts.list()
+await data.book.list()
 
-// Get all objects with status equal published
-data.posts.where('status', 'published').list()
+// Get all objects with author equal Mario Puzo
+await data.book.where('author', 'Mario Puzo').list()
 
 // Get object by ID
-data.posts.find(1)
+await data.book.find(1)
 
 // Get first object matching query
-data.posts.where('status', 'draft').first()
+await data.book.where('category', 'Crime').first()
 
 // Get object with given IDs
-data.posts.find([4, 5, 10])
+await data.book.find([4, 5, 10])
 
 // Get object with given ID or throw error
-data.posts.findOrFail(15)
+await data.book.findOrFail(15)
 
-// Get first object with views > 100 or throw error
-data.posts.where('views', '>', 100).firstOrFail())
+// Get first object with pages > 100 or throw error +await data.book.where('pages', '>', 100).firstOrFail())

Retrieving Related Objects

-
// Author column is reference to user class
-// It will be replaced with given author object
-data.posts.with('author').list()
-
-// You can load many references, relations at once
-// Comments column will be replaced with list of related comments
-data.posts.with(['author', 'comments']).list()
+
// Categories column ids will be replaced with objects from targeted class (category)
+await data.book.with('categories').list()

Filtering

-
// Get all posts with views = 100
-data.posts.where('views', '=', 100).list()
+      
// Get all books with pages = 500
+await data.book.where('pages', '=', 500).list()
 
 // You can ommit second param to simply check if value is equal
-data.posts.where('views', 100).list()
+await data.book.where('pages', 500).list()
 
-// Get all post with views greater than 100
+// Get all books with pages greater than 100
 // Other selectors: <, <=, >, >=, =, !=
-data.posts.where('views', '>', 200).list()
+await data.book.where('pages', '>', 100).list()
 
-// Get all posts with one of the statuses
-data.posts.where('status', 'in', ['draft', 'published']).list()
-data.posts.whereIn('status', ['draft', 'published']).list()
+// Get all books with one of the statuses
+await data.book.where('status', 'in', ['draft', 'published']).list()
+await data.book.whereIn('status', ['draft', 'published']).list()
 
-// Get all posts with id not in given array
-data.posts.where('id', 'nin', [10, 20]).list()
-data.posts.whereNotIn('id', [10, 20]).list()
+// Get all books with id not in given array
+await data.book.where('id', 'nin', [10, 20]).list()
+await data.book.whereNotIn('id', [10, 20]).list()
 
-// Get all posts with title
-data.posts.whereNotNull('title').list()
+// Get all books with title
+await data.book.whereNotNull('title').list()
 
-// Get all posts without title
-data.posts.whereNull('title')
+// Get all books without title
+await data.book.whereNull('title')
 
-// Get all posts with views between 100 and 1000
-data.posts.whereBetween('views', 100, 1000)
+// Get all books with pages between 200 and 400
+await data.book.whereBetween('pages', 200, 400)
 
-// Get all published posts with id between 100 and 200
-data.posts
+// Get all published books with id between 100 and 200
+await data.book
   .where([
-    ['id', 'gt', 100],
-    ['id', 'lt', 200]
+    ['id', '>', 100],
+    ['id', '<', 200]
     ['status', 'published']
   ]).list()
 
-// Get posts with views count lower than 100 or higher than 1000
-data.posts
-  .where('views', '<', 100)
-  .orWhere('views', '>', 1000)
+// Get books with number of pages lower than 100 or higher than 300
+await data.book
+  .where('pages', '<', 100)
+  .orWhere('pages', '>', 300)
   .list()
@@ -451,27 +455,27 @@

Filtering

.where('title', 'icontains', 'test')

Ordering, Limit, & Offset

-
// Get all posts ordered descending by creation date
-data.posts.orderBy('created_at', 'DESC').list()
+      
// Get all books ordered descending by creation date
+await data.book.orderBy('created_at', 'DESC').list()
 
-// Get all posts ordered ascending by creation date
-data.posts.orderBy('created_at', 'ASC').list()
-
// Skip 5 posts and then get next 10 posts
-data.posts.skip(5).take(10).list()
+// Get all books ordered ascending by creation date +await data.book.orderBy('created_at', 'ASC').list()
+
// Skip 5 books and then get next 10 books
+await data.book.skip(5).take(10).list()

Helper methods

// Get only selected columns
-data.posts
-  .fields('id', 'title', 'user.first_name as author')
+await data.book
+  .fields('id', 'title', 'user.username as author')
   .list()
 
 // Get values of given column
-data.posts.pluck('title')
+await data.book.pluck('title')
 
 // Get single object key
-data.posts.where('id', 10).value('title')
+await data.book.where('id', 10).value('title')
 
 // Get number of objects matching query
-data.posts.where('id', '>', 10).count()
+await data.book.where('id', '>', 10).count()