Skip to content

Commit 6a3da5a

Browse files
committed
feat: implement Schema support in MongoDriver buildTable
1 parent 015893f commit 6a3da5a

19 files changed

+138
-510
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ await runtimeDb.close()
232232
```ts
233233
import { Knex } from 'knex'
234234
import { Database, TableBuilder } from '@secjs/database'
235+
import database from './database'
235236

236237
// All SQL Drivers from Database are using Knex as query builder and for Mongo NoSQL, mongoose.
237238
await database.createTable('products', (tableBuilder: Knex.TableBuilder) => {
@@ -254,16 +255,17 @@ await database.createTable('product_details', (tableBuilder: Knex.TableBuilder)
254255
// Changing the connection to mongo database
255256
database.connection('mongo')
256257

257-
// With mongo connection we do not have to specify the id because
258-
// mongoose auto create the _id property
259-
await database.createTable('products', (tableBuilder: TableBuilder) => {
260-
tableBuilder.string('name').nullable()
261-
tableBuilder.integer('quantity').nullable().defaultTo(0)
258+
// With mongo connection we can't create the table. But we can set our schema
259+
// in buildTable method
260+
const productSchema = new Schema({
261+
name: String,
262+
quantity: Number,
262263
})
263264

264-
await database.createTable('product_details', (tableBuilder: TableBuilder) => {
265-
tableBuilder.string('detail').nullable()
266-
tableBuilder.integer('productId').references('id').inTable('products')
265+
database.buildTable({
266+
name: 'Product',
267+
collection: 'products',
268+
schema: productSchema
267269
})
268270

269271
// Drop table products from database

index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export * from './src/Contracts/DriverContract'
1212
export * from './src/Contracts/DatabaseContract'
1313
export * from './src/Contracts/TransactionContract'
1414

15-
export * from './src/Builders/TableBuilder'
16-
export * from './src/Builders/ColumnBuilder'
17-
export * from './src/Builders/ReferenceColumnBuilder'
18-
1915
export * from './src/Utils/Transaction'
2016
export * from './src/Utils/DriverFactory'
2117
export * from './src/Utils/ConnectionFactory'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/database",
3-
"version": "1.0.9",
3+
"version": "1.1.0",
44
"description": "Handle your application database with factories, seeders and query builder in Node.js",
55
"license": "MIT",
66
"author": "João Lenon <lenon@secjs.com.br>",

src/Builders/ColumnBuilder.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/Builders/ReferenceColumnBuilder.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/Builders/TableBuilder.ts

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/Contracts/DatabaseContract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export interface DatabaseContract {
371371
* @param tableName Table selected to run the query.
372372
*
373373
*/
374-
buildTable(tableName: string): DatabaseContract
374+
buildTable(tableName: string | any): DatabaseContract
375375

376376
/**
377377
* BuildSelect method

src/Contracts/DriverContract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export interface DriverContract {
124124
* @param tableName Table selected to run the query.
125125
*
126126
*/
127-
buildTable(tableName: string): DriverContract
127+
buildTable(tableName: string | any): DriverContract
128128

129129
/**
130130
* BuildSelect method

src/Database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class Database implements DatabaseContract {
239239
return this.driver.close()
240240
}
241241

242-
buildTable(tableName: string): DatabaseContract {
242+
buildTable(tableName: string | any): DatabaseContract {
243243
this.driver.buildTable(tableName)
244244

245245
return this

0 commit comments

Comments
 (0)