Skip to content

Commit b6224f6

Browse files
committed
initial commit - converting test command into package
0 parents  commit b6224f6

26 files changed

+605
-0
lines changed

.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# http://editorconfig.org
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.json]
12+
insert_final_newline = ignore
13+
14+
[**.min.js]
15+
indent_style = ignore
16+
insert_final_newline = ignore
17+
18+
[MakeFile]
19+
indent_style = space
20+
21+
[*.md]
22+
trim_trailing_whitespace = false

.github/lock.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
ignoreUnless: {{ STALE_BOT }}
3+
---
4+
# Configuration for Lock Threads - https://github.com/dessant/lock-threads-app
5+
6+
# Number of days of inactivity before a closed issue or pull request is locked
7+
daysUntilLock: 60
8+
9+
# Skip issues and pull requests created before a given timestamp. Timestamp must
10+
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
11+
skipCreatedBefore: false
12+
13+
# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
14+
exemptLabels: ['Type: Security']
15+
16+
# Label to add before locking, such as `outdated`. Set to `false` to disable
17+
lockLabel: false
18+
19+
# Comment to post before locking. Set to `false` to disable
20+
lockComment: >
21+
This thread has been automatically locked since there has not been
22+
any recent activity after it was closed. Please open a new issue for
23+
related bugs.
24+
25+
# Assign `resolved` as the reason for locking. Set to `false` to disable
26+
setLockReason: false

.github/stale.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
ignoreUnless: {{ STALE_BOT }}
3+
---
4+
# Number of days of inactivity before an issue becomes stale
5+
daysUntilStale: 60
6+
7+
# Number of days of inactivity before a stale issue is closed
8+
daysUntilClose: 7
9+
10+
# Issues with these labels will never be considered stale
11+
exemptLabels:
12+
- 'Type: Security'
13+
14+
# Label to use when marking an issue as stale
15+
staleLabel: 'Status: Abandoned'
16+
17+
# Comment to post when marking an issue as stale. Set to `false` to disable
18+
markComment: >
19+
This issue has been automatically marked as stale because it has not had
20+
recent activity. It will be closed if no further activity occurs. Thank you
21+
for your contributions.
22+
23+
# Comment to post when closing a stale issue. Set to `false` to disable
24+
closeComment: false

.github/workflows/test.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: test
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- name: Install
13+
run: npm install
14+
- name: Run lint
15+
run: npm run lint
16+
17+
typecheck:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Install
22+
run: npm install
23+
- name: Run typecheck
24+
run: npm run typecheck
25+
26+
tests:
27+
runs-on: ${{ matrix.os }}
28+
strategy:
29+
matrix:
30+
os: [ubuntu-latest, windows-latest]
31+
node-version:
32+
- 20.10.0
33+
- 21.x
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: Use Node.js ${{ matrix.node-version }}
37+
uses: actions/setup-node@v1
38+
with:
39+
node-version: ${{ matrix.node-version }}
40+
- name: Install
41+
run: npm install
42+
- name: Run tests
43+
run: npm test
44+
windows:
45+
runs-on: windows-latest
46+
strategy:
47+
matrix:
48+
node-version:
49+
- 20.10.0
50+
- 21.x
51+
steps:
52+
- uses: actions/checkout@v2
53+
- name: Use Node.js ${{ matrix.node-version }}
54+
uses: actions/setup-node@v1
55+
with:
56+
node-version: ${{ matrix.node-version }}
57+
- name: Install
58+
run: npm install
59+
- name: Run tests
60+
run: npm test

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node_modules
2+
coverage
3+
.DS_STORE
4+
.nyc_output
5+
.idea
6+
.vscode/
7+
*.sublime-project
8+
*.sublime-workspace
9+
*.log
10+
build
11+
dist
12+
yarn.lock
13+
shrinkwrap.yaml
14+
package-lock.json

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
docs
3+
coverage
4+
*.html

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The MIT License
2+
3+
Copyright (c) 2023
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @adocasts.com/generate-models
2+
3+
> Generate Lucid Models from an existing database schema
4+
5+
👋 Hey there! This is still a work in progress

bin/test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { assert } from '@japa/assert'
2+
import { configure, processCLIArgs, run } from '@japa/runner'
3+
4+
processCLIArgs(process.argv.splice(2))
5+
6+
configure({
7+
files: ['tests/**/*.spec.ts'],
8+
plugins: [assert()],
9+
})
10+
11+
run()

commands/generate_models.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { inject } from '@adonisjs/core'
2+
import { BaseCommand } from '@adonisjs/core/ace'
3+
import type { CommandOptions } from '@adonisjs/core/types/ace'
4+
import Schema from '../src/db/schema.js'
5+
import Model from '../src/model/index.js'
6+
7+
export default class GenerateModels extends BaseCommand {
8+
static commandName = 'generate:models'
9+
static description = 'Generate model files from an existing database'
10+
11+
static options: CommandOptions = {
12+
startApp: true,
13+
}
14+
15+
@inject()
16+
async run(schema: Schema) {
17+
const tables = await schema.getTables()
18+
const models = Model.build(tables)
19+
20+
console.log({ models })
21+
}
22+
}

configure.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
|--------------------------------------------------------------------------
3+
| Configure hook
4+
|--------------------------------------------------------------------------
5+
|
6+
| The configure hook is called when someone runs "node ace configure <package>"
7+
| command. You are free to perform any operations inside this function to
8+
| configure the package.
9+
|
10+
| To make things easier, you have access to the underlying "ConfigureCommand"
11+
| instance and you can use codemods to modify the source files.
12+
|
13+
*/
14+
15+
import ConfigureCommand from '@adonisjs/core/commands/configure'
16+
17+
export async function configure(_command: ConfigureCommand) {}

index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
|--------------------------------------------------------------------------
3+
| Package entrypoint
4+
|--------------------------------------------------------------------------
5+
|
6+
| Export values from the package entrypoint as you see fit.
7+
|
8+
*/
9+
10+
export { configure } from './configure.js'

package.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"name": "generate-models",
3+
"description": "Generate Lucid Models from an existing database schema",
4+
"version": "0.0.0",
5+
"engines": {
6+
"node": ">=20.6.0"
7+
},
8+
"type": "module",
9+
"files": [
10+
"build/src",
11+
"build/providers",
12+
"build/stubs",
13+
"build/index.d.ts",
14+
"build/index.js"
15+
],
16+
"exports": {
17+
".": "./build/index.js",
18+
"./types": "./build/src/types.js"
19+
},
20+
"scripts": {
21+
"clean": "del-cli build",
22+
"copy:templates": "copyfiles \"stubs/**/*.stub\" build",
23+
"typecheck": "tsc --noEmit",
24+
"lint": "eslint . --ext=.ts",
25+
"format": "prettier --write .",
26+
"quick:test": "node --import=./tsnode.esm.js --enable-source-maps bin/test.ts",
27+
"pretest": "npm run lint",
28+
"test": "c8 npm run quick:test",
29+
"prebuild": "npm run lint && npm run clean",
30+
"build": "tsc",
31+
"postbuild": "npm run copy:templates",
32+
"release": "np",
33+
"version": "npm run build",
34+
"prepublishOnly": "npm run build"
35+
},
36+
"devDependencies": {
37+
"@adonisjs/assembler": "^7.7.0",
38+
"@adonisjs/core": "^6.12.0",
39+
"@adonisjs/eslint-config": "^1.3.0",
40+
"@adonisjs/lucid": "^21.2.0",
41+
"@adonisjs/prettier-config": "^1.3.0",
42+
"@adonisjs/tsconfig": "^1.3.0",
43+
"@japa/assert": "^3.0.0",
44+
"@japa/runner": "^3.1.4",
45+
"@swc/core": "^1.6.3",
46+
"@types/node": "^20.14.5",
47+
"c8": "^10.1.2",
48+
"copyfiles": "^2.4.1",
49+
"del-cli": "^5.1.0",
50+
"eslint": "^8.57.0",
51+
"knex-schema-inspector": "^3.1.0",
52+
"np": "^10.0.6",
53+
"prettier": "^3.3.2",
54+
"ts-node": "^10.9.2",
55+
"typescript": "^5.4.5"
56+
},
57+
"peerDependencies": {
58+
"@adonisjs/core": "^6.2.0",
59+
"@adonisjs/lucid": "^21.2.0"
60+
},
61+
"author": "tomgobich,adocasts.com",
62+
"license": "MIT",
63+
"keywords": [
64+
"adonisjs",
65+
"lucid",
66+
"model",
67+
"generate"
68+
],
69+
"eslintConfig": {
70+
"extends": "@adonisjs/eslint-config/package"
71+
},
72+
"prettier": "@adonisjs/prettier-config",
73+
"publishConfig": {
74+
"access": "public",
75+
"tag": "latest"
76+
},
77+
"np": {
78+
"message": "chore(release): %s",
79+
"tag": "latest",
80+
"branch": "main",
81+
"anyBranch": false
82+
},
83+
"c8": {
84+
"reporter": [
85+
"text",
86+
"html"
87+
],
88+
"exclude": [
89+
"tests/**"
90+
]
91+
}
92+
}

providers/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The providers directory
2+
3+
The `providers` directory contains the service providers exported by your application. Make sure to register these providers within the `exports` collection (aka package entrypoints) defined within the `package.json` file.
4+
5+
Learn more about [package entrypoints](https://nodejs.org/api/packages.html#package-entry-points).

src/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# The src directory
2+
3+
The `src` directory is where you organize your package code. Feel free to create additional sub-directories as you see fit.

src/db/schema.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import db from '@adonisjs/lucid/services/db'
2+
import { default as schemaInspectorImport } from 'knex-schema-inspector'
3+
import type { Column } from 'knex-schema-inspector/dist/types/column.js'
4+
import type { SchemaInspector } from 'knex-schema-inspector/dist/types/schema-inspector.js'
5+
const schemaInspector = schemaInspectorImport.default
6+
7+
export type TableSchema = {
8+
name: string
9+
columns: Column[]
10+
}
11+
12+
export default class Schema {
13+
declare inspector: SchemaInspector
14+
15+
constructor() {
16+
const knex = db.connection().getWriteClient()
17+
this.inspector = schemaInspector(knex)
18+
}
19+
20+
async getTables(): Promise<TableSchema[]> {
21+
const tableNames = await this.inspector.tables()
22+
const promises = tableNames.map(async (name) => ({
23+
name,
24+
columns: await this.inspector.columnInfo(name),
25+
}))
26+
27+
return Promise.all(promises)
28+
}
29+
}

src/enums/relationship_types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
enum RelationshipTypes {
2+
BELONGS_TO = 'belongsTo',
3+
HAS_ONE = 'hasOne',
4+
HAS_MANY = 'hasMany',
5+
MANY_TO_MANY = 'manyToMany',
6+
}
7+
8+
export default RelationshipTypes

0 commit comments

Comments
 (0)