Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD example on README.md file #383

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var sql = require('sql');

//(optionally) set the SQL dialect
sql.setDialect('postgres');
//possible dialects: mssql, mysql, postgres (default), sqlite
//possible dialects: mssql, mysql, postgres (default), oracle and sqlite

//first we define our tables
var user = sql.define({
Expand Down Expand Up @@ -108,7 +108,45 @@ console.log(user.select().where(user.state.equals('WA')).toQuery().text);
// "SELECT "user".* FROM "user" WHERE ("user"."state_or_province" = $1)"
```

There are a __lot__ more examples included in the [test/dialects](https://github.com/brianc/node-sql/tree/master/test/dialects) folder. We encourage you to read through them if you have any questions on usage!
### CURD Example

```js
const sql = require('sql');
sql.setDialect('oracle');
const user = sql.define({
name: 'user',
columns: ['name', 'username', 'email']
});
// Create
let create = user.insert({name: 'Name', email: '[email protected]'}).toQuery();

console.log(create.text);
// "INSERT INTO "user" ("name", "email") VALUES (:1, :2)"

// Read
let read = user.select(user.star()).where(user.name.equals("Test")).toQuery();

console.log(read.text);
// "SELECT "user".* FROM "user" WHERE ("user"."name" = :1)

// Update
let update = user.update({email: '[email protected]', username: 'teste-user'})
.where(user.email.equals('[email protected]').or(user.email.equals('[email protected]'))).toQuery();

console.log(update.text);
// "UPDATE "user" SET "email" = :1, "username" = :2 WHERE ("user"."email" = :3)"

// Delete
let deleteUser = user.delete().where({username: 'test'}).toQuery();

console.log(deleteUser.text);
// "DELETE FROM "user" WHERE ("user"."username" = :1)"
```
## More examples:

There are a __lot__ more examples included in the [test/dialects](https://github.com/brianc/node-sql/tree/master/test/dialects) folder and on
[node-sql-examples.github.io](https://node-sql-examples.github.io).
We encourage you to read through them if you have any questions on usage!

## from the command line
You can use the [sql-generate module](https://github.com/tmont/node-sql-generate)
Expand Down