@stacksjs/orm 0.59.11 → 0.61.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +110931 -3
- package/package.json +1 -1
- package/src/drivers/dynamodb.ts +16 -0
- package/src/drivers/mysql.ts +0 -0
- package/src/drivers/postgres.ts +0 -0
- package/src/drivers/sqlite.ts +0 -0
- package/src/generated/types.ts +24 -0
- package/src/index.ts +11 -44
- package/src/types.ts +11 -0
- package/src/utils.ts +49 -0
- package/dist/index.d.ts +0 -1
package/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const dynamodbClient = {
|
|
2
|
+
// async createTable(tableName: string, callback: (table: Table) => void): Promise<void> {
|
|
3
|
+
// const table = new Table()
|
|
4
|
+
// callback(table)
|
|
5
|
+
// table.execute() // Simulate the execution of the table creation
|
|
6
|
+
// console.log(`Table "${tableName}" created.`)
|
|
7
|
+
// },
|
|
8
|
+
|
|
9
|
+
async dropTable(tableName: string): Promise<void> {
|
|
10
|
+
console.log(`Table "${tableName}" dropped.`)
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
async find(id: number): Promise<any> {
|
|
14
|
+
return { id }
|
|
15
|
+
},
|
|
16
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ProjectsTable } from '../../../../orm/src/models/Project'
|
|
2
|
+
import type { AccessTokensTable } from '../../../../orm/src/models/AccessToken'
|
|
3
|
+
import type { TeamsTable } from '../../../../orm/src/models/Team'
|
|
4
|
+
import type { SubscribersTable } from '../../../../orm/src/models/Subscriber'
|
|
5
|
+
import type { DeploymentsTable } from '../../../../orm/src/models/Deployment'
|
|
6
|
+
import type { UsersTable } from '../../../../orm/src/models/User'
|
|
7
|
+
import type { PostsTable } from '../../../../orm/src/models/Post'
|
|
8
|
+
import type { Generated } from 'kysely'
|
|
9
|
+
|
|
10
|
+
export interface TeamAccessTokensTable {
|
|
11
|
+
id: Generated<number>
|
|
12
|
+
team_id: number
|
|
13
|
+
accesstoken_id: number
|
|
14
|
+
}
|
|
15
|
+
export interface Database {
|
|
16
|
+
projects: ProjectsTable
|
|
17
|
+
access_tokens: AccessTokensTable
|
|
18
|
+
team_access_tokens: TeamAccessTokensTable
|
|
19
|
+
teams: TeamsTable
|
|
20
|
+
subscribers: SubscribersTable
|
|
21
|
+
deployments: DeploymentsTable
|
|
22
|
+
users: UsersTable
|
|
23
|
+
posts: PostsTable
|
|
24
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,49 +1,16 @@
|
|
|
1
|
-
// import {
|
|
2
|
-
// import { database } from '@stacksjs/config'
|
|
1
|
+
// import { config } from '@stacksjs/config'
|
|
3
2
|
|
|
4
|
-
//
|
|
3
|
+
// import { dynamodbClient } from './drivers/dynamodb'
|
|
5
4
|
|
|
6
|
-
// if (database.
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
// })
|
|
14
|
-
// }
|
|
15
|
-
// else if (database.driver === 'mysql') {
|
|
16
|
-
// dialect = new MysqlDialect({
|
|
17
|
-
// pool: createMysqlPool({
|
|
18
|
-
// host: database.host,
|
|
19
|
-
// database: database.database,
|
|
20
|
-
// user: database.username,
|
|
21
|
-
// password: database.password,
|
|
22
|
-
// }),
|
|
23
|
-
// })
|
|
24
|
-
// }
|
|
25
|
-
|
|
26
|
-
// if (database.driver === 'sqlite') {
|
|
27
|
-
// dialect = new SqliteDialect()
|
|
28
|
-
// }
|
|
29
|
-
|
|
30
|
-
// const db = new QueryBuilder({
|
|
31
|
-
// dialect,
|
|
5
|
+
// if (config.database.default === 'dynamodb') {
|
|
6
|
+
// dynamodbClient.createTable('users', (table) => {
|
|
7
|
+
// table.string('id').primary()
|
|
8
|
+
// table.string('name')
|
|
9
|
+
// table.string('email')
|
|
10
|
+
// table.string('password')
|
|
11
|
+
// table.timestamps()
|
|
32
12
|
// })
|
|
33
|
-
|
|
34
|
-
// const user = User.find(1)
|
|
35
|
-
|
|
36
|
-
// export async function find(tableName: string, id: number) {
|
|
37
|
-
// const result = await db
|
|
38
|
-
// .selectFrom(tableName)
|
|
39
|
-
// .selectAll()
|
|
40
|
-
// .where('id', '=', id)
|
|
41
|
-
// .execute()
|
|
42
|
-
|
|
43
|
-
// if (result)
|
|
44
|
-
// return result[0]
|
|
45
|
-
// else
|
|
46
|
-
// return null
|
|
47
13
|
// }
|
|
48
14
|
|
|
49
|
-
export
|
|
15
|
+
export * from './utils'
|
|
16
|
+
export * from './generated/types'
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface OrmDriver {
|
|
2
|
+
find: (id: number) => Promise<any>
|
|
3
|
+
create: (data: any) => Promise<any>
|
|
4
|
+
update: (id: number, data: any) => Promise<any>
|
|
5
|
+
delete: (id: number) => Promise<any>
|
|
6
|
+
all: () => Promise<any[]>
|
|
7
|
+
where: (column: string, value: any) => Promise<any[]>
|
|
8
|
+
// Custom methods
|
|
9
|
+
// createTable: (tableName: string, callback: (table: Table) => void) => Promise<void>;
|
|
10
|
+
// dropTable: (tableName: string) => Promise<void>; -- i don't think we need this bc we do not support the down method
|
|
11
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { generator, parser, traverse } from '@stacksjs/build'
|
|
2
|
+
import { fs } from '@stacksjs/storage'
|
|
3
|
+
import { plural, snakeCase } from '@stacksjs/strings'
|
|
4
|
+
import type { Attributes } from '@stacksjs/types'
|
|
5
|
+
import type { Model } from '@stacksjs/types'
|
|
6
|
+
|
|
7
|
+
type ModelPath = string
|
|
8
|
+
|
|
9
|
+
export async function modelTableName(model: Model | ModelPath): Promise<string> {
|
|
10
|
+
if (typeof model === 'string') {
|
|
11
|
+
model = (await import(model)).default as Model
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return model.table ?? snakeCase(plural(model?.name || ''))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function extractFieldsFromModel(filePath: string) {
|
|
18
|
+
// Read the TypeScript file
|
|
19
|
+
const content = fs.readFileSync(filePath, 'utf8')
|
|
20
|
+
|
|
21
|
+
// Parse the file content into an AST
|
|
22
|
+
const ast = parser.parse(content, {
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
plugins: ['typescript', 'classProperties', 'decorators-legacy'],
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
let fields: Attributes | undefined
|
|
28
|
+
|
|
29
|
+
// Traverse the AST to find the `fields` object
|
|
30
|
+
traverse(ast, {
|
|
31
|
+
ObjectExpression(path) {
|
|
32
|
+
// Look for the `fields` key in the object
|
|
33
|
+
const fieldsProperty = path.node.properties.find((property) => property.key?.name === 'attributes')
|
|
34
|
+
|
|
35
|
+
if (fieldsProperty?.value) {
|
|
36
|
+
// Convert the AST back to code (stringify)
|
|
37
|
+
const generated = generator(fieldsProperty.value, {}, content)
|
|
38
|
+
fields = generated.code
|
|
39
|
+
path.stop() // Stop traversing further once we found the fields
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
return fields
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function userModels() {
|
|
48
|
+
return import.meta.glob<{ default: Model }>(path.userModelsPath('*.ts'))
|
|
49
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const wipOrm = true;
|