SQL-next is a wrapper around node-mysql, which provides MongoDB like queries and promise-based api.
Features
- JSON queries
- Protection from sql-injection
- Selectors (not yet done)
- Promise based api
Checkout roadmap to see what's coming.
Installing
$ npm install sql-next
Quick start
An example of finding an item:
; ; init;
Output:
_id: 2 header: 'Hello world!' body: 'Lorem ipsum...'
API
Class Client
:
Class Database
:
Class Table
:
Interfaces:
Other
Client
Class Methods
-
Client.connect(config: string | IConfig)
Connects to mysql server.;const client = ;tryawait client;console;catch errorconsole;
-
Client.close()
Closes connection.await client;console;
-
Client.db(name: string): Database
Returns a new database instance that shares the same connection withClient
.const client = ;const db = client;
-
Client.switchUser(config: ConnectionOptions)
Reconnects with new user credentials.const client = ;client;
-
Client.query<T>(sql: string): Promise<T>
Performs a raw query globally.const client = ;const news = await client;
Database
Class Methods
-
Database.tables(): Promise<string[]>
Returns a list of tables in a database.;const client = ;const db = client;const tables = await db;console; // ['users', 'news', ...]
-
Database.table<T>(name: string): Table<T>
Returns a new table instance that shares the same connection withClient
.;const client = ;const db = client;const table = db;const news = await table;console; // [{_id: 1, title: 'lorem ipsum'}, ...]
Properties
Database.name
Table<T>
Class Methods
-
Table.find(filter?: IQueryFilter<T>, options?: IQueryOptions): Promise<T[]>
Fetches multiple items from a table. You can also set an offset or a limit, by settingoptions
. See todo for advanced filtering.const table = db;const news = await table;
-
Table.findOne(filter?: IQueryFilter<T>): Promise<T[]>
Returns a single item from a table. See todo for advanced filtering.const table = db;const item = await table;
-
Table.count(filter?: IQueryFilter<T>): Promise<number>
Counts items in a table.const table = db;const count = await table;console; // 141
-
Table.insert(items: T[]): Promise<T[]>
Inserts multiple items to a table and returns each of them with replaced_id
property.const table = db;const first second = await table;console; // 1, 2
-
Table.insertOne(items: T): Promise<T>
Inserts a single item with replaced_id
property, coresponding to added record.const table = db;const data = await table;console; // { _id: 3, title: 'Cooking tips' }
-
Table.update(filter: IQueryFilter<T>, update: IUpdateItem<T>): Promise<T>
Updates every items matchingfilter
and replaces their fields withupdate
.table;
Properties
Table.name
IConfig
Interface
ISSLConfig
Interface ; ;
IQueryFilter
Interface ;
It means that for a type you pass, it will make every key optional and property as original or a regex expression. Also it will include selectors like $or
.
IQuerySelector
Interface
IQueryOptions
Interface
Other
Advanced filtering
Let's say we want to find a group of items with _authorId
field equals to 2.
We can do it like this:
; table.find;
And what if we want _categoryId
to be 1.
table.find;
You can see, that combining properties together works as AND selector.
There are other selectors as well.
$or
This will search for the items with _authorId
= 2 and _categoryId
= 1 or 2.
table.find;