Skip to content

Commit c2b09a7

Browse files
committed
Implement create method
1 parent aeb5a49 commit c2b09a7

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

__tests__/sql/index.test.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ describe('sqlite', async () => {
4444
primaryKey: true,
4545
autoIncrement: true,
4646
})
47-
id?: number
47+
id: number
4848

4949
@Column({
5050
// server default
5151
default: 'devup',
5252
})
53-
username?: string
53+
username: string
5454

5555
@Column({
5656
// local default
5757
default: () => 'Hello',
58+
nullable: true,
5859
})
5960
tag?: string
6061

@@ -63,18 +64,39 @@ describe('sqlite', async () => {
6364
})
6465
createdAt: Date
6566
}
67+
68+
const hasRevision = await pool.hasRevision()
69+
if (!hasRevision) {
70+
await pool.setupRevision()
71+
}
6672
// get current revision
67-
const currentRevision = await pool.getCurrentRevision()
68-
// // create revision
73+
expect(await pool.getCurrentRevision()).toBe(0)
74+
// create revision
6975
const nextRevision = await pool.createRevision()
76+
expect(nextRevision).toBe(1)
7077
const revisionSql = await pool.getRevisionSql(nextRevision, true)
71-
console.log(revisionSql)
78+
expect(revisionSql).toEqual([
79+
{
80+
sql: `CREATE TABLE "User" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "username" TEXT, "tag" TEXT, "createdAt" TEXT NOT NULL DEFAULT (CURRENT_TIMESTAMP))`,
81+
revision: 1,
82+
},
83+
])
84+
expect(await pool.getCurrentRevision()).toBe(0)
7285
// // migrate to next revision
73-
// await pool.migrate(nextRevision)
86+
await pool.migrate(nextRevision)
87+
88+
expect(await pool.getCurrentRevision()).toBe(1)
89+
const user = User.create({
90+
username: 'devup',
91+
createdAt: new Date(),
92+
id: 1,
93+
tag: '',
94+
})
95+
96+
user.username = 'devup'
97+
await user.save()
7498

75-
// const user = new User()
76-
// user.username = 'devup'
77-
// await user.save()
99+
const user2 = new User()
78100

79101
// const user = await User.findById(1)
80102
// expect(user?.username).toEqual('devup')

libs/node/model/main.d.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
export * from './index'
22
export class DevupModel {
3+
static create<T extends typeof DevupModel>(
4+
this: T,
5+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6+
params: {
7+
[key in ExtractFieldKeys<InstanceType<T>>]: InstanceType<T>[key]
8+
},
9+
): InstanceType<T> {
10+
throw new Error('Method not implemented.')
11+
}
12+
313
save(): Promise<void> {
414
throw new Error('Method not implemented.')
515
}
616

7-
static findById<T, I extends InstanceType<T>>(
17+
static findById<T extends typeof DevupModel>(
818
this: T,
919
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1020
id: number,
11-
): Promise<I> {
21+
): Promise<InstanceType<T>> {
1222
throw new Error('Method not implemented.')
1323
}
1424

15-
static findAll<T, I extends InstanceType<T>>(
25+
static findAll<T extends typeof DevupModel>(
1626
this: T,
1727
// eslint-disable-next-line @typescript-eslint/no-unused-vars
18-
options: DeepPartial<FindOptions<I>>,
19-
): Promise<I[]> {
28+
options: DeepPartial<FindOptions<InstanceType<T>>>,
29+
): Promise<InstanceType<T>[]> {
2030
throw new Error('Method not implemented.')
2131
}
2232

23-
static findOne<T, I extends InstanceType<T>>(
33+
static findOne<T extends typeof DevupModel>(
2434
this: T,
2535
// eslint-disable-next-line @typescript-eslint/no-unused-vars
26-
options: DeepPartial<FilterOptions<I>>,
27-
): Promise<I> {
36+
options: DeepPartial<FilterOptions<InstanceType<T>>>,
37+
): Promise<InstanceType<T>> {
2838
throw new Error('Method not implemented.')
2939
}
3040
}

libs/node/model/src/column.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use devup_node_struct::column::{devup_model_column_type::JsDevupModelColumnType,
22
use model::{
33
DevupModelFieldType,
44
column::{
5-
DevupModelColumn, DevupModelColumnDefault, DevupModelColumnIndex, DevupModelColumnType,
6-
DevupModelColumnUnique,
5+
DevupModelColumn, DevupModelColumnDefault, DevupModelColumnIndex, DevupModelColumnUnique,
76
},
87
};
98

0 commit comments

Comments
 (0)